diff --git a/.gitignore b/.gitignore index b457816..c294e4d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -/bin/ # Swap files *.swp diff --git a/internal/sbi/api_httpcallback.go b/internal/sbi/api_httpcallback.go index f1ddb79..f2f980d 100644 --- a/internal/sbi/api_httpcallback.go +++ b/internal/sbi/api_httpcallback.go @@ -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 { @@ -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) } @@ -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) } diff --git a/internal/sbi/api_oam.go b/internal/sbi/api_oam.go index 177be54..d6e4199 100644 --- a/internal/sbi/api_oam.go +++ b/internal/sbi/api_oam.go @@ -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( @@ -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) } diff --git a/internal/sbi/api_smpolicy.go b/internal/sbi/api_smpolicy.go index 0ac65b6..9e770fd 100644 --- a/internal/sbi/api_smpolicy.go +++ b/internal/sbi/api_smpolicy.go @@ -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) } diff --git a/internal/sbi/api_uepolicy.go b/internal/sbi/api_uepolicy.go index 52cfab4..9eb778d 100644 --- a/internal/sbi/api_uepolicy.go +++ b/internal/sbi/api_uepolicy.go @@ -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) } diff --git a/internal/sbi/consumer/nrf_service.go b/internal/sbi/consumer/nrf_service.go index 37f2a4a..17cbc13 100644 --- a/internal/sbi/consumer/nrf_service.go +++ b/internal/sbi/consumer/nrf_service.go @@ -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") } @@ -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 { @@ -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 { @@ -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 } diff --git a/internal/sbi/processor/bdtpolicy.go b/internal/sbi/processor/bdtpolicy.go index 02151ce..05de96d 100644 --- a/internal/sbi/processor/bdtpolicy.go +++ b/internal/sbi/processor/bdtpolicy.go @@ -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) diff --git a/internal/sbi/server.go b/internal/sbi/server.go index 0240a80..93e7abb 100644 --- a/internal/sbi/server.go +++ b/internal/sbi/server.go @@ -9,7 +9,6 @@ import ( "sync" "time" - "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" @@ -25,10 +24,6 @@ import ( logger_util "github.com/free5gc/util/logger" ) -const ( - CorsConfigMaxAge = 86400 -) - type Route struct { Method string Pattern string @@ -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) @@ -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) @@ -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 { diff --git a/pkg/service/init.go b/pkg/service/init.go index 47ec3d0..abba858 100644 --- a/pkg/service/init.go +++ b/pkg/service/init.go @@ -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()) } }