Skip to content

Commit

Permalink
refactor: move udr client into consumer udr_service
Browse files Browse the repository at this point in the history
  • Loading branch information
HanHongChen committed Oct 22, 2024
1 parent 923d339 commit 2ec87a9
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 76 deletions.
168 changes: 168 additions & 0 deletions internal/sbi/consumer/udr_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, &param)
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, &param)
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, &param)
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,
Expand Down
23 changes: 10 additions & 13 deletions internal/sbi/processor/ampolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Check failure on line 223 in internal/sbi/processor/ampolicy.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

unnecessary leading newline (whitespace)

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 {
Expand Down
64 changes: 33 additions & 31 deletions internal/sbi/processor/bdtpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, &param)
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]",
Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
&param); 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)
}
Expand Down
Loading

0 comments on commit 2ec87a9

Please sign in to comment.