Skip to content

Commit

Permalink
refactor:fix server.go & management function
Browse files Browse the repository at this point in the history
  • Loading branch information
KunLee76 committed Jun 13, 2024
1 parent 8aa6f48 commit 4d89348
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 61 deletions.
38 changes: 8 additions & 30 deletions internal/sbi/api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ func (s *Server) NFInstance(c *gin.Context) {
}

func (s *Server) RegisterNFInstance(c *gin.Context) {
auth_err := authorizationCheck(c, "nnrf-nfm")
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
}
// auth_err := authorizationCheck(c, "nnrf-nfm")
// if auth_err != nil {
// c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
// return
// }

// step 1: retrieve http request body
var nfprofile models.NfProfile
var nfProfile models.NfProfile

requestBody, err := c.GetRawData()
if err != nil {
Expand All @@ -122,7 +122,7 @@ func (s *Server) RegisterNFInstance(c *gin.Context) {
}

// step 2: convert requestBody to openapi models
err = openapi.Deserialize(&nfprofile, requestBody, "application/json")
err = openapi.Deserialize(&nfProfile, requestBody, "application/json")
if err != nil {
problemDetail := "[Request Body] " + err.Error()
rsp := models.ProblemDetails{
Expand All @@ -137,30 +137,8 @@ func (s *Server) RegisterNFInstance(c *gin.Context) {

logger.NfmLog.Infoln("Handle NFRegisterRequest")

nfProfile := models.NfProfile{}
header, response, isUpdate, problemDetails := s.Processor().NFRegisterProcedure(c, nfProfile)

if response != nil {
for key, val := range header {
c.Header(key, val[0])
}
if isUpdate {
logger.NfmLog.Traceln("update success")
s.Processor().NFRegisterProcedure(c, nfProfile)

Check failure on line 141 in internal/sbi/api_management.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

File is not `gofumpt`-ed (gofumpt)
c.JSON(http.StatusOK, response)
}
logger.NfmLog.Traceln("register success")
c.JSON(http.StatusCreated, response)
} else if problemDetails != nil {
logger.NfmLog.Traceln("register failed")
c.JSON(int(problemDetails.Status), problemDetails)
}
problemDetails = &models.ProblemDetails{
Status: http.StatusForbidden,
Cause: "UNSPECIFIED",
}
logger.NfmLog.Traceln("register failed")
c.JSON(http.StatusForbidden, problemDetails)
}

Check failure on line 142 in internal/sbi/api_management.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

unnecessary trailing newline (whitespace)

// UpdateNFInstance - Update NF Instance profile
Expand Down
50 changes: 25 additions & 25 deletions internal/sbi/processor/nfmanagement.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (p *Processor) GetNFInstancesProcedure(
c.JSON(http.StatusForbidden, nil)
}

func (p *Processor) NFDeregisterProcedure(c *gin.Context, nfInstanceID string) *models.ProblemDetails { // OK
func (p *Processor) NFDeregisterProcedure(c *gin.Context, nfInstanceID string) {
collName := "NfProfile"
filter := bson.M{"nfInstanceId": nfInstanceID}

Expand All @@ -196,7 +196,8 @@ func (p *Processor) NFDeregisterProcedure(c *gin.Context, nfInstanceID string) *
Detail: err.Error(),
Cause: "SYSTEM_FAILURE",
}
return problemDetail
c.JSON(http.StatusInternalServerError, problemDetail)
return
}

// nfProfile data for response
Expand All @@ -208,7 +209,8 @@ func (p *Processor) NFDeregisterProcedure(c *gin.Context, nfInstanceID string) *
Cause: "NOTIFICATION_ERROR",
Detail: err.Error(),
}
return problemDetails
c.JSON(int(problemDetails.Status), problemDetails)
return
}

if len(nfProfiles) == 0 {
Expand All @@ -218,7 +220,8 @@ func (p *Processor) NFDeregisterProcedure(c *gin.Context, nfInstanceID string) *
Cause: "RESOURCE_URI_STRUCTURE_NOT_FOUND",
Detail: fmt.Sprintf("NFProfile[%s] not found", nfInstanceID),
}
return problemDetails
c.JSON(int(problemDetails.Status), problemDetails)
return
}

uriList := nrf_context.GetNotificationUri(nfProfiles[0])
Expand All @@ -230,7 +233,8 @@ func (p *Processor) NFDeregisterProcedure(c *gin.Context, nfInstanceID string) *
for _, uri := range uriList {
problemDetails := SendNFStatusNotify(Notification_event, nfInstanceUri, uri, nil)
if problemDetails != nil {
return problemDetails
c.JSON(int(problemDetails.Status), problemDetails)
return
}
}

Expand All @@ -245,7 +249,8 @@ func (p *Processor) NFDeregisterProcedure(c *gin.Context, nfInstanceID string) *
Detail: err.Error(),
Cause: "SYSTEM_FAILURE",
}
return problemDetail
c.JSON(http.StatusInternalServerError, problemDetail)
return
}
if factory.NrfConfig.GetOAuth() {
nfCertPath := oauth.GetNFCertPath(factory.NrfConfig.GetCertBasePath(), string(nfInstanceType), nfInstanceID)
Expand All @@ -254,10 +259,10 @@ func (p *Processor) NFDeregisterProcedure(c *gin.Context, nfInstanceID string) *
logger.NfmLog.Warningf("Can not delete NFCertPem file: %v: %v", nfCertPath, err)
}
}
return nil
c.JSON(http.StatusNoContent, nil)
}

