Skip to content

Commit

Permalink
fix: import cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
pf-lin authored and ming-hsien committed May 8, 2024
1 parent 5c65d5b commit c4d5932
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 42 deletions.
4 changes: 3 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/free5gc/smf/pkg/factory"
"github.com/free5gc/smf/pkg/service"
logger_util "github.com/free5gc/util/logger"
"github.com/free5gc/smf/pkg/utils"
"github.com/free5gc/util/version"
)

Expand Down Expand Up @@ -81,7 +82,8 @@ func action(cliCtx *cli.Context) error {
}
factory.UERoutingConfig = ueRoutingCfg

smf, err := service.NewApp(cfg, tlsKeyLogPath)
pfcpStart, pfcpTerminate := utils.InitPFCPFunc()
smf, err := service.NewApp(cfg, tlsKeyLogPath, pfcpStart, pfcpTerminate)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/pfcp/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
smf_context "github.com/free5gc/smf/internal/context"
"github.com/free5gc/smf/internal/logger"
pfcp_message "github.com/free5gc/smf/internal/pfcp/message"
"github.com/free5gc/smf/internal/sbi/producer"
"github.com/free5gc/smf/pkg/service"
)

func HandlePfcpHeartbeatRequest(msg *pfcpUdp.Message) {
Expand Down Expand Up @@ -200,7 +200,7 @@ func HandlePfcpSessionReportRequest(msg *pfcpUdp.Message) {
// After receiving the Usage Report, it should send charging request to the CHF
// and update the URR with the quota or other charging information according to
// the charging response
producer.ReportUsageAndUpdateQuota(smContext)
service.GetApp().Processor().ReportUsageAndUpdateQuota(smContext)
}

// TS 23.502 4.2.3.3 2b. Send Data Notification Ack, SMF->UPF
Expand Down
42 changes: 20 additions & 22 deletions internal/sbi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

// "log"
"net/http"
"runtime/debug"
"sync"
Expand All @@ -13,16 +12,14 @@ import (
"github.com/free5gc/openapi/models"
smf_context "github.com/free5gc/smf/internal/context"
"github.com/free5gc/smf/internal/logger"
"github.com/free5gc/smf/internal/sbi/consumer"
"github.com/free5gc/smf/internal/sbi/processor"

// "github.com/free5gc/smf/internal/sbi/processor"
util_oauth "github.com/free5gc/smf/internal/util/oauth"
"github.com/free5gc/smf/pkg/app"
"github.com/free5gc/smf/pkg/factory"
"github.com/free5gc/util/httpwrapper"
logger_util "github.com/free5gc/util/logger"
"github.com/gin-gonic/gin"
// "github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -57,13 +54,17 @@ type Server struct {

httpServer *http.Server
router *gin.Engine
consumer *consumer.Consumer
processor *processor.Processor
}

func NewServer(smf app.App, tlsKeyLogPath string) (_ *Server, err error) {
func NewServer(
smf app.App, tlsKeyLogPath string, consumer *consumer.Consumer, processor *processor.Processor,
) (_ *Server, err error) {
s := &Server{
App: smf,
// router: logger_util.NewGinWithLogrus(logger.GinLog),
App: smf,
consumer: consumer,
processor: processor,
}

smf_context.InitSmfContext(factory.SmfConfig)
Expand All @@ -73,15 +74,6 @@ func NewServer(smf app.App, tlsKeyLogPath string) (_ *Server, err error) {

s.router = newRouter(s)

// err := consumer.SendNFRegistration()
// if err != nil {
// retry_err := consumer.RetrySendNFRegistration(10)
// if retry_err != nil {
// logger.InitLog.Errorln(retry_err)
// return
// }
// }

bindAddr := fmt.Sprintf("%s:%d", s.Context().BindingIPv4, s.Context().SBIPort)
if s.httpServer, err = httpwrapper.NewHttp2Server(bindAddr, tlsKeyLogPath, s.router); err != nil {
logger.InitLog.Errorf("Initialize HTTP server failed: %v", err)
Expand Down Expand Up @@ -123,7 +115,10 @@ func newRouter(s *Server) *gin.Engine {
case models.ServiceName_NSMF_OAM:
smfOAMGroup := router.Group(factory.SmfOamUriPrefix)
smfOAMRoutes := s.getOAMRoutes()
// TODO: Add authorization check
routerAuthorizationCheck := util_oauth.NewRouterAuthorizationCheck(models.ServiceName_NSMF_OAM)
smfOAMGroup.Use(func(c *gin.Context) {
routerAuthorizationCheck.Check(c, smf_context.GetSelf())
})
applyRoutes(smfOAMGroup, smfOAMRoutes)
}
}
Expand All @@ -132,11 +127,14 @@ func newRouter(s *Server) *gin.Engine {
}

func (s *Server) Run(traceCtx context.Context, wg *sync.WaitGroup) error {
// var err error
// _, s.Context().NfId, err = s.Consumer().RegisterNFInstance(s.CancelContext())
// if err != nil {
// logger.InitLog.Errorf("SMF register to NRF Error[%s]", err.Error())
// }
err := s.consumer.RegisterNFInstance()
if err != nil {
retry_err := s.consumer.RetrySendNFRegistration(10)
if retry_err != nil {
logger.InitLog.Errorln(retry_err)
return err
}
}

wg.Add(1)
go s.startServer(wg)
Expand Down
41 changes: 24 additions & 17 deletions pkg/service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@ import (
"os"
"runtime/debug"
"sync"
"time"

"github.com/sirupsen/logrus"

smf_context "github.com/free5gc/smf/internal/context"
"github.com/free5gc/smf/internal/logger"
"github.com/free5gc/smf/internal/pfcp"
"github.com/free5gc/smf/internal/pfcp/udp"
"github.com/free5gc/smf/internal/sbi"
"github.com/free5gc/smf/internal/sbi/consumer"
"github.com/free5gc/smf/internal/sbi/processor"
"github.com/free5gc/smf/pkg/app"
"github.com/free5gc/smf/pkg/association"
"github.com/free5gc/smf/pkg/factory"
)

Expand All @@ -35,7 +31,7 @@ type SmfApp struct {
wg sync.WaitGroup

pfcpStart func(*SmfApp)
pfcpTerminate func(*SmfApp)
pfcpTerminate func()
}

var _ app.App = &SmfApp{}
Expand All @@ -44,17 +40,35 @@ func GetApp() *SmfApp {
return SMF
}

func NewApp(cfg *factory.Config, tlsKeyLogPath string) (*SmfApp, error) {
func NewApp(
cfg *factory.Config, tlsKeyLogPath string, pfcpStart func(*SmfApp), pfcpTerminate func(),
) (*SmfApp, error) {
smf := &SmfApp{
cfg: cfg,
wg: sync.WaitGroup{},
pfcpStart: pfcpStart,
pfcpTerminate: pfcpTerminate,
}
smf.SetLogEnable(cfg.GetLogEnable())
smf.SetLogLevel(cfg.GetLogLevel())
smf.SetReportCaller(cfg.GetLogReportCaller())

// Initialize consumer
consumer, err := consumer.NewConsumer(smf)
if err != nil {
return nil, err
}
smf.consumer = consumer

// Initialize processor
processor, err := processor.NewProcessor(smf, consumer)
if err != nil {
return nil, err
}
smf.processor = processor

// TODO: Initialize sbi server
sbiServer, err := sbi.NewServer(smf, tlsKeyLogPath)
sbiServer, err := sbi.NewServer(smf, tlsKeyLogPath, consumer, processor)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -136,17 +150,9 @@ func (a *SmfApp) Start() error {
a.sbiServer.Run(context.Background(), &a.wg)

Check failure on line 150 in pkg/service/init.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

Error return value of `a.sbiServer.Run` is not checked (errcheck)
go a.listenShutDownEvent()

udp.Run(pfcp.Dispatch)

ctx, cancel := context.WithCancel(context.Background())
smf_context.GetSelf().Ctx = ctx
smf_context.GetSelf().PFCPCancelFunc = cancel
for _, upNode := range smf_context.GetSelf().UserPlaneInformation.UPFs {
upNode.UPF.Ctx, upNode.UPF.CancelFunc = context.WithCancel(context.Background())
go association.ToBeAssociatedWithUPF(ctx, upNode.UPF, a.processor)
}
// Initialize PFCP server
a.pfcpStart(a)

time.Sleep(1000 * time.Millisecond)
return nil
}

Expand All @@ -167,6 +173,7 @@ func (a *SmfApp) listenShutDownEvent() {

func (a *SmfApp) Terminate() {
logger.MainLog.Infof("Terminating SMF...")
a.pfcpTerminate()
// deregister with NRF
problemDetails, err := a.Consumer().SendDeregisterNFInstance()
if problemDetails != nil {
Expand Down
41 changes: 41 additions & 0 deletions pkg/utils/pfcp_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package utils

import (
"context"
"time"

smf_context "github.com/free5gc/smf/internal/context"
"github.com/free5gc/smf/internal/pfcp"
"github.com/free5gc/smf/internal/pfcp/udp"
"github.com/free5gc/smf/pkg/association"
"github.com/free5gc/smf/pkg/service"
)

var (
pfcpStart func(a *service.SmfApp)
pfcpStop func()
)

func InitPFCPFunc() (func(a *service.SmfApp), func()) {
pfcpStart = func(a *service.SmfApp) {
// Initialize PFCP server
udp.Run(pfcp.Dispatch)

ctx, cancel := context.WithCancel(context.Background())
smf_context.GetSelf().Ctx = ctx
smf_context.GetSelf().PFCPCancelFunc = cancel
for _, upNode := range smf_context.GetSelf().UserPlaneInformation.UPFs {
upNode.UPF.Ctx, upNode.UPF.CancelFunc = context.WithCancel(context.Background())
go association.ToBeAssociatedWithUPF(ctx, upNode.UPF, a.Processor())
}

// Wait for PFCF start
time.Sleep(1000 * time.Millisecond)
}

pfcpStop = func() {
udp.Server.Close()

Check failure on line 37 in pkg/utils/pfcp_util.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

Error return value of `udp.Server.Close` is not checked (errcheck)
}

return pfcpStart, pfcpStop
}

0 comments on commit c4d5932

Please sign in to comment.