Skip to content

Commit

Permalink
Merge pull request #25 from andy89923/refactor/processor
Browse files Browse the repository at this point in the history
refactor: remove runtimeRepo and use pkg/app instead
  • Loading branch information
ianchen0119 authored Apr 30, 2024
2 parents a722608 + 8a042ad commit 86c0b22
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 86 deletions.
4 changes: 1 addition & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions internal/context/chf_context_init.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package context

import (
"fmt"
"math"
"os"
"strconv"
Expand Down Expand Up @@ -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) {
Expand Down
26 changes: 0 additions & 26 deletions internal/repository/runtime.go

This file was deleted.

14 changes: 6 additions & 8 deletions internal/sbi/consumer/consumer.go
Original file line number Diff line number Diff line change
@@ -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{
Expand Down
6 changes: 3 additions & 3 deletions internal/sbi/consumer/nrf_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
14 changes: 4 additions & 10 deletions internal/sbi/processor/processor.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
21 changes: 10 additions & 11 deletions internal/sbi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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())
}
Expand Down Expand Up @@ -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()
Expand Down
19 changes: 19 additions & 0 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
@@ -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
}
53 changes: 31 additions & 22 deletions pkg/service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -116,7 +125,7 @@ func (c *ChfApp) SetLogLevel(level string) {
return
}

c.RuntimeRepo.Config().SetLogLevel(level)
c.Config().SetLogLevel(level)
logger.Log.SetLevel(lvl)
}

Expand All @@ -126,7 +135,7 @@ func (c *ChfApp) SetReportCaller(reportCaller bool) {
return
}

c.RuntimeRepo.Config().SetLogReportCaller(reportCaller)
c.Config().SetLogReportCaller(reportCaller)
logger.Log.SetReportCaller(reportCaller)
}

Expand Down

0 comments on commit 86c0b22

Please sign in to comment.