From 8a042ad62eb766338f9bea0e113ff2688f97a953 Mon Sep 17 00:00:00 2001 From: "CTFang@WireLab" Date: Tue, 30 Apr 2024 06:29:29 +0000 Subject: [PATCH] refactor: remove runtimeRepo and use pkg/app instead --- cmd/main.go | 4 +-- internal/context/chf_context_init.go | 3 -- internal/repository/runtime.go | 26 -------------- internal/sbi/consumer/consumer.go | 14 ++++---- internal/sbi/consumer/nrf_service.go | 6 ++-- internal/sbi/processor/processor.go | 14 +++----- internal/sbi/server.go | 21 ++++++----- pkg/app/app.go | 19 ++++++++++ pkg/service/init.go | 53 ++++++++++++++++------------ 9 files changed, 74 insertions(+), 86 deletions(-) delete mode 100644 internal/repository/runtime.go create mode 100644 pkg/app/app.go diff --git a/cmd/main.go b/cmd/main.go index 7f39093..e8630e9 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -23,7 +23,6 @@ import ( "github.com/urfave/cli" "github.com/free5gc/chf/internal/logger" - "github.com/free5gc/chf/internal/repository" "github.com/free5gc/chf/pkg/factory" "github.com/free5gc/chf/pkg/service" logger_util "github.com/free5gc/util/logger" @@ -82,9 +81,8 @@ func action(cliCtx *cli.Context) error { return err } factory.ChfConfig = cfg - runtimeRepo := repository.NewRuntimeRepository(cfg) - chf, err := service.NewApp(ctx, runtimeRepo, tlsKeyLogPath) + chf, err := service.NewApp(ctx, cfg, tlsKeyLogPath) if err != nil { sigCh <- nil return err diff --git a/internal/context/chf_context_init.go b/internal/context/chf_context_init.go index 485a937..2d90f23 100644 --- a/internal/context/chf_context_init.go +++ b/internal/context/chf_context_init.go @@ -1,7 +1,6 @@ package context import ( - "fmt" "math" "os" "strconv" @@ -90,8 +89,6 @@ func InitChfContext(context *CHFContext) { context.NfService = make(map[models.ServiceName]models.NfService) AddNfServices(&context.NfService, config, context) - - fmt.Println("chf context = ", context) } func AddNfServices(serviceMap *map[models.ServiceName]models.NfService, config *factory.Config, context *CHFContext) { diff --git a/internal/repository/runtime.go b/internal/repository/runtime.go deleted file mode 100644 index 9eb4fae..0000000 --- a/internal/repository/runtime.go +++ /dev/null @@ -1,26 +0,0 @@ -package repository - -import ( - chf_context "github.com/free5gc/chf/internal/context" - "github.com/free5gc/chf/pkg/factory" -) - -type RuntimeRepository struct { - config *factory.Config - chfCtx *chf_context.CHFContext -} - -func NewRuntimeRepository(cfg *factory.Config) *RuntimeRepository { - return &RuntimeRepository{ - config: cfg, - chfCtx: chf_context.GetSelf(), - } -} - -func (rr RuntimeRepository) Config() *factory.Config { - return rr.config -} - -func (rr RuntimeRepository) Context() *chf_context.CHFContext { - return rr.chfCtx -} diff --git a/internal/sbi/consumer/consumer.go b/internal/sbi/consumer/consumer.go index 7f958be..6b1033f 100644 --- a/internal/sbi/consumer/consumer.go +++ b/internal/sbi/consumer/consumer.go @@ -1,27 +1,25 @@ package consumer import ( - "github.com/free5gc/chf/internal/repository" + chf_context "github.com/free5gc/chf/internal/context" "github.com/free5gc/openapi/Nnrf_NFDiscovery" "github.com/free5gc/openapi/Nnrf_NFManagement" ) -type Chf interface { - // Consumer doesn't need any App component now +type ConsumerChf interface { + Context() *chf_context.CHFContext } type Consumer struct { - Chf - RuntimeRepository *repository.RuntimeRepository + ConsumerChf *nnrfService } -func NewConsumer(chf Chf, runtimeRepo *repository.RuntimeRepository) (*Consumer, error) { +func NewConsumer(chf ConsumerChf) (*Consumer, error) { c := &Consumer{ - Chf: chf, - RuntimeRepository: runtimeRepo, + ConsumerChf: chf, } c.nnrfService = &nnrfService{ diff --git a/internal/sbi/consumer/nrf_service.go b/internal/sbi/consumer/nrf_service.go index 45cd1c2..e1b60c1 100644 --- a/internal/sbi/consumer/nrf_service.go +++ b/internal/sbi/consumer/nrf_service.go @@ -76,7 +76,7 @@ func (s *nnrfService) SendSearchNFInstances( nrfUri string, targetNfType, requestNfType models.NfType, param Nnrf_NFDiscovery.SearchNFInstancesParamOpts) ( *models.SearchResult, error) { // Set client and set url - chfContext := s.consumer.RuntimeRepository.Context() + chfContext := s.consumer.Context() client := s.getNFDiscClient(chfContext.NrfUri) @@ -109,7 +109,7 @@ func (s *nnrfService) SendDeregisterNFInstance() (problemDetails *models.Problem return pd, err } - chfContext := s.consumer.RuntimeRepository.Context() + chfContext := s.consumer.Context() client := s.getNFManagementClient(chfContext.NrfUri) var res *http.Response @@ -136,7 +136,7 @@ func (s *nnrfService) SendDeregisterNFInstance() (problemDetails *models.Problem func (s *nnrfService) RegisterNFInstance(ctx context.Context) ( resouceNrfUri string, retrieveNfInstanceID string, err error) { - chfContext := s.consumer.RuntimeRepository.Context() + chfContext := s.consumer.Context() client := s.getNFManagementClient(chfContext.NrfUri) nfProfile, err := s.buildNfProfile(chfContext) diff --git a/internal/sbi/processor/processor.go b/internal/sbi/processor/processor.go index d4fcdd6..42e87d5 100644 --- a/internal/sbi/processor/processor.go +++ b/internal/sbi/processor/processor.go @@ -1,16 +1,11 @@ package processor -import ( - "github.com/free5gc/chf/internal/repository" -) - -type Chf interface { +type ProcessorChf interface { // Processor doesn't need any App component now } type Processor struct { - Chf - RuntimeRepository *repository.RuntimeRepository + ProcessorChf } type HandlerResponse struct { @@ -19,10 +14,9 @@ type HandlerResponse struct { Body interface{} } -func NewProcessor(chf Chf, runtimeRepo *repository.RuntimeRepository) (*Processor, error) { +func NewProcessor(chf ProcessorChf) (*Processor, error) { p := &Processor{ - Chf: chf, - RuntimeRepository: runtimeRepo, + ProcessorChf: chf, } return p, nil } diff --git a/internal/sbi/server.go b/internal/sbi/server.go index 381f622..816e9c3 100644 --- a/internal/sbi/server.go +++ b/internal/sbi/server.go @@ -13,7 +13,6 @@ import ( chf_context "github.com/free5gc/chf/internal/context" "github.com/free5gc/chf/internal/logger" - "github.com/free5gc/chf/internal/repository" "github.com/free5gc/chf/internal/sbi/consumer" "github.com/free5gc/chf/internal/sbi/processor" "github.com/free5gc/chf/internal/util" @@ -52,24 +51,24 @@ func applyRoutes(group *gin.RouterGroup, routes []Route) { } } -type chf interface { +type ServerChf interface { Consumer() *consumer.Consumer Processor() *processor.Processor + Config() *factory.Config + Context() *chf_context.CHFContext } type Server struct { - chf - RuntimeRepository *repository.RuntimeRepository + ServerChf httpServer *http.Server router *gin.Engine } -func NewServer(chf chf, runtimeRepo *repository.RuntimeRepository, tlsKeyLogPath string) (*Server, error) { +func NewServer(chf ServerChf, tlsKeyLogPath string) (*Server, error) { s := &Server{ - chf: chf, - RuntimeRepository: runtimeRepo, - router: logger_util.NewGinWithLogrus(logger.GinLog), + ServerChf: chf, + router: logger_util.NewGinWithLogrus(logger.GinLog), } routes := s.getConvergenChargingRoutes() @@ -92,7 +91,7 @@ func NewServer(chf chf, runtimeRepo *repository.RuntimeRepository, tlsKeyLogPath MaxAge: CorsConfigMaxAge, })) - cfg := s.RuntimeRepository.Config() + cfg := s.Config() bindAddr := cfg.GetSbiBindingAddr() logger.SBILog.Infof("Binding addr: [%s]", bindAddr) var err error @@ -107,7 +106,7 @@ func NewServer(chf chf, runtimeRepo *repository.RuntimeRepository, tlsKeyLogPath func (s *Server) Run(traceCtx context.Context, wg *sync.WaitGroup) error { var err error - _, s.RuntimeRepository.Context().NfId, err = s.Consumer().RegisterNFInstance(context.Background()) + _, s.Context().NfId, err = s.Consumer().RegisterNFInstance(context.Background()) if err != nil { logger.InitLog.Errorf("CHF register to NRF Error[%s]", err.Error()) } @@ -143,7 +142,7 @@ func (s *Server) startServer(wg *sync.WaitGroup) { logger.SBILog.Infof("Start SBI server (listen on %s)", s.httpServer.Addr) var err error - cfg := s.RuntimeRepository.Config() + cfg := s.Config() scheme := cfg.GetSbiScheme() if scheme == "http" { err = s.httpServer.ListenAndServe() diff --git a/pkg/app/app.go b/pkg/app/app.go new file mode 100644 index 0000000..eeb63ee --- /dev/null +++ b/pkg/app/app.go @@ -0,0 +1,19 @@ +package app + +import ( + chf_context "github.com/free5gc/chf/internal/context" + "github.com/free5gc/chf/pkg/factory" +) + +type App interface { + SetLogEnable(enable bool) + SetLogLevel(level string) + SetReportCaller(reportCaller bool) + + // tlsKeyLogPath would be remove + Start(tlsKeyLogPath string) + Terminate() + + Context() *chf_context.CHFContext + Config() *factory.Config +} diff --git a/pkg/service/init.go b/pkg/service/init.go index 8302083..649de9b 100644 --- a/pkg/service/init.go +++ b/pkg/service/init.go @@ -12,26 +12,27 @@ import ( "github.com/free5gc/chf/internal/cgf" chf_context "github.com/free5gc/chf/internal/context" "github.com/free5gc/chf/internal/logger" - "github.com/free5gc/chf/internal/repository" "github.com/free5gc/chf/internal/sbi" "github.com/free5gc/chf/internal/sbi/consumer" "github.com/free5gc/chf/internal/sbi/processor" "github.com/free5gc/chf/pkg/abmf" + "github.com/free5gc/chf/pkg/app" + "github.com/free5gc/chf/pkg/factory" "github.com/free5gc/chf/pkg/rf" ) var CHF *ChfApp -var _ App = &ChfApp{} - -type App interface { - Consumer() *consumer.Consumer - Processor() *processor.Processor -} +var _ app.App = &ChfApp{} type ChfApp struct { - App - RuntimeRepo *repository.RuntimeRepository + app.App + consumer.ConsumerChf + processor.ProcessorChf + sbi.ServerChf + + chfCtx *chf_context.CHFContext + cfg *factory.Config ctx context.Context cancel context.CancelFunc @@ -42,32 +43,32 @@ type ChfApp struct { processor *processor.Processor } -func NewApp(ctx context.Context, RuntimeRepo *repository.RuntimeRepository, tlsKeyLogPath string) (*ChfApp, error) { +func NewApp(ctx context.Context, cfg *factory.Config, tlsKeyLogPath string) (*ChfApp, error) { chf := &ChfApp{ - RuntimeRepo: RuntimeRepo, - wg: sync.WaitGroup{}, + cfg: cfg, + wg: sync.WaitGroup{}, } - chf.SetLogEnable(RuntimeRepo.Config().GetLogEnable()) - chf.SetLogLevel(RuntimeRepo.Config().GetLogLevel()) - chf.SetReportCaller(RuntimeRepo.Config().GetLogReportCaller()) - + chf.SetLogEnable(cfg.GetLogEnable()) + chf.SetLogLevel(cfg.GetLogLevel()) + chf.SetReportCaller(cfg.GetLogReportCaller()) chf_context.Init() - processor, err_p := processor.NewProcessor(chf, chf.RuntimeRepo) + processor, err_p := processor.NewProcessor(chf) if err_p != nil { return chf, err_p } chf.processor = processor - consumer, err := consumer.NewConsumer(chf, chf.RuntimeRepo) + consumer, err := consumer.NewConsumer(chf) if err != nil { return chf, err } chf.consumer = consumer chf.ctx, chf.cancel = context.WithCancel(ctx) + chf.chfCtx = chf_context.GetSelf() - if chf.sbiServer, err = sbi.NewServer(chf, chf.RuntimeRepo, tlsKeyLogPath); err != nil { + if chf.sbiServer, err = sbi.NewServer(chf, tlsKeyLogPath); err != nil { return nil, err } CHF = chf @@ -87,6 +88,14 @@ func (a *ChfApp) Processor() *processor.Processor { return a.processor } +func (a *ChfApp) Context() *chf_context.CHFContext { + return a.chfCtx +} + +func (a *ChfApp) Config() *factory.Config { + return a.cfg +} + func (c *ChfApp) SetLogEnable(enable bool) { logger.MainLog.Infof("Log enable is set to [%v]", enable) if enable && logger.Log.Out == os.Stderr { @@ -95,7 +104,7 @@ func (c *ChfApp) SetLogEnable(enable bool) { return } - c.RuntimeRepo.Config().SetLogEnable(enable) + c.Config().SetLogEnable(enable) if enable { logger.Log.SetOutput(os.Stderr) } else { @@ -116,7 +125,7 @@ func (c *ChfApp) SetLogLevel(level string) { return } - c.RuntimeRepo.Config().SetLogLevel(level) + c.Config().SetLogLevel(level) logger.Log.SetLevel(lvl) } @@ -126,7 +135,7 @@ func (c *ChfApp) SetReportCaller(reportCaller bool) { return } - c.RuntimeRepo.Config().SetLogReportCaller(reportCaller) + c.Config().SetLogReportCaller(reportCaller) logger.Log.SetReportCaller(reportCaller) }