/src/open5gs/lib/sbi/openapi/model/ncgi.c
Line | Count | Source |
1 | | |
2 | | #include <stdlib.h> |
3 | | #include <string.h> |
4 | | #include <stdio.h> |
5 | | #include "ncgi.h" |
6 | | |
7 | | OpenAPI_ncgi_t *OpenAPI_ncgi_create( |
8 | | OpenAPI_plmn_id_t *plmn_id, |
9 | | char *nr_cell_id, |
10 | | char *nid |
11 | | ) |
12 | 0 | { |
13 | 0 | OpenAPI_ncgi_t *ncgi_local_var = ogs_malloc(sizeof(OpenAPI_ncgi_t)); |
14 | 0 | ogs_assert(ncgi_local_var); |
15 | | |
16 | 0 | ncgi_local_var->plmn_id = plmn_id; |
17 | 0 | ncgi_local_var->nr_cell_id = nr_cell_id; |
18 | 0 | ncgi_local_var->nid = nid; |
19 | |
|
20 | 0 | return ncgi_local_var; |
21 | 0 | } |
22 | | |
23 | | void OpenAPI_ncgi_free(OpenAPI_ncgi_t *ncgi) |
24 | 0 | { |
25 | 0 | OpenAPI_lnode_t *node = NULL; |
26 | |
|
27 | 0 | if (NULL == ncgi) { |
28 | 0 | return; |
29 | 0 | } |
30 | 0 | if (ncgi->plmn_id) { |
31 | 0 | OpenAPI_plmn_id_free(ncgi->plmn_id); |
32 | 0 | ncgi->plmn_id = NULL; |
33 | 0 | } |
34 | 0 | if (ncgi->nr_cell_id) { |
35 | 0 | ogs_free(ncgi->nr_cell_id); |
36 | 0 | ncgi->nr_cell_id = NULL; |
37 | 0 | } |
38 | 0 | if (ncgi->nid) { |
39 | 0 | ogs_free(ncgi->nid); |
40 | 0 | ncgi->nid = NULL; |
41 | 0 | } |
42 | 0 | ogs_free(ncgi); |
43 | 0 | } |
44 | | |
45 | | cJSON *OpenAPI_ncgi_convertToJSON(OpenAPI_ncgi_t *ncgi) |
46 | 0 | { |
47 | 0 | cJSON *item = NULL; |
48 | 0 | OpenAPI_lnode_t *node = NULL; |
49 | |
|
50 | 0 | if (ncgi == NULL) { |
51 | 0 | ogs_error("OpenAPI_ncgi_convertToJSON() failed [Ncgi]"); |
52 | 0 | return NULL; |
53 | 0 | } |
54 | | |
55 | 0 | item = cJSON_CreateObject(); |
56 | 0 | if (!ncgi->plmn_id) { |
57 | 0 | ogs_error("OpenAPI_ncgi_convertToJSON() failed [plmn_id]"); |
58 | 0 | return NULL; |
59 | 0 | } |
60 | 0 | cJSON *plmn_id_local_JSON = OpenAPI_plmn_id_convertToJSON(ncgi->plmn_id); |
61 | 0 | if (plmn_id_local_JSON == NULL) { |
62 | 0 | ogs_error("OpenAPI_ncgi_convertToJSON() failed [plmn_id]"); |
63 | 0 | goto end; |
64 | 0 | } |
65 | 0 | cJSON_AddItemToObject(item, "plmnId", plmn_id_local_JSON); |
66 | 0 | if (item->child == NULL) { |
67 | 0 | ogs_error("OpenAPI_ncgi_convertToJSON() failed [plmn_id]"); |
68 | 0 | goto end; |
69 | 0 | } |
70 | | |
71 | 0 | if (!ncgi->nr_cell_id) { |
72 | 0 | ogs_error("OpenAPI_ncgi_convertToJSON() failed [nr_cell_id]"); |
73 | 0 | return NULL; |
74 | 0 | } |
75 | 0 | if (cJSON_AddStringToObject(item, "nrCellId", ncgi->nr_cell_id) == NULL) { |
76 | 0 | ogs_error("OpenAPI_ncgi_convertToJSON() failed [nr_cell_id]"); |
77 | 0 | goto end; |
78 | 0 | } |
79 | | |
80 | 0 | if (ncgi->nid) { |
81 | 0 | if (cJSON_AddStringToObject(item, "nid", ncgi->nid) == NULL) { |
82 | 0 | ogs_error("OpenAPI_ncgi_convertToJSON() failed [nid]"); |
83 | 0 | goto end; |
84 | 0 | } |
85 | 0 | } |
86 | | |
87 | 0 | end: |
88 | 0 | return item; |
89 | 0 | } |
90 | | |
91 | | OpenAPI_ncgi_t *OpenAPI_ncgi_parseFromJSON(cJSON *ncgiJSON) |
92 | 0 | { |
93 | 0 | OpenAPI_ncgi_t *ncgi_local_var = NULL; |
94 | 0 | OpenAPI_lnode_t *node = NULL; |
95 | 0 | cJSON *plmn_id = NULL; |
96 | 0 | OpenAPI_plmn_id_t *plmn_id_local_nonprim = NULL; |
97 | 0 | cJSON *nr_cell_id = NULL; |
98 | 0 | cJSON *nid = NULL; |
99 | 0 | plmn_id = cJSON_GetObjectItemCaseSensitive(ncgiJSON, "plmnId"); |
100 | 0 | if (!plmn_id) { |
101 | 0 | ogs_error("OpenAPI_ncgi_parseFromJSON() failed [plmn_id]"); |
102 | 0 | goto end; |
103 | 0 | } |
104 | 0 | plmn_id_local_nonprim = OpenAPI_plmn_id_parseFromJSON(plmn_id); |
105 | 0 | if (!plmn_id_local_nonprim) { |
106 | 0 | ogs_error("OpenAPI_plmn_id_parseFromJSON failed [plmn_id]"); |
107 | 0 | goto end; |
108 | 0 | } |
109 | | |
110 | 0 | nr_cell_id = cJSON_GetObjectItemCaseSensitive(ncgiJSON, "nrCellId"); |
111 | 0 | if (!nr_cell_id) { |
112 | 0 | ogs_error("OpenAPI_ncgi_parseFromJSON() failed [nr_cell_id]"); |
113 | 0 | goto end; |
114 | 0 | } |
115 | 0 | if (!cJSON_IsString(nr_cell_id)) { |
116 | 0 | ogs_error("OpenAPI_ncgi_parseFromJSON() failed [nr_cell_id]"); |
117 | 0 | goto end; |
118 | 0 | } |
119 | | |
120 | 0 | nid = cJSON_GetObjectItemCaseSensitive(ncgiJSON, "nid"); |
121 | 0 | if (nid) { |
122 | 0 | if (!cJSON_IsString(nid) && !cJSON_IsNull(nid)) { |
123 | 0 | ogs_error("OpenAPI_ncgi_parseFromJSON() failed [nid]"); |
124 | 0 | goto end; |
125 | 0 | } |
126 | 0 | } |
127 | | |
128 | 0 | ncgi_local_var = OpenAPI_ncgi_create ( |
129 | 0 | plmn_id_local_nonprim, |
130 | 0 | ogs_strdup(nr_cell_id->valuestring), |
131 | 0 | nid && !cJSON_IsNull(nid) ? ogs_strdup(nid->valuestring) : NULL |
132 | 0 | ); |
133 | |
|
134 | 0 | return ncgi_local_var; |
135 | 0 | end: |
136 | 0 | if (plmn_id_local_nonprim) { |
137 | 0 | OpenAPI_plmn_id_free(plmn_id_local_nonprim); |
138 | 0 | plmn_id_local_nonprim = NULL; |
139 | 0 | } |
140 | 0 | return NULL; |
141 | 0 | } |
142 | | |
143 | | OpenAPI_ncgi_t *OpenAPI_ncgi_copy(OpenAPI_ncgi_t *dst, OpenAPI_ncgi_t *src) |
144 | 0 | { |
145 | 0 | cJSON *item = NULL; |
146 | 0 | char *content = NULL; |
147 | |
|
148 | 0 | ogs_assert(src); |
149 | 0 | item = OpenAPI_ncgi_convertToJSON(src); |
150 | 0 | if (!item) { |
151 | 0 | ogs_error("OpenAPI_ncgi_convertToJSON() failed"); |
152 | 0 | return NULL; |
153 | 0 | } |
154 | | |
155 | 0 | content = cJSON_Print(item); |
156 | 0 | cJSON_Delete(item); |
157 | |
|
158 | 0 | if (!content) { |
159 | 0 | ogs_error("cJSON_Print() failed"); |
160 | 0 | return NULL; |
161 | 0 | } |
162 | | |
163 | 0 | item = cJSON_Parse(content); |
164 | 0 | ogs_free(content); |
165 | 0 | if (!item) { |
166 | 0 | ogs_error("cJSON_Parse() failed"); |
167 | 0 | return NULL; |
168 | 0 | } |
169 | | |
170 | 0 | OpenAPI_ncgi_free(dst); |
171 | 0 | dst = OpenAPI_ncgi_parseFromJSON(item); |
172 | 0 | cJSON_Delete(item); |
173 | |
|
174 | 0 | return dst; |
175 | 0 | } |
176 | | |