Skip to content

Commit

Permalink
refactor: implement processor
Browse files Browse the repository at this point in the history
  • Loading branch information
KunLee76 committed May 12, 2024
1 parent 468b237 commit fc815f9
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 1,220 deletions.
55 changes: 26 additions & 29 deletions internal/sbi/api_accesstoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/

package sbi
package sbi

import (
"encoding/json"
"net/http"
"reflect"

"github.com/gin-gonic/gin"

"github.com/free5gc/nrf/internal/logger"
"github.com/free5gc/nrf/pkg/factory"
"github.com/free5gc/openapi/models"
"github.com/free5gc/util/httpwrapper"
"strings"

logger_util "github.com/free5gc/util/logger"
)
import (
"encoding/json"
"net/http"
"reflect"

"github.com/gin-gonic/gin"

"github.com/free5gc/nrf/internal/logger"
"github.com/free5gc/nrf/pkg/factory"
"github.com/free5gc/openapi/models"
)

// Index is the index handler.
func Index(c *gin.Context) {
c.String(http.StatusOK, "Hello World!")
}

func (s *Server) getAccessTokenRoutes() []Route {
return []Route{
Expand All @@ -38,15 +39,16 @@ func (s *Server) getAccessTokenRoutes() []Route {
Method: http.MethodPost,
Pattern: "/oauth2/token",
APIFunc: s.apiAccessTokenRequest,
},
},
},
}
}

// AccessTokenRequest - Access Token Request
func (s *Server) apiAccessTokenRequest(c *gin.Context) {
logger.AccTokenLog.Infoln("In apiAccessTokenRequest")
func (s *Server) apiAccessTokenRequest(c *gin.Context) {
logger.AccTokenLog.Debugln("Handle AccessTokenRequest")

authEnable := factory.NrfConfig.GetOAuth()

if !factory.NrfConfig.GetOAuth() {
if !authEnable {
rsp := models.ProblemDetails{
Title: "OAuth2 not enable",
Status: http.StatusBadRequest,
Expand Down Expand Up @@ -108,11 +110,6 @@ func (s *Server) getAccessTokenRoutes() []Route {
c.JSON(http.StatusBadRequest, rsp)
return
}

req := httpwrapper.NewRequest(c.Request, accessTokenReq)
req.Params["paramName"] = c.Params.ByName("paramName")

httpResponse := producer.HandleAccessTokenRequest(req)

c.JSON(httpResponse.Status, httpResponse.Body)
hdlRsp := s.Processor().AccessTokenProcedure(accessTokenReq)
s.buildAndSendHttpResponse(c, hdlRsp, false)
}
7 changes: 3 additions & 4 deletions internal/sbi/api_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
"github.com/gin-gonic/gin"

"github.com/free5gc/nrf/internal/logger"
"github.com/free5gc/nrf/internal/sbi/producer"
"github.com/free5gc/nrf/internal/sbi/processor"
"github.com/free5gc/openapi"
"github.com/free5gc/openapi/models"
"github.com/free5gc/util/httpwrapper"
"github.com/free5gc/nrf/internal/sbi"
)

func (s *Server) getNFDiscoveryRoutes() []Route {
Expand All @@ -41,15 +40,15 @@

// SearchNFInstances - Search a collection of NF Instances
func (s *Server) getSearchNFInstances(c *gin.Context) {
auth_err := authorizationCheck(c)
auth_err :=authorizationCheck(c, "nnrf-disc")
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
}

req := httpwrapper.NewRequest(c.Request, nil)
req.Query = c.Request.URL.Query()
httpResponse := producer.HandleNFDiscoveryRequest(req)
httpResponse := processor.HandleNFDiscoveryRequest(req)

responseBody, err := openapi.Serialize(httpResponse.Body, "application/json")
if err != nil {
Expand Down
32 changes: 16 additions & 16 deletions internal/sbi/api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
"github.com/free5gc/openapi/models"
timedecode "github.com/free5gc/util/mapstruct"
"github.com/free5gc/util/mongoapi"
"github.com/free5gc/nrf/internal/sbi/producer"
"github.com/free5gc/openapi"
"github.com/free5gc/util/httpwrapper"
"github.com/free5gc/nrf/internal/sbi/processor"
)

func (s *Server) getNFManagementRoutes() []Route {
Expand Down Expand Up @@ -76,7 +76,7 @@ func (s *Server) getNFManagementRoutes() []Route {

// getDeregisterNFInstance - Deregisters a given NF Instance
func (s *Server) getDeregisterNFInstance(c *gin.Context) {
auth_err := authorizationCheck(c)
auth_err := authorizationCheck(c, "nnrf-nfm")
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
Expand All @@ -85,7 +85,7 @@ func (s *Server) getDeregisterNFInstance(c *gin.Context) {
req := httpwrapper.NewRequest(c.Request, nil)
req.Params["nfInstanceID"] = c.Params.ByName("nfInstanceID")

httpResponse := producer.HandleNFDeregisterRequest(req)
httpResponse := processor.HandleNFDeregisterRequest(req)

responseBody, err := openapi.Serialize(httpResponse.Body, "application/json")
if err != nil {
Expand All @@ -103,7 +103,7 @@ func (s *Server) getDeregisterNFInstance(c *gin.Context) {

// GetNFInstance - Read the profile of a given NF Instance
func (s *Server) getNFInstance(c *gin.Context) {
auth_err := authorizationCheck(c)
auth_err := authorizationCheck(c, "nnrf-nfm")
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
Expand All @@ -112,7 +112,7 @@ func (s *Server) getNFInstance(c *gin.Context) {
req := httpwrapper.NewRequest(c.Request, nil)
req.Params["nfInstanceID"] = c.Params.ByName("nfInstanceID")

httpResponse := producer.HandleGetNFInstanceRequest(req)
httpResponse := processor.HandleGetNFInstanceRequest(req)

responseBody, err := openapi.Serialize(httpResponse.Body, "application/json")
if err != nil {
Expand Down Expand Up @@ -170,7 +170,7 @@ func (s *Server) getRegisterNFInstance(c *gin.Context) {
req := httpwrapper.NewRequest(c.Request, nfprofile)

// step 4: call producer
httpResponse := producer.HandleNFRegisterRequest(req)
httpResponse := processor.HandleNFRegisterRequest(req)

for key, val := range httpResponse.Header {
c.Header(key, val[0])
Expand All @@ -192,7 +192,7 @@ func (s *Server) getRegisterNFInstance(c *gin.Context) {

// UpdateNFInstance - Update NF Instance profile
func (s *Server) getUpdateNFInstance(c *gin.Context) {
auth_err := authorizationCheck(c)
auth_err := authorizationCheck(c, "nnrf-nfm")
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
Expand All @@ -216,7 +216,7 @@ func (s *Server) getUpdateNFInstance(c *gin.Context) {
req.Params["nfInstanceID"] = c.Params.ByName("nfInstanceID")
req.Body = requestBody

httpResponse := producer.HandleUpdateNFInstanceRequest(req)
httpResponse := processor.HandleUpdateNFInstanceRequest(req)

responseBody, err := openapi.Serialize(httpResponse.Body, "application/json")
if err != nil {
Expand All @@ -234,7 +234,7 @@ func (s *Server) getUpdateNFInstance(c *gin.Context) {

// GetNFInstances - Retrieves a collection of NF Instances
func (s *Server) getNFInstances(c *gin.Context) {
auth_err := authorizationCheck(c)
auth_err := authorizationCheck(c, "nnrf-nfm")
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
Expand All @@ -243,7 +243,7 @@ func (s *Server) getNFInstances(c *gin.Context) {
req := httpwrapper.NewRequest(c.Request, nil)
req.Query = c.Request.URL.Query()

httpResponse := producer.HandleGetNFInstancesRequest(req)
httpResponse := processor.HandleGetNFInstancesRequest(req)

responseBody, err := openapi.Serialize(httpResponse.Body, "application/json")
if err != nil {
Expand All @@ -261,7 +261,7 @@ func (s *Server) getNFInstances(c *gin.Context) {

// RemoveSubscription - Deletes a subscription
func (s *Server) getRemoveSubscription(c *gin.Context) {
auth_err := authorizationCheck(c)
auth_err := authorizationCheck(c, "nnrf-nfm")
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
Expand All @@ -270,7 +270,7 @@ func (s *Server) getRemoveSubscription(c *gin.Context) {
req := httpwrapper.NewRequest(c.Request, nil)
req.Params["subscriptionID"] = c.Params.ByName("subscriptionID")

httpResponse := producer.HandleRemoveSubscriptionRequest(req)
httpResponse := processor.HandleRemoveSubscriptionRequest(req)

responseBody, err := openapi.Serialize(httpResponse.Body, "application/json")
if err != nil {
Expand All @@ -288,7 +288,7 @@ func (s *Server) getRemoveSubscription(c *gin.Context) {

// UpdateSubscription - Updates a subscription
func (s *Server) getUpdateSubscription(c *gin.Context) {
auth_err := authorizationCheck(c)
auth_err := authorizationCheck(c, "nnrf-nfm")
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
Expand All @@ -311,7 +311,7 @@ func (s *Server) getUpdateSubscription(c *gin.Context) {
req.Params["subscriptionID"] = c.Params.ByName("subscriptionID")
req.Body = requestBody

httpResponse := producer.HandleUpdateSubscriptionRequest(req)
httpResponse := processor.HandleUpdateSubscriptionRequest(req)
responseBody, err := openapi.Serialize(httpResponse.Body, "application/json")
if err != nil {
logger.NfmLog.Warnln(err)
Expand All @@ -330,7 +330,7 @@ func (s *Server) getUpdateSubscription(c *gin.Context) {

// CreateSubscription - Create a new subscription
func (s *Server) getCreateSubscription(c *gin.Context) {
auth_err := authorizationCheck(c)
auth_err := authorizationCheck(c, "nnrf-nfm")
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
Expand Down Expand Up @@ -368,7 +368,7 @@ func (s *Server) getCreateSubscription(c *gin.Context) {

req := httpwrapper.NewRequest(c.Request, subscription)

httpResponse := producer.HandleCreateSubscriptionRequest(req)
httpResponse := processor.HandleCreateSubscriptionRequest(req)
responseBody, err := openapi.Serialize(httpResponse.Body, "application/json")
if err != nil {
logger.NfmLog.Errorln(err)
Expand Down
48 changes: 0 additions & 48 deletions internal/sbi/discovery/api_nf_instances_store.go

This file was deleted.

91 changes: 0 additions & 91 deletions internal/sbi/discovery/routers.go

This file was deleted.

Loading

0 comments on commit fc815f9

Please sign in to comment.