Skip to content

Commit

Permalink
Merge pull request #37 from a3828162/refactor/sbi-logic
Browse files Browse the repository at this point in the history
refactor
  • Loading branch information
ianchen0119 authored Jun 27, 2024
2 parents 095f899 + 972efcf commit 3f28447
Show file tree
Hide file tree
Showing 80 changed files with 4,164 additions and 5,776 deletions.
20 changes: 15 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import (
"context"
"os"
"os/signal"
"path/filepath"
"runtime/debug"
"syscall"

"github.com/urfave/cli"

Expand All @@ -14,8 +17,6 @@ import (
"github.com/free5gc/util/version"
)

var UDM *service.UdmApp

func main() {
defer func() {
if p := recover(); p != nil {
Expand Down Expand Up @@ -52,18 +53,27 @@ func action(cliCtx *cli.Context) error {

logger.MainLog.Infoln("UDM version: ", version.GetVersion())

ctx, cancel := context.WithCancel(context.Background())
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)

go func() {
<-sigCh // Wait for interrupt signal to gracefully shutdown
cancel() // Notify each goroutine and wait them stopped
}()

cfg, err := factory.ReadConfig(cliCtx.String("config"))
if err != nil {
return err
}
factory.UdmConfig = cfg
udm, err := service.NewApp(cfg)

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

udm.Start(tlsKeyLogPath)
udm.Start()

return nil
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.8.4
github.com/urfave/cli v1.22.5
go.uber.org/mock v0.4.0
golang.org/x/crypto v0.21.0
gopkg.in/yaml.v2 v2.4.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZ
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
Expand Down
1 change: 1 addition & 0 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
func Init() {
GetSelf().NfService = make(map[models.ServiceName]models.NfService)
GetSelf().EeSubscriptionIDGenerator = idgenerator.NewGenerator(1, math.MaxInt32)
InitUdmContext(GetSelf())
}

type NFContext interface {
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 (
CfgLog *logrus.Entry
CtxLog *logrus.Entry
GinLog *logrus.Entry
SBILog *logrus.Entry
ConsumerLog *logrus.Entry
HttpLog *logrus.Entry
UeauLog *logrus.Entry
Expand All @@ -40,6 +41,7 @@ func init() {
CfgLog = NfLog.WithField(logger_util.FieldCategory, "CFG")
CtxLog = NfLog.WithField(logger_util.FieldCategory, "CTX")
GinLog = NfLog.WithField(logger_util.FieldCategory, "GIN")
SBILog = NfLog.WithField(logger_util.FieldCategory, "SBI")
ConsumerLog = NfLog.WithField(logger_util.FieldCategory, "Consumer")
ProcLog = NfLog.WithField(logger_util.FieldCategory, "Proc")
HttpLog = NfLog.WithField(logger_util.FieldCategory, "HTTP")
Expand Down
130 changes: 130 additions & 0 deletions internal/sbi/api_eventexposure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package sbi

import (
"net/http"
"strings"

"github.com/gin-gonic/gin"

"github.com/free5gc/openapi"
"github.com/free5gc/openapi/models"
"github.com/free5gc/udm/internal/logger"
)

func (s *Server) getEventExposureRoutes() []Route {
return []Route{
{
"Index",
"GET",
"/",
s.HandleIndex,
},

{
"HTTPCreateEeSubscription",
strings.ToUpper("Post"),
"/:ueIdentity/ee-subscriptions",
s.HandleCreateEeSubscription,
},

{
"HTTPDeleteEeSubscription",
strings.ToUpper("Delete"),
"/:ueIdentity/ee-subscriptions/:subscriptionId",
s.HandleDeleteEeSubscription,
},

{
"HTTPUpdateEeSubscription",
strings.ToUpper("Patch"),
"/:ueIdentity/ee-subscriptions/:subscriptionId",
s.HandleUpdateEeSubscription,
},
}
}

// HTTPCreateEeSubscription - Subscribe
func (s *Server) HandleCreateEeSubscription(c *gin.Context) {
var eesubscription models.EeSubscription

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

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

logger.EeLog.Infoln("Handle Create EE Subscription")

ueIdentity := c.Params.ByName("ueIdentity")

s.Processor().CreateEeSubscriptionProcedure(c, ueIdentity, eesubscription)
}

func (s *Server) HandleDeleteEeSubscription(c *gin.Context) {
ueIdentity := c.Params.ByName("ueIdentity")
subscriptionID := c.Params.ByName("subscriptionId")

s.Processor().DeleteEeSubscriptionProcedure(c, ueIdentity, subscriptionID)
}

func (s *Server) HandleUpdateEeSubscription(c *gin.Context) {
var patchList []models.PatchItem

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

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

ueIdentity := c.Params.ByName("ueIdentity")
subscriptionID := c.Params.ByName("subscriptionId")

logger.EeLog.Infoln("Handle Update EE subscription")
logger.EeLog.Warnln("Update EE Subscription is not implemented")

s.Processor().UpdateEeSubscriptionProcedure(c, ueIdentity, subscriptionID, patchList)
}

func (s *Server) HandleIndex(c *gin.Context) {
c.String(http.StatusOK, "Hello World!")
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
package httpcallback
package sbi

import (
"net/http"
"strings"

"github.com/gin-gonic/gin"

"github.com/free5gc/openapi"
"github.com/free5gc/openapi/models"
"github.com/free5gc/udm/internal/logger"
"github.com/free5gc/udm/internal/sbi/producer"
"github.com/free5gc/util/httpwrapper"
)

func HTTPDataChangeNotificationToNF(c *gin.Context) {
func (s *Server) getHttpCallBackRoutes() []Route {
return []Route{
{
"Index",
"GET",
"/",
s.HandleIndex,
},

{
"DataChangeNotificationToNF",
strings.ToUpper("Post"),
"/sdm-subscriptions",
s.HandleDataChangeNotificationToNF,
},
}
}

func (s *Server) HandleDataChangeNotificationToNF(c *gin.Context) {
var dataChangeNotify models.DataChangeNotify
// step 1: retrieve http request body
requestBody, err := c.GetRawData()
if err != nil {
problemDetail := models.ProblemDetails{
Expand All @@ -28,7 +44,6 @@ func HTTPDataChangeNotificationToNF(c *gin.Context) {
return
}

// step 2: convert requestBody to openapi models
err = openapi.Deserialize(&dataChangeNotify, requestBody, "application/json")
if err != nil {
problemDetail := "[Request Body] " + err.Error()
Expand All @@ -42,20 +57,9 @@ func HTTPDataChangeNotificationToNF(c *gin.Context) {
return
}

req := httpwrapper.NewRequest(c.Request, dataChangeNotify)
req.Params["supi"] = c.Params.ByName("supi")
supi := c.Params.ByName("supi")

rsp := producer.HandleDataChangeNotificationToNFRequest(req)
responseBody, err := openapi.Serialize(rsp.Body, "application/json")
if err != nil {
logger.CallbackLog.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)
}
logger.CallbackLog.Infof("Handle DataChangeNotificationToNF")

s.Processor().DataChangeNotificationProcedure(c, dataChangeNotify.NotifyItems, supi)
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
/*
* Nudm_PP
*
* Nudm Parameter Provision Service
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/

package parameterprovision
package sbi

import (
"net/http"
"strings"

"github.com/gin-gonic/gin"

"github.com/free5gc/openapi"
"github.com/free5gc/openapi/models"
"github.com/free5gc/udm/internal/logger"
"github.com/free5gc/udm/internal/sbi/producer"
"github.com/free5gc/util/httpwrapper"
)

// Update - provision parameters
func HTTPUpdate(c *gin.Context) {
func (s *Server) getParameterProvisionRoutes() []Route {
return []Route{
{
"Index",
"GET",
"/",
s.HandleIndex,
},

{
"Update",
strings.ToUpper("Patch"),
"/:gpsi/pp-data",
s.HandleUpdate,
},
}
}

func (s *Server) HandleUpdate(c *gin.Context) {
var ppDataReq models.PpData

// step 1: retrieve http request body
Expand Down Expand Up @@ -53,21 +60,18 @@ func HTTPUpdate(c *gin.Context) {
return
}

req := httpwrapper.NewRequest(c.Request, ppDataReq)
req.Params["gspi"] = c.Params.ByName("gpsi")

rsp := producer.HandleUpdateRequest(req)

responseBody, err := openapi.Serialize(rsp.Body, "application/json")
if err != nil {
logger.PpLog.Errorln(err)
problemDetails := models.ProblemDetails{
Status: http.StatusInternalServerError,
Cause: "SYSTEM_FAILURE",
Detail: err.Error(),
gpsi := c.Params.ByName("gpsi")
if gpsi == "" {
problemDetails := &models.ProblemDetails{
Status: http.StatusBadRequest,
Cause: "NO_GPSI",
}
c.JSON(http.StatusInternalServerError, problemDetails)
} else {
c.Data(rsp.Status, "application/json", responseBody)
c.JSON(int(problemDetails.Status), problemDetails)
return
}

logger.PpLog.Infoln("Handle UpdateRequest")

// step 3: handle the message
s.Processor().UpdateProcedure(c, ppDataReq, gpsi)
}
Loading

0 comments on commit 3f28447

Please sign in to comment.