From 2ec87a9ad0b1507b617f806bc54ae40bd9b6eec1 Mon Sep 17 00:00:00 2001 From: chh Date: Tue, 22 Oct 2024 13:21:39 +0000 Subject: [PATCH] refactor: move udr client into consumer udr_service --- internal/sbi/consumer/udr_service.go | 168 ++++++++++++++++++ internal/sbi/processor/ampolicy.go | 23 ++- internal/sbi/processor/bdtpolicy.go | 64 +++---- internal/sbi/processor/policyauthorization.go | 32 ++-- internal/sbi/processor/smpolicy.go | 24 ++- 5 files changed, 235 insertions(+), 76 deletions(-) diff --git a/internal/sbi/consumer/udr_service.go b/internal/sbi/consumer/udr_service.go index f471cd1..64a9f34 100644 --- a/internal/sbi/consumer/udr_service.go +++ b/internal/sbi/consumer/udr_service.go @@ -42,6 +42,174 @@ func (s *nudrService) getDataSubscription(uri string) *DataRepository.APIClient s.nfDataSubClients[uri] = client return client } +func (s *nudrService) GetSessionManagementPolicyData(uri string, + ueId string, sliceInfo *models.Snssai, dnn string) ( + resp *DataRepository.ReadSessionManagementPolicyDataResponse, + problemDetails *models.ProblemDetails, err error, +) { + if uri == "" { + problemDetail := util.GetProblemDetail("Can't find any UDR which supported to this PCF", "GetSessionManagementPolicyData Can't find UDR URI") + return nil, &problemDetail, nil + } + + if ueId == "" { + problemDetail := util.GetProblemDetail("Can't find any UDR which supported to this PCF", "GetSessionManagementPolicyData Can't find UE ID") + return nil, &problemDetail, nil + } + + if sliceInfo == nil { + problemDetails := util.GetProblemDetail("Can't find any UDR which supported to this PCF", "GetSessionManagementPolicyData Can't find Slice Info") + return nil, &problemDetails, nil + } + + if dnn == "" { + problemDetails := util.GetProblemDetail("Can't find any UDR which supported to this PCF", "GetSessionManagementPolicyData Can't find DNN") + return nil, &problemDetails, nil + } + + ctx, pd, err := s.consumer.Context().GetTokenCtx(models.ServiceName_NUDR_DR, models.NrfNfManagementNfType_UDR) + if err != nil { + return nil, nil, err + } else if pd != nil { + return nil, pd, nil + } + + client := s.getDataSubscription(uri) + param := DataRepository.ReadSessionManagementPolicyDataRequest{ + UeId: &ueId, + Snssai: sliceInfo, + Dnn: &dnn, + } + resp, err = client.SessionManagementPolicyDataDocumentApi.ReadSessionManagementPolicyData(ctx, ¶m) + if err == nil { + return resp, nil, nil + } + problem := err.(openapi.GenericOpenAPIError).Model().(models.ProblemDetails) + problemDetails = &problem + return nil, problemDetails, nil +} + +func (s *nudrService) CreateBdtData(uri string, bdtData *models.BdtData) ( + problemDetails *models.ProblemDetails, err error, +) { + if uri == "" { + problemDetail := util.GetProblemDetail("Can't find any UDR which supported to this PCF", "CreateBdtData Can't find UDR URI") + return &problemDetail, nil + } + + ctx, pd, err := s.consumer.Context().GetTokenCtx(models.ServiceName_NUDR_DR, models.NrfNfManagementNfType_UDR) + if err != nil { + return nil, err + } else if pd != nil { + return pd, nil + } + + client := s.getDataSubscription(uri) + param := DataRepository.CreateIndividualBdtDataRequest{ + BdtData: bdtData, + } + _, err = client.IndividualBdtDataDocumentApi.CreateIndividualBdtData(ctx, ¶m) + if err == nil { + return nil, nil + } + problem := err.(openapi.GenericOpenAPIError).Model().(models.ProblemDetails) + problemDetails = &problem + return problemDetails, nil +} + +func (s *nudrService) CreateBdtPolicyContext(uri string, req *DataRepository.ReadBdtDataRequest) ( + resp *DataRepository.ReadBdtDataResponse, problemDetails *models.ProblemDetails, err error, +) { + if uri == "" { + problemDetail := util.GetProblemDetail("Can't find any UDR which supported to this PCF", "CreateBdtData Can't find UDR URI") + return nil, &problemDetail, nil + } + ctx, pd, err := s.consumer.Context().GetTokenCtx(models.ServiceName_NUDR_DR, models.NrfNfManagementNfType_UDR) + if err != nil { + return nil, nil, err + } else if pd != nil { + return nil, pd, nil + } + + client := s.getDataSubscription(uri) + resp, err = client.BdtDataStoreApi.ReadBdtData(ctx, req) + if err == nil { + return resp, nil, nil + } + problem := err.(openapi.GenericOpenAPIError).Model().(models.ProblemDetails) + problemDetails = &problem + return nil, problemDetails, nil +} + +func (s *nudrService) GetBdtData(uri string, bdtRefId string) ( + resp *DataRepository.ReadIndividualBdtDataResponse, problemDetails *models.ProblemDetails, err error, +) { + if uri == "" { + problemDetail := util.GetProblemDetail("Can't find any UDR which supported to this PCF", "GetBdtData Can't find UDR URI") + return nil, &problemDetail, nil + } + + if bdtRefId == "" { + problemDetail := util.GetProblemDetail("Can't find any UDR which supported to this PCF", "GetBdtData Can't find BdtRefId") + return nil, &problemDetail, nil + } + + ctx, pd, err := s.consumer.Context().GetTokenCtx(models.ServiceName_NUDR_DR, models.NrfNfManagementNfType_UDR) + if err != nil { + return nil, nil, err + } else if pd != nil { + return nil, pd, nil + } + + readBdtDataReq := DataRepository.ReadIndividualBdtDataRequest{ + BdtReferenceId: &bdtRefId, + } + + client := s.getDataSubscription(uri) + resp, err = client.IndividualBdtDataDocumentApi.ReadIndividualBdtData(ctx, &readBdtDataReq) + if err == nil { + return resp, nil, nil + } + problem := err.(openapi.GenericOpenAPIError).Model().(models.ProblemDetails) + problemDetails = &problem + return nil, problemDetails, nil +} + +func (s *nudrService) GetAccessAndMobilityPolicyData(ue *pcf_context.UeContext) ( + resp *DataRepository.ReadAccessAndMobilityPolicyDataResponse, + problemDetails *models.ProblemDetails, err error, +) { + if ue.Supi == "" { + problemDetail := util.GetProblemDetail("Can't find corresponding SUPI with UE", util.USER_UNKNOWN) + logger.ConsumerLog.Warn("Can't find corresponding SUPI with UE") + return nil, &problemDetail, nil + } + + if ue.UdrUri == "" { + problemDetail := util.GetProblemDetail("Can't find corresponding UDR with UE", util.USER_UNKNOWN) + logger.ConsumerLog.Warnf("Can't find corresponding UDR with UE[%s]", ue.Supi) + return nil, &problemDetail, nil + } + + ctx, pd, err := s.consumer.Context().GetTokenCtx(models.ServiceName_NUDR_DR, models.NrfNfManagementNfType_UDR) + if err != nil { + return nil, nil, err + } else if pd != nil { + return nil, pd, nil + } + + client := s.getDataSubscription(ue.UdrUri) + param := DataRepository.ReadAccessAndMobilityPolicyDataRequest{ + UeId: &ue.Supi, + } + resp, err = client.AccessAndMobilityPolicyDataDocumentApi.ReadAccessAndMobilityPolicyData(ctx, ¶m) + if err == nil { + return resp, nil, nil + } + problem := err.(openapi.GenericOpenAPIError).Model().(models.ProblemDetails) + problemDetails = &problem + return nil, problemDetails, nil +} func (s *nudrService) CreateInfluenceDataSubscription(ue *pcf_context.UeContext, request models.SmPolicyContextData) ( subscriptionID string, problemDetails *models.ProblemDetails, err error, diff --git a/internal/sbi/processor/ampolicy.go b/internal/sbi/processor/ampolicy.go index 927859c..9056c6a 100644 --- a/internal/sbi/processor/ampolicy.go +++ b/internal/sbi/processor/ampolicy.go @@ -11,7 +11,6 @@ import ( "github.com/free5gc/openapi" "github.com/free5gc/openapi/models" "github.com/free5gc/openapi/pcf/AMPolicyControl" - "github.com/free5gc/openapi/udr/DataRepository" pcf_context "github.com/free5gc/pcf/internal/context" "github.com/free5gc/pcf/internal/logger" "github.com/free5gc/pcf/internal/util" @@ -219,22 +218,20 @@ func (p *Processor) PostPoliciesProcedure(polAssoId string, assolId := fmt.Sprintf("%s-%d", ue.Supi, ue.PolAssociationIDGenerator) amPolicy := ue.AMPolicyData[assolId] - ctx, pd, err := p.Context().GetTokenCtx(models.ServiceName_NUDR_DR, models.NrfNfManagementNfType_UDR) - if err != nil { - return nil, "", pd - } - if amPolicy == nil || amPolicy.AmPolicyData == nil { - client := util.GetNudrClient(udrUri) - request := DataRepository.ReadAccessAndMobilityPolicyDataRequest{ - UeId: &ue.Supi, - } - response, err := client.AccessAndMobilityPolicyDataDocumentApi.ReadAccessAndMobilityPolicyData(ctx, &request) + + response, problemDetail, err := p.Consumer().GetAccessAndMobilityPolicyData(ue) + if err != nil { - problemDetail := util.GetProblemDetail("Can't find UE AM Policy Data in UDR", util.USER_UNKNOWN) - logger.AmPolicyLog.Errorf("Can't find UE[%s] AM Policy Data in UDR", ue.Supi) + logger.AmPolicyLog.Errorf("Get Access And Mobility Policy Data Error Problem[%+v]", err) + problemDetail := util.GetProblemDetail("Get Access And Mobility Policy Data error[%+v]", err.Error()) + return nil, "", &problemDetail + } else if problemDetail != nil { + logger.AmPolicyLog.Errorf("Get Access And Mobility Policy Data Failed Problem[%+v]", problemDetail) + problemDetail := util.GetProblemDetail("Get Access And Mobility Policy Data Failed[%+v]", problemDetail.Cause) return nil, "", &problemDetail } + if response == nil { amPolicy = ue.NewUeAMPolicyData(assolId, policyAssociationRequest) } else { diff --git a/internal/sbi/processor/bdtpolicy.go b/internal/sbi/processor/bdtpolicy.go index 0b1768f..f23a956 100644 --- a/internal/sbi/processor/bdtpolicy.go +++ b/internal/sbi/processor/bdtpolicy.go @@ -54,7 +54,7 @@ func (p *Processor) HandleUpdateBDTPolicyContextProcedure( pcfSelf := p.Context() var bdtPolicy *models.BdtPolicy - if value, ok := p.Context().BdtPolicyPool.Load(bdtPolicyID); ok { + if value, ok := pcfSelf.BdtPolicyPool.Load(bdtPolicyID); ok { bdtPolicy = value.(*models.BdtPolicy) } else { // not found @@ -77,18 +77,27 @@ func (p *Processor) HandleUpdateBDTPolicyContextProcedure( if polReq.NwAreaInfo != nil { bdtData.NwAreaInfo = polReq.NwAreaInfo } - param := DataRepository.CreateIndividualBdtDataRequest{ - BdtData: &bdtData, - } - client := util.GetNudrClient(p.getDefaultUdrUri(pcfSelf)) - ctx, pd, err := p.Context().GetTokenCtx(models.ServiceName_NUDR_DR, models.NrfNfManagementNfType_UDR) - if err != nil { + + udrUri := p.getDefaultUdrUri(pcfSelf) + if udrUri == "" { + // Can't find any UDR support this Ue + pd := &models.ProblemDetails{ + Status: http.StatusServiceUnavailable, + Detail: "Can't find any UDR which supported to this PCF", + } + logger.BdtPolicyLog.Warnf(pd.Detail) c.JSON(int(pd.Status), pd) return } - _, err = client.IndividualBdtDataDocumentApi.CreateIndividualBdtData(ctx, ¶m) + pd, err := p.Consumer().CreateBdtData(udrUri, &bdtData) if err != nil { logger.BdtPolicyLog.Warnf("UDR Put BdtDate error[%s]", err.Error()) + c.JSON(http.StatusInternalServerError, err.Error()) + return + } else if pd != nil { + logger.BdtPolicyLog.Warnf("UDR Put BdtDate fault[%s]", pd.Detail) + c.JSON(int(pd.Status), pd) + return } logger.BdtPolicyLog.Tracef("bdtPolicyID[%s] has Updated with SelTransPolicyId[%d]", @@ -111,7 +120,7 @@ func (p *Processor) HandleCreateBDTPolicyContextRequest( requestMsg models.BdtReqData, ) { // step 1: log - logger.BdtPolicyLog.Infof("Handle CreateBDTPolicyContext") + logger.BdtPolicyLog.Infof("Handle CreateBdtPolicyContext") var problemDetails *models.ProblemDetails @@ -141,20 +150,10 @@ func (p *Processor) HandleCreateBDTPolicyContextRequest( return } pcfSelf.DefaultUdrURI = udrUri - pcfSelf.SetDefaultUdrURI(udrUri) // Query BDT DATA array from UDR - ctx, pd, err := p.Context().GetTokenCtx(models.ServiceName_NUDR_DR, models.NrfNfManagementNfType_UDR) - if err != nil { - c.JSON(int(pd.Status), pd) - return - } - - client := util.GetNudrClient(udrUri) req := DataRepository.ReadBdtDataRequest{} - resp, err := client.BdtDataStoreApi.ReadBdtData(ctx, &req) - bdtDatas := resp.BdtData - + resp, problemDetails, err := p.Consumer().CreateBdtPolicyContext(udrUri, &req) if err != nil { problemDetails = &models.ProblemDetails{ Status: http.StatusServiceUnavailable, @@ -163,7 +162,12 @@ func (p *Processor) HandleCreateBDTPolicyContextRequest( logger.BdtPolicyLog.Warnf("Query to UDR failed") c.JSON(int(problemDetails.Status), problemDetails) return + } else if problemDetails != nil { + c.JSON(int(problemDetails.Status), problemDetails) + return } + + bdtDatas := resp.BdtData // TODO: decide BDT Policy from other bdt policy data response.BdtReqData = deepcopy.Copy(requestMsg).(*models.BdtReqData) var bdtData *models.BdtData @@ -210,22 +214,20 @@ func (p *Processor) HandleCreateBDTPolicyContextRequest( pcfSelf.BdtPolicyPool.Store(bdtPolicyID, response) // Update UDR BDT Data(PUT) - param := DataRepository.CreateIndividualBdtDataRequest{ - BdtData: bdtData, - } - - if _, rspErr := client.IndividualBdtDataDocumentApi.CreateIndividualBdtData(ctx, - ¶m); rspErr != nil { - logger.BdtPolicyLog.Warnf("UDR Put BdtDate error[%s]", rspErr.Error()) + problemDetails, err = p.Consumer().CreateBdtData(udrUri, bdtData) + if err != nil { + logger.BdtPolicyLog.Warnf("UDR Put BdtDate error[%s]", err.Error()) + c.JSON(http.StatusInternalServerError, err.Error()) + return + } else if problemDetails != nil { + logger.BdtPolicyLog.Warnf("UDR Put BdtDate fault[%s]", problemDetails.Detail) + c.JSON(int(problemDetails.Status), problemDetails) + return } locationHeader := util.GetResourceUri(models.ServiceName_NPCF_BDTPOLICYCONTROL, bdtPolicyID) logger.BdtPolicyLog.Tracef("BDT Policy Id[%s] Create", bdtPolicyID) - if problemDetails != nil { - c.JSON(int(problemDetails.Status), problemDetails) - return - } c.Header("Location", locationHeader) c.JSON(http.StatusCreated, response) } diff --git a/internal/sbi/processor/policyauthorization.go b/internal/sbi/processor/policyauthorization.go index a079ddc..9d59773 100644 --- a/internal/sbi/processor/policyauthorization.go +++ b/internal/sbi/processor/policyauthorization.go @@ -12,7 +12,6 @@ import ( "github.com/free5gc/openapi" "github.com/free5gc/openapi/models" "github.com/free5gc/openapi/pcf/PolicyAuthorization" - "github.com/free5gc/openapi/udr/DataRepository" pcf_context "github.com/free5gc/pcf/internal/context" "github.com/free5gc/pcf/internal/logger" "github.com/free5gc/pcf/internal/util" @@ -956,7 +955,6 @@ func (p *Processor) HandleUpdateEventsSubscContext( appSessCtx.EvsNotif = nil } - // r15 : UpdateEventsSubscResponse => EvsNotif和eventsSubscReqData裡面的東西被拆出來放在 r17 : EventsSubscPutData裡面, resp := models.EventsSubscPutData{ Events: eventsSubscReqData.Events, NotifUri: eventsSubscReqData.NotifUri, @@ -1027,16 +1025,16 @@ func (p *Processor) SendAppSessionEventNotification(appSession *pcf_context.AppS req := PolicyAuthorization.UpdateEventsSubscEventNotificationPostRequest{ PcfPolicyAuthorizationEventsNotification: &request, } - httpResponse, err := client.EventsSubscriptionDocumentApi.UpdateEventsSubscEventNotificationPost( + rsp, err := client.EventsSubscriptionDocumentApi.UpdateEventsSubscEventNotificationPost( ctx, uri, &req) if err != nil { - if httpResponse == nil { + if rsp == nil { logger.PolicyAuthLog.Warnf("Send App Session Event Notification Error") } else { logger.PolicyAuthLog.Warnf("Send App Session Event Notification Failed[%s]", err.Error()) } return - } else if httpResponse == nil { + } else if rsp == nil { logger.PolicyAuthLog.Warnln("Send App Session Event Notification Failed[HTTP Response is nil]") return } @@ -1062,16 +1060,16 @@ func (p *Processor) SendAppSessionTermination(appSession *pcf_context.AppSession req := PolicyAuthorization.PostAppSessionsTerminationRequestPostRequest{ TerminationInfo: &request, } - httpResponse, err := client.ApplicationSessionsCollectionApi.PostAppSessionsTerminationRequestPost( + rsp, err := client.ApplicationSessionsCollectionApi.PostAppSessionsTerminationRequestPost( ctx, uri, &req) if err != nil { - if httpResponse != nil { + if rsp != nil { logger.PolicyAuthLog.Warnf("Send App Session Termination Error") } else { logger.PolicyAuthLog.Warnf("Send App Session Termination Failed[%s]", err.Error()) } return - } else if httpResponse == nil { + } else if rsp == nil { logger.PolicyAuthLog.Warnln("Send App Session Termination Failed[HTTP Response is nil]") return } @@ -1096,19 +1094,17 @@ func (p *Processor) handleBDTPolicyInd(pcfSelf *pcf_context.PCFContext, requestSuppFeat).String(), } - ctx, _, err := p.Context().GetTokenCtx(models.ServiceName_NUDR_DR, models.NrfNfManagementNfType_UDR) - if err != nil { + udrUri := p.getDefaultUdrUri(pcfSelf) + if udrUri == "" { + err = fmt.Errorf("Can't find any UDR which supported to this PCF") return err } - - client := util.GetNudrClient(p.getDefaultUdrUri(pcfSelf)) - readBdtDataReq := DataRepository.ReadIndividualBdtDataRequest{ - BdtReferenceId: &req.BdtRefId, - } - resp, err1 := client.IndividualBdtDataDocumentApi.ReadIndividualBdtData(ctx, &readBdtDataReq) + resp, pd, err := p.Consumer().GetBdtData(udrUri, req.BdtRefId) bdtData := resp.BdtData - if err1 != nil { - return fmt.Errorf("UDR Get BdtData error[%s]", err1.Error()) + if err != nil { + return fmt.Errorf("UDR Get BdtData error[%s]", err.Error()) + } else if pd != nil { + return fmt.Errorf("UDR Get BdtData fault[%s]", pd.Detail) } else if resp == nil { return fmt.Errorf("UDR Get BdtData error") } else { diff --git a/internal/sbi/processor/smpolicy.go b/internal/sbi/processor/smpolicy.go index 1294080..e9c82f9 100644 --- a/internal/sbi/processor/smpolicy.go +++ b/internal/sbi/processor/smpolicy.go @@ -67,26 +67,22 @@ func (p *Processor) HandleCreateSmPolicyRequest( smPolicyID := fmt.Sprintf("%s-%d", ue.Supi, request.PduSessionId) smPolicyData := ue.SmPolicyData[smPolicyID] if smPolicyData == nil || smPolicyData.SmPolicyData == nil { - client := util.GetNudrClient(udrUri) - - ctx, pd, err1 := p.Context().GetTokenCtx(models.ServiceName_NUDR_DR, models.NrfNfManagementNfType_UDR) - if err1 != nil { - c.JSON(int(pd.Status), pd) - return - } - req := DataRepository.ReadSessionManagementPolicyDataRequest{ - UeId: &ue.Supi, - Snssai: request.SliceInfo, - Dnn: &request.Dnn, - } var response *DataRepository.ReadSessionManagementPolicyDataResponse - response, err1 = client.SessionManagementPolicyDataDocumentApi.ReadSessionManagementPolicyData(ctx, &req) + response, pd, err := p.Consumer().GetSessionManagementPolicyData( + udrUri, + ue.Supi, + request.SliceInfo, + request.Dnn, + ) smData = response.SmPolicyData - if err1 != nil || response == nil { + if err != nil || response == nil { problemDetail := util.GetProblemDetail("Can't find UE SM Policy Data in UDR", util.USER_UNKNOWN) logger.SmPolicyLog.Warnf("Can't find UE[%s] SM Policy Data in UDR", ue.Supi) c.JSON(int(problemDetail.Status), problemDetail) return + } else if pd != nil { + c.JSON(int(pd.Status), pd) + return } // TODO: subscribe to UDR } else {