Cell down alarm notification [Issue-Id: ODUHIGH-430]
[o-du/l2.git] / src / o1 / ves / JsonHelper.cpp
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2020-2021] [HCL Technologies Ltd.]                          #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 /* This file contains functions to support Json related operation (read/write)*/
20
21 #include <bits/stdc++.h>
22 #include "JsonHelper.hpp"
23 #include "VesUtils.hpp"
24
25 using namespace std;
26
27 /*******************************************************************
28  *
29  * @brief wraps cJSON_CreateObject cJSON library function
30  *
31  * @details
32  *
33  *    Function : createNode
34  *
35  *    Functionality:
36  *      - wraps cJSON_CreateObject cJSON library function
37  *
38  * @params[in] void
39  * @return pointer to cJSON object     - success
40  *         NULL                      - failure
41  *
42  * ****************************************************************/
43
44 cJSON * JsonHelper::createNode()
45 {
46    return  cJSON_CreateObject();
47 }
48
49 /*******************************************************************
50  *
51  * @brief wraps cJSON_AddStringToObject cJSON library function
52  *
53  * @details
54  *
55  *    Function : addNodeToObject
56  *
57  *    Functionality:
58  *      - wraps cJSON_AddStringToObject cJSON library function
59  *
60  * @params[in] cJSON * parent, const char * nodeName, const char* value
61  * @return pointer to cJSON object     - success
62  *         NULL                        - failure
63  *
64  * ****************************************************************/
65
66 cJSON* JsonHelper::addNodeToObject(cJSON * parent, \
67                                           const char * nodeName, \
68                                           const char* value)
69 {
70    return cJSON_AddStringToObject(parent, nodeName, value);
71 }
72
73 /*******************************************************************
74  *
75  * @brief wraps cJSON_AddNumberToObject cJSON library function
76  *
77  * @details
78  *
79  *    Function : addNodeToObject
80  *
81  *    Functionality:
82  *      - wraps cJSON_AddNumberToObject cJSON library function
83  *
84  * @params[in] cJSON * parent, const char * nodeName, double value
85  * @return pointer to cJSON object     - success
86  *         NULL                        - failure
87  *
88  * ****************************************************************/
89
90 cJSON* JsonHelper::addNodeToObject(cJSON * parent, \
91                                           const char * nodeName, double value)
92 {
93    return cJSON_AddNumberToObject(parent, nodeName, (double) value);
94 }
95
96
97 /*******************************************************************
98  *
99  * @brief wraps cJSON_AddNumberToObject cJSON library function
100  *
101  * @details
102  *
103  *    Function : addNodeToObject
104  *
105  *    Functionality:
106  *      - wraps cJSON_AddNumberToObject cJSON library function
107  *
108  * @params[in] cJSON * parent, const char * nodeName, bool value
109  * @return pointer to cJSON object     - success
110  *         NULL                        - failure
111  *
112  * ****************************************************************/
113
114 cJSON* JsonHelper::addNodeToObject(cJSON * parent, \
115                                           const char * nodeName, bool value)
116 {
117    return cJSON_AddBoolToObject(parent, nodeName, (bool) value);
118 }
119
120 /*******************************************************************
121  *
122  * @brief wraps cJSON_AddItemToObject cJSON library function
123  *
124  * @details
125  *
126  *    Function : addJsonNodeToObject
127  *
128  *    Functionality:
129  *      - wraps cJSON_AddItemToObject cJSON library function
130  *
131  * @params[in] cJSON * parent, const char * nodeName, cJSON * node
132  * @return cJSON_bool non zero  - success
133  *         cJSON_bool 0         - failure
134  *
135  * ****************************************************************/
136
137 cJSON_bool JsonHelper::addJsonNodeToObject(cJSON * parent, \
138                                    const char * nodeName, cJSON * node)
139 {
140    return cJSON_AddItemToObject(parent, nodeName, node);
141 }
142
143
144 /*******************************************************************
145  *
146  * @brief wraps cJSON_Delete cJSON library function
147  *
148  * @details
149  *
150  *    Function : deleteNode
151  *
152  *    Functionality:
153  *      - wrapper of cJSON_AddNumberToObject
154  *
155  * @params[in] cJSON * node
156  * @return void
157  *
158  * ****************************************************************/
159
160
161 void JsonHelper::deleteNode(cJSON * node)
162 {
163    cJSON_Delete(node);
164 }
165
166 /*******************************************************************
167  *
168  * @brief wraps cJSON_PrintUnformatted cJSON library function
169  *
170  * @details
171  *
172  *    Function : addNodeToObject
173  *
174  *    Functionality:
175  *      - wraps cJSON_PrintUnformatted cJSON library function
176  *
177  * @params[in] cJSON * node
178  * @return char pointer to string  - success
179  *         nullptr                 - failure
180  *
181  * ****************************************************************/
182
183 char *JsonHelper::printUnformatted(cJSON * node)
184 {
185    return cJSON_PrintUnformatted(node);
186 }
187
188 /*******************************************************************
189  *
190  * @brief wraps cJSON_PrintUnformatted cJSON library function
191  *
192  * @details
193  *
194  *    Function : addNodeToObject
195  *
196  *    Functionality:
197  *      - wraps cJSON_PrintUnformatted cJSON library function
198  *
199  * @params[in] cJSON * node
200  * @return char pointer to json message  - success
201  *         nullptr                       - failure
202  *
203  * ****************************************************************/
204
205 char *JsonHelper::print(cJSON * node)
206 {
207    return cJSON_Print(node);
208 }
209
210 /*******************************************************************
211  *
212  * @brief wraps cJSON_PrintUnformatted cJSON library function
213  *
214  * @details
215  *
216  *    Function : addNodeToObject
217  *
218  *    Functionality:
219  *      - wraps cJSON_PrintUnformatted cJSON library function
220  *
221  * @params[in] void
222  * @return char pointer to string  - success
223  *         nullptr                 - failure
224  *
225  * ****************************************************************/
226
227 const char *JsonHelper::getError()
228 {
229    return cJSON_GetErrorPtr();
230 }
231
232
233 /*******************************************************************
234  *
235  * @brief get value of the provided node
236  *
237  * @details
238  *
239  *    Function : getValue
240  *
241  *    Functionality:
242  *      - fetch the value of the provided node
243  *
244  * @params[in] void
245  * @return pointer to string value         - success
246  *         NULL                            - failure
247  *
248  * ****************************************************************/
249
250 string JsonHelper::getValue(cJSON *json, const char *node)
251 {
252    cJSON *object;
253    string value = "";
254    object = cJSON_GetObjectItem(json, node);
255    if(object)
256    {
257       value = object->valuestring;
258       O1_LOG("O1 VES : [ %s] : [%s]\n",node, value.c_str() );
259    }
260    else
261       O1_LOG("O1 VES : node [ %s] not found\n",node);
262    return value;
263 }
264
265 /*******************************************************************
266  *
267  * @brief wraps cJSON_GetObjectItem cJSON library function
268  *
269  * @details
270  *
271  *    Function : addNodeToObject
272  *
273  *    Functionality:
274  *      - wraps cJSON_GetObjectItem cJSON library function
275  *
276  * @params[in] cJSON *json, const char *node
277  * @return pointer to cJSON node   - success
278  *         nullptr                 - failure
279  *
280  * ****************************************************************/
281
282 cJSON * JsonHelper::getNode(cJSON *json, const char *node)
283 {
284    return cJSON_GetObjectItem(json, node);
285 }
286
287
288 /*******************************************************************
289  *
290  * @brief reads json file
291  *
292  * @details
293  *
294  *    Function : read
295  *
296  *    Functionality:
297  *      - opens and reads json file and returns point to that file.
298  *
299  * @params[in] const char * fileName
300  * @return point to json file root node    - success
301  *         NULL                            - failure
302  *
303  * ****************************************************************/
304
305 cJSON* JsonHelper::read(const char * fileName)
306 {
307    std::fstream fs(fileName, std::ios::in | std::ios::binary);
308
309    if (!fs) {
310       O1_LOG("\nO1 JsonHelper : Cannot open file %s", fileName);
311       return NULL;
312    }
313
314    std::stringstream iss;
315
316    iss << fs.rdbuf();
317
318    cJSON *json = cJSON_Parse(iss.str().c_str());
319    return json;
320 }
321
322 cJSON* JsonHelper::createArray() 
323 {
324    return cJSON_CreateArray();
325 }
326
327 cJSON_bool JsonHelper::addJsonNodeToArray(cJSON * array, cJSON* node)
328 {
329    return cJSON_AddItemToArray(array, node);
330 }
331
332
333
334 /**********************************************************************
335   End of file
336  **********************************************************************/