Skip to content

Commit

Permalink
fix: fix pcf terminate error
Browse files Browse the repository at this point in the history
  • Loading branch information
HanHongChen committed Oct 22, 2024
1 parent cbef1c8 commit 923d339
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 35 deletions.
83 changes: 49 additions & 34 deletions internal/sbi/consumer/nrf_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package consumer

import (
"context"
"fmt"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -81,12 +82,16 @@ func (s *nnrfService) SendSearchNFInstances(
if err != nil {
return nil, err
}
param.TargetNfType = &targetNfType
param.RequesterNfType = &requestNfType
res, err := client.NFInstancesStoreApi.SearchNFInstances(ctx, &param)
result := res.SearchResult
if err != nil {
logger.ConsumerLog.Errorf("SearchNFInstances failed: %+v", err)
return nil, err
}

result := res.SearchResult

return &result, nil
}

Expand Down Expand Up @@ -199,44 +204,54 @@ func (s *nnrfService) SendRegisterNFInstance(ctx context.Context) (

var nf models.NrfNfManagementNfProfile
var res *NFManagement.RegisterNFInstanceResponse
req := &NFManagement.RegisterNFInstanceRequest{
NfInstanceID: &pcfContext.NfId,
NrfNfManagementNfProfile: &nfProfile,
}
for {
res, err = client.NFInstanceIDDocumentApi.RegisterNFInstance(ctx, req)
if err != nil || res == nil {
logger.ConsumerLog.Errorf("PCF register to NRF Error[%v]", err)
time.Sleep(2 * time.Second)
continue
}
nf = res.NrfNfManagementNfProfile

if res.Location == "" {
// NFUpdate
break
} else {
// NFRegister
resourceUri := res.Location
resouceNrfUri = resourceUri[:strings.Index(resourceUri, "/nnrf-nfm/")]
retrieveNfInstanceID = 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:
req := &NFManagement.RegisterNFInstanceRequest{
NfInstanceID: &pcfContext.NfId,
NrfNfManagementNfProfile: &nfProfile,
}
pcf_context.GetSelf().OAuth2Required = oauth2
if oauth2 && pcf_context.GetSelf().NrfCertPem == "" {
logger.CfgLog.Error("OAuth2 enable but no nrfCertPem provided in config.")
res, err = client.NFInstanceIDDocumentApi.RegisterNFInstance(ctx, req)
if err != nil || res == nil {
logger.ConsumerLog.Errorf("PCF register to NRF Error[%v]", err)
time.Sleep(2 * time.Second)
continue
}
nf = res.NrfNfManagementNfProfile

if res.Location == "" {
// NFUpdate
finish = true
} else {
// NFRegister
resourceUri := res.Location
resouceNrfUri = resourceUri[:strings.Index(resourceUri, "/nnrf-nfm/")]
retrieveNfInstanceID = 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)
}
}
pcf_context.GetSelf().OAuth2Required = oauth2
if oauth2 && pcf_context.GetSelf().NrfCertPem == "" {
logger.CfgLog.Error("OAuth2 enable but no nrfCertPem provided in config.")
}

finish = true

}

break
}
}

return resouceNrfUri, retrieveNfInstanceID, err
}

Expand Down
3 changes: 2 additions & 1 deletion internal/sbi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type pcf interface {
app.App
Processor() *processor.Processor
Consumer() *consumer.Consumer
CancelContext() context.Context
}

type Server struct {
Expand Down Expand Up @@ -126,7 +127,7 @@ func NewServer(pcf pcf, tlsKeyLogPath string) (*Server, error) {

func (s *Server) Run(traceCtx context.Context, wg *sync.WaitGroup) error {
var err error
_, s.Context().NfId, err = s.Consumer().SendRegisterNFInstance(context.Background())
_, s.Context().NfId, err = s.Consumer().SendRegisterNFInstance(s.CancelContext())
if err != nil {
logger.InitLog.Errorf("PCF register to NRF Error[%s]", err.Error())
}
Expand Down

0 comments on commit 923d339

Please sign in to comment.