diff --git a/internal/sbi/consumer/nrf_service.go b/internal/sbi/consumer/nrf_service.go index 3aa24e07..bec522c8 100644 --- a/internal/sbi/consumer/nrf_service.go +++ b/internal/sbi/consumer/nrf_service.go @@ -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 } } @@ -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") diff --git a/internal/sbi/server.go b/internal/sbi/server.go index 04f3371d..1bd6a58a 100644 --- a/internal/sbi/server.go +++ b/internal/sbi/server.go @@ -32,6 +32,7 @@ type ServerSmf interface { Consumer() *consumer.Consumer Processor() *processor.Processor + CancelContext() context.Context } type Server struct { @@ -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)