/src/open5gs/lib/sbi/openapi/model/sepp_info.c
Line | Count | Source |
1 | | |
2 | | #include <stdlib.h> |
3 | | #include <string.h> |
4 | | #include <stdio.h> |
5 | | #include "sepp_info.h" |
6 | | |
7 | | OpenAPI_sepp_info_t *OpenAPI_sepp_info_create( |
8 | | char *sepp_prefix, |
9 | | OpenAPI_list_t* sepp_ports, |
10 | | OpenAPI_list_t *remote_plmn_list, |
11 | | OpenAPI_list_t *remote_snpn_list, |
12 | | OpenAPI_list_t *n32_purposes |
13 | | ) |
14 | 27 | { |
15 | 27 | OpenAPI_sepp_info_t *sepp_info_local_var = ogs_malloc(sizeof(OpenAPI_sepp_info_t)); |
16 | 27 | ogs_assert(sepp_info_local_var); |
17 | | |
18 | 27 | sepp_info_local_var->sepp_prefix = sepp_prefix; |
19 | 27 | sepp_info_local_var->sepp_ports = sepp_ports; |
20 | 27 | sepp_info_local_var->remote_plmn_list = remote_plmn_list; |
21 | 27 | sepp_info_local_var->remote_snpn_list = remote_snpn_list; |
22 | 27 | sepp_info_local_var->n32_purposes = n32_purposes; |
23 | | |
24 | 27 | return sepp_info_local_var; |
25 | 27 | } |
26 | | |
27 | | void OpenAPI_sepp_info_free(OpenAPI_sepp_info_t *sepp_info) |
28 | 27 | { |
29 | 27 | OpenAPI_lnode_t *node = NULL; |
30 | | |
31 | 27 | if (NULL == sepp_info) { |
32 | 0 | return; |
33 | 0 | } |
34 | 27 | if (sepp_info->sepp_prefix) { |
35 | 0 | ogs_free(sepp_info->sepp_prefix); |
36 | 0 | sepp_info->sepp_prefix = NULL; |
37 | 0 | } |
38 | 27 | if (sepp_info->sepp_ports) { |
39 | 10 | OpenAPI_list_for_each(sepp_info->sepp_ports, node) { |
40 | 10 | OpenAPI_map_t *localKeyValue = (OpenAPI_map_t*)node->data; |
41 | 10 | ogs_free(localKeyValue->key); |
42 | 10 | ogs_free(localKeyValue->value); |
43 | 10 | OpenAPI_map_free(localKeyValue); |
44 | 10 | } |
45 | 5 | OpenAPI_list_free(sepp_info->sepp_ports); |
46 | 5 | sepp_info->sepp_ports = NULL; |
47 | 5 | } |
48 | 27 | if (sepp_info->remote_plmn_list) { |
49 | 82 | OpenAPI_list_for_each(sepp_info->remote_plmn_list, node) { |
50 | 82 | OpenAPI_plmn_id_free(node->data); |
51 | 82 | } |
52 | 10 | OpenAPI_list_free(sepp_info->remote_plmn_list); |
53 | 10 | sepp_info->remote_plmn_list = NULL; |
54 | 10 | } |
55 | 27 | if (sepp_info->remote_snpn_list) { |
56 | 74 | OpenAPI_list_for_each(sepp_info->remote_snpn_list, node) { |
57 | 74 | OpenAPI_plmn_id_nid_free(node->data); |
58 | 74 | } |
59 | 9 | OpenAPI_list_free(sepp_info->remote_snpn_list); |
60 | 9 | sepp_info->remote_snpn_list = NULL; |
61 | 9 | } |
62 | 27 | if (sepp_info->n32_purposes) { |
63 | 0 | OpenAPI_list_free(sepp_info->n32_purposes); |
64 | 0 | sepp_info->n32_purposes = NULL; |
65 | 0 | } |
66 | 27 | ogs_free(sepp_info); |
67 | 27 | } |
68 | | |
69 | | cJSON *OpenAPI_sepp_info_convertToJSON(OpenAPI_sepp_info_t *sepp_info) |
70 | 0 | { |
71 | 0 | cJSON *item = NULL; |
72 | 0 | OpenAPI_lnode_t *node = NULL; |
73 | |
|
74 | 0 | if (sepp_info == NULL) { |
75 | 0 | ogs_error("OpenAPI_sepp_info_convertToJSON() failed [SeppInfo]"); |
76 | 0 | return NULL; |
77 | 0 | } |
78 | | |
79 | 0 | item = cJSON_CreateObject(); |
80 | 0 | if (sepp_info->sepp_prefix) { |
81 | 0 | if (cJSON_AddStringToObject(item, "seppPrefix", sepp_info->sepp_prefix) == NULL) { |
82 | 0 | ogs_error("OpenAPI_sepp_info_convertToJSON() failed [sepp_prefix]"); |
83 | 0 | goto end; |
84 | 0 | } |
85 | 0 | } |
86 | | |
87 | 0 | if (sepp_info->sepp_ports) { |
88 | 0 | cJSON *sepp_ports = cJSON_AddObjectToObject(item, "seppPorts"); |
89 | 0 | if (sepp_ports == NULL) { |
90 | 0 | ogs_error("OpenAPI_sepp_info_convertToJSON() failed [sepp_ports]"); |
91 | 0 | goto end; |
92 | 0 | } |
93 | 0 | cJSON *localMapObject = sepp_ports; |
94 | 0 | if (sepp_info->sepp_ports) { |
95 | 0 | OpenAPI_list_for_each(sepp_info->sepp_ports, node) { |
96 | 0 | OpenAPI_map_t *localKeyValue = (OpenAPI_map_t*)node->data; |
97 | 0 | if (localKeyValue == NULL) { |
98 | 0 | ogs_error("OpenAPI_sepp_info_convertToJSON() failed [sepp_ports]"); |
99 | 0 | goto end; |
100 | 0 | } |
101 | 0 | if (localKeyValue->key == NULL) { |
102 | 0 | ogs_error("OpenAPI_sepp_info_convertToJSON() failed [sepp_ports]"); |
103 | 0 | goto end; |
104 | 0 | } |
105 | 0 | if (localKeyValue->value == NULL) { |
106 | 0 | ogs_error("OpenAPI_sepp_info_convertToJSON() failed [inner]"); |
107 | 0 | goto end; |
108 | 0 | } |
109 | 0 | if (cJSON_AddNumberToObject(localMapObject, localKeyValue->key, *(double *)localKeyValue->value) == NULL) { |
110 | 0 | ogs_error("OpenAPI_sepp_info_convertToJSON() failed [inner]"); |
111 | 0 | goto end; |
112 | 0 | } |
113 | 0 | } |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | 0 | if (sepp_info->remote_plmn_list) { |
118 | 0 | cJSON *remote_plmn_listList = cJSON_AddArrayToObject(item, "remotePlmnList"); |
119 | 0 | if (remote_plmn_listList == NULL) { |
120 | 0 | ogs_error("OpenAPI_sepp_info_convertToJSON() failed [remote_plmn_list]"); |
121 | 0 | goto end; |
122 | 0 | } |
123 | 0 | OpenAPI_list_for_each(sepp_info->remote_plmn_list, node) { |
124 | 0 | cJSON *itemLocal = OpenAPI_plmn_id_convertToJSON(node->data); |
125 | 0 | if (itemLocal == NULL) { |
126 | 0 | ogs_error("OpenAPI_sepp_info_convertToJSON() failed [remote_plmn_list]"); |
127 | 0 | goto end; |
128 | 0 | } |
129 | 0 | cJSON_AddItemToArray(remote_plmn_listList, itemLocal); |
130 | 0 | } |
131 | 0 | } |
132 | | |
133 | 0 | if (sepp_info->remote_snpn_list) { |
134 | 0 | cJSON *remote_snpn_listList = cJSON_AddArrayToObject(item, "remoteSnpnList"); |
135 | 0 | if (remote_snpn_listList == NULL) { |
136 | 0 | ogs_error("OpenAPI_sepp_info_convertToJSON() failed [remote_snpn_list]"); |
137 | 0 | goto end; |
138 | 0 | } |
139 | 0 | OpenAPI_list_for_each(sepp_info->remote_snpn_list, node) { |
140 | 0 | cJSON *itemLocal = OpenAPI_plmn_id_nid_convertToJSON(node->data); |
141 | 0 | if (itemLocal == NULL) { |
142 | 0 | ogs_error("OpenAPI_sepp_info_convertToJSON() failed [remote_snpn_list]"); |
143 | 0 | goto end; |
144 | 0 | } |
145 | 0 | cJSON_AddItemToArray(remote_snpn_listList, itemLocal); |
146 | 0 | } |
147 | 0 | } |
148 | | |
149 | 0 | if (sepp_info->n32_purposes != OpenAPI_n32_purpose_NULL) { |
150 | 0 | cJSON *n32_purposesList = cJSON_AddArrayToObject(item, "n32Purposes"); |
151 | 0 | if (n32_purposesList == NULL) { |
152 | 0 | ogs_error("OpenAPI_sepp_info_convertToJSON() failed [n32_purposes]"); |
153 | 0 | goto end; |
154 | 0 | } |
155 | 0 | OpenAPI_list_for_each(sepp_info->n32_purposes, node) { |
156 | 0 | if (cJSON_AddStringToObject(n32_purposesList, "", OpenAPI_n32_purpose_ToString((intptr_t)node->data)) == NULL) { |
157 | 0 | ogs_error("OpenAPI_sepp_info_convertToJSON() failed [n32_purposes]"); |
158 | 0 | goto end; |
159 | 0 | } |
160 | 0 | } |
161 | 0 | } |
162 | | |
163 | 0 | end: |
164 | 0 | return item; |
165 | 0 | } |
166 | | |
167 | | OpenAPI_sepp_info_t *OpenAPI_sepp_info_parseFromJSON(cJSON *sepp_infoJSON) |
168 | 59 | { |
169 | 59 | OpenAPI_sepp_info_t *sepp_info_local_var = NULL; |
170 | 59 | OpenAPI_lnode_t *node = NULL; |
171 | 59 | cJSON *sepp_prefix = NULL; |
172 | 59 | cJSON *sepp_ports = NULL; |
173 | 59 | OpenAPI_list_t *sepp_portsList = NULL; |
174 | 59 | cJSON *remote_plmn_list = NULL; |
175 | 59 | OpenAPI_list_t *remote_plmn_listList = NULL; |
176 | 59 | cJSON *remote_snpn_list = NULL; |
177 | 59 | OpenAPI_list_t *remote_snpn_listList = NULL; |
178 | 59 | cJSON *n32_purposes = NULL; |
179 | 59 | OpenAPI_list_t *n32_purposesList = NULL; |
180 | 59 | sepp_prefix = cJSON_GetObjectItemCaseSensitive(sepp_infoJSON, "seppPrefix"); |
181 | 59 | if (sepp_prefix) { |
182 | 0 | if (!cJSON_IsString(sepp_prefix) && !cJSON_IsNull(sepp_prefix)) { |
183 | 0 | ogs_error("OpenAPI_sepp_info_parseFromJSON() failed [sepp_prefix]"); |
184 | 0 | goto end; |
185 | 0 | } |
186 | 0 | } |
187 | | |
188 | 59 | sepp_ports = cJSON_GetObjectItemCaseSensitive(sepp_infoJSON, "seppPorts"); |
189 | 59 | if (sepp_ports) { |
190 | 13 | cJSON *sepp_ports_local_map = NULL; |
191 | 13 | if (!cJSON_IsObject(sepp_ports) && !cJSON_IsNull(sepp_ports)) { |
192 | 1 | ogs_error("OpenAPI_sepp_info_parseFromJSON() failed [sepp_ports]"); |
193 | 1 | goto end; |
194 | 1 | } |
195 | 12 | if (cJSON_IsObject(sepp_ports)) { |
196 | 12 | sepp_portsList = OpenAPI_list_create(); |
197 | 12 | OpenAPI_map_t *localMapKeyPair = NULL; |
198 | 116 | cJSON_ArrayForEach(sepp_ports_local_map, sepp_ports) { |
199 | 116 | cJSON *localMapObject = sepp_ports_local_map; |
200 | 116 | double *localDouble = NULL; |
201 | 116 | int *localInt = NULL; |
202 | 116 | if (!cJSON_IsNumber(localMapObject)) { |
203 | 7 | ogs_error("OpenAPI_sepp_info_parseFromJSON() failed [inner]"); |
204 | 7 | goto end; |
205 | 7 | } |
206 | 109 | localDouble = (double *)ogs_calloc(1, sizeof(double)); |
207 | 109 | if (!localDouble) { |
208 | 0 | ogs_error("OpenAPI_sepp_info_parseFromJSON() failed [inner]"); |
209 | 0 | goto end; |
210 | 0 | } |
211 | 109 | *localDouble = localMapObject->valuedouble; |
212 | 109 | localMapKeyPair = OpenAPI_map_create(ogs_strdup(localMapObject->string), localDouble); |
213 | 109 | if (localMapKeyPair == NULL) { |
214 | 0 | ogs_error("OpenAPI_sepp_info_parseFromJSON() failed [sepp_ports]"); |
215 | 0 | goto end; |
216 | 0 | } |
217 | 109 | OpenAPI_list_add(sepp_portsList, localMapKeyPair); |
218 | 109 | } |
219 | 12 | } |
220 | 12 | } |
221 | | |
222 | 51 | remote_plmn_list = cJSON_GetObjectItemCaseSensitive(sepp_infoJSON, "remotePlmnList"); |
223 | 51 | if (remote_plmn_list) { |
224 | 21 | cJSON *remote_plmn_list_local = NULL; |
225 | 21 | if (!cJSON_IsArray(remote_plmn_list)) { |
226 | 1 | ogs_error("OpenAPI_sepp_info_parseFromJSON() failed [remote_plmn_list]"); |
227 | 1 | goto end; |
228 | 1 | } |
229 | | |
230 | 20 | remote_plmn_listList = OpenAPI_list_create(); |
231 | | |
232 | 166 | cJSON_ArrayForEach(remote_plmn_list_local, remote_plmn_list) { |
233 | 166 | if (!cJSON_IsObject(remote_plmn_list_local)) { |
234 | 1 | ogs_error("OpenAPI_sepp_info_parseFromJSON() failed [remote_plmn_list]"); |
235 | 1 | goto end; |
236 | 1 | } |
237 | 165 | OpenAPI_plmn_id_t *remote_plmn_listItem = OpenAPI_plmn_id_parseFromJSON(remote_plmn_list_local); |
238 | 165 | if (!remote_plmn_listItem) { |
239 | 9 | ogs_error("No remote_plmn_listItem"); |
240 | 9 | goto end; |
241 | 9 | } |
242 | 156 | OpenAPI_list_add(remote_plmn_listList, remote_plmn_listItem); |
243 | 156 | } |
244 | 20 | } |
245 | | |
246 | 40 | remote_snpn_list = cJSON_GetObjectItemCaseSensitive(sepp_infoJSON, "remoteSnpnList"); |
247 | 40 | if (remote_snpn_list) { |
248 | 22 | cJSON *remote_snpn_list_local = NULL; |
249 | 22 | if (!cJSON_IsArray(remote_snpn_list)) { |
250 | 1 | ogs_error("OpenAPI_sepp_info_parseFromJSON() failed [remote_snpn_list]"); |
251 | 1 | goto end; |
252 | 1 | } |
253 | | |
254 | 21 | remote_snpn_listList = OpenAPI_list_create(); |
255 | | |
256 | 184 | cJSON_ArrayForEach(remote_snpn_list_local, remote_snpn_list) { |
257 | 184 | if (!cJSON_IsObject(remote_snpn_list_local)) { |
258 | 2 | ogs_error("OpenAPI_sepp_info_parseFromJSON() failed [remote_snpn_list]"); |
259 | 2 | goto end; |
260 | 2 | } |
261 | 182 | OpenAPI_plmn_id_nid_t *remote_snpn_listItem = OpenAPI_plmn_id_nid_parseFromJSON(remote_snpn_list_local); |
262 | 182 | if (!remote_snpn_listItem) { |
263 | 10 | ogs_error("No remote_snpn_listItem"); |
264 | 10 | goto end; |
265 | 10 | } |
266 | 172 | OpenAPI_list_add(remote_snpn_listList, remote_snpn_listItem); |
267 | 172 | } |
268 | 21 | } |
269 | | |
270 | 27 | n32_purposes = cJSON_GetObjectItemCaseSensitive(sepp_infoJSON, "n32Purposes"); |
271 | 27 | if (n32_purposes) { |
272 | 0 | cJSON *n32_purposes_local = NULL; |
273 | 0 | if (!cJSON_IsArray(n32_purposes)) { |
274 | 0 | ogs_error("OpenAPI_sepp_info_parseFromJSON() failed [n32_purposes]"); |
275 | 0 | goto end; |
276 | 0 | } |
277 | | |
278 | 0 | n32_purposesList = OpenAPI_list_create(); |
279 | |
|
280 | 0 | cJSON_ArrayForEach(n32_purposes_local, n32_purposes) { |
281 | 0 | OpenAPI_n32_purpose_e localEnum = OpenAPI_n32_purpose_NULL; |
282 | 0 | if (!cJSON_IsString(n32_purposes_local)) { |
283 | 0 | ogs_error("OpenAPI_sepp_info_parseFromJSON() failed [n32_purposes]"); |
284 | 0 | goto end; |
285 | 0 | } |
286 | 0 | localEnum = OpenAPI_n32_purpose_FromString(n32_purposes_local->valuestring); |
287 | 0 | if (!localEnum) { |
288 | 0 | ogs_info("Enum value \"%s\" for field \"n32_purposes\" is not supported. Ignoring it ...", |
289 | 0 | n32_purposes_local->valuestring); |
290 | 0 | } else { |
291 | 0 | OpenAPI_list_add(n32_purposesList, (void *)localEnum); |
292 | 0 | } |
293 | 0 | } |
294 | 0 | if (n32_purposesList->count == 0) { |
295 | 0 | ogs_error("OpenAPI_sepp_info_parseFromJSON() failed: Expected n32_purposesList to not be empty (after ignoring unsupported enum values)."); |
296 | 0 | goto end; |
297 | 0 | } |
298 | 0 | } |
299 | | |
300 | 27 | sepp_info_local_var = OpenAPI_sepp_info_create ( |
301 | 27 | sepp_prefix && !cJSON_IsNull(sepp_prefix) ? ogs_strdup(sepp_prefix->valuestring) : NULL, |
302 | 27 | sepp_ports ? sepp_portsList : NULL, |
303 | 27 | remote_plmn_list ? remote_plmn_listList : NULL, |
304 | 27 | remote_snpn_list ? remote_snpn_listList : NULL, |
305 | 27 | n32_purposes ? n32_purposesList : NULL |
306 | 27 | ); |
307 | | |
308 | 27 | return sepp_info_local_var; |
309 | 32 | end: |
310 | 32 | if (sepp_portsList) { |
311 | 99 | OpenAPI_list_for_each(sepp_portsList, node) { |
312 | 99 | OpenAPI_map_t *localKeyValue = (OpenAPI_map_t*)node->data; |
313 | 99 | ogs_free(localKeyValue->key); |
314 | 99 | ogs_free(localKeyValue->value); |
315 | 99 | OpenAPI_map_free(localKeyValue); |
316 | 99 | } |
317 | 7 | OpenAPI_list_free(sepp_portsList); |
318 | 7 | sepp_portsList = NULL; |
319 | 7 | } |
320 | 32 | if (remote_plmn_listList) { |
321 | 74 | OpenAPI_list_for_each(remote_plmn_listList, node) { |
322 | 74 | OpenAPI_plmn_id_free(node->data); |
323 | 74 | } |
324 | 10 | OpenAPI_list_free(remote_plmn_listList); |
325 | 10 | remote_plmn_listList = NULL; |
326 | 10 | } |
327 | 32 | if (remote_snpn_listList) { |
328 | 98 | OpenAPI_list_for_each(remote_snpn_listList, node) { |
329 | 98 | OpenAPI_plmn_id_nid_free(node->data); |
330 | 98 | } |
331 | 12 | OpenAPI_list_free(remote_snpn_listList); |
332 | 12 | remote_snpn_listList = NULL; |
333 | 12 | } |
334 | 32 | if (n32_purposesList) { |
335 | 0 | OpenAPI_list_free(n32_purposesList); |
336 | 0 | n32_purposesList = NULL; |
337 | 0 | } |
338 | 32 | return NULL; |
339 | 27 | } |
340 | | |
341 | | OpenAPI_sepp_info_t *OpenAPI_sepp_info_copy(OpenAPI_sepp_info_t *dst, OpenAPI_sepp_info_t *src) |
342 | 0 | { |
343 | 0 | cJSON *item = NULL; |
344 | 0 | char *content = NULL; |
345 | |
|
346 | 0 | ogs_assert(src); |
347 | 0 | item = OpenAPI_sepp_info_convertToJSON(src); |
348 | 0 | if (!item) { |
349 | 0 | ogs_error("OpenAPI_sepp_info_convertToJSON() failed"); |
350 | 0 | return NULL; |
351 | 0 | } |
352 | | |
353 | 0 | content = cJSON_Print(item); |
354 | 0 | cJSON_Delete(item); |
355 | |
|
356 | 0 | if (!content) { |
357 | 0 | ogs_error("cJSON_Print() failed"); |
358 | 0 | return NULL; |
359 | 0 | } |
360 | | |
361 | 0 | item = cJSON_Parse(content); |
362 | 0 | ogs_free(content); |
363 | 0 | if (!item) { |
364 | 0 | ogs_error("cJSON_Parse() failed"); |
365 | 0 | return NULL; |
366 | 0 | } |
367 | | |
368 | 0 | OpenAPI_sepp_info_free(dst); |
369 | 0 | dst = OpenAPI_sepp_info_parseFromJSON(item); |
370 | 0 | cJSON_Delete(item); |
371 | |
|
372 | 0 | return dst; |
373 | 0 | } |
374 | | |