Skip to content

Commit

Permalink
style: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
HanHongChen committed Jun 10, 2024
1 parent c4ec427 commit 9b72159
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 39 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/bin/
# Swap files
*.swp

Expand Down
25 changes: 25 additions & 0 deletions internal/sbi/api_httpcallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/free5gc/openapi"
"github.com/free5gc/openapi/models"
"github.com/free5gc/pcf/internal/logger"
"github.com/free5gc/pcf/internal/util"
)

func (s *Server) getHttpCallBackRoutes() []Route {
Expand Down Expand Up @@ -95,6 +96,14 @@ func (s *Server) HTTPUdrPolicyDataChangeNotify(c *gin.Context) {
}

supi := c.Params.ByName("supi")
if supi == "" {
problemDetails := &models.ProblemDetails{
Title: util.ERROR_INITIAL_PARAMETERS,
Status: http.StatusBadRequest,
}
c.JSON(http.StatusBadRequest, problemDetails)
return
}
s.Processor().HandlePolicyDataChangeNotify(c, supi, policyDataChangeNotification)
}

Expand Down Expand Up @@ -129,6 +138,22 @@ func (s *Server) HTTPUdrInfluenceDataUpdateNotify(c *gin.Context) {
}

supi := c.Params.ByName("supi")
if supi == "" {
problemDetails := &models.ProblemDetails{
Title: util.ERROR_INITIAL_PARAMETERS,
Status: http.StatusBadRequest,
}
c.JSON(http.StatusBadRequest, problemDetails)
return
}
pduSessionId := c.Params.ByName("pduSessionId")
if pduSessionId == "" {
problemDetails := &models.ProblemDetails{
Title: util.ERROR_INITIAL_PARAMETERS,
Status: http.StatusBadRequest,
}
c.JSON(http.StatusBadRequest, problemDetails)
return
}
s.Processor().HandleInfluenceDataUpdateNotify(c, supi, pduSessionId, trafficInfluDataNotif)
}
30 changes: 30 additions & 0 deletions internal/sbi/api_oam.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,32 @@ package sbi
import (
"net/http"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"

"github.com/free5gc/openapi/models"
"github.com/free5gc/pcf/internal/util"
)

const (
CorsConfigMaxAge = 86400
)

