Skip to content

Commit

Permalink
feat: purify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
andy89923 committed Apr 25, 2024
1 parent e420a6a commit 17ca732
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
9 changes: 4 additions & 5 deletions internal/sbi/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ import (
chf_context "github.com/free5gc/chf/internal/context"
)

type chf interface {
type Chf interface {
Config() *factory.Config
Context() *chf_context.CHFContext
CancelContext() context.Context
}

type Consumer struct {
chf
Chf

// consumer services
*nnrfService
}

func NewConsumer(chf chf) (*Consumer, error) {
func NewConsumer(chf Chf) (*Consumer, error) {
c := &Consumer{
chf: chf,
Chf: 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.chf.Context()
chfContext := s.consumer.Chf.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.chf.Context()
chfContext := s.consumer.Chf.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.chf.Context()
chfContext := s.consumer.Chf.Context()

client := s.getNFManagementClient(chfContext.NrfUri)
nfProfile, err := s.buildNfProfile(chfContext)
Expand Down
8 changes: 4 additions & 4 deletions internal/sbi/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"github.com/free5gc/chf/pkg/factory"
)

type chf interface {
type Chf interface {
Config() *factory.Config
Context() *chf_context.CHFContext
Consumer() *consumer.Consumer
CancelContext() context.Context
}

type Processor struct {
chf
Chf
}

type HandlerResponse struct {
Expand All @@ -25,9 +25,9 @@ type HandlerResponse struct {
Body interface{}
}

func NewProcessor(chf chf) (*Processor, error) {
func NewProcessor(chf Chf) (*Processor, error) {
p := &Processor{
chf: chf,
Chf: chf,
}
return p, nil
}
11 changes: 5 additions & 6 deletions internal/sbi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func applyRoutes(group *gin.RouterGroup, routes []Route) {
}
}

type chf interface {
type Chf interface {
Config() *factory.Config
Context() *chf_context.CHFContext
CancelContext() context.Context
Expand All @@ -61,20 +61,19 @@ type chf interface {
}

type Server struct {
chf
Chf

httpServer *http.Server
router *gin.Engine
}

func NewServer(chf chf, tlsKeyLogPath string) (*Server, error) {
func NewServer(chf Chf, tlsKeyLogPath string) (*Server, error) {
s := &Server{
chf: chf,
Chf: chf,
router: logger_util.NewGinWithLogrus(logger.GinLog),
}

endpoints := s.getConvergenChargingEndpoints()
// group := s.router.Group(openapi.ServiceBaseUri(models.ServiceName_NCHF_CONVERGEDCHARGING))
group := s.router.Group(factory.ConvergedChargingResUriPrefix)
routerAuthorizationCheck := util.NewRouterAuthorizationCheck(models.ServiceName_NCHF_CONVERGEDCHARGING)
group.Use(func(c *gin.Context) {
Expand Down Expand Up @@ -109,7 +108,7 @@ func NewServer(chf chf, tlsKeyLogPath string) (*Server, error) {

func (s *Server) Run(traceCtx context.Context, wg *sync.WaitGroup) error {
var err error
_, s.Context().NfId, err = s.Consumer().RegisterNFInstance(s.CancelContext())
_, 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
17 changes: 9 additions & 8 deletions pkg/service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ var _ App = &ChfApp{}
type App interface {
Config() *factory.Config
Context() *chf_context.CHFContext
CancelContext() context.Context
Consumer() *consumer.Consumer
}

var CHF App

type ChfApp struct {
cfg *factory.Config
chfCtx *chf_context.CHFContext
Expand Down Expand Up @@ -70,6 +70,8 @@ func NewApp(ctx context.Context, cfg *factory.Config, tlsKeyLogPath string) (*Ch
if chf.sbiServer, err = sbi.NewServer(chf, tlsKeyLogPath); err != nil {
return nil, err
}
CHF = chf

return chf, nil
}

Expand Down Expand Up @@ -166,11 +168,7 @@ func (a *ChfApp) listenShutdownEvent() {
}()

<-a.ctx.Done()

if a.sbiServer != nil {
a.sbiServer.Stop()
a.Terminate()
}
a.Stop()
}

func (c *ChfApp) Terminate() {
Expand All @@ -188,7 +186,10 @@ func (c *ChfApp) Terminate() {
}

func (a *ChfApp) Stop() {
a.cancel()
if a.sbiServer != nil {
a.sbiServer.Stop()
a.Terminate()
}
}

func (a *ChfApp) WaitRoutineStopped() {
Expand Down

0 comments on commit 17ca732

Please sign in to comment.