Skip to content

Commit

Permalink
Merge pull request #98 from andy89923/fix/golangci-lint
Browse files Browse the repository at this point in the history
feat: use config file in billing server
  • Loading branch information
ianchen0119 authored May 23, 2024
2 parents f3f91f2 + 21055d6 commit 879e9f0
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 273 deletions.
365 changes: 209 additions & 156 deletions .golangci.yml

Large diffs are not rendered by default.

23 changes: 14 additions & 9 deletions backend/WebUI/api_charging.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import (
"github.com/free5gc/webconsole/backend/webui_context"
)

const (
ChargingOffline = "Offline"
ChargingOnline = "Online"
)

// Get vol from CDR
// TS 32.297: Charging Data Record (CDR) file format and transfer
func parseCDR(supi string) (map[int64]RatingGroupDataUsage, error) {
Expand Down Expand Up @@ -84,7 +89,7 @@ func GetChargingData(c *gin.Context) {
}
logger.BillingLog.Traceln(chargingMethod)

if chargingMethod != "Offline" && chargingMethod != "Online" {
if chargingMethod != ChargingOffline && chargingMethod != ChargingOnline {
c.JSON(http.StatusBadRequest, gin.H{"cause": "not support chargingMethod" + chargingMethod})
return
}
Expand Down Expand Up @@ -176,9 +181,9 @@ func GetChargingRecord(c *gin.Context) {
"ueId": supi,
"ratingGroup": rg,
}
chargingDataInterface, err := mongoapi.RestfulAPIGetOne(chargingDataColl, filter)
if err != nil {
logger.ProcLog.Errorf("PostSubscriberByID err: %+v", err)
chargingDataInterface, err_get := mongoapi.RestfulAPIGetOne(chargingDataColl, filter)
if err_get != nil {
logger.ProcLog.Errorf("PostSubscriberByID err: %+v", err_get)
}
if len(chargingDataInterface) == 0 {
logger.BillingLog.Warningf("ratingGroup: %d not found in mongoapi, may change the rg id", rg)
Expand All @@ -193,10 +198,10 @@ func GetChargingRecord(c *gin.Context) {
logger.BillingLog.Debugf("add ratingGroup: %d, supi: %s, method: %s", rg, supi, chargingData.ChargingMethod)

switch chargingData.ChargingMethod {
case "Offline":
unitcost, err := strconv.ParseInt(chargingData.UnitCost, 10, 64)
if err != nil {
logger.BillingLog.Error("Offline unitCost strconv: ", err.Error())
case ChargingOffline:
unitcost, err_parse := strconv.ParseInt(chargingData.UnitCost, 10, 64)
if err_parse != nil {
logger.BillingLog.Error("Offline unitCost strconv: ", err_parse.Error())
unitcost = 1
}

Expand All @@ -218,7 +223,7 @@ func GetChargingRecord(c *gin.Context) {
pdu_level.unitcost = unitcost
}
offlineChargingSliceTypeMap[key] = pdu_level
case "Online":
case ChargingOnline:
tmpInt, err1 := strconv.Atoi(chargingData.Quota)
if err1 != nil {
logger.BillingLog.Error("Quota strconv: ", err1, rg, du, chargingData)
Expand Down
16 changes: 8 additions & 8 deletions backend/WebUI/api_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ func GetSmfUserPlaneInfo() (interface{}, error) {
// TODO: support fetching data from multiple SMF
if smfUris := webuiSelf.GetOamUris(models.NfType_SMF); smfUris != nil {
requestUri := fmt.Sprintf("%s/nsmf-oam/v1/user-plane-info/", smfUris[0])
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUri, nil)
if err != nil {
logger.ProcLog.Error(err)
return jsonData, err
req, err_req := http.NewRequestWithContext(ctx, http.MethodGet, requestUri, nil)
if err_req != nil {
logger.ProcLog.Error(err_req)
return jsonData, err_req
}
resp, err := httpsClient.Do(req)
if err != nil {
logger.ProcLog.Error(err)
return jsonData, err
resp, err_rsp := httpsClient.Do(req)
if err_rsp != nil {
logger.ProcLog.Error(err_rsp)
return jsonData, err_rsp
}
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
Expand Down
85 changes: 42 additions & 43 deletions backend/WebUI/api_webui.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,16 +713,16 @@ func PutTenantByID(c *gin.Context) {
}

var tenantData Tenant
if err := c.ShouldBindJSON(&tenantData); err != nil {
if err = c.ShouldBindJSON(&tenantData); err != nil {
c.JSON(http.StatusBadRequest, gin.H{})
return
}
tenantData.TenantId = tenantId

tenantBsonM := toBsonM(tenantData)
filterTenantIdOnly = bson.M{"tenantId": tenantId}
if _, err := mongoapi.RestfulAPIPost(tenantDataColl, filterTenantIdOnly, tenantBsonM); err != nil {
logger.ProcLog.Errorf("PutTenantByID err: %+v", err)
if _, err_post := mongoapi.RestfulAPIPost(tenantDataColl, filterTenantIdOnly, tenantBsonM); err_post != nil {
logger.ProcLog.Errorf("PutTenantByID err: %+v", err_post)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
Expand Down Expand Up @@ -894,8 +894,8 @@ func PostUserByID(c *gin.Context) {

userBsonM := toBsonM(userData)
filterUserIdOnly := bson.M{"tenantId": userData.TenantId, "userId": userData.UserId}
if _, err := mongoapi.RestfulAPIPost(userDataColl, filterUserIdOnly, userBsonM); err != nil {
logger.ProcLog.Errorf("PostUserByID err: %+v", err)
if _, err_post := mongoapi.RestfulAPIPost(userDataColl, filterUserIdOnly, userBsonM); err_post != nil {
logger.ProcLog.Errorf("PostUserByID err: %+v", err_post)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
Expand Down Expand Up @@ -946,9 +946,9 @@ func PutUserByID(c *gin.Context) {

if newUserData.Email != "" && newUserData.Email != userData.Email {
filterEmail := bson.M{"email": newUserData.Email}
sameEmailInterface, err := mongoapi.RestfulAPIGetOne(userDataColl, filterEmail)
if err != nil {
logger.ProcLog.Errorf("PutUserByID err: %+v", err)
sameEmailInterface, err_get := mongoapi.RestfulAPIGetOne(userDataColl, filterEmail)
if err_get != nil {
logger.ProcLog.Errorf("PutUserByID err: %+v", err_get)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
Expand All @@ -960,18 +960,18 @@ func PutUserByID(c *gin.Context) {
}

if newUserData.EncryptedPassword != "" {
hash, err := bcrypt.GenerateFromPassword([]byte(newUserData.EncryptedPassword), 12)
if err != nil {
logger.ProcLog.Errorf("PutUserByID err: %+v", err)
hash, err_gen := bcrypt.GenerateFromPassword([]byte(newUserData.EncryptedPassword), 12)
if err_gen != nil {
logger.ProcLog.Errorf("PutUserByID err: %+v", err_gen)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
userData.EncryptedPassword = string(hash)
}

userBsonM := toBsonM(userData)
if _, err := mongoapi.RestfulAPIPost(userDataColl, filterUserIdOnly, userBsonM); err != nil {
logger.ProcLog.Errorf("PutUserByID err: %+v", err)
if _, err_post := mongoapi.RestfulAPIPost(userDataColl, filterUserIdOnly, userBsonM); err_post != nil {
logger.ProcLog.Errorf("PutUserByID err: %+v", err_post)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
Expand Down Expand Up @@ -1035,16 +1035,15 @@ func GetSubscribers(c *gin.Context) {
tenantId := amData["tenantId"]

filterUeIdOnly := bson.M{"ueId": ueId}
authSubsDataInterface, err := mongoapi.RestfulAPIGetOne(authSubsDataColl, filterUeIdOnly)
if err != nil {
logger.ProcLog.Errorf("GetSubscribers err: %+v", err)
authSubsDataInterface, err_get := mongoapi.RestfulAPIGetOne(authSubsDataColl, filterUeIdOnly)
if err_get != nil {
logger.ProcLog.Errorf("GetSubscribers err: %+v", err_get)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}

var authSubsData AuthSub
err = json.Unmarshal(mapToByte(authSubsDataInterface), &authSubsData)
if err != nil {
if err = json.Unmarshal(mapToByte(authSubsDataInterface), &authSubsData); err != nil {
logger.ProcLog.Errorf("GetSubscribers err: %+v", err)
c.JSON(http.StatusInternalServerError, gin.H{})
return
Expand Down Expand Up @@ -1140,55 +1139,55 @@ func GetSubscriberByID(c *gin.Context) {
}

var authSubsData models.AuthenticationSubscription
if err := json.Unmarshal(mapToByte(authSubsDataInterface), &authSubsData); err != nil {
if err = json.Unmarshal(mapToByte(authSubsDataInterface), &authSubsData); err != nil {
logger.ProcLog.Errorf("GetSubscriberByID err: %+v", err)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
var amDataData models.AccessAndMobilitySubscriptionData
if err := json.Unmarshal(mapToByte(amDataDataInterface), &amDataData); err != nil {
if err = json.Unmarshal(mapToByte(amDataDataInterface), &amDataData); err != nil {
logger.ProcLog.Errorf("GetSubscriberByID err: %+v", err)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
var smDataData []models.SessionManagementSubscriptionData
if err := json.Unmarshal(sliceToByte(smDataDataInterface), &smDataData); err != nil {
if err = json.Unmarshal(sliceToByte(smDataDataInterface), &smDataData); err != nil {
logger.ProcLog.Errorf("GetSubscriberByID err: %+v", err)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
var smfSelData models.SmfSelectionSubscriptionData
if err := json.Unmarshal(mapToByte(smfSelDataInterface), &smfSelData); err != nil {
if err = json.Unmarshal(mapToByte(smfSelDataInterface), &smfSelData); err != nil {
logger.ProcLog.Errorf("GetSubscriberByID err: %+v", err)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
var amPolicyData models.AmPolicyData
if err := json.Unmarshal(mapToByte(amPolicyDataInterface), &amPolicyData); err != nil {
if err = json.Unmarshal(mapToByte(amPolicyDataInterface), &amPolicyData); err != nil {
logger.ProcLog.Errorf("GetSubscriberByID err: %+v", err)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
var smPolicyData models.SmPolicyData
if err := json.Unmarshal(mapToByte(smPolicyDataInterface), &smPolicyData); err != nil {
if err = json.Unmarshal(mapToByte(smPolicyDataInterface), &smPolicyData); err != nil {
logger.ProcLog.Errorf("GetSubscriberByID err: %+v", err)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
var flowRules []FlowRule
if err := json.Unmarshal(sliceToByte(flowRuleDataInterface), &flowRules); err != nil {
if err = json.Unmarshal(sliceToByte(flowRuleDataInterface), &flowRules); err != nil {
logger.ProcLog.Errorf("GetSubscriberByID err: %+v", err)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
var qosFlows []QosFlow
if err := json.Unmarshal(sliceToByte(qosFlowInterface), &qosFlows); err != nil {
if err = json.Unmarshal(sliceToByte(qosFlowInterface), &qosFlows); err != nil {
logger.ProcLog.Errorf("GetSubscriberByID err: %+v", err)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
var chargingDatas []ChargingData
if err := json.Unmarshal(sliceToByte(chargingDatasInterface), &chargingDatas); err != nil {
if err = json.Unmarshal(sliceToByte(chargingDatasInterface), &chargingDatas); err != nil {
logger.ProcLog.Errorf("GetSubscriberByID err: %+v", err)
c.JSON(http.StatusInternalServerError, gin.H{})
return
Expand Down Expand Up @@ -1327,9 +1326,9 @@ func PostSubscriberByID(c *gin.Context) {

// Lookup same UE ID of other tenant's subscription.
if claims != nil {
authSubsDataInterface, err := mongoapi.RestfulAPIGetOne(authSubsDataColl, filterUeIdOnly)
if err != nil {
logger.ProcLog.Errorf("PostSubscriberByID err: %+v", err)
authSubsDataInterface, err_get := mongoapi.RestfulAPIGetOne(authSubsDataColl, filterUeIdOnly)
if err_get != nil {
logger.ProcLog.Errorf("PostSubscriberByID err: %+v", err_get)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
Expand Down Expand Up @@ -1536,7 +1535,7 @@ func dbOperation(ueId string, servingPlmnId string, method string, subsData *Sub

chargingDataBsonM := toBsonM(chargingData)
// Clear quota for offline charging flow
if chargingData.ChargingMethod == "Offline" {
if chargingData.ChargingMethod == ChargingOffline {
chargingDataBsonM["quota"] = "0"
}

Expand Down Expand Up @@ -1583,8 +1582,8 @@ func dbOperation(ueId string, servingPlmnId string, method string, subsData *Sub
chargingDataBsonM["ueId"] = ueId
chargingDataBsonM["servingPlmnId"] = servingPlmnId

if _, err := mongoapi.RestfulAPIPutOne(chargingDataColl, chargingFilter, chargingDataBsonM); err != nil {
logger.ProcLog.Errorf("PostSubscriberByID err: %+v", err)
if _, err_put := mongoapi.RestfulAPIPutOne(chargingDataColl, chargingFilter, chargingDataBsonM); err != nil {
logger.ProcLog.Errorf("PostSubscriberByID err: %+v", err_put)
}
}
}
Expand Down Expand Up @@ -1728,16 +1727,16 @@ func PatchSubscriberByID(c *gin.Context) {
c.JSON(http.StatusNoContent, gin.H{})
}

func removeCdrFile(CdrFilePath string) {
files, err := filepath.Glob(CdrFilePath + "*.cdr")
func removeCdrFile(cdrFilePath string) {
files, err := filepath.Glob(cdrFilePath + "*.cdr")
if err != nil {
logger.BillingLog.Warnf("CDR file not found in %s", CdrFilePath)
logger.BillingLog.Warnf("CDR file not found in %s", cdrFilePath)
}

for _, file := range files {
if _, err := os.Stat(file); err == nil {
if _, err = os.Stat(file); err == nil {
logger.BillingLog.Infof("Remove CDR file: " + file)
if err := os.Remove(file); err != nil {
if err = os.Remove(file); err != nil {
logger.BillingLog.Warnf("Failed to remove CDR file: %s\n", file)
}
}
Expand Down Expand Up @@ -1907,16 +1906,16 @@ func ChangePasswordInfo(c *gin.Context) {
}

if newUserData.EncryptedPassword != "" {
hash, err := bcrypt.GenerateFromPassword([]byte(newUserData.EncryptedPassword), 12)
if err != nil {
logger.ProcLog.Errorf("GenerateFromPassword err: %+v", err)
hash, err_gen := bcrypt.GenerateFromPassword([]byte(newUserData.EncryptedPassword), 12)
if err_gen != nil {
logger.ProcLog.Errorf("GenerateFromPassword err: %+v", err_gen)
}
userData.EncryptedPassword = string(hash)
}

userBsonM := toBsonM(userData)
if _, err := mongoapi.RestfulAPIPost(userDataColl, filterEmailOnly, userBsonM); err != nil {
logger.ProcLog.Errorf("PutUserByID err: %+v", err)
if _, err_put := mongoapi.RestfulAPIPost(userDataColl, filterEmailOnly, userBsonM); err_put != nil {
logger.ProcLog.Errorf("PutUserByID err: %+v", err_put)
}

c.JSON(http.StatusOK, userData)
Expand Down
8 changes: 4 additions & 4 deletions backend/billing/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ func PullCDRFile(c *ftp.ServerConn, fileName string) ([]byte, error) {
}

defer func() {
if err := r.Close(); err != nil {
if err = r.Close(); err != nil {
logger.BillingLog.Error(err)
}
}()

logger.BillingLog.Info("Pull CDR file success")

if err := c.Quit(); err != nil {
if err = c.Quit(); err != nil {
return nil, err
}

cdr, err1 := io.ReadAll(r)
cdr, err_read := io.ReadAll(r)

return cdr, err1
return cdr, err_read
}
Loading

0 comments on commit 879e9f0

Please sign in to comment.