func (p *Processor) UpdateNFInstanceProcedure( // OK
func (p *Processor) UpdateNFInstanceProcedure(
c *gin.Context, nfInstanceID string, patchJSON []byte,
) map[string]interface{} {
collName := "NfProfile"
Expand Down Expand Up @@ -301,7 +306,7 @@ func (p *Processor) UpdateNFInstanceProcedure( // OK
return nf
}

func (p *Processor) GetNFInstanceProcedure(c *gin.Context, nfInstanceID string) { // OK
func (p *Processor) GetNFInstanceProcedure(c *gin.Context, nfInstanceID string) {
collName := "NfProfile"
filter := bson.M{"nfInstanceId": nfInstanceID}
response, err := mongoapi.RestfulAPIGetOne(collName, filter)
Expand All @@ -316,10 +321,7 @@ func (p *Processor) GetNFInstanceProcedure(c *gin.Context, nfInstanceID string)
c.JSON(http.StatusOK, response)
}

func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfile) ( // OK
header http.Header, response bson.M,
update bool, problemDetails *models.ProblemDetails,
) {
func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfile) {
logger.NfmLog.Traceln("[NRF] In NFRegisterProcedure")
var nf models.NfProfile

Expand All @@ -331,7 +333,7 @@ func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfi
Detail: err.Error(),
}
c.JSON(int(problemDetails.Status), problemDetails)
return nil, nil, false, problemDetails
return
}

// make location header
Expand All @@ -347,7 +349,7 @@ func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfi
Cause: "SYSTEM_FAILURE",
}
c.JSON(int(problemDetails.Status), problemDetails)
return nil, nil, false, problemDetails
return
}
putData := bson.M{}
err = json.Unmarshal(tmp, &putData)
Expand All @@ -360,7 +362,7 @@ func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfi
Cause: "SYSTEM_FAILURE",
}
c.JSON(int(problemDetails.Status), problemDetails)
return nil, nil, false, problemDetails
return
}
// set db info
collName := "NfProfile"
Expand All @@ -378,7 +380,7 @@ func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfi
Cause: "SYSTEM_FAILURE",
}
c.JSON(int(problemDetails.Status), problemDetails)
return nil, nil, false, problemDetails
return
}

if existed {
Expand All @@ -394,14 +396,13 @@ func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfi
problemDetails := SendNFStatusNotify(Notification_event, nfInstanceUri, uri, &nfProfile)
if problemDetails != nil {
c.JSON(int(problemDetails.Status), problemDetails)
return nil, nil, true, problemDetails
return
}
}

header := make(http.Header)
header.Add("Location", locationHeaderValue)
c.Header("Location", locationHeaderValue)
c.JSON(http.StatusOK, putData)
return header, putData, true, nil
return
} else { // Create NF Profile case
logger.NfmLog.Infoln("Create NF Profile")
uriList := nrf_context.GetNotificationUri(nf)
Expand All @@ -413,12 +414,11 @@ func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfi
problemDetails := SendNFStatusNotify(Notification_event, nfInstanceUri, uri, &nfProfile)
if problemDetails != nil {
c.JSON(int(problemDetails.Status), problemDetails)
return nil, nil, false, problemDetails
return
}
}

header := make(http.Header)
header.Add("Location", locationHeaderValue)
c.Header("Location", locationHeaderValue)
logger.NfmLog.Infoln("Location header: ", locationHeaderValue)

if factory.NrfConfig.GetOAuth() {
Expand All @@ -429,7 +429,7 @@ func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfi
}
}
c.JSON(http.StatusCreated, putData)
return header, putData, false, nil
return
}
}

Expand Down
24 changes: 18 additions & 6 deletions internal/sbi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ func authorizationCheck(c *gin.Context, serviceName string) error {
func applyRoutes(group *gin.RouterGroup, routes []Route) {
for _, route := range routes {
switch route.Method {
case "http.MethodGet":
case http.MethodGet:
group.GET(route.Pattern, route.APIFunc)
case "http.MethodPost":
case http.MethodPost:
group.POST(route.Pattern, route.APIFunc)
case "http.MethodPut":
case http.MethodPut:
group.PUT(route.Pattern, route.APIFunc)
case "http.MethodPatch":
case http.MethodPatch:
group.PATCH(route.Pattern, route.APIFunc)
case "http.MethodDelete":
case http.MethodDelete:
group.DELETE(route.Pattern, route.APIFunc)
}
}
Expand All @@ -94,6 +94,18 @@ func NewServer(nrf nrf, tlsKeyLogPath string) (*Server, error) {
nrf: nrf,
}

nfManagementRoutes := s.getNFManagementRoutes()
nfManagementGroup := s.router.Group(factory.NrfNfmResUriPrefix)
applyRoutes(nfManagementGroup, nfManagementRoutes)

nfDiscoveryRoutes := s.getNFDiscoveryRoutes()
nfDiscoveryGroup := s.router.Group(factory.NrfDiscResUriPrefix)
applyRoutes(nfDiscoveryGroup, nfDiscoveryRoutes)

accessTokenRoutes := s.getAccessTokenRoutes()
accessTokenGroup := s.router.Group(factory.NrfAccTokenResUriPrefix)
applyRoutes(accessTokenGroup, accessTokenRoutes)

cfg := s.Config()
bindAddr := cfg.GetSbiBindingAddr()
logger.SBILog.Infof("Binding addr: [%s]", bindAddr)
Expand Down Expand Up @@ -154,7 +166,7 @@ func (s *Server) startServer(wg *sync.WaitGroup) {
// else {
// err = fmt.Errorf("No support this scheme[%s]", scheme)
// }

logger.SBILog.Errorln("Test_Error ")
if err != nil && err != http.ErrServerClosed {
logger.SBILog.Errorf("SBI server error: %v", err)
}
Expand Down

0 comments on commit 4d89348

Please sign in to comment.