Skip to content

Commit

Permalink
fix: unable to terminate when registration with NRF is unsuccessful
Browse files Browse the repository at this point in the history
  • Loading branch information
pf-lin committed Oct 22, 2024
1 parent d1c5442 commit dcc6060
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 49 deletions.
78 changes: 35 additions & 43 deletions internal/sbi/consumer/nrf_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,43 @@ func (s *nnrfService) RegisterNFInstance(ctx context.Context) error {
}

// Check data (Use RESTful PUT)
for {
res, err = client.NFInstanceIDDocumentApi.RegisterNFInstance(ctx, registerNFInstanceRequest)
if err != nil || res == nil {
logger.ConsumerLog.Infof("SMF register to NRF Error[%s]", err.Error())
time.Sleep(2 * time.Second)
continue
}
nf = res.NrfNfManagementNfProfile

// http.StatusOK
if res.Location == "" {
// NFUpdate
break
} else { // http.StatusCreated
// NFRegister
resourceUri := res.Location
smfContext.NfInstanceID = resourceUri[strings.LastIndex(resourceUri, "/")+1:]

oauth2 := false
if nf.CustomInfo != nil {
v, ok := nf.CustomInfo["oauth2"].(bool)
if ok {
oauth2 = v
logger.MainLog.Infoln("OAuth2 setting receive from NRF:", oauth2)
}
finish := false
for !finish {
select {
case <-ctx.Done():
return fmt.Errorf("RegisterNFInstance context done")
default:
res, err = client.NFInstanceIDDocumentApi.RegisterNFInstance(ctx, registerNFInstanceRequest)
if err != nil || res == nil {
logger.ConsumerLog.Errorf("SMF register to NRF Error[%s]", err.Error())
time.Sleep(2 * time.Second)
continue
}
smfContext.OAuth2Required = oauth2
if oauth2 && smfContext.NrfCertPem == "" {
logger.CfgLog.Error("OAuth2 enable but no nrfCertPem provided in config.")
nf = res.NrfNfManagementNfProfile

// http.StatusOK
if res.Location == "" {
// NFUpdate
finish = true
} else { // http.StatusCreated
// NFRegister
resourceUri := res.Location
smfContext.NfInstanceID = resourceUri[strings.LastIndex(resourceUri, "/")+1:]

oauth2 := false
if nf.CustomInfo != nil {
v, ok := nf.CustomInfo["oauth2"].(bool)
if ok {
oauth2 = v
logger.MainLog.Infoln("OAuth2 setting receive from NRF:", oauth2)
}
}
smfContext.OAuth2Required = oauth2
if oauth2 && smfContext.NrfCertPem == "" {
logger.CfgLog.Error("OAuth2 enable but no nrfCertPem provided in config.")
}
finish = true
}
break
}
}

Expand Down Expand Up @@ -153,20 +159,6 @@ func (s *nnrfService) buildNfProfile(smfContext *smf_context.SMFContext) (
return profile, err
}

func (s *nnrfService) RetrySendNFRegistration(maxRetry int) error {
retryCount := 0
for retryCount < maxRetry {
err := s.RegisterNFInstance(context.Background())
if err == nil {
return nil
}
logger.ConsumerLog.Warnf("Send NFRegistration Failed by %v", err)
retryCount++
}

return fmt.Errorf("[SMF] Retry NF Registration has meet maximum")
}

func (s *nnrfService) SendDeregisterNFInstance() (err error) {
logger.ConsumerLog.Infof("Send Deregister NFInstance")

Expand Down
9 changes: 3 additions & 6 deletions internal/sbi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type ServerSmf interface {

Consumer() *consumer.Consumer
Processor() *processor.Processor
CancelContext() context.Context
}

type Server struct {
Expand Down Expand Up @@ -107,13 +108,9 @@ func newRouter(s *Server) *gin.Engine {
}

func (s *Server) Run(traceCtx context.Context, wg *sync.WaitGroup) error {
err := s.Consumer().RegisterNFInstance(context.Background())
err := s.Consumer().RegisterNFInstance(s.CancelContext())
if err != nil {
retry_err := s.Consumer().RetrySendNFRegistration(10)
if retry_err != nil {
logger.InitLog.Errorln(retry_err)
return err
}
return err
}

wg.Add(1)
Expand Down

0 comments on commit dcc6060

Please sign in to comment.