Skip to content

Commit 3f28447

Browse files
authored
Merge pull request #37 from a3828162/refactor/sbi-logic
refactor
2 parents 095f899 + 972efcf commit 3f28447

File tree

80 files changed

+4164
-5776
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+4164
-5776
lines changed

cmd/main.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package main
22

33
import (
4+
"context"
45
"os"
6+
"os/signal"
57
"path/filepath"
68
"runtime/debug"
9+
"syscall"
710

811
"github.com/urfave/cli"
912

@@ -14,8 +17,6 @@ import (
1417
"github.com/free5gc/util/version"
1518
)
1619

17-
var UDM *service.UdmApp
18-
1920
func main() {
2021
defer func() {
2122
if p := recover(); p != nil {
@@ -52,18 +53,27 @@ func action(cliCtx *cli.Context) error {
5253

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

56+
ctx, cancel := context.WithCancel(context.Background())
57+
sigCh := make(chan os.Signal, 1)
58+
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
59+
60+
go func() {
61+
<-sigCh // Wait for interrupt signal to gracefully shutdown
62+
cancel() // Notify each goroutine and wait them stopped
63+
}()
64+
5565
cfg, err := factory.ReadConfig(cliCtx.String("config"))
5666
if err != nil {
5767
return err
5868
}
5969
factory.UdmConfig = cfg
60-
udm, err := service.NewApp(cfg)
70+
71+
udm, err := service.NewApp(ctx, cfg, tlsKeyLogPath)
6172
if err != nil {
6273
return err
6374
}
64-
UDM = udm
6575

66-
udm.Start(tlsKeyLogPath)
76+
udm.Start()
6777

6878
return nil
6979
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/sirupsen/logrus v1.8.1
1515
github.com/stretchr/testify v1.8.4
1616
github.com/urfave/cli v1.22.5
17+
go.uber.org/mock v0.4.0
1718
golang.org/x/crypto v0.21.0
1819
gopkg.in/yaml.v2 v2.4.0
1920
)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZ
103103
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
104104
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
105105
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
106+
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
107+
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
106108
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
107109
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
108110
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=

internal/context/context.go

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const (
3434
func Init() {
3535
GetSelf().NfService = make(map[models.ServiceName]models.NfService)
3636
GetSelf().EeSubscriptionIDGenerator = idgenerator.NewGenerator(1, math.MaxInt32)
37+
InitUdmContext(GetSelf())
3738
}
3839

3940
type NFContext interface {

internal/logger/logger.go

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var (
1414
CfgLog *logrus.Entry
1515
CtxLog *logrus.Entry
1616
GinLog *logrus.Entry
17+
SBILog *logrus.Entry
1718
ConsumerLog *logrus.Entry
1819
HttpLog *logrus.Entry
1920
UeauLog *logrus.Entry
@@ -40,6 +41,7 @@ func init() {
4041
CfgLog = NfLog.WithField(logger_util.FieldCategory, "CFG")
4142
CtxLog = NfLog.WithField(logger_util.FieldCategory, "CTX")
4243
GinLog = NfLog.WithField(logger_util.FieldCategory, "GIN")
44+
SBILog = NfLog.WithField(logger_util.FieldCategory, "SBI")
4345
ConsumerLog = NfLog.WithField(logger_util.FieldCategory, "Consumer")
4446
ProcLog = NfLog.WithField(logger_util.FieldCategory, "Proc")
4547
HttpLog = NfLog.WithField(logger_util.FieldCategory, "HTTP")

internal/sbi/api_eventexposure.go

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package sbi
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
7+
"github.com/gin-gonic/gin"
8+
9+
"github.com/free5gc/openapi"
10+
"github.com/free5gc/openapi/models"
11+
"github.com/free5gc/udm/internal/logger"
12+
)
13+
14+
func (s *Server) getEventExposureRoutes() []Route {
15+
return []Route{
16+
{
17+
"Index",
18+
"GET",
19+
"/",
20+
s.HandleIndex,
21+
},
22+
23+
{
24+
"HTTPCreateEeSubscription",
25+
strings.ToUpper("Post"),
26+
"/:ueIdentity/ee-subscriptions",
27+
s.HandleCreateEeSubscription,
28+
},
29+
30+
{
31+
"HTTPDeleteEeSubscription",
32+
strings.ToUpper("Delete"),
33+
"/:ueIdentity/ee-subscriptions/:subscriptionId",
34+
s.HandleDeleteEeSubscription,
35+
},
36+
37+
{
38+
"HTTPUpdateEeSubscription",
39+
strings.ToUpper("Patch"),
40+
"/:ueIdentity/ee-subscriptions/:subscriptionId",
41+
s.HandleUpdateEeSubscription,
42+
},
43+
}
44+
}
45+
46+
// HTTPCreateEeSubscription - Subscribe
47+
func (s *Server) HandleCreateEeSubscription(c *gin.Context) {
48+
var eesubscription models.EeSubscription
49+
50+
requestBody, err := c.GetRawData()
51+
if err != nil {
52+
logger.EeLog.Errorf("Get Request Body error: %+v", err)
53+
problemDetail := models.ProblemDetails{
54+
Title: "System failure",
55+
Status: http.StatusInternalServerError,
56+
Detail: err.Error(),
57+
Cause: "SYSTEM_FAILURE",
58+
}
59+
c.JSON(http.StatusInternalServerError, problemDetail)
60+
return
61+
}
62+
63+
err = openapi.Deserialize(&eesubscription, requestBody, "application/json")
64+
if err != nil {
65+
problemDetail := "[Request Body] " + err.Error()
66+
rsp := models.ProblemDetails{
67+
Title: "Malformed request syntax",
68+
Status: http.StatusBadRequest,
69+
Detail: problemDetail,
70+
}
71+
logger.EeLog.Errorln(problemDetail)
72+
c.JSON(http.StatusBadRequest, rsp)
73+
return
74+
}
75+
76+
logger.EeLog.Infoln("Handle Create EE Subscription")
77+
78+
ueIdentity := c.Params.ByName("ueIdentity")
79+
80+
s.Processor().CreateEeSubscriptionProcedure(c, ueIdentity, eesubscription)
81+
}
82+
83+
func (s *Server) HandleDeleteEeSubscription(c *gin.Context) {
84+
ueIdentity := c.Params.ByName("ueIdentity")
85+
subscriptionID := c.Params.ByName("subscriptionId")
86+
87+
s.Processor().DeleteEeSubscriptionProcedure(c, ueIdentity, subscriptionID)
88+
}
89+
90+
func (s *Server) HandleUpdateEeSubscription(c *gin.Context) {
91+
var patchList []models.PatchItem
92+
93+
requestBody, err := c.GetRawData()
94+
if err != nil {
95+
logger.EeLog.Errorf("Get Request Body error: %+v", err)
96+
problemDetail := models.ProblemDetails{
97+
Title: "System failure",
98+
Status: http.StatusInternalServerError,
99+
Detail: err.Error(),
100+
Cause: "SYSTEM_FAILURE",
101+
}
102+
c.JSON(http.StatusInternalServerError, problemDetail)
103+
return
104+
}
105+
106+
err = openapi.Deserialize(&patchList, requestBody, "application/json")
107+
if err != nil {
108+
problemDetail := "[Request Body] " + err.Error()
109+
rsp := models.ProblemDetails{
110+
Title: "Malformed request syntax",
111+
Status: http.StatusBadRequest,
112+
Detail: problemDetail,
113+
}
114+
logger.EeLog.Errorln(problemDetail)
115+
c.JSON(http.StatusBadRequest, rsp)
116+
return
117+
}
118+
119+
ueIdentity := c.Params.ByName("ueIdentity")
120+
subscriptionID := c.Params.ByName("subscriptionId")
121+
122+
logger.EeLog.Infoln("Handle Update EE subscription")
123+
logger.EeLog.Warnln("Update EE Subscription is not implemented")
124+
125+
s.Processor().UpdateEeSubscriptionProcedure(c, ueIdentity, subscriptionID, patchList)
126+
}
127+
128+
func (s *Server) HandleIndex(c *gin.Context) {
129+
c.String(http.StatusOK, "Hello World!")
130+
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
1-
package httpcallback
1+
package sbi
22

33
import (
44
"net/http"
5+
"strings"
56

67
"github.com/gin-gonic/gin"
78

89
"github.com/free5gc/openapi"
910
"github.com/free5gc/openapi/models"
1011
"github.com/free5gc/udm/internal/logger"
11-
"github.com/free5gc/udm/internal/sbi/producer"
12-
"github.com/free5gc/util/httpwrapper"
1312
)
1413

15-
func HTTPDataChangeNotificationToNF(c *gin.Context) {
14+
func (s *Server) getHttpCallBackRoutes() []Route {
15+
return []Route{
16+
{
17+
"Index",
18+
"GET",
19+
"/",
20+
s.HandleIndex,
21+
},
22+
23+
{
24+
"DataChangeNotificationToNF",
25+
strings.ToUpper("Post"),
26+
"/sdm-subscriptions",
27+
s.HandleDataChangeNotificationToNF,
28+
},
29+
}
30+
}
31+
32+
func (s *Server) HandleDataChangeNotificationToNF(c *gin.Context) {
1633
var dataChangeNotify models.DataChangeNotify
17-
// step 1: retrieve http request body
1834
requestBody, err := c.GetRawData()
1935
if err != nil {
2036
problemDetail := models.ProblemDetails{
@@ -28,7 +44,6 @@ func HTTPDataChangeNotificationToNF(c *gin.Context) {
2844
return
2945
}
3046

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

45-
req := httpwrapper.NewRequest(c.Request, dataChangeNotify)
46-
req.Params["supi"] = c.Params.ByName("supi")
60+
supi := c.Params.ByName("supi")
4761

48-
rsp := producer.HandleDataChangeNotificationToNFRequest(req)
49-
responseBody, err := openapi.Serialize(rsp.Body, "application/json")
50-
if err != nil {
51-
logger.CallbackLog.Errorln(err)
52-
problemDetails := models.ProblemDetails{
53-
Status: http.StatusInternalServerError,
54-
Cause: "SYSTEM_FAILURE",
55-
Detail: err.Error(),
56-
}
57-
c.JSON(http.StatusInternalServerError, problemDetails)
58-
} else {
59-
c.Data(rsp.Status, "application/json", responseBody)
60-
}
62+
logger.CallbackLog.Infof("Handle DataChangeNotificationToNF")
63+
64+
s.Processor().DataChangeNotificationProcedure(c, dataChangeNotify.NotifyItems, supi)
6165
}
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1-
/*
2-
* Nudm_PP
3-
*
4-
* Nudm Parameter Provision Service
5-
*
6-
* API version: 1.0.0
7-
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
8-
*/
9-
10-
package parameterprovision
1+
package sbi
112

123
import (
134
"net/http"
5+
"strings"
146

157
"github.com/gin-gonic/gin"
168

179
"github.com/free5gc/openapi"
1810
"github.com/free5gc/openapi/models"
1911
"github.com/free5gc/udm/internal/logger"
20-
"github.com/free5gc/udm/internal/sbi/producer"
21-
"github.com/free5gc/util/httpwrapper"
2212
)
2313

24-
// Update - provision parameters
25-
func HTTPUpdate(c *gin.Context) {
14+
func (s *Server) getParameterProvisionRoutes() []Route {
15+
return []Route{
16+
{
17+
"Index",
18+
"GET",
19+
"/",
20+
s.HandleIndex,
21+
},
22+
23+
{
24+
"Update",
25+
strings.ToUpper("Patch"),
26+
"/:gpsi/pp-data",
27+
s.HandleUpdate,
28+
},
29+
}
30+
}
31+
32+
func (s *Server) HandleUpdate(c *gin.Context) {
2633
var ppDataReq models.PpData
2734

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

56-
req := httpwrapper.NewRequest(c.Request, ppDataReq)
57-
req.Params["gspi"] = c.Params.ByName("gpsi")
58-
59-
rsp := producer.HandleUpdateRequest(req)
60-
61-
responseBody, err := openapi.Serialize(rsp.Body, "application/json")
62-
if err != nil {
63-
logger.PpLog.Errorln(err)
64-
problemDetails := models.ProblemDetails{
65-
Status: http.StatusInternalServerError,
66-
Cause: "SYSTEM_FAILURE",
67-
Detail: err.Error(),
63+
gpsi := c.Params.ByName("gpsi")
64+
if gpsi == "" {
65+
problemDetails := &models.ProblemDetails{
66+
Status: http.StatusBadRequest,
67+
Cause: "NO_GPSI",
6868
}
69-
c.JSON(http.StatusInternalServerError, problemDetails)
70-
} else {
71-
c.Data(rsp.Status, "application/json", responseBody)
69+
c.JSON(int(problemDetails.Status), problemDetails)
70+
return
7271
}
72+
73+
logger.PpLog.Infoln("Handle UpdateRequest")
74+
75+
// step 3: handle the message
76+
s.Processor().UpdateProcedure(c, ppDataReq, gpsi)
7377
}

0 commit comments

Comments
 (0)