-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_nf_instance_id_document.go
161 lines (141 loc) · 4.68 KB
/
api_nf_instance_id_document.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* NRF NFManagement Service
*
* NRF NFManagement Service
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package management
import (
"github.com/nycu-ucr/gin"
"github.com/nycu-ucr/gonet/http"
"github.com/nycu-ucr/http_wrapper"
"github.com/nycu-ucr/nrf/logger"
"github.com/nycu-ucr/nrf/producer"
"github.com/nycu-ucr/openapi"
"github.com/nycu-ucr/openapi/models"
)
// DeregisterNFInstance - Deregisters a given NF Instance
func HTTPDeregisterNFInstance(c *gin.Context) {
// parse nfInstanceId
req := http_wrapper.NewRequest(c.Request, nil)
req.Params["nfInstanceID"] = c.Params.ByName("nfInstanceID")
httpResponse := producer.HandleNFDeregisterRequest(req)
responseBody, err := openapi.Serialize(httpResponse.Body, "application/json")
if err != nil {
logger.ManagementLog.Warnln(err)
problemDetails := models.ProblemDetails{
Status: http.StatusInternalServerError,
Cause: "SYSTEM_FAILURE",
Detail: err.Error(),
}
c.JSON(http.StatusInternalServerError, problemDetails)
} else {
c.Data(httpResponse.Status, "application/json", responseBody)
}
}
// GetNFInstance - Read the profile of a given NF Instance
func HTTPGetNFInstance(c *gin.Context) {
req := http_wrapper.NewRequest(c.Request, nil)
req.Params["nfInstanceID"] = c.Params.ByName("nfInstanceID")
httpResponse := producer.HandleGetNFInstanceRequest(req)
responseBody, err := openapi.Serialize(httpResponse.Body, "application/json")
if err != nil {
logger.ManagementLog.Warnln(err)
problemDetails := models.ProblemDetails{
Status: http.StatusInternalServerError,
Cause: "SYSTEM_FAILURE",
Detail: err.Error(),
}
c.JSON(http.StatusInternalServerError, problemDetails)
} else {
c.Data(httpResponse.Status, "application/json", responseBody)
}
}
// RegisterNFInstance - Register a new NF Instance
func HTTPRegisterNFInstance(c *gin.Context) {
// println("Start HTTPRegisterNFInstance")
// t1 := time.Now()
var nfprofile models.NfProfile
// step 1: retrieve http request body
requestBody, err := c.GetRawData()
if err != nil {
problemDetail := models.ProblemDetails{
Title: "System failure",
Status: http.StatusInternalServerError,
Detail: err.Error(),
Cause: "SYSTEM_FAILURE",
}
logger.ManagementLog.Errorf("Get Request Body error: %+v", err)
c.JSON(http.StatusInternalServerError, problemDetail)
return
}
// step 2: convert requestBody to openapi models
err = openapi.Deserialize(&nfprofile, requestBody, "application/json")
if err != nil {
problemDetail := "[Request Body] " + err.Error()
rsp := models.ProblemDetails{
Title: "Malformed request syntax",
Status: http.StatusBadRequest,
Detail: problemDetail,
}
logger.ManagementLog.Errorln(problemDetail)
c.JSON(http.StatusBadRequest, rsp)
return
}
// step 3: encapsulate the request by http_wrapper package
req := http_wrapper.NewRequest(c.Request, nfprofile)
// step 4: call producer
httpResponse := producer.HandleNFRegisterRequest(req)
for key, val := range httpResponse.Header {
c.Header(key, val[0])
}
responseBody, err := openapi.Serialize(httpResponse.Body, "application/json")
if err != nil {
logger.ManagementLog.Warnln(err)
problemDetails := models.ProblemDetails{
Status: http.StatusInternalServerError,
Cause: "SYSTEM_FAILURE",
Detail: err.Error(),
}
c.JSON(http.StatusInternalServerError, problemDetails)
} else {
c.Data(httpResponse.Status, "application/json", responseBody)
}
// t2 := time.Now()
// logger.ManagementLog.Infof("\u001b[32;1mHTTPRegisterNFInstance Time\u001b[0m: %v (second)", t2.Sub(t1).Seconds())
// println("End HTTPRegisterNFInstance")
}
// UpdateNFInstance - Update NF Instance profile
func HTTPUpdateNFInstance(c *gin.Context) {
// step 1: retrieve http request body
requestBody, err := c.GetRawData()
if err != nil {
problemDetail := models.ProblemDetails{
Title: "System failure",
Status: http.StatusInternalServerError,
Detail: err.Error(),
Cause: "SYSTEM_FAILURE",
}
logger.ManagementLog.Errorf("Get Request Body error: %+v", err)
c.JSON(http.StatusInternalServerError, problemDetail)
return
}
req := http_wrapper.NewRequest(c.Request, nil)
req.Params["nfInstanceID"] = c.Params.ByName("nfInstanceID")
req.Body = requestBody
httpResponse := producer.HandleUpdateNFInstanceRequest(req)
responseBody, err := openapi.Serialize(httpResponse.Body, "application/json")
if err != nil {
logger.ManagementLog.Warnln(err)
problemDetails := models.ProblemDetails{
Status: http.StatusInternalServerError,
Cause: "SYSTEM_FAILURE",
Detail: err.Error(),
}
c.JSON(http.StatusInternalServerError, problemDetails)
} else {
c.Data(httpResponse.Status, "application/json", responseBody)
}
}