/src/open5gs/lib/sbi/openapi/model/nssai.c
Line | Count | Source |
1 | | |
2 | | #include <stdlib.h> |
3 | | #include <string.h> |
4 | | #include <stdio.h> |
5 | | #include "nssai.h" |
6 | | |
7 | | OpenAPI_nssai_t *OpenAPI_nssai_create( |
8 | | char *supported_features, |
9 | | OpenAPI_list_t *default_single_nssais, |
10 | | OpenAPI_list_t *single_nssais, |
11 | | char *provisioning_time, |
12 | | OpenAPI_list_t* additional_snssai_data, |
13 | | bool is_suppress_nssrg_ind, |
14 | | int suppress_nssrg_ind |
15 | | ) |
16 | 141 | { |
17 | 141 | OpenAPI_nssai_t *nssai_local_var = ogs_malloc(sizeof(OpenAPI_nssai_t)); |
18 | 141 | ogs_assert(nssai_local_var); |
19 | | |
20 | 141 | nssai_local_var->supported_features = supported_features; |
21 | 141 | nssai_local_var->default_single_nssais = default_single_nssais; |
22 | 141 | nssai_local_var->single_nssais = single_nssais; |
23 | 141 | nssai_local_var->provisioning_time = provisioning_time; |
24 | 141 | nssai_local_var->additional_snssai_data = additional_snssai_data; |
25 | 141 | nssai_local_var->is_suppress_nssrg_ind = is_suppress_nssrg_ind; |
26 | 141 | nssai_local_var->suppress_nssrg_ind = suppress_nssrg_ind; |
27 | | |
28 | 141 | return nssai_local_var; |
29 | 141 | } |
30 | | |
31 | | void OpenAPI_nssai_free(OpenAPI_nssai_t *nssai) |
32 | 131 | { |
33 | 131 | OpenAPI_lnode_t *node = NULL; |
34 | | |
35 | 131 | if (NULL == nssai) { |
36 | 0 | return; |
37 | 0 | } |
38 | 131 | if (nssai->supported_features) { |
39 | 2 | ogs_free(nssai->supported_features); |
40 | 2 | nssai->supported_features = NULL; |
41 | 2 | } |
42 | 131 | if (nssai->default_single_nssais) { |
43 | 131 | OpenAPI_list_for_each(nssai->default_single_nssais, node) { |
44 | 129 | OpenAPI_snssai_free(node->data); |
45 | 129 | } |
46 | 131 | OpenAPI_list_free(nssai->default_single_nssais); |
47 | 131 | nssai->default_single_nssais = NULL; |
48 | 131 | } |
49 | 131 | if (nssai->single_nssais) { |
50 | 98 | OpenAPI_list_for_each(nssai->single_nssais, node) { |
51 | 96 | OpenAPI_snssai_free(node->data); |
52 | 96 | } |
53 | 98 | OpenAPI_list_free(nssai->single_nssais); |
54 | 98 | nssai->single_nssais = NULL; |
55 | 98 | } |
56 | 131 | if (nssai->provisioning_time) { |
57 | 0 | ogs_free(nssai->provisioning_time); |
58 | 0 | nssai->provisioning_time = NULL; |
59 | 0 | } |
60 | 131 | if (nssai->additional_snssai_data) { |
61 | 0 | OpenAPI_list_for_each(nssai->additional_snssai_data, node) { |
62 | 0 | OpenAPI_map_t *localKeyValue = (OpenAPI_map_t*)node->data; |
63 | 0 | ogs_free(localKeyValue->key); |
64 | 0 | OpenAPI_additional_snssai_data_free(localKeyValue->value); |
65 | 0 | OpenAPI_map_free(localKeyValue); |
66 | 0 | } |
67 | 0 | OpenAPI_list_free(nssai->additional_snssai_data); |
68 | 0 | nssai->additional_snssai_data = NULL; |
69 | 0 | } |
70 | 131 | ogs_free(nssai); |
71 | 131 | } |
72 | | |
73 | | cJSON *OpenAPI_nssai_convertToJSON(OpenAPI_nssai_t *nssai) |
74 | 0 | { |
75 | 0 | cJSON *item = NULL; |
76 | 0 | OpenAPI_lnode_t *node = NULL; |
77 | |
|
78 | 0 | if (nssai == NULL) { |
79 | 0 | ogs_error("OpenAPI_nssai_convertToJSON() failed [Nssai]"); |
80 | 0 | return NULL; |
81 | 0 | } |
82 | | |
83 | 0 | item = cJSON_CreateObject(); |
84 | 0 | if (nssai->supported_features) { |
85 | 0 | if (cJSON_AddStringToObject(item, "supportedFeatures", nssai->supported_features) == NULL) { |
86 | 0 | ogs_error("OpenAPI_nssai_convertToJSON() failed [supported_features]"); |
87 | 0 | goto end; |
88 | 0 | } |
89 | 0 | } |
90 | | |
91 | 0 | if (!nssai->default_single_nssais) { |
92 | 0 | ogs_error("OpenAPI_nssai_convertToJSON() failed [default_single_nssais]"); |
93 | 0 | return NULL; |
94 | 0 | } |
95 | 0 | cJSON *default_single_nssaisList = cJSON_AddArrayToObject(item, "defaultSingleNssais"); |
96 | 0 | if (default_single_nssaisList == NULL) { |
97 | 0 | ogs_error("OpenAPI_nssai_convertToJSON() failed [default_single_nssais]"); |
98 | 0 | goto end; |
99 | 0 | } |
100 | 0 | OpenAPI_list_for_each(nssai->default_single_nssais, node) { |
101 | 0 | cJSON *itemLocal = OpenAPI_snssai_convertToJSON(node->data); |
102 | 0 | if (itemLocal == NULL) { |
103 | 0 | ogs_error("OpenAPI_nssai_convertToJSON() failed [default_single_nssais]"); |
104 | 0 | goto end; |
105 | 0 | } |
106 | 0 | cJSON_AddItemToArray(default_single_nssaisList, itemLocal); |
107 | 0 | } |
108 | | |
109 | 0 | if (nssai->single_nssais) { |
110 | 0 | cJSON *single_nssaisList = cJSON_AddArrayToObject(item, "singleNssais"); |
111 | 0 | if (single_nssaisList == NULL) { |
112 | 0 | ogs_error("OpenAPI_nssai_convertToJSON() failed [single_nssais]"); |
113 | 0 | goto end; |
114 | 0 | } |
115 | 0 | OpenAPI_list_for_each(nssai->single_nssais, node) { |
116 | 0 | cJSON *itemLocal = OpenAPI_snssai_convertToJSON(node->data); |
117 | 0 | if (itemLocal == NULL) { |
118 | 0 | ogs_error("OpenAPI_nssai_convertToJSON() failed [single_nssais]"); |
119 | 0 | goto end; |
120 | 0 | } |
121 | 0 | cJSON_AddItemToArray(single_nssaisList, itemLocal); |
122 | 0 | } |
123 | 0 | } |
124 | | |
125 | 0 | if (nssai->provisioning_time) { |
126 | 0 | if (cJSON_AddStringToObject(item, "provisioningTime", nssai->provisioning_time) == NULL) { |
127 | 0 | ogs_error("OpenAPI_nssai_convertToJSON() failed [provisioning_time]"); |
128 | 0 | goto end; |
129 | 0 | } |
130 | 0 | } |
131 | | |
132 | 0 | if (nssai->additional_snssai_data) { |
133 | 0 | cJSON *additional_snssai_data = cJSON_AddObjectToObject(item, "additionalSnssaiData"); |
134 | 0 | if (additional_snssai_data == NULL) { |
135 | 0 | ogs_error("OpenAPI_nssai_convertToJSON() failed [additional_snssai_data]"); |
136 | 0 | goto end; |
137 | 0 | } |
138 | 0 | cJSON *localMapObject = additional_snssai_data; |
139 | 0 | if (nssai->additional_snssai_data) { |
140 | 0 | OpenAPI_list_for_each(nssai->additional_snssai_data, node) { |
141 | 0 | OpenAPI_map_t *localKeyValue = (OpenAPI_map_t*)node->data; |
142 | 0 | if (localKeyValue == NULL) { |
143 | 0 | ogs_error("OpenAPI_nssai_convertToJSON() failed [additional_snssai_data]"); |
144 | 0 | goto end; |
145 | 0 | } |
146 | 0 | if (localKeyValue->key == NULL) { |
147 | 0 | ogs_error("OpenAPI_nssai_convertToJSON() failed [additional_snssai_data]"); |
148 | 0 | goto end; |
149 | 0 | } |
150 | 0 | cJSON *itemLocal = localKeyValue->value ? |
151 | 0 | OpenAPI_additional_snssai_data_convertToJSON(localKeyValue->value) : |
152 | 0 | cJSON_CreateNull(); |
153 | 0 | if (itemLocal == NULL) { |
154 | 0 | ogs_error("OpenAPI_nssai_convertToJSON() failed [inner]"); |
155 | 0 | goto end; |
156 | 0 | } |
157 | 0 | cJSON_AddItemToObject(localMapObject, localKeyValue->key, itemLocal); |
158 | 0 | } |
159 | 0 | } |
160 | 0 | } |
161 | | |
162 | 0 | if (nssai->is_suppress_nssrg_ind) { |
163 | 0 | if (cJSON_AddBoolToObject(item, "suppressNssrgInd", nssai->suppress_nssrg_ind) == NULL) { |
164 | 0 | ogs_error("OpenAPI_nssai_convertToJSON() failed [suppress_nssrg_ind]"); |
165 | 0 | goto end; |
166 | 0 | } |
167 | 0 | } |
168 | | |
169 | 0 | end: |
170 | 0 | return item; |
171 | 0 | } |
172 | | |
173 | | OpenAPI_nssai_t *OpenAPI_nssai_parseFromJSON(cJSON *nssaiJSON) |
174 | 208 | { |
175 | 208 | OpenAPI_nssai_t *nssai_local_var = NULL; |
176 | 208 | OpenAPI_lnode_t *node = NULL; |
177 | 208 | cJSON *supported_features = NULL; |
178 | 208 | cJSON *default_single_nssais = NULL; |
179 | 208 | OpenAPI_list_t *default_single_nssaisList = NULL; |
180 | 208 | cJSON *single_nssais = NULL; |
181 | 208 | OpenAPI_list_t *single_nssaisList = NULL; |
182 | 208 | cJSON *provisioning_time = NULL; |
183 | 208 | cJSON *additional_snssai_data = NULL; |
184 | 208 | OpenAPI_list_t *additional_snssai_dataList = NULL; |
185 | 208 | cJSON *suppress_nssrg_ind = NULL; |
186 | 208 | supported_features = cJSON_GetObjectItemCaseSensitive(nssaiJSON, "supportedFeatures"); |
187 | 208 | if (supported_features) { |
188 | 23 | if (!cJSON_IsString(supported_features) && !cJSON_IsNull(supported_features)) { |
189 | 8 | ogs_error("OpenAPI_nssai_parseFromJSON() failed [supported_features]"); |
190 | 8 | goto end; |
191 | 8 | } |
192 | 23 | } |
193 | | |
194 | 200 | default_single_nssais = cJSON_GetObjectItemCaseSensitive(nssaiJSON, "defaultSingleNssais"); |
195 | 200 | if (!default_single_nssais) { |
196 | 43 | ogs_error("OpenAPI_nssai_parseFromJSON() failed [default_single_nssais]"); |
197 | 43 | goto end; |
198 | 43 | } |
199 | 157 | cJSON *default_single_nssais_local = NULL; |
200 | 157 | if (!cJSON_IsArray(default_single_nssais)) { |
201 | 2 | ogs_error("OpenAPI_nssai_parseFromJSON() failed [default_single_nssais]"); |
202 | 2 | goto end; |
203 | 2 | } |
204 | | |
205 | 155 | default_single_nssaisList = OpenAPI_list_create(); |
206 | | |
207 | 155 | cJSON_ArrayForEach(default_single_nssais_local, default_single_nssais) { |
208 | 153 | if (!cJSON_IsObject(default_single_nssais_local)) { |
209 | 2 | ogs_error("OpenAPI_nssai_parseFromJSON() failed [default_single_nssais]"); |
210 | 2 | goto end; |
211 | 2 | } |
212 | 151 | OpenAPI_snssai_t *default_single_nssaisItem = OpenAPI_snssai_parseFromJSON(default_single_nssais_local); |
213 | 151 | if (!default_single_nssaisItem) { |
214 | 5 | ogs_error("No default_single_nssaisItem"); |
215 | 5 | goto end; |
216 | 5 | } |
217 | 146 | OpenAPI_list_add(default_single_nssaisList, default_single_nssaisItem); |
218 | 146 | } |
219 | | |
220 | 148 | single_nssais = cJSON_GetObjectItemCaseSensitive(nssaiJSON, "singleNssais"); |
221 | 148 | if (single_nssais) { |
222 | 112 | cJSON *single_nssais_local = NULL; |
223 | 112 | if (!cJSON_IsArray(single_nssais)) { |
224 | 2 | ogs_error("OpenAPI_nssai_parseFromJSON() failed [single_nssais]"); |
225 | 2 | goto end; |
226 | 2 | } |
227 | | |
228 | 110 | single_nssaisList = OpenAPI_list_create(); |
229 | | |
230 | 110 | cJSON_ArrayForEach(single_nssais_local, single_nssais) { |
231 | 109 | if (!cJSON_IsObject(single_nssais_local)) { |
232 | 1 | ogs_error("OpenAPI_nssai_parseFromJSON() failed [single_nssais]"); |
233 | 1 | goto end; |
234 | 1 | } |
235 | 108 | OpenAPI_snssai_t *single_nssaisItem = OpenAPI_snssai_parseFromJSON(single_nssais_local); |
236 | 108 | if (!single_nssaisItem) { |
237 | 4 | ogs_error("No single_nssaisItem"); |
238 | 4 | goto end; |
239 | 4 | } |
240 | 104 | OpenAPI_list_add(single_nssaisList, single_nssaisItem); |
241 | 104 | } |
242 | 110 | } |
243 | | |
244 | 141 | provisioning_time = cJSON_GetObjectItemCaseSensitive(nssaiJSON, "provisioningTime"); |
245 | 141 | if (provisioning_time) { |
246 | 0 | if (!cJSON_IsString(provisioning_time) && !cJSON_IsNull(provisioning_time)) { |
247 | 0 | ogs_error("OpenAPI_nssai_parseFromJSON() failed [provisioning_time]"); |
248 | 0 | goto end; |
249 | 0 | } |
250 | 0 | } |
251 | | |
252 | 141 | additional_snssai_data = cJSON_GetObjectItemCaseSensitive(nssaiJSON, "additionalSnssaiData"); |
253 | 141 | if (additional_snssai_data) { |
254 | 0 | cJSON *additional_snssai_data_local_map = NULL; |
255 | 0 | if (!cJSON_IsObject(additional_snssai_data) && !cJSON_IsNull(additional_snssai_data)) { |
256 | 0 | ogs_error("OpenAPI_nssai_parseFromJSON() failed [additional_snssai_data]"); |
257 | 0 | goto end; |
258 | 0 | } |
259 | 0 | if (cJSON_IsObject(additional_snssai_data)) { |
260 | 0 | additional_snssai_dataList = OpenAPI_list_create(); |
261 | 0 | OpenAPI_map_t *localMapKeyPair = NULL; |
262 | 0 | cJSON_ArrayForEach(additional_snssai_data_local_map, additional_snssai_data) { |
263 | 0 | cJSON *localMapObject = additional_snssai_data_local_map; |
264 | 0 | if (cJSON_IsObject(localMapObject)) { |
265 | 0 | localMapKeyPair = OpenAPI_map_create( |
266 | 0 | ogs_strdup(localMapObject->string), OpenAPI_additional_snssai_data_parseFromJSON(localMapObject)); |
267 | 0 | } else if (cJSON_IsNull(localMapObject)) { |
268 | 0 | localMapKeyPair = OpenAPI_map_create(ogs_strdup(localMapObject->string), NULL); |
269 | 0 | } else { |
270 | 0 | ogs_error("OpenAPI_nssai_parseFromJSON() failed [inner]"); |
271 | 0 | goto end; |
272 | 0 | } |
273 | 0 | if (localMapKeyPair == NULL) { |
274 | 0 | ogs_error("OpenAPI_nssai_parseFromJSON() failed [additional_snssai_data]"); |
275 | 0 | goto end; |
276 | 0 | } |
277 | 0 | OpenAPI_list_add(additional_snssai_dataList, localMapKeyPair); |
278 | 0 | } |
279 | 0 | } |
280 | 0 | } |
281 | | |
282 | 141 | suppress_nssrg_ind = cJSON_GetObjectItemCaseSensitive(nssaiJSON, "suppressNssrgInd"); |
283 | 141 | if (suppress_nssrg_ind) { |
284 | 0 | if (!cJSON_IsBool(suppress_nssrg_ind)) { |
285 | 0 | ogs_error("OpenAPI_nssai_parseFromJSON() failed [suppress_nssrg_ind]"); |
286 | 0 | goto end; |
287 | 0 | } |
288 | 0 | } |
289 | | |
290 | 141 | nssai_local_var = OpenAPI_nssai_create ( |
291 | 141 | supported_features && !cJSON_IsNull(supported_features) ? ogs_strdup(supported_features->valuestring) : NULL, |
292 | 141 | default_single_nssaisList, |
293 | 141 | single_nssais ? single_nssaisList : NULL, |
294 | 141 | provisioning_time && !cJSON_IsNull(provisioning_time) ? ogs_strdup(provisioning_time->valuestring) : NULL, |
295 | 141 | additional_snssai_data ? additional_snssai_dataList : NULL, |
296 | 141 | suppress_nssrg_ind ? true : false, |
297 | 141 | suppress_nssrg_ind ? suppress_nssrg_ind->valueint : 0 |
298 | 141 | ); |
299 | | |
300 | 141 | return nssai_local_var; |
301 | 67 | end: |
302 | 67 | if (default_single_nssaisList) { |
303 | 14 | OpenAPI_list_for_each(default_single_nssaisList, node) { |
304 | 7 | OpenAPI_snssai_free(node->data); |
305 | 7 | } |
306 | 14 | OpenAPI_list_free(default_single_nssaisList); |
307 | 14 | default_single_nssaisList = NULL; |
308 | 14 | } |
309 | 67 | if (single_nssaisList) { |
310 | 5 | OpenAPI_list_for_each(single_nssaisList, node) { |
311 | 1 | OpenAPI_snssai_free(node->data); |
312 | 1 | } |
313 | 5 | OpenAPI_list_free(single_nssaisList); |
314 | 5 | single_nssaisList = NULL; |
315 | 5 | } |
316 | 67 | if (additional_snssai_dataList) { |
317 | 0 | OpenAPI_list_for_each(additional_snssai_dataList, node) { |
318 | 0 | OpenAPI_map_t *localKeyValue = (OpenAPI_map_t*)node->data; |
319 | 0 | ogs_free(localKeyValue->key); |
320 | 0 | OpenAPI_additional_snssai_data_free(localKeyValue->value); |
321 | 0 | OpenAPI_map_free(localKeyValue); |
322 | 0 | } |
323 | 0 | OpenAPI_list_free(additional_snssai_dataList); |
324 | 0 | additional_snssai_dataList = NULL; |
325 | 0 | } |
326 | 67 | return NULL; |
327 | 141 | } |
328 | | |
329 | | OpenAPI_nssai_t *OpenAPI_nssai_copy(OpenAPI_nssai_t *dst, OpenAPI_nssai_t *src) |
330 | 0 | { |
331 | 0 | cJSON *item = NULL; |
332 | 0 | char *content = NULL; |
333 | |
|
334 | 0 | ogs_assert(src); |
335 | 0 | item = OpenAPI_nssai_convertToJSON(src); |
336 | 0 | if (!item) { |
337 | 0 | ogs_error("OpenAPI_nssai_convertToJSON() failed"); |
338 | 0 | return NULL; |
339 | 0 | } |
340 | | |
341 | 0 | content = cJSON_Print(item); |
342 | 0 | cJSON_Delete(item); |
343 | |
|
344 | 0 | if (!content) { |
345 | 0 | ogs_error("cJSON_Print() failed"); |
346 | 0 | return NULL; |
347 | 0 | } |
348 | | |
349 | 0 | item = cJSON_Parse(content); |
350 | 0 | ogs_free(content); |
351 | 0 | if (!item) { |
352 | 0 | ogs_error("cJSON_Parse() failed"); |
353 | 0 | return NULL; |
354 | 0 | } |
355 | | |
356 | 0 | OpenAPI_nssai_free(dst); |
357 | 0 | dst = OpenAPI_nssai_parseFromJSON(item); |
358 | 0 | cJSON_Delete(item); |
359 | |
|
360 | 0 | return dst; |
361 | 0 | } |
362 | | |