Skip to content

Commit

Permalink
refactor: construct an interface for database connection
Browse files Browse the repository at this point in the history
  • Loading branch information
ian60509 committed Jun 3, 2024
1 parent 1e926a5 commit 8395d10
Show file tree
Hide file tree
Showing 29 changed files with 123 additions and 80 deletions.
20 changes: 20 additions & 0 deletions internal/database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package database

import (
"go.mongodb.org/mongo-driver/bson"

"github.com/free5gc/openapi/models"
)

const (
APPDATA_INFLUDATA_DB_COLLECTION_NAME = "applicationData.influenceData"
APPDATA_INFLUDATA_SUBSC_DB_COLLECTION_NAME = "applicationData.influenceData.subsToNotify"
APPDATA_PFD_DB_COLLECTION_NAME = "applicationData.pfds"
)

type DbConnector interface {
PatchDataToDBAndNotify(collName string, ueId string, patchItem []models.PatchItem, filter bson.M) error
GetDataFromDB(collName string, filter bson.M) (map[string]interface{}, *models.ProblemDetails)
GetDataFromDBWithArg(collName string, filter bson.M, strength int) (map[string]interface{}, *models.ProblemDetails)
DeleteDataFromDB(collName string, filter bson.M)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package processor
package mongodb

import (
"encoding/json"
Expand All @@ -8,17 +8,25 @@ import (
"github.com/free5gc/openapi"
"github.com/free5gc/openapi/models"
"github.com/free5gc/udr/internal/logger"
"github.com/free5gc/udr/internal/sbi/processor"
"github.com/free5gc/udr/internal/util"
"github.com/free5gc/udr/pkg/factory"
"github.com/free5gc/util/mongoapi"
)

const (
APPDATA_INFLUDATA_DB_COLLECTION_NAME = "applicationData.influenceData"
APPDATA_INFLUDATA_SUBSC_DB_COLLECTION_NAME = "applicationData.influenceData.subsToNotify"
APPDATA_PFD_DB_COLLECTION_NAME = "applicationData.pfds"
)
type MongoDbImplement struct {
*factory.Mongodb
}

func patchDataToDBAndNotify(collName string, ueId string, patchItem []models.PatchItem, filter bson.M) error {
func NewMongoDbImplement(m *factory.Mongodb) *MongoDbImplement {
return &MongoDbImplement{
Mongodb: m,
}
}

func (m *MongoDbImplement) PatchDataToDBAndNotify(
collName string, ueId string, patchItem []models.PatchItem, filter bson.M,
) error {
var err error
origValue, err := mongoapi.RestfulAPIGetOne(collName, filter)
if err != nil {
Expand All @@ -38,11 +46,14 @@ func patchDataToDBAndNotify(collName string, ueId string, patchItem []models.Pat
if err != nil {
return err
}
PreHandleOnDataChangeNotify(ueId, CurrentResourceUri, patchItem, origValue, newValue)
processor.PreHandleOnDataChangeNotify(ueId, processor.CurrentResourceUri, patchItem, origValue, newValue)
return nil
}

func getDataFromDB(collName string, filter bson.M) (map[string]interface{}, *models.ProblemDetails) {
func (m *MongoDbImplement) GetDataFromDB(
collName string, filter bson.M) (
map[string]interface{}, *models.ProblemDetails,
) {
data, err := mongoapi.RestfulAPIGetOne(collName, filter)
if err != nil {
return nil, openapi.ProblemDetailsSystemFailure(err.Error())
Expand All @@ -53,7 +64,7 @@ func getDataFromDB(collName string, filter bson.M) (map[string]interface{}, *mod
return data, nil
}

func getDataFromDBWithArg(collName string, filter bson.M, strength int) (
func (m *MongoDbImplement) GetDataFromDBWithArg(collName string, filter bson.M, strength int) (
map[string]interface{}, *models.ProblemDetails,
) {
data, err := mongoapi.RestfulAPIGetOne(collName, filter, strength)
Expand All @@ -68,7 +79,7 @@ func getDataFromDBWithArg(collName string, filter bson.M, strength int) (
return data, nil
}

func deleteDataFromDB(collName string, filter bson.M) {
func (m *MongoDbImplement) DeleteDataFromDB(collName string, filter bson.M) {
if err := mongoapi.RestfulAPIDeleteOne(collName, filter); err != nil {
logger.DataRepoLog.Errorf("deleteDataFromDB: %+v", err)
}
Expand Down
6 changes: 2 additions & 4 deletions internal/sbi/api_datarepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,15 +737,13 @@ func (s *Server) getDataRepositoryRoutes() []Route {
"/application-data/influenceData/:influenceId",
s.HandleApplicationDataInfluenceDataInfluenceIdPut,
},

{
"ApplicationDataInfluenceDataInfluenceIdPut",
strings.ToUpper("Post"),
"/application-data/influenceData/:influenceId",
s.HandleApplicationDataInfluenceDataInfluenceIdPost,
},


}
}

Expand Down Expand Up @@ -2573,7 +2571,7 @@ func (s *Server) HandleApplicationDataInfluenceDataInfluenceIdPut(c *gin.Context

s.Processor().ApplicationDataInfluenceDataInfluenceIdPutProcedure(c, collName, influenceId, &trafficInfluData)
}

func (s *Server) HandleApplicationDataInfluenceDataInfluenceIdPost(c *gin.Context) {
s.Processor().ApplicationDataInfluenceDataInfluenceIdPostProcedure(c)
}

8 changes: 4 additions & 4 deletions internal/sbi/api_sanity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/stretchr/testify/require"

"github.com/free5gc/openapi/models"
db "github.com/free5gc/udr/internal/database"
"github.com/free5gc/udr/internal/logger"
"github.com/free5gc/udr/internal/sbi/processor"
"github.com/free5gc/udr/pkg/factory"
util_logger "github.com/free5gc/util/logger"
"github.com/free5gc/util/mongoapi"
Expand All @@ -37,11 +37,11 @@ func setupHttpServer() *gin.Engine {
func setupMongoDB(t *testing.T) {
err := mongoapi.SetMongoDB("test5gc", "mongodb://localhost:27017")
require.Nil(t, err)
err = mongoapi.Drop(processor.APPDATA_INFLUDATA_DB_COLLECTION_NAME)
err = mongoapi.Drop(db.APPDATA_INFLUDATA_DB_COLLECTION_NAME)
require.Nil(t, err)
err = mongoapi.Drop(processor.APPDATA_INFLUDATA_SUBSC_DB_COLLECTION_NAME)
err = mongoapi.Drop(db.APPDATA_INFLUDATA_SUBSC_DB_COLLECTION_NAME)
require.Nil(t, err)
err = mongoapi.Drop(processor.APPDATA_PFD_DB_COLLECTION_NAME)
err = mongoapi.Drop(db.APPDATA_PFD_DB_COLLECTION_NAME)
require.Nil(t, err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (p *Processor) QueryAmDataProcedure(c *gin.Context, collName string, ueId s
logger.DataRepoLog.Infof("QueryAmDataProcedure: ueId: %s, servingPlmnId: %s", ueId, servingPlmnId)

filter := bson.M{"ueId": ueId, "servingPlmnId": servingPlmnId}
data, pd := getDataFromDB(collName, filter)
data, pd := p.GetDataFromDB(collName, filter)
if pd != nil {
c.JSON(int(pd.Status), pd)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (p *Processor) AmfContext3gppProcedure(
c *gin.Context, collName string, ueId string, patchItem []models.PatchItem,
) {
filter := bson.M{"ueId": ueId}
if err := patchDataToDBAndNotify(collName, ueId, patchItem, filter); err != nil {
if err := p.PatchDataToDBAndNotify(collName, ueId, patchItem, filter); err != nil {
logger.DataRepoLog.Errorf("AmfContext3gppProcedure err: %+v", err)
problemDetails := util.ProblemDetailsModifyNotAllowed("")
c.JSON(int(problemDetails.Status), problemDetails)
Expand All @@ -48,7 +48,7 @@ func (p *Processor) CreateAmfContext3gppProcedure(c *gin.Context, collName strin

func (p *Processor) QueryAmfContext3gppProcedure(c *gin.Context, collName string, ueId string) {
filter := bson.M{"ueId": ueId}
data, pd := getDataFromDB(collName, filter)
data, pd := p.GetDataFromDB(collName, filter)
if pd != nil {
logger.DataRepoLog.Errorf("QueryAmfContext3gppProcedure err: %s", pd.Detail)
c.JSON(int(pd.Status), pd)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (p *Processor) AmfContextNon3gppProcedure(
c *gin.Context, ueId string, collName string, patchItem []models.PatchItem,
filter bson.M,
) {
if err := patchDataToDBAndNotify(collName, ueId, patchItem, filter); err != nil {
if err := p.PatchDataToDBAndNotify(collName, ueId, patchItem, filter); err != nil {
logger.DataRepoLog.Errorf("AmfContextNon3gppProcedure err: %+v", err)
pd := util.ProblemDetailsSystemFailure(err.Error())
c.JSON(int(pd.Status), pd)
Expand All @@ -50,7 +50,7 @@ func (p *Processor) CreateAmfContextNon3gppProcedure(

func (p *Processor) QueryAmfContextNon3gppProcedure(c *gin.Context, collName string, ueId string) {
filter := bson.M{"ueId": ueId}
data, pd := getDataFromDB(collName, filter)
data, pd := p.GetDataFromDB(collName, filter)
if pd != nil {
logger.DataRepoLog.Errorf("QueryAmfContextNon3gppProcedure err: %s", pd.Detail)
c.JSON(int(pd.Status), pd)
Expand Down
4 changes: 2 additions & 2 deletions internal/sbi/processor/authentication_data_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (p *Processor) ModifyAuthenticationProcedure(
c *gin.Context, collName string, ueId string, patchItem []models.PatchItem,
) {
filter := bson.M{"ueId": ueId}
if err := patchDataToDBAndNotify(collName, ueId, patchItem, filter); err != nil {
if err := p.PatchDataToDBAndNotify(collName, ueId, patchItem, filter); err != nil {
logger.DataRepoLog.Errorf("ModifyAuthenticationProcedure err: %+v", err)
c.JSON(http.StatusInternalServerError, util.ProblemDetailsModifyNotAllowed(""))
}
Expand All @@ -33,7 +33,7 @@ func (p *Processor) ModifyAuthenticationProcedure(

func (p *Processor) QueryAuthSubsDataProcedure(c *gin.Context, collName string, ueId string) {
filter := bson.M{"ueId": ueId}
data, pd := getDataFromDB(collName, filter)
data, pd := p.GetDataFromDB(collName, filter)
if pd != nil {
if pd.Status == http.StatusNotFound {
logger.DataRepoLog.Warnf("QueryAuthSubsDataProcedure err: %s", pd.Title)
Expand Down
2 changes: 1 addition & 1 deletion internal/sbi/processor/authentication_so_r_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (p *Processor) CreateAuthenticationSoRProcedure(c *gin.Context, collName st

func (p *Processor) QueryAuthSoRProcedure(c *gin.Context, collName string, ueId string) {
filter := bson.M{"ueId": ueId}
data, pd := getDataFromDB(collName, filter)
data, pd := p.GetDataFromDB(collName, filter)
if pd != nil {
logger.DataRepoLog.Errorf("QueryAuthSoRProcedure err: %s", pd.Detail)
c.JSON(int(pd.Status), pd)
Expand Down
2 changes: 1 addition & 1 deletion internal/sbi/processor/authentication_status_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (p *Processor) CreateAuthenticationStatusProcedure(c *gin.Context, collName

func (p *Processor) QueryAuthenticationStatusProcedure(c *gin.Context, collName string, ueId string) {
filter := bson.M{"ueId": ueId}
data, pd := getDataFromDB(collName, filter)
data, pd := p.GetDataFromDB(collName, filter)

if pd != nil {
logger.DataRepoLog.Errorf("QueryAuthenticationStatusProcedure err: %s", pd.Detail)
Expand Down
37 changes: 19 additions & 18 deletions internal/sbi/processor/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@ import (
"github.com/free5gc/openapi"
"github.com/free5gc/openapi/models"
udr_context "github.com/free5gc/udr/internal/context"
db "github.com/free5gc/udr/internal/database"
"github.com/free5gc/udr/internal/logger"
"github.com/free5gc/udr/internal/util"
"github.com/free5gc/util/mongoapi"
)

func (p *Processor) DeleteApplicationDataIndividualPfdFromDBProcedure(c *gin.Context, appID string) {
filter := bson.M{"applicationId": appID}
deleteDataFromDB(APPDATA_PFD_DB_COLLECTION_NAME, filter)
p.DeleteDataFromDB(db.APPDATA_PFD_DB_COLLECTION_NAME, filter)
c.Status(http.StatusNoContent)
}

func (p *Processor) GetApplicationDataIndividualPfdFromDBProcedure(c *gin.Context, appID string) {
filter := bson.M{"applicationId": appID}
data, pd := getDataFromDB(APPDATA_PFD_DB_COLLECTION_NAME, filter)
data, pd := p.GetDataFromDB(db.APPDATA_PFD_DB_COLLECTION_NAME, filter)
if pd != nil {
logger.DataRepoLog.Errorf("getApplicationDataIndividualPfdFromDB err: %s", pd.Detail)
c.JSON(int(pd.Status), pd)
Expand All @@ -50,7 +51,7 @@ func (p *Processor) PutApplicationDataIndividualPfdToDBProcedure(
filter := bson.M{"applicationId": appID}
data := util.ToBsonM(*pfdDataForApp)

existed, err := mongoapi.RestfulAPIPutOne(APPDATA_PFD_DB_COLLECTION_NAME, filter, data)
existed, err := mongoapi.RestfulAPIPutOne(db.APPDATA_PFD_DB_COLLECTION_NAME, filter, data)
if err != nil {
logger.DataRepoLog.Errorf("putApplicationDataIndividualPfdToDB err: %+v", err)
c.JSON(http.StatusInternalServerError, nil)
Expand All @@ -69,7 +70,7 @@ func (p *Processor) GetApplicationDataPfdsFromDBProcedure(c *gin.Context, pfdsAp
var matchedPfds []map[string]interface{}
if len(pfdsAppIDs) == 0 {
var err error
matchedPfds, err = mongoapi.RestfulAPIGetMany(APPDATA_PFD_DB_COLLECTION_NAME, filter)
matchedPfds, err = mongoapi.RestfulAPIGetMany(db.APPDATA_PFD_DB_COLLECTION_NAME, filter)
if err != nil {
logger.DataRepoLog.Errorf("getApplicationDataPfdsFromDB err: %+v", err)
c.JSON(http.StatusOK, nil)
Expand All @@ -78,7 +79,7 @@ func (p *Processor) GetApplicationDataPfdsFromDBProcedure(c *gin.Context, pfdsAp
} else {
for _, v := range pfdsAppIDs {
filter := bson.M{"applicationId": v}
data, pd := getDataFromDB(APPDATA_PFD_DB_COLLECTION_NAME, filter)
data, pd := p.GetDataFromDB(db.APPDATA_PFD_DB_COLLECTION_NAME, filter)
if pd == nil {
matchedPfds = append(matchedPfds, data)
}
Expand All @@ -91,15 +92,15 @@ func (p *Processor) PolicyDataBdtDataBdtReferenceIdDeleteProcedure(
c *gin.Context, collName string, bdtReferenceId string,
) {
filter := bson.M{"bdtReferenceId": bdtReferenceId}
deleteDataFromDB(collName, filter)
p.DeleteDataFromDB(collName, filter)
c.Status(http.StatusNoContent)
}

func (p *Processor) PolicyDataBdtDataBdtReferenceIdGetProcedure(
c *gin.Context, collName string, bdtReferenceId string,
) {
filter := bson.M{"bdtReferenceId": bdtReferenceId}
data, pd := getDataFromDB(collName, filter)
data, pd := p.GetDataFromDB(collName, filter)
if pd != nil {
logger.DataRepoLog.Errorf("PolicyDataBdtDataBdtReferenceIdGetProcedure err: %s", pd.Detail)
c.JSON(int(pd.Status), pd)
Expand Down Expand Up @@ -141,7 +142,7 @@ func (p *Processor) PolicyDataBdtDataGetProcedure(c *gin.Context, collName strin

func (p *Processor) PolicyDataPlmnsPlmnIdUePolicySetGetProcedure(c *gin.Context, collName string, plmnId string) {
filter := bson.M{"plmnId": plmnId}
data, pd := getDataFromDB(collName, filter)
data, pd := p.GetDataFromDB(collName, filter)
if pd != nil {
logger.DataRepoLog.Errorf("PolicyDataPlmnsPlmnIdUePolicySetGetProcedure err: %s", pd.Detail)
c.JSON(int(pd.Status), pd)
Expand All @@ -154,7 +155,7 @@ func (p *Processor) PolicyDataSponsorConnectivityDataSponsorIdGetProcedure(c *gi
sponsorId string,
) {
filter := bson.M{"sponsorId": sponsorId}
data, pd := getDataFromDB(collName, filter)
data, pd := p.GetDataFromDB(collName, filter)
if pd != nil {
logger.DataRepoLog.Errorf("PolicyDataSponsorConnectivityDataSponsorIdGetProcedure err: %s", pd.Detail)
c.JSON(int(pd.Status), pd)
Expand Down Expand Up @@ -210,7 +211,7 @@ func (p *Processor) PolicyDataUesUeIdAmDataGetProcedure(c *gin.Context, collName
ueId string,
) {
filter := bson.M{"ueId": ueId}
data, pd := getDataFromDB(collName, filter)
data, pd := p.GetDataFromDB(collName, filter)
if pd != nil {
logger.DataRepoLog.Errorf("PolicyDataUesUeIdAmDataGetProcedure err: %s", pd.Detail)
c.JSON(int(pd.Status), pd)
Expand All @@ -222,7 +223,7 @@ func (p *Processor) PolicyDataUesUeIdOperatorSpecificDataGetProcedure(c *gin.Con
ueId string,
) {
filter := bson.M{"ueId": ueId}
data, pd := getDataFromDB(collName, filter)
data, pd := p.GetDataFromDB(collName, filter)
if pd != nil {
logger.DataRepoLog.Errorf("PolicyDataUesUeIdOperatorSpecificDataGetProcedure err: %s", pd.Detail)
c.JSON(int(pd.Status), pd)
Expand Down Expand Up @@ -273,7 +274,7 @@ func (p *Processor) PolicyDataUesUeIdSmDataGetProcedure(
) {
filter := bson.M{"ueId": ueId}

smPolicyData, pd := getDataFromDBWithArg(collName, filter, mongoapi.COLLATION_STRENGTH_IGNORE_CASE)
smPolicyData, pd := p.GetDataFromDBWithArg(collName, filter, mongoapi.COLLATION_STRENGTH_IGNORE_CASE)
if pd != nil {
c.JSON(int(pd.Status), pd)
return
Expand Down Expand Up @@ -366,7 +367,7 @@ func (p *Processor) PolicyDataUesUeIdSmDataPatchProcedure(c *gin.Context, collNa
successAll = false
} else {
var usageMonData models.UsageMonData
usageMonDataBsonM, pd := getDataFromDB(collName, filter)
usageMonDataBsonM, pd := p.GetDataFromDB(collName, filter)
if pd != nil && pd.Status == http.StatusInternalServerError {
logger.DataRepoLog.Errorf("PolicyDataUesUeIdSmDataPatchProcedure err: %s", pd.Detail)
c.JSON(int(pd.Status), pd)
Expand All @@ -380,7 +381,7 @@ func (p *Processor) PolicyDataUesUeIdSmDataPatchProcedure(c *gin.Context, collNa
}

if successAll {
smPolicyDataBsonM, pd := getDataFromDB(collName, filter)
smPolicyDataBsonM, pd := p.GetDataFromDB(collName, filter)
if pd != nil {
logger.DataRepoLog.Errorf("PolicyDataUesUeIdSmDataPatchProcedure err: %s", pd.Detail)
c.JSON(int(pd.Status), pd)
Expand Down Expand Up @@ -419,15 +420,15 @@ func (p *Processor) PolicyDataUesUeIdSmDataUsageMonIdDeleteProcedure(
c *gin.Context, collName string, ueId string, usageMonId string,
) {
filter := bson.M{"ueId": ueId, "usageMonId": usageMonId}
deleteDataFromDB(collName, filter)
p.DeleteDataFromDB(collName, filter)
c.Status(http.StatusNoContent)
}

func (p *Processor) PolicyDataUesUeIdSmDataUsageMonIdGetProcedure(c *gin.Context, collName string, usageMonId string,
ueId string,
) {
filter := bson.M{"ueId": ueId, "usageMonId": usageMonId}
data, pd := getDataFromDB(collName, filter)
data, pd := p.GetDataFromDB(collName, filter)
if pd != nil {
logger.DataRepoLog.Errorf("PolicyDataUesUeIdSmDataUsageMonIdGetProcedure err: %s", pd.Detail)
pd := util.ProblemDetailsNotFound("DATA_NOT_FOUND")
Expand Down Expand Up @@ -458,7 +459,7 @@ func (p *Processor) PolicyDataUesUeIdSmDataUsageMonIdPutProcedure(

func (p *Processor) PolicyDataUesUeIdUePolicySetGetProcedure(c *gin.Context, collName string, ueId string) {
filter := bson.M{"ueId": ueId}
data, pd := getDataFromDB(collName, filter)
data, pd := p.GetDataFromDB(collName, filter)
if pd != nil {
logger.DataRepoLog.Errorf("PolicyDataUesUeIdUePolicySetGetProcedure err: %s", pd.Detail)
pd := util.ProblemDetailsNotFound("DATA_NOT_FOUND")
Expand All @@ -483,7 +484,7 @@ func (p *Processor) PolicyDataUesUeIdUePolicySetPatchProcedure(c *gin.Context, c
}

var uePolicySet models.UePolicySet
uePolicySetBsonM, pd := getDataFromDB(collName, filter)
uePolicySetBsonM, pd := p.GetDataFromDB(collName, filter)
if pd != nil {
logger.DataRepoLog.Errorf("PolicyDataUesUeIdUePolicySetPatchProcedure err: %s", pd.Detail)
c.JSON(int(pd.Status), pd)
Expand Down
Loading

0 comments on commit 8395d10

Please sign in to comment.