From 6b8a2acadf228af8abe99d73dc3571825b6852eb Mon Sep 17 00:00:00 2001 From: kun Date: Tue, 11 Jun 2024 14:43:57 +0000 Subject: [PATCH] fix api_management + nfmanagement & fix some errors from conversation comments --- internal/context/management_data.go | 2 +- internal/sbi/api_accesstoken.go | 4 +- internal/sbi/api_management.go | 131 ++----------------------- internal/sbi/processor/nfmanagement.go | 44 +++++---- 4 files changed, 32 insertions(+), 149 deletions(-) diff --git a/internal/context/management_data.go b/internal/context/management_data.go index c3bfdc5..b420594 100644 --- a/internal/context/management_data.go +++ b/internal/context/management_data.go @@ -498,7 +498,7 @@ func nnrfUriList(originalUL *UriList, UL *UriList, location []string) { UL.Link = *b } -func GetNofificationUri(nfProfile models.NfProfile) []string { +func GetNotificationUri(nfProfile models.NfProfile) []string { var uriList []string // nfTypeCond diff --git a/internal/sbi/api_accesstoken.go b/internal/sbi/api_accesstoken.go index d547daa..c799a9f 100644 --- a/internal/sbi/api_accesstoken.go +++ b/internal/sbi/api_accesstoken.go @@ -23,7 +23,7 @@ import ( // Index is the index handler. func Index(c *gin.Context) { - c.String(http.StatusOK, "Hello World!") + c.String(http.StatusOK, "msg") } func (s *Server) getAccessTokenRoutes() []Route { @@ -32,7 +32,7 @@ func (s *Server) getAccessTokenRoutes() []Route { Method: http.MethodGet, Pattern: "/", APIFunc: func(ctx *gin.Context) { - ctx.JSON(http.StatusOK, gin.H{"status": "Hello World!"}) + ctx.JSON(http.StatusOK, gin.H{"status": "msg"}) }, }, { diff --git a/internal/sbi/api_management.go b/internal/sbi/api_management.go index da14a31..db9a1ed 100644 --- a/internal/sbi/api_management.go +++ b/internal/sbi/api_management.go @@ -12,7 +12,6 @@ import ( "go.mongodb.org/mongo-driver/bson" "github.com/free5gc/nrf/internal/logger" - //"github.com/free5gc/nrf/internal/sbi/processor" "github.com/free5gc/openapi" "github.com/free5gc/openapi/models" timedecode "github.com/free5gc/util/mapstruct" @@ -72,7 +71,7 @@ func (s *Server) getNFManagementRoutes() []Route { } // DeregisterNFInstance - Deregisters a given NF Instance -func (s *Server) DeregisterNFInstance(c *gin.Context) { +func (s *Server) DeregisterNFInstance(c *gin.Context) { //OK auth_err := authorizationCheck(c, "nnrf-nfm") if auth_err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()}) @@ -87,60 +86,25 @@ func (s *Server) DeregisterNFInstance(c *gin.Context) { // if problemDetails != nil { // c.JSON(http.StatusInternalServerError, problemDetails) // } else { - // c.JSON(http.StatusNoContent, nil) + // c.JSON(http.StatusNoContent, nil) //這個沒用到,確認一下 // } } // GetNFInstance - Read the profile of a given NF Instance -func (s *Server) NFInstance(c *gin.Context) { +func (s *Server) NFInstance(c *gin.Context) { //OK auth_err := authorizationCheck(c, "nnrf-nfm") if auth_err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()}) return } - //--------------------- logger.NfmLog.Infoln("Handle GetNFInstanceRequest") nfInstanceId := c.Params.ByName("nfInstanceID") - // response := s.processor.GetNFInstanceProcedure(nfInstanceId) s.Processor().GetNFInstanceProcedure(c, nfInstanceId) - - // if response != nil { - // //return httpwrapper.NewResponse(http.StatusOK, nil, response) - // c.JSON(http.StatusOK, response) - // } else { - // problemDetails := &models.ProblemDetails{ - // Status: http.StatusNotFound, - // Cause: "UNSPECIFIED", - // } - // //return httpwrapper.NewResponse(int(problemDetails.Status), nil, problemDetails) - // c.JSON(int(problemDetails.Status), problemDetails) } -//-------------------- - -// req := httpwrapper.NewRequest(c.Request, nil) -// req.Params["nfInstanceID"] = c.Params.ByName("nfInstanceID") -// //nfInstanceID := c.Params.ByName("nfInstanceID") -// httpResponse := s.processor.HandleGetNFInstanceRequest(req) -// //s.processor.HandleGetNFInstanceRequest(c, nfInstanceID) - -// responseBody, err := openapi.Serialize(httpResponse.Body, "application/json") -// if err != nil { -// logger.NfmLog.Warnln(err) -// problemDetails := models.ProblemDetails{ -// Status: http.StatusInternalServerError, -// Cause: "SYSTEM_FAILURE", -// Detail: err.Error(), -// } -// c.JSON(http.StatusInternalServerError, problemDetails) -// } else { -// c.Data(httpResponse.Status, "application/json", responseBody) -// } - -// RegisterNFInstance - Register a new NF Instance -func (s *Server) RegisterNFInstance(c *gin.Context) { +func (s *Server) RegisterNFInstance(c *gin.Context) { //OK auth_err := authorizationCheck(c, "nnrf-nfm") if auth_err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()}) @@ -176,10 +140,8 @@ func (s *Server) RegisterNFInstance(c *gin.Context) { c.JSON(http.StatusBadRequest, rsp) return } - //-------------------- logger.NfmLog.Infoln("Handle NFRegisterRequest") - // nfProfile := request.Body.(models.NfProfile) nfProfile := models.NfProfile{} header, response, isUpdate, problemDetails := s.Processor().NFRegisterProcedure(c, nfProfile) @@ -191,15 +153,12 @@ func (s *Server) RegisterNFInstance(c *gin.Context) { if isUpdate { logger.NfmLog.Traceln("update success") - // return httpwrapper.NewResponse(http.StatusOK, header, response) c.JSON(http.StatusOK, response) } logger.NfmLog.Traceln("register success") - // return httpwrapper.NewResponse(http.StatusCreated, header, response) c.JSON(http.StatusCreated, response) } else if problemDetails != nil { logger.NfmLog.Traceln("register failed") - // return httpwrapper.NewResponse(int(problemDetails.Status), nil, problemDetails) c.JSON(int(problemDetails.Status), problemDetails) } problemDetails = &models.ProblemDetails{ @@ -207,34 +166,11 @@ func (s *Server) RegisterNFInstance(c *gin.Context) { Cause: "UNSPECIFIED", } logger.NfmLog.Traceln("register failed") - // return httpwrapper.NewResponse(http.StatusForbidden, nil, problemDetails) c.JSON(http.StatusForbidden, problemDetails) } -//------------------- -// step 3: encapsulate the request by http_wrapper package -//req := httpwrapper.NewRequest(c.Request, nfprofile) -//s.processor.HandleNFRegisterRequest(c, nfprofile) - -// for key, val := range httpResponse.Header { -// c.Header(key, val[0]) -// } - -// responseBody, err := openapi.Serialize(httpResponse.Body, "application/json") -// if err != nil { -// logger.NfmLog.Warnln(err) -// problemDetails := models.ProblemDetails{ -// Status: http.StatusInternalServerError, -// Cause: "SYSTEM_FAILURE", -// Detail: err.Error(), -// } -// c.JSON(http.StatusInternalServerError, problemDetails) -// } else { -// c.Data(httpResponse.Status, "application/json", responseBody) -// } - // UpdateNFInstance - Update NF Instance profile -func (s *Server) getUpdateNFInstance(c *gin.Context) { +func (s *Server) getUpdateNFInstance(c *gin.Context) { //OK auth_err := authorizationCheck(c, "nnrf-nfm") if auth_err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()}) @@ -255,12 +191,6 @@ func (s *Server) getUpdateNFInstance(c *gin.Context) { return } - // req := httpwrapper.NewRequest(c.Request, nil) - // req.Params["nfInstanceID"] = c.Params.ByName("nfInstanceID") - // req.Body = requestBody - - // httpResponse := s.processor.HandleUpdateNFInstanceRequest(req) - nfInstanceID := c.Params.ByName("nfInstanceID") s.Processor().UpdateNFInstanceProcedure(c, nfInstanceID, requestBody) } @@ -273,22 +203,12 @@ func (s *Server) getNFInstances(c *gin.Context) { return } - // req := httpwrapper.NewRequest(c.Request, nil) - // req.Query = c.Request.URL.Query() - // httpResponse := s.processor.HandleGetNFInstancesRequest(req) //82 - - // query := c.Request.URL.Query() - // values := processor.Values(query) // Convert query to processor.Values - logger.NfmLog.Infoln("Handle GetNFInstancesRequest") - // nfType := request.Query.Get("nf-type") nfType := c.Request.URL.Query().Get("nf-type") - // limit_param := request.Query.Get("limit") limit_param := c.Request.URL.Query().Get("limit") limit := 0 if limit_param != "" { var err error - // limit, err = strconv.Atoi(request.Query.Get("limit")) limit, err = strconv.Atoi(limit_param) if err != nil { logger.NfmLog.Errorln("Error in string conversion: ", limit) @@ -297,8 +217,6 @@ func (s *Server) getNFInstances(c *gin.Context) { Status: http.StatusBadRequest, Detail: err.Error(), } - - // return httpwrapper.NewResponse(int(problemDetails.Status), nil, problemDetails) c.JSON(int(problemDetails.Status), problemDetails) } if limit < 1 { @@ -307,35 +225,12 @@ func (s *Server) getNFInstances(c *gin.Context) { Status: http.StatusBadRequest, Detail: "limit must be greater than 0", } - // return httpwrapper.NewResponse(int(problemDetails.Status), nil, problemDetails) c.JSON(int(problemDetails.Status), problemDetails) } } s.Processor().GetNFInstancesProcedure(c, nfType, limit) - //--------------------- - // response, problemDetails := GetNFInstancesProcedure(c , nfType, limit) - - // if response != nil { - // logger.NfmLog.Traceln("GetNFInstances success") - // //return httpwrapper.NewResponse(http.StatusOK, nil, response) - // c.JSON(http.StatusOK, response) - // } else if problemDetails != nil { - // logger.NfmLog.Traceln("GetNFInstances failed") - // //return httpwrapper.NewResponse(int(problemDetails.Status), nil, problemDetails) - // c.JSON(int(problemDetails.Status), problemDetails) - // } - // problemDetails = &models.ProblemDetails{ - // Status: http.StatusForbidden, - // Cause: "UNSPECIFIED", - // } - // logger.NfmLog.Traceln("GetNFInstances failed") - // //return httpwrapper.NewResponse(http.StatusForbidden, nil, problemDetails) - // c.JSON(http.StatusForbidden, problemDetails) - - //---------------------- - // responseBody, err := openapi.Serialize(httpResponse.Body, "application/json") // if err != nil { // logger.NfmLog.Warnln(err) @@ -427,7 +322,7 @@ func (s *Server) UpdateSubscription(c *gin.Context) { // Provide SubsciptionId for each request (add by one each time) // CreateSubscription - Create a new subscription -func (s *Server) CreateSubscription(c *gin.Context) { +func (s *Server) CreateSubscription(c *gin.Context) { //OK auth_err := authorizationCheck(c, "nnrf-nfm") if auth_err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()}) @@ -464,21 +359,7 @@ func (s *Server) CreateSubscription(c *gin.Context) { return } - // req := httpwrapper.NewRequest(c.Request, subscription) s.Processor().HandleCreateSubscriptionRequest(c, subscription) - // httpResponse := s.processor.HandleCreateSubscriptionRequest(req) - // responseBody, err := openapi.Serialize(httpResponse.Body, "application/json") - // if err != nil { - // logger.NfmLog.Errorln(err) - // problemDetails := models.ProblemDetails{ - // Status: http.StatusInternalServerError, - // Cause: "SYSTEM_FAILURE", - // Detail: err.Error(), - // } - // c.JSON(http.StatusInternalServerError, problemDetails) - // } else { - // c.Data(httpResponse.Status, "application/json", responseBody) - // } } func (s *Server) GetLocalIp() string { diff --git a/internal/sbi/processor/nfmanagement.go b/internal/sbi/processor/nfmanagement.go index a791423..827a71d 100644 --- a/internal/sbi/processor/nfmanagement.go +++ b/internal/sbi/processor/nfmanagement.go @@ -6,7 +6,6 @@ import ( "fmt" "net/http" "os" - //"strconv" "time" "github.com/gin-gonic/gin" @@ -19,7 +18,6 @@ import ( "github.com/free5gc/openapi/Nnrf_NFManagement" "github.com/free5gc/openapi/models" "github.com/free5gc/openapi/oauth" - //"github.com/free5gc/util/httpwrapper" timedecode "github.com/free5gc/util/mapstruct" "github.com/free5gc/util/mongoapi" ) @@ -215,7 +213,7 @@ func RemoveSubscriptionProcedure(subscriptionID string) { } } -func (p *Processor) GetNFInstancesProcedure( +func (p *Processor) GetNFInstancesProcedure( //OK c *gin.Context, nfType string, limit int, ) { // (*nrf_context.UriList, *models.ProblemDetails) { collName := "urilist" @@ -249,8 +247,10 @@ func (p *Processor) GetNFInstancesProcedure( Detail: err.Error(), Cause: "SYSTEM_FAILURE", } + c.JSON(http.StatusInternalServerError, problemDetail) // return nil, problemDetail - c.JSON(int(problemDetail.Status), problemDetail) + //c.JSON(int(problemDetail.Status), problemDetail) + //c.Data(http.StatusInternalServerError, "application/json", []byte("error in GetNFInstancesProcedure")) } rspUriList.Link.Item = append(rspUriList.Link.Item, originalUL.Link.Item...) if nfType != "" && rspUriList.NfType == "" { @@ -261,9 +261,12 @@ func (p *Processor) GetNFInstancesProcedure( nrf_context.NnrfUriListLimit(rspUriList, limit) // return rspUriList, nil c.JSON(http.StatusOK, rspUriList) + + logger.NfmLog.Traceln("GetNFInstances failed") + c.JSON(http.StatusForbidden, nil) } -func (p *Processor) NFDeregisterProcedure(c *gin.Context, nfInstanceID string) *models.ProblemDetails { +func (p *Processor) NFDeregisterProcedure(c *gin.Context, nfInstanceID string) *models.ProblemDetails { //OK collName := "NfProfile" filter := bson.M{"nfInstanceId": nfInstanceID} @@ -314,7 +317,7 @@ func (p *Processor) NFDeregisterProcedure(c *gin.Context, nfInstanceID string) * return problemDetails } - uriList := nrf_context.GetNofificationUri(nfProfiles[0]) + uriList := nrf_context.GetNotificationUri(nfProfiles[0]) nfInstanceType := nfProfiles[0].NfType nfInstanceUri := nrf_context.GetNfInstanceURI(nfInstanceID) // set info for NotificationData @@ -350,7 +353,7 @@ func (p *Processor) NFDeregisterProcedure(c *gin.Context, nfInstanceID string) * return nil } -func (p *Processor) UpdateNFInstanceProcedure( +func (p *Processor) UpdateNFInstanceProcedure( //OK c *gin.Context, nfInstanceID string, patchJSON []byte, ) map[string]interface{} { collName := "NfProfile" @@ -381,7 +384,7 @@ func (p *Processor) UpdateNFInstanceProcedure( return nil } - uriList := nrf_context.GetNofificationUri(nfProfiles[0]) + uriList := nrf_context.GetNotificationUri(nfProfiles[0]) // set info for NotificationData Notification_event := models.NotificationEventType_PROFILE_CHANGED @@ -394,7 +397,7 @@ func (p *Processor) UpdateNFInstanceProcedure( return nf } -func (p *Processor) GetNFInstanceProcedure(c *gin.Context, nfInstanceID string) { +func (p *Processor) GetNFInstanceProcedure(c *gin.Context, nfInstanceID string) { //OK collName := "NfProfile" filter := bson.M{"nfInstanceId": nfInstanceID} response, err := mongoapi.RestfulAPIGetOne(collName, filter) @@ -409,7 +412,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) ( +func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfile) ( //OK header http.Header, response bson.M, update bool, problemDetails *models.ProblemDetails, ) { @@ -423,8 +426,8 @@ func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfi Status: http.StatusBadRequest, Detail: err.Error(), } + c.JSON(int(problemDetails.Status), problemDetails) return nil, nil, false, problemDetails - // c.JSON(int(problemDetails.Status), problemDetails) } // make location header @@ -439,8 +442,8 @@ func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfi Detail: err.Error(), Cause: "SYSTEM_FAILURE", } + c.JSON(int(problemDetails.Status), problemDetails) return nil, nil, false, problemDetails - // c.JSON(int(problemDetails.Status), problemDetails) } putData := bson.M{} err = json.Unmarshal(tmp, &putData) @@ -452,8 +455,8 @@ func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfi Detail: err.Error(), Cause: "SYSTEM_FAILURE", } + c.JSON(int(problemDetails.Status), problemDetails) return nil, nil, false, problemDetails - // c.JSON(int(problemDetails.Status), problemDetails) } // set db info collName := "NfProfile" @@ -470,13 +473,13 @@ func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfi Detail: err.Error(), Cause: "SYSTEM_FAILURE", } + c.JSON(int(problemDetails.Status), problemDetails) return nil, nil, false, problemDetails - // c.JSON(int(problemDetails.Status), problemDetails) } if existed { logger.NfmLog.Infoln("RestfulAPIPutOne Update") - uriList := nrf_context.GetNofificationUri(nf) + uriList := nrf_context.GetNotificationUri(nf) // set info for NotificationData Notification_event := models.NotificationEventType_PROFILE_CHANGED @@ -486,18 +489,18 @@ func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfi for _, uri := range uriList { problemDetails := SendNFStatusNotify(Notification_event, nfInstanceUri, uri, &nfProfile) if problemDetails != nil { + c.JSON(int(problemDetails.Status), problemDetails) return nil, nil, true, problemDetails - // c.JSON(int(problemDetails.Status), problemDetails) } } header := make(http.Header) header.Add("Location", locationHeaderValue) - // return header, putData, true, nil c.JSON(http.StatusOK, putData) + return header, putData, true, nil } else { // Create NF Profile case logger.NfmLog.Infoln("Create NF Profile") - uriList := nrf_context.GetNofificationUri(nf) + uriList := nrf_context.GetNotificationUri(nf) // set info for NotificationData Notification_event := models.NotificationEventType_REGISTERED nfInstanceUri := locationHeaderValue @@ -505,8 +508,8 @@ func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfi for _, uri := range uriList { problemDetails := SendNFStatusNotify(Notification_event, nfInstanceUri, uri, &nfProfile) if problemDetails != nil { + c.JSON(int(problemDetails.Status), problemDetails) return nil, nil, false, problemDetails - // c.JSON(int(problemDetails.Status), problemDetails) } } @@ -521,10 +524,9 @@ func (p *Processor) NFRegisterProcedure(c *gin.Context, nfProfile models.NfProfi logger.NfmLog.Warnln(err) } } + c.JSON(http.StatusCreated, putData) return header, putData, false, nil - // c.JSON(http.StatusCreated, putData) } - return nil, nil, false, nil } func copyNotificationNfProfile(notifProfile *models.NfProfileNotificationData, nfProfile *models.NfProfile) {