func (s *Server) setCorsHeader(c *gin.Context) {
// TODO: 1. turn these values into configurable variables
// TODO: 2. use the official cors middleware
s.router.Use(cors.New(cors.Config{
AllowMethods: []string{"GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE"},
AllowHeaders: []string{
"Origin", "Content-Length", "Content-Type", "User-Agent",
"Referrer", "Host", "Token", "X-Requested-With",
},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
AllowAllOrigins: true,
MaxAge: CorsConfigMaxAge,
}))

c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set(
Expand All @@ -19,6 +41,14 @@ func (s *Server) HTTPOAMGetAmPolicy(c *gin.Context) {
s.setCorsHeader(c)

supi := c.Params.ByName("supi")
if supi == "" {
problemDetails := &models.ProblemDetails{
Title: util.ERROR_INITIAL_PARAMETERS,
Status: http.StatusBadRequest,
}
c.JSON(http.StatusBadRequest, problemDetails)
return
}
s.Processor().HandleOAMGetAmPolicyRequest(c, supi)
}

Expand Down
8 changes: 8 additions & 0 deletions internal/sbi/api_smpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,13 @@ func (s *Server) HTTPSmPoliciesSmPolicyIdUpdatePost(c *gin.Context) {
}

smPolicyId := c.Params.ByName("smPolicyId")
if smPolicyId == "" {
problemDetails := &models.ProblemDetails{
Title: util.ERROR_INITIAL_PARAMETERS,
Status: http.StatusBadRequest,
}
c.JSON(http.StatusBadRequest, problemDetails)
return
}
s.Processor().HandleUpdateSmPolicyContextRequest(c, smPolicyId, smPolicyUpdateContextData)
}
4 changes: 4 additions & 0 deletions internal/sbi/api_uepolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,20 @@ func (s *Server) getUePolicyRoutes() []Route {

// PoliciesPolAssoIdDelete -
func (s *Server) PoliciesPolAssoIdDelete(c *gin.Context) {
c.JSON(http.StatusNotImplemented, nil)
}

// PoliciesPolAssoIdGet -
func (s *Server) PoliciesPolAssoIdGet(c *gin.Context) {
c.JSON(http.StatusNotImplemented, nil)
}

// PoliciesPolAssoIdUpdatePost -
func (s *Server) PoliciesPolAssoIdUpdatePost(c *gin.Context) {
c.JSON(http.StatusNotImplemented, nil)
}

// PoliciesPost -
func (s *Server) PoliciesPost(c *gin.Context) {
c.JSON(http.StatusNotImplemented, nil)
}
20 changes: 3 additions & 17 deletions internal/sbi/consumer/nrf_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ func (s *nnrfService) SendSearchNFInstances(
if err != nil {
logger.ConsumerLog.Errorf("SearchNFInstances failed: %+v", err)
}

defer func() {
if resCloseErr := res.Body.Close(); resCloseErr != nil {
logger.ConsumerLog.Errorf("NFInstancesStoreApi response body cannot close: %+v", resCloseErr)
}
}()

if res != nil && res.StatusCode == http.StatusTemporaryRedirect {
return nil, fmt.Errorf("Temporary Redirect For Non NRF Consumer")
}
Expand All @@ -108,14 +110,6 @@ func (s *nnrfService) SendNFInstancesUDR(nrfUri, id string) string {
localVarOptionals := Nnrf_NFDiscovery.SearchNFInstancesParamOpts{
// DataSet: optional.NewInterface(models.DataSetId_SUBSCRIPTION),
}
// switch types {
// case NFDiscoveryToUDRParamSupi:
// localVarOptionals.Supi = optional.NewString(id)
// case NFDiscoveryToUDRParamExtGroupId:
// localVarOptionals.ExternalGroupIdentity = optional.NewString(id)
// case NFDiscoveryToUDRParamGpsi:
// localVarOptionals.Gpsi = optional.NewString(id)
// }

result, err := s.SendSearchNFInstances(nrfUri, targetNfType, requestNfType, localVarOptionals)
if err != nil {
Expand Down Expand Up @@ -156,14 +150,6 @@ func (s *nnrfService) SendNFInstancesAMF(nrfUri string, guami models.Guami, serv
localVarOptionals := Nnrf_NFDiscovery.SearchNFInstancesParamOpts{
Guami: optional.NewInterface(util.MarshToJsonString(guami)),
}
// switch types {
// case NFDiscoveryToUDRParamSupi:
// localVarOptionals.Supi = optional.NewString(id)
// case NFDiscoveryToUDRParamExtGroupId:
// localVarOptionals.ExternalGroupIdentity = optional.NewString(id)
// case NFDiscoveryToUDRParamGpsi:
// localVarOptionals.Gpsi = optional.NewString(id)
// }

result, err := s.SendSearchNFInstances(nrfUri, targetNfType, requestNfType, localVarOptionals)
if err != nil {
Expand Down Expand Up @@ -226,7 +212,7 @@ func (s *nnrfService) SendRegisterNFInstance(ctx context.Context) (
for {
nf, res, err = client.NFInstanceIDDocumentApi.RegisterNFInstance(context.TODO(), pcfContext.NfId, nfProfile)
if err != nil || res == nil {
logger.ConsumerLog.Errorf("CHF register to NRF Error[%v]", err)
logger.ConsumerLog.Errorf("PCF register to NRF Error[%v]", err)
time.Sleep(2 * time.Second)
continue
}
Expand Down
1 change: 0 additions & 1 deletion internal/sbi/processor/bdtpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ func (p *Processor) HandleCreateBDTPolicyContextRequest(
var problemDetails *models.ProblemDetails

// step 2: retrieve request and check mandatory contents
// requestMsg := request.Body.(models.BdtReqData)
if requestMsg.AspId == "" || requestMsg.DesTimeInt == nil || requestMsg.NumOfUes == 0 || requestMsg.VolPerUe == nil {
logger.BdtPolicyLog.Errorf("Required BdtReqData not found: AspId[%+v], DesTimeInt[%+v], NumOfUes[%+v], VolPerUe[%+v]",
requestMsg.AspId, requestMsg.DesTimeInt, requestMsg.NumOfUes, requestMsg.VolPerUe)
Expand Down
21 changes: 2 additions & 19 deletions internal/sbi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"sync"
"time"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"

Expand All @@ -25,10 +24,6 @@ import (
logger_util "github.com/free5gc/util/logger"
)

const (
CorsConfigMaxAge = 86400
)

type Route struct {
Method string
Pattern string
Expand Down Expand Up @@ -116,18 +111,6 @@ func NewServer(pcf pcf, tlsKeyLogPath string) (*Server, error) {
uePolicyGroup := s.router.Group(factory.PcfUePolicyCtlResUriPrefix)
applyRoutes(uePolicyGroup, uePolicyRoutes)

s.router.Use(cors.New(cors.Config{
AllowMethods: []string{"GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE"},
AllowHeaders: []string{
"Origin", "Content-Length", "Content-Type", "User-Agent",
"Referrer", "Host", "Token", "X-Requested-With",
},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
AllowAllOrigins: true,
MaxAge: CorsConfigMaxAge,
}))

cfg := s.Config()
bindAddr := cfg.GetSbiBindingAddr()
logger.SBILog.Infof("Binding addr: [%s]", bindAddr)
Expand All @@ -145,7 +128,7 @@ func (s *Server) Run(traceCtx context.Context, wg *sync.WaitGroup) error {
var err error
_, s.Context().NfId, err = s.Consumer().SendRegisterNFInstance(context.Background())
if err != nil {
logger.InitLog.Errorf("CHF register to NRF Error[%s]", err.Error())
logger.InitLog.Errorf("PCF register to NRF Error[%s]", err.Error())
}

wg.Add(1)
Expand All @@ -154,7 +137,7 @@ func (s *Server) Run(traceCtx context.Context, wg *sync.WaitGroup) error {
return nil
}

func (s *Server) Stop(traceCtx context.Context) {
func (s *Server) Shutdown(traceCtx context.Context) {
const defaultShutdownTimeout time.Duration = 2 * time.Second

if s.httpServer != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (a *PcfApp) listenShutdownEvent() {

func (a *PcfApp) CallServerStop() {
if a.sbiServer != nil {
a.sbiServer.Stop(context.Background())
a.sbiServer.Shutdown(context.Background())
}
}

Expand Down

0 comments on commit 9b72159

Please sign in to comment.