/src/open5gs/lib/sbi/openapi/model/upu_data.c
Line | Count | Source |
1 | | |
2 | | #include <stdlib.h> |
3 | | #include <string.h> |
4 | | #include <stdio.h> |
5 | | #include "upu_data.h" |
6 | | |
7 | | OpenAPI_upu_data_t *OpenAPI_upu_data_create( |
8 | | char *sec_packet, |
9 | | OpenAPI_list_t *default_conf_nssai, |
10 | | char *routing_id, |
11 | | bool is_drei, |
12 | | int drei, |
13 | | bool is_drei_eps, |
14 | | int drei_eps, |
15 | | bool is_aol, |
16 | | int aol, |
17 | | OpenAPI_list_t *disaster_cond_plmn_ids |
18 | | ) |
19 | 0 | { |
20 | 0 | OpenAPI_upu_data_t *upu_data_local_var = ogs_malloc(sizeof(OpenAPI_upu_data_t)); |
21 | 0 | ogs_assert(upu_data_local_var); |
22 | | |
23 | 0 | upu_data_local_var->sec_packet = sec_packet; |
24 | 0 | upu_data_local_var->default_conf_nssai = default_conf_nssai; |
25 | 0 | upu_data_local_var->routing_id = routing_id; |
26 | 0 | upu_data_local_var->is_drei = is_drei; |
27 | 0 | upu_data_local_var->drei = drei; |
28 | 0 | upu_data_local_var->is_drei_eps = is_drei_eps; |
29 | 0 | upu_data_local_var->drei_eps = drei_eps; |
30 | 0 | upu_data_local_var->is_aol = is_aol; |
31 | 0 | upu_data_local_var->aol = aol; |
32 | 0 | upu_data_local_var->disaster_cond_plmn_ids = disaster_cond_plmn_ids; |
33 | |
|
34 | 0 | return upu_data_local_var; |
35 | 0 | } |
36 | | |
37 | | void OpenAPI_upu_data_free(OpenAPI_upu_data_t *upu_data) |
38 | 0 | { |
39 | 0 | OpenAPI_lnode_t *node = NULL; |
40 | |
|
41 | 0 | if (NULL == upu_data) { |
42 | 0 | return; |
43 | 0 | } |
44 | 0 | if (upu_data->sec_packet) { |
45 | 0 | ogs_free(upu_data->sec_packet); |
46 | 0 | upu_data->sec_packet = NULL; |
47 | 0 | } |
48 | 0 | if (upu_data->default_conf_nssai) { |
49 | 0 | OpenAPI_list_for_each(upu_data->default_conf_nssai, node) { |
50 | 0 | OpenAPI_snssai_free(node->data); |
51 | 0 | } |
52 | 0 | OpenAPI_list_free(upu_data->default_conf_nssai); |
53 | 0 | upu_data->default_conf_nssai = NULL; |
54 | 0 | } |
55 | 0 | if (upu_data->routing_id) { |
56 | 0 | ogs_free(upu_data->routing_id); |
57 | 0 | upu_data->routing_id = NULL; |
58 | 0 | } |
59 | 0 | if (upu_data->disaster_cond_plmn_ids) { |
60 | 0 | OpenAPI_list_for_each(upu_data->disaster_cond_plmn_ids, node) { |
61 | 0 | OpenAPI_plmn_id_free(node->data); |
62 | 0 | } |
63 | 0 | OpenAPI_list_free(upu_data->disaster_cond_plmn_ids); |
64 | 0 | upu_data->disaster_cond_plmn_ids = NULL; |
65 | 0 | } |
66 | 0 | ogs_free(upu_data); |
67 | 0 | } |
68 | | |
69 | | cJSON *OpenAPI_upu_data_convertToJSON(OpenAPI_upu_data_t *upu_data) |
70 | 0 | { |
71 | 0 | cJSON *item = NULL; |
72 | 0 | OpenAPI_lnode_t *node = NULL; |
73 | |
|
74 | 0 | if (upu_data == NULL) { |
75 | 0 | ogs_error("OpenAPI_upu_data_convertToJSON() failed [UpuData]"); |
76 | 0 | return NULL; |
77 | 0 | } |
78 | | |
79 | 0 | item = cJSON_CreateObject(); |
80 | 0 | if (upu_data->sec_packet) { |
81 | 0 | if (cJSON_AddStringToObject(item, "secPacket", upu_data->sec_packet) == NULL) { |
82 | 0 | ogs_error("OpenAPI_upu_data_convertToJSON() failed [sec_packet]"); |
83 | 0 | goto end; |
84 | 0 | } |
85 | 0 | } |
86 | | |
87 | 0 | if (upu_data->default_conf_nssai) { |
88 | 0 | cJSON *default_conf_nssaiList = cJSON_AddArrayToObject(item, "defaultConfNssai"); |
89 | 0 | if (default_conf_nssaiList == NULL) { |
90 | 0 | ogs_error("OpenAPI_upu_data_convertToJSON() failed [default_conf_nssai]"); |
91 | 0 | goto end; |
92 | 0 | } |
93 | 0 | OpenAPI_list_for_each(upu_data->default_conf_nssai, node) { |
94 | 0 | cJSON *itemLocal = OpenAPI_snssai_convertToJSON(node->data); |
95 | 0 | if (itemLocal == NULL) { |
96 | 0 | ogs_error("OpenAPI_upu_data_convertToJSON() failed [default_conf_nssai]"); |
97 | 0 | goto end; |
98 | 0 | } |
99 | 0 | cJSON_AddItemToArray(default_conf_nssaiList, itemLocal); |
100 | 0 | } |
101 | 0 | } |
102 | | |
103 | 0 | if (upu_data->routing_id) { |
104 | 0 | if (cJSON_AddStringToObject(item, "routingId", upu_data->routing_id) == NULL) { |
105 | 0 | ogs_error("OpenAPI_upu_data_convertToJSON() failed [routing_id]"); |
106 | 0 | goto end; |
107 | 0 | } |
108 | 0 | } |
109 | | |
110 | 0 | if (upu_data->is_drei) { |
111 | 0 | if (cJSON_AddBoolToObject(item, "drei", upu_data->drei) == NULL) { |
112 | 0 | ogs_error("OpenAPI_upu_data_convertToJSON() failed [drei]"); |
113 | 0 | goto end; |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | 0 | if (upu_data->is_drei_eps) { |
118 | 0 | if (cJSON_AddBoolToObject(item, "dreiEps", upu_data->drei_eps) == NULL) { |
119 | 0 | ogs_error("OpenAPI_upu_data_convertToJSON() failed [drei_eps]"); |
120 | 0 | goto end; |
121 | 0 | } |
122 | 0 | } |
123 | | |
124 | 0 | if (upu_data->is_aol) { |
125 | 0 | if (cJSON_AddBoolToObject(item, "aol", upu_data->aol) == NULL) { |
126 | 0 | ogs_error("OpenAPI_upu_data_convertToJSON() failed [aol]"); |
127 | 0 | goto end; |
128 | 0 | } |
129 | 0 | } |
130 | | |
131 | 0 | if (upu_data->disaster_cond_plmn_ids) { |
132 | 0 | cJSON *disaster_cond_plmn_idsList = cJSON_AddArrayToObject(item, "disasterCondPlmnIds"); |
133 | 0 | if (disaster_cond_plmn_idsList == NULL) { |
134 | 0 | ogs_error("OpenAPI_upu_data_convertToJSON() failed [disaster_cond_plmn_ids]"); |
135 | 0 | goto end; |
136 | 0 | } |
137 | 0 | OpenAPI_list_for_each(upu_data->disaster_cond_plmn_ids, node) { |
138 | 0 | cJSON *itemLocal = OpenAPI_plmn_id_convertToJSON(node->data); |
139 | 0 | if (itemLocal == NULL) { |
140 | 0 | ogs_error("OpenAPI_upu_data_convertToJSON() failed [disaster_cond_plmn_ids]"); |
141 | 0 | goto end; |
142 | 0 | } |
143 | 0 | cJSON_AddItemToArray(disaster_cond_plmn_idsList, itemLocal); |
144 | 0 | } |
145 | 0 | } |
146 | | |
147 | 0 | end: |
148 | 0 | return item; |
149 | 0 | } |
150 | | |
151 | | OpenAPI_upu_data_t *OpenAPI_upu_data_parseFromJSON(cJSON *upu_dataJSON) |
152 | 0 | { |
153 | 0 | OpenAPI_upu_data_t *upu_data_local_var = NULL; |
154 | 0 | OpenAPI_lnode_t *node = NULL; |
155 | 0 | cJSON *sec_packet = NULL; |
156 | 0 | cJSON *default_conf_nssai = NULL; |
157 | 0 | OpenAPI_list_t *default_conf_nssaiList = NULL; |
158 | 0 | cJSON *routing_id = NULL; |
159 | 0 | cJSON *drei = NULL; |
160 | 0 | cJSON *drei_eps = NULL; |
161 | 0 | cJSON *aol = NULL; |
162 | 0 | cJSON *disaster_cond_plmn_ids = NULL; |
163 | 0 | OpenAPI_list_t *disaster_cond_plmn_idsList = NULL; |
164 | 0 | sec_packet = cJSON_GetObjectItemCaseSensitive(upu_dataJSON, "secPacket"); |
165 | 0 | if (sec_packet) { |
166 | 0 | if (!cJSON_IsString(sec_packet) && !cJSON_IsNull(sec_packet)) { |
167 | 0 | ogs_error("OpenAPI_upu_data_parseFromJSON() failed [sec_packet]"); |
168 | 0 | goto end; |
169 | 0 | } |
170 | 0 | } |
171 | | |
172 | 0 | default_conf_nssai = cJSON_GetObjectItemCaseSensitive(upu_dataJSON, "defaultConfNssai"); |
173 | 0 | if (default_conf_nssai) { |
174 | 0 | cJSON *default_conf_nssai_local = NULL; |
175 | 0 | if (!cJSON_IsArray(default_conf_nssai)) { |
176 | 0 | ogs_error("OpenAPI_upu_data_parseFromJSON() failed [default_conf_nssai]"); |
177 | 0 | goto end; |
178 | 0 | } |
179 | | |
180 | 0 | default_conf_nssaiList = OpenAPI_list_create(); |
181 | |
|
182 | 0 | cJSON_ArrayForEach(default_conf_nssai_local, default_conf_nssai) { |
183 | 0 | if (!cJSON_IsObject(default_conf_nssai_local)) { |
184 | 0 | ogs_error("OpenAPI_upu_data_parseFromJSON() failed [default_conf_nssai]"); |
185 | 0 | goto end; |
186 | 0 | } |
187 | 0 | OpenAPI_snssai_t *default_conf_nssaiItem = OpenAPI_snssai_parseFromJSON(default_conf_nssai_local); |
188 | 0 | if (!default_conf_nssaiItem) { |
189 | 0 | ogs_error("No default_conf_nssaiItem"); |
190 | 0 | goto end; |
191 | 0 | } |
192 | 0 | OpenAPI_list_add(default_conf_nssaiList, default_conf_nssaiItem); |
193 | 0 | } |
194 | 0 | } |
195 | | |
196 | 0 | routing_id = cJSON_GetObjectItemCaseSensitive(upu_dataJSON, "routingId"); |
197 | 0 | if (routing_id) { |
198 | 0 | if (!cJSON_IsString(routing_id) && !cJSON_IsNull(routing_id)) { |
199 | 0 | ogs_error("OpenAPI_upu_data_parseFromJSON() failed [routing_id]"); |
200 | 0 | goto end; |
201 | 0 | } |
202 | 0 | } |
203 | | |
204 | 0 | drei = cJSON_GetObjectItemCaseSensitive(upu_dataJSON, "drei"); |
205 | 0 | if (drei) { |
206 | 0 | if (!cJSON_IsBool(drei)) { |
207 | 0 | ogs_error("OpenAPI_upu_data_parseFromJSON() failed [drei]"); |
208 | 0 | goto end; |
209 | 0 | } |
210 | 0 | } |
211 | | |
212 | 0 | drei_eps = cJSON_GetObjectItemCaseSensitive(upu_dataJSON, "dreiEps"); |
213 | 0 | if (drei_eps) { |
214 | 0 | if (!cJSON_IsBool(drei_eps)) { |
215 | 0 | ogs_error("OpenAPI_upu_data_parseFromJSON() failed [drei_eps]"); |
216 | 0 | goto end; |
217 | 0 | } |
218 | 0 | } |
219 | | |
220 | 0 | aol = cJSON_GetObjectItemCaseSensitive(upu_dataJSON, "aol"); |
221 | 0 | if (aol) { |
222 | 0 | if (!cJSON_IsBool(aol)) { |
223 | 0 | ogs_error("OpenAPI_upu_data_parseFromJSON() failed [aol]"); |
224 | 0 | goto end; |
225 | 0 | } |
226 | 0 | } |
227 | | |
228 | 0 | disaster_cond_plmn_ids = cJSON_GetObjectItemCaseSensitive(upu_dataJSON, "disasterCondPlmnIds"); |
229 | 0 | if (disaster_cond_plmn_ids) { |
230 | 0 | cJSON *disaster_cond_plmn_ids_local = NULL; |
231 | 0 | if (!cJSON_IsArray(disaster_cond_plmn_ids)) { |
232 | 0 | ogs_error("OpenAPI_upu_data_parseFromJSON() failed [disaster_cond_plmn_ids]"); |
233 | 0 | goto end; |
234 | 0 | } |
235 | | |
236 | 0 | disaster_cond_plmn_idsList = OpenAPI_list_create(); |
237 | |
|
238 | 0 | cJSON_ArrayForEach(disaster_cond_plmn_ids_local, disaster_cond_plmn_ids) { |
239 | 0 | if (!cJSON_IsObject(disaster_cond_plmn_ids_local)) { |
240 | 0 | ogs_error("OpenAPI_upu_data_parseFromJSON() failed [disaster_cond_plmn_ids]"); |
241 | 0 | goto end; |
242 | 0 | } |
243 | 0 | OpenAPI_plmn_id_t *disaster_cond_plmn_idsItem = OpenAPI_plmn_id_parseFromJSON(disaster_cond_plmn_ids_local); |
244 | 0 | if (!disaster_cond_plmn_idsItem) { |
245 | 0 | ogs_error("No disaster_cond_plmn_idsItem"); |
246 | 0 | goto end; |
247 | 0 | } |
248 | 0 | OpenAPI_list_add(disaster_cond_plmn_idsList, disaster_cond_plmn_idsItem); |
249 | 0 | } |
250 | 0 | } |
251 | | |
252 | 0 | upu_data_local_var = OpenAPI_upu_data_create ( |
253 | 0 | sec_packet && !cJSON_IsNull(sec_packet) ? ogs_strdup(sec_packet->valuestring) : NULL, |
254 | 0 | default_conf_nssai ? default_conf_nssaiList : NULL, |
255 | 0 | routing_id && !cJSON_IsNull(routing_id) ? ogs_strdup(routing_id->valuestring) : NULL, |
256 | 0 | drei ? true : false, |
257 | 0 | drei ? drei->valueint : 0, |
258 | 0 | drei_eps ? true : false, |
259 | 0 | drei_eps ? drei_eps->valueint : 0, |
260 | 0 | aol ? true : false, |
261 | 0 | aol ? aol->valueint : 0, |
262 | 0 | disaster_cond_plmn_ids ? disaster_cond_plmn_idsList : NULL |
263 | 0 | ); |
264 | |
|
265 | 0 | return upu_data_local_var; |
266 | 0 | end: |
267 | 0 | if (default_conf_nssaiList) { |
268 | 0 | OpenAPI_list_for_each(default_conf_nssaiList, node) { |
269 | 0 | OpenAPI_snssai_free(node->data); |
270 | 0 | } |
271 | 0 | OpenAPI_list_free(default_conf_nssaiList); |
272 | 0 | default_conf_nssaiList = NULL; |
273 | 0 | } |
274 | 0 | if (disaster_cond_plmn_idsList) { |
275 | 0 | OpenAPI_list_for_each(disaster_cond_plmn_idsList, node) { |
276 | 0 | OpenAPI_plmn_id_free(node->data); |
277 | 0 | } |
278 | 0 | OpenAPI_list_free(disaster_cond_plmn_idsList); |
279 | 0 | disaster_cond_plmn_idsList = NULL; |
280 | 0 | } |
281 | 0 | return NULL; |
282 | 0 | } |
283 | | |
284 | | OpenAPI_upu_data_t *OpenAPI_upu_data_copy(OpenAPI_upu_data_t *dst, OpenAPI_upu_data_t *src) |
285 | 0 | { |
286 | 0 | cJSON *item = NULL; |
287 | 0 | char *content = NULL; |
288 | |
|
289 | 0 | ogs_assert(src); |
290 | 0 | item = OpenAPI_upu_data_convertToJSON(src); |
291 | 0 | if (!item) { |
292 | 0 | ogs_error("OpenAPI_upu_data_convertToJSON() failed"); |
293 | 0 | return NULL; |
294 | 0 | } |
295 | | |
296 | 0 | content = cJSON_Print(item); |
297 | 0 | cJSON_Delete(item); |
298 | |
|
299 | 0 | if (!content) { |
300 | 0 | ogs_error("cJSON_Print() failed"); |
301 | 0 | return NULL; |
302 | 0 | } |
303 | | |
304 | 0 | item = cJSON_Parse(content); |
305 | 0 | ogs_free(content); |
306 | 0 | if (!item) { |
307 | 0 | ogs_error("cJSON_Parse() failed"); |
308 | 0 | return NULL; |
309 | 0 | } |
310 | | |
311 | 0 | OpenAPI_upu_data_free(dst); |
312 | 0 | dst = OpenAPI_upu_data_parseFromJSON(item); |
313 | 0 | cJSON_Delete(item); |
314 | |
|
315 | 0 | return dst; |
316 | 0 | } |
317 | | |