Skip to content

Commit

Permalink
feat: ChfApp and Server
Browse files Browse the repository at this point in the history
  • Loading branch information
andy89923 committed Apr 10, 2024
1 parent dd4e234 commit c963dca
Show file tree
Hide file tree
Showing 6 changed files with 520 additions and 77 deletions.
7 changes: 5 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func action(cliCtx *cli.Context) error {
wg.Add(1)
go func() {
defer wg.Done()
<-sigCh // Wait for interrupt signal to gracefully shutdown UPF
<-sigCh // Wait for interrupt signal to gracefully shutdown
cancel() // Notify each goroutine and wait them stopped
if CHF != nil {
CHF.WaitRoutineStopped()
Expand All @@ -88,14 +88,17 @@ func action(cliCtx *cli.Context) error {
}
factory.ChfConfig = cfg

chf, err := service.NewApp(ctx, cfg)
chf, err := service.NewApp(ctx, cfg, tlsKeyLogPath)
if err != nil {
return err
}
CHF = chf

chf.Start(tlsKeyLogPath)

<-ctx.Done()
chf.WaitRoutineStopped()

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
InitLog *logrus.Entry
CfgLog *logrus.Entry
CtxLog *logrus.Entry
SBILog *logrus.Entry
ConsumerLog *logrus.Entry
GinLog *logrus.Entry
ChargingdataPostLog *logrus.Entry
Expand All @@ -39,6 +40,7 @@ func init() {
InitLog = NfLog.WithField(logger_util.FieldCategory, "Init")
CfgLog = NfLog.WithField(logger_util.FieldCategory, "CFG")
CtxLog = NfLog.WithField(logger_util.FieldCategory, "CTX")
SBILog = NfLog.WithField(logger_util.FieldCategory, "SBI")
ConsumerLog = NfLog.WithField(logger_util.FieldCategory, "Consumer")
GinLog = NfLog.WithField(logger_util.FieldCategory, "GIN")
ChargingdataPostLog = NfLog.WithField(logger_util.FieldCategory, "ChargingPost")
Expand Down
241 changes: 241 additions & 0 deletions internal/sbi/api_convergedcharging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
/*
* Nchf_ConvergedCharging
*
* ConvergedCharging Service © 2021, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TSDSI, TTA, TTC). All rights reserved.
*
* API version: 3.0.3
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/

package sbi

import (
"net/http"
"strconv"
"strings"

"github.com/free5gc/chf/internal/sbi/producer"
"github.com/free5gc/openapi"
"github.com/free5gc/openapi/models"

"github.com/gin-gonic/gin"

"github.com/free5gc/chf/internal/logger"
"github.com/free5gc/util/httpwrapper"
)

// Index is the index handler.
func Index(c *gin.Context) {
c.String(http.StatusOK, "Hello World!")
}

func (s *Server) getConvergenChargingEndpoints() []Endpoint {
return []Endpoint{
{
Method: http.MethodGet,
Pattern: "/",
APIFunc: Index,
},
{
Method: http.MethodPost,
Pattern: "/chargingdata/:ChargingDataRef/release",
APIFunc: s.ChargingdataChargingDataRefReleasePost,
},
{
Method: http.MethodPost,
Pattern: "/chargingdata/:ChargingDataRef/update",
APIFunc: s.ChargingdataChargingDataRefUpdatePost,
},
{
Method: http.MethodPost,
Pattern: "/chargingdata",
APIFunc: s.ChargingdataPost,
},
{
Method: http.MethodGet,
Pattern: "/recharging",
APIFunc: s.RechargeGet,
},
{
Method: http.MethodPut,
Pattern: "/recharging/:rechargingInfo",
APIFunc: s.RechargePut,
},
}
}

// ChargingdataChargingDataRefReleasePost -
func (s *Server) ChargingdataChargingDataRefReleasePost(c *gin.Context) {
var chargingDataReq models.ChargingDataRequest

requestBody, err := c.GetRawData()
if err != nil {
problemDetail := models.ProblemDetails{
Title: "System failure",
Status: http.StatusInternalServerError,
Detail: err.Error(),
Cause: "SYSTEM_FAILURE",
}
logger.ChargingdataPostLog.Errorf("Get Request Body error: %+v", err)
c.JSON(http.StatusInternalServerError, problemDetail)
return
}

err = openapi.Deserialize(&chargingDataReq, requestBody, "application/json")
if err != nil {
problemDetail := "[Request Body] " + err.Error()
rsp := models.ProblemDetails{
Title: "Malformed request syntax",
Status: http.StatusBadRequest,
Detail: problemDetail,
}
logger.ChargingdataPostLog.Errorln(problemDetail)
c.JSON(http.StatusBadRequest, rsp)
return
}

req := httpwrapper.NewRequest(c.Request, chargingDataReq)
req.Params["ChargingDataRef"] = c.Param("ChargingDataRef")

rsp := producer.HandleChargingdataRelease(req)

for key, value := range rsp.Header {
c.Header(key, value[0])
}
responseBody, err := openapi.Serialize(rsp.Body, "application/json")
if err != nil {
logger.ChargingdataPostLog.Errorln(err)
problemDetails := models.ProblemDetails{
Status: http.StatusInternalServerError,
Cause: "SYSTEM_FAILURE",
Detail: err.Error(),
}
c.JSON(http.StatusInternalServerError, problemDetails)
} else {
c.Data(rsp.Status, "application/json", responseBody)
}
}

// ChargingdataChargingDataRefUpdatePost
func (s *Server) ChargingdataChargingDataRefUpdatePost(c *gin.Context) {
var chargingDataReq models.ChargingDataRequest

requestBody, err := c.GetRawData()
if err != nil {
problemDetail := models.ProblemDetails{
Title: "System failure",
Status: http.StatusInternalServerError,
Detail: err.Error(),
Cause: "SYSTEM_FAILURE",
}
logger.ChargingdataPostLog.Errorf("Get Request Body error: %+v", err)
c.JSON(http.StatusInternalServerError, problemDetail)
return
}

err = openapi.Deserialize(&chargingDataReq, requestBody, "application/json")
if err != nil {
problemDetail := "[Request Body] " + err.Error()
rsp := models.ProblemDetails{
Title: "Malformed request syntax",
Status: http.StatusBadRequest,
Detail: problemDetail,
}
logger.ChargingdataPostLog.Errorln(problemDetail)
c.JSON(http.StatusBadRequest, rsp)
return
}

req := httpwrapper.NewRequest(c.Request, chargingDataReq)
req.Params["ChargingDataRef"] = c.Param("ChargingDataRef")

rsp := producer.HandleChargingdataUpdate(req)

for key, value := range rsp.Header {
c.Header(key, value[0])
}
responseBody, err := openapi.Serialize(rsp.Body, "application/json")
if err != nil {
logger.ChargingdataPostLog.Errorln(err)
problemDetails := models.ProblemDetails{
Status: http.StatusInternalServerError,
Cause: "SYSTEM_FAILURE",
Detail: err.Error(),
}
c.JSON(http.StatusInternalServerError, problemDetails)
} else {
c.Data(rsp.Status, "application/json", responseBody)
}
}

// ChargingdataPost
func (s *Server) ChargingdataPost(c *gin.Context) {
var chargingDataReq models.ChargingDataRequest

requestBody, err := c.GetRawData()
if err != nil {
problemDetail := models.ProblemDetails{
Title: "System failure",
Status: http.StatusInternalServerError,
Detail: err.Error(),
Cause: "SYSTEM_FAILURE",
}
logger.ChargingdataPostLog.Errorf("Get Request Body error: %+v", err)
c.JSON(http.StatusInternalServerError, problemDetail)
return
}

err = openapi.Deserialize(&chargingDataReq, requestBody, "application/json")
if err != nil {
problemDetail := "[Request Body] " + err.Error()
rsp := models.ProblemDetails{
Title: "Malformed request syntax",
Status: http.StatusBadRequest,
Detail: problemDetail,
}
logger.ChargingdataPostLog.Errorln(problemDetail)
c.JSON(http.StatusBadRequest, rsp)
return
}

req := httpwrapper.NewRequest(c.Request, chargingDataReq)

rsp := producer.HandleChargingdataInitial(req)

for key, value := range rsp.Header {
c.Header(key, value[0])
}
responseBody, err := openapi.Serialize(rsp.Body, "application/json")
if err != nil {
logger.ChargingdataPostLog.Errorln(err)
problemDetails := models.ProblemDetails{
Status: http.StatusInternalServerError,
Cause: "SYSTEM_FAILURE",
Detail: err.Error(),
}
c.JSON(http.StatusInternalServerError, problemDetails)
} else {
c.Data(rsp.Status, "application/json", responseBody)
}
}

func (s *Server) RechargeGet(c *gin.Context) {
c.String(http.StatusOK, "recharge")
}

func (s *Server) RechargePut(c *gin.Context) {
rechargingInfo := c.Param("rechargingInfo")
ueIdRatingGroup := strings.Split(rechargingInfo, "_")
ueId := ueIdRatingGroup[0]
rgStr := ueIdRatingGroup[1]
rg, err := strconv.Atoi(rgStr)
if err != nil {
logger.RechargingLog.Errorf("UE[%s] fail to recharge for rating group %s", ueId, rgStr)
}

logger.RechargingLog.Warnf("UE[%s] Recharg for rating group %d", ueId, rg)

producer.NotifyRecharge(ueId, int32(rg))

c.JSON(http.StatusNoContent, gin.H{})
}
Loading

0 comments on commit c963dca

Please sign in to comment.