This repository has been archived by the owner on Apr 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
nssaiavailability_store.go
206 lines (174 loc) · 5.6 KB
/
nssaiavailability_store.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* NSSF NSSAI Availability
*
* NSSF NSSAI Availability Service
*/
package nssf_producer
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"reflect"
jsonpatch "github.com/evanphx/json-patch"
. "free5gc/lib/openapi/models"
"free5gc/src/nssf/factory"
"free5gc/src/nssf/logger"
. "free5gc/src/nssf/plugin"
"free5gc/src/nssf/util"
)
// NSSAIAvailability DELETE method
func nssaiavailabilityDelete(nfId string, d *ProblemDetails) (status int) {
for i, amfConfig := range factory.NssfConfig.Configuration.AmfList {
if amfConfig.NfId == nfId {
factory.NssfConfig.Configuration.AmfList = append(
factory.NssfConfig.Configuration.AmfList[:i],
factory.NssfConfig.Configuration.AmfList[i+1:]...)
status = http.StatusNoContent
return
}
}
problemDetail := fmt.Sprintf("AMF ID '%s' does not exist", nfId)
*d = ProblemDetails{
Title: util.UNSUPPORTED_RESOURCE,
Status: http.StatusNotFound,
Detail: problemDetail,
}
status = http.StatusNotFound
return
}
// NSSAIAvailability PATCH method
func nssaiavailabilityPatch(nfId string,
p PatchDocument,
a *AuthorizedNssaiAvailabilityInfo,
d *ProblemDetails) (status int) {
var amfIdx int
var original []byte
hitAmf := false
for amfIdx, amfConfig := range factory.NssfConfig.Configuration.AmfList {
if amfConfig.NfId == nfId {
// Since json-patch package does not have idea of optional field of datatype,
// provide with null or empty value instead of omitting the field
temp := factory.NssfConfig.Configuration.AmfList[amfIdx].SupportedNssaiAvailabilityData
const DUMMY_STRING string = "DUMMY"
for i := range temp {
for j := range temp[i].SupportedSnssaiList {
if temp[i].SupportedSnssaiList[j].Sd == "" {
temp[i].SupportedSnssaiList[j].Sd = DUMMY_STRING
}
}
}
original, _ = json.Marshal(temp)
original = bytes.ReplaceAll(original, []byte(DUMMY_STRING), []byte(""))
// original, _ = json.Marshal(factory.NssfConfig.Configuration.AmfList[amfIdx].SupportedNssaiAvailabilityData)
hitAmf = true
break
}
}
if !hitAmf {
problemDetail := fmt.Sprintf("AMF ID '%s' does not exist", nfId)
*d = ProblemDetails{
Title: util.UNSUPPORTED_RESOURCE,
Status: http.StatusNotFound,
Detail: problemDetail,
}
status = http.StatusNotFound
return
}
// TODO: Check if returned HTTP status codes or problem details are proper when errors occur
// Provide JSON string with null or empty value in `Value` of `PatchItem`
for i, patchItem := range p {
if reflect.ValueOf(patchItem.Value).Kind() == reflect.Map {
_, exist := patchItem.Value.(map[string]interface{})["sst"]
_, notExist := patchItem.Value.(map[string]interface{})["sd"]
if exist && !notExist {
p[i].Value.(map[string]interface{})["sd"] = ""
}
}
}
patchJson, _ := json.Marshal(p)
patch, err := jsonpatch.DecodePatch(patchJson)
if err != nil {
*d = ProblemDetails{
Title: util.MALFORMED_REQUEST,
Status: http.StatusBadRequest,
Detail: err.Error(),
}
status = http.StatusBadRequest
return
}
modified, err := patch.Apply(original)
if err != nil {
*d = ProblemDetails{
Title: util.INVALID_REQUEST,
Status: http.StatusConflict,
Detail: err.Error(),
}
status = http.StatusConflict
return
}
err = json.Unmarshal(modified, &factory.NssfConfig.Configuration.AmfList[amfIdx].SupportedNssaiAvailabilityData)
if err != nil {
*d = ProblemDetails{
Title: util.INVALID_REQUEST,
Status: http.StatusBadRequest,
Detail: err.Error(),
}
status = http.StatusBadRequest
return
}
// Return all authorized NSSAI availability information
a.AuthorizedNssaiAvailabilityData, _ = util.AuthorizeOfAmfFromConfig(nfId)
// TODO: Return authorized NSSAI availability information of updated TAI only
return http.StatusOK
}
// NSSAIAvailability PUT method
func nssaiavailabilityPut(nfId string,
n NssaiAvailabilityInfo,
a *AuthorizedNssaiAvailabilityInfo,
d *ProblemDetails) (status int) {
for _, s := range n.SupportedNssaiAvailabilityData {
if !util.CheckSupportedNssaiInPlmn(s.SupportedSnssaiList, *s.Tai.PlmnId) {
*d = ProblemDetails{
Title: util.UNSUPPORTED_RESOURCE,
Status: http.StatusForbidden,
Detail: "S-NSSAI in Requested NSSAI is not supported in PLMN",
Cause: "SNSSAI_NOT_SUPPORTED",
}
status = http.StatusForbidden
return
}
}
// TODO: Currently authorize all the provided S-NSSAIs
// Take some issue into consideration e.g. operator policies
hitAmf := false
// Find AMF configuration of given NfId
// If found, then update the SupportedNssaiAvailabilityData
for i, amfConfig := range factory.NssfConfig.Configuration.AmfList {
if amfConfig.NfId == nfId {
factory.NssfConfig.Configuration.AmfList[i].SupportedNssaiAvailabilityData = n.SupportedNssaiAvailabilityData
hitAmf = true
break
}
}
// If no AMF record is found, create a new one
if !hitAmf {
var amfConfig factory.AmfConfig
amfConfig.NfId = nfId
amfConfig.SupportedNssaiAvailabilityData = n.SupportedNssaiAvailabilityData
factory.NssfConfig.Configuration.AmfList = append(factory.NssfConfig.Configuration.AmfList,
amfConfig)
}
// Return all authorized NSSAI availability information
// a.AuthorizedNssaiAvailabilityData, _ = authorizeOfAmfFromConfig(nfId)
// Return authorized NSSAI availability information of updated TAI only
for _, s := range n.SupportedNssaiAvailabilityData {
authorizedNssaiAvailabilityData, err := util.AuthorizeOfAmfTaFromConfig(nfId, *s.Tai)
if err == nil {
a.AuthorizedNssaiAvailabilityData = append(a.AuthorizedNssaiAvailabilityData, authorizedNssaiAvailabilityData)
} else {
logger.Nssaiavailability.Warnf(err.Error())
}
}
return http.StatusOK
}