From c4d593205aa750b140abdad7fd5a1b61a1c8e902 Mon Sep 17 00:00:00 2001 From: pf-lin Date: Fri, 3 May 2024 15:32:16 +0000 Subject: [PATCH] fix: import cycle --- cmd/main.go | 4 ++- internal/pfcp/handler/handler.go | 4 +-- internal/sbi/server.go | 42 +++++++++++++++----------------- pkg/service/init.go | 41 ++++++++++++++++++------------- pkg/utils/pfcp_util.go | 41 +++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 42 deletions(-) create mode 100644 pkg/utils/pfcp_util.go diff --git a/cmd/main.go b/cmd/main.go index 6fe0d6fb..2691fdc8 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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" ) @@ -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 } diff --git a/internal/pfcp/handler/handler.go b/internal/pfcp/handler/handler.go index 47418290..73b9f373 100644 --- a/internal/pfcp/handler/handler.go +++ b/internal/pfcp/handler/handler.go @@ -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) { @@ -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 diff --git a/internal/sbi/server.go b/internal/sbi/server.go index 3276b70b..9d5ecbba 100644 --- a/internal/sbi/server.go +++ b/internal/sbi/server.go @@ -4,7 +4,6 @@ import ( "context" "fmt" - // "log" "net/http" "runtime/debug" "sync" @@ -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 ( @@ -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) @@ -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) @@ -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) } } @@ -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) diff --git a/pkg/service/init.go b/pkg/service/init.go index 05e484f4..6054bca6 100644 --- a/pkg/service/init.go +++ b/pkg/service/init.go @@ -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" ) @@ -35,7 +31,7 @@ type SmfApp struct { wg sync.WaitGroup pfcpStart func(*SmfApp) - pfcpTerminate func(*SmfApp) + pfcpTerminate func() } var _ app.App = &SmfApp{} @@ -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 } @@ -136,17 +150,9 @@ func (a *SmfApp) Start() error { a.sbiServer.Run(context.Background(), &a.wg) 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 } @@ -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 { diff --git a/pkg/utils/pfcp_util.go b/pkg/utils/pfcp_util.go new file mode 100644 index 00000000..7f6760a6 --- /dev/null +++ b/pkg/utils/pfcp_util.go @@ -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() + } + + return pfcpStart, pfcpStop +}