Initial VES for DANOS vRouter
[demo.git] / vnfs / VESreporting_vFW5.0_DANOS / evel / evel-library / code / evel_library / evel_jsonobject.c
1 /*************************************************************************//**
2  *
3  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
4  *
5  * Unless otherwise specified, all software contained herein is
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
17  ****************************************************************************/
18
19 /**************************************************************************//**
20  * @file
21  * Implementation of EVEL functions relating to json_object.
22  *
23  ****************************************************************************/
24
25 #include <string.h>
26 #include <assert.h>
27 #include <stdlib.h>
28
29 #include "jsmn.h"
30 #include "evel.h"
31 #include "evel_internal.h"
32
33 /**************************************************************************//**
34  * Create a new json object.
35  *
36  * @note    The mandatory fields on the Other must be supplied to this factory
37  *          function and are immutable once set.  Optional fields have explicit
38  *          setter functions, but again values may only be set once so that the
39  *          Other has immutable properties.
40  * @param name       name of the object.
41  * @returns pointer to the newly manufactured ::EVEL_JSON_OBJECT.
42  *          not used (i.e. posted) it must be released using ::evel_free_jsonobject.
43  * @retval  NULL  Failed to create the json object.
44  *****************************************************************************/
45 EVEL_JSON_OBJECT * evel_new_jsonobject(const char *const name)
46 {
47   EVEL_JSON_OBJECT *jobj = NULL;
48   EVEL_ENTER();
49
50   /***************************************************************************/
51   /* Check preconditions.                                                    */
52   /***************************************************************************/
53   assert(name != NULL);
54
55   /***************************************************************************/
56   /* Allocate the json object.                                                     */
57   /***************************************************************************/
58   jobj = malloc(sizeof(EVEL_JSON_OBJECT));
59   if (jobj == NULL)
60   {
61     log_error_state("Out of memory");
62     goto exit_label;
63   }
64   memset(jobj, 0, sizeof(EVEL_JSON_OBJECT));
65   EVEL_DEBUG("New json object is at %lp", jobj);
66
67   /***************************************************************************/
68   /* Initialize the fields.  Optional string values are   */
69   /* uninitialized (NULL).                                                   */
70   /***************************************************************************/
71   jobj->object_name = strdup(name);
72   evel_init_option_string(&jobj->objectschema);
73   evel_init_option_string(&jobj->objectschemaurl);
74   evel_init_option_string(&jobj->nfsubscribedobjname);
75   evel_init_option_string(&jobj->nfsubscriptionid);
76   dlist_initialize(&jobj->jsonobjectinstances);
77
78 exit_label:
79   EVEL_EXIT();
80   return jobj;
81 }
82
83
84 /**************************************************************************//**
85  * Create a new json object instance.
86  *
87  * @note    The mandatory fields on the Other must be supplied to this factory
88  *          function and are immutable once set.  Optional fields have explicit
89  *          setter functions, but again values may only be set once so that the
90  *          Other has immutable properties.
91  * @param   yourjson       json string.
92  * @returns pointer to the newly manufactured ::EVEL_JSON_OBJECT_INSTANCE.
93  *          not used (i.e. posted) it must be released using ::evel_free_jsonobjectinstance.
94  * @retval  NULL  Failed to create the json object instance.
95  *****************************************************************************/
96 EVEL_JSON_OBJECT_INSTANCE * evel_new_jsonobjinstance(const char *const yourjson)
97 {
98   EVEL_JSON_OBJECT_INSTANCE *jobjinst = NULL;
99   jsmntok_t *key;
100   int resultCode;
101   jsmn_parser p;
102   jsmntok_t tokens[MAX_JSON_TOKENS]; // a number >= total number of tokens
103   int len=0;
104
105
106   EVEL_ENTER();
107
108   /***************************************************************************/
109   /* Check preconditions.                                                    */
110   /***************************************************************************/
111   assert(yourjson != NULL);
112   len = strlen(yourjson)+1;
113   assert(len > 0);
114
115   /***************************************************************************/
116   /*  Validate JSON for json object */
117   /***************************************************************************/
118   jsmn_init(&p);
119   resultCode = jsmn_parse(&p, yourjson, len, tokens, sizeof(tokens)/sizeof(tokens[0]));
120   if( resultCode < 0 ){
121     log_error_state("Failed to parse json for object");
122     goto exit_label;
123   }
124
125   if (resultCode < 1 || tokens[0].type != JSMN_OBJECT) {
126     log_error_state("Error json object expected");
127     goto exit_label;
128   }
129
130   /***************************************************************************/
131   /* Allocate the json object.                                                     */
132   /***************************************************************************/
133   jobjinst = malloc(sizeof(EVEL_JSON_OBJECT_INSTANCE));
134   if (jobjinst == NULL)
135   {
136     log_error_state("Out of memory");
137     goto exit_label;
138   }
139   memset(jobjinst, 0, sizeof(EVEL_JSON_OBJECT_INSTANCE));
140
141   /***************************************************************************/
142   /* Initialize the fields.  Optional key values are   */
143   /* uninitialized (NULL).                                                   */
144   /***************************************************************************/
145   jobjinst->jsonstring = strdup(yourjson);
146   dlist_initialize(&jobjinst->object_keys);
147
148 exit_label:
149   EVEL_EXIT();
150   return jobjinst;
151 }
152
153
154 /**************************************************************************//**
155  * Create a new internal key.
156  *
157  * @note    The mandatory fields on the Other must be supplied to this factory
158  *          function and are immutable once set.  Optional fields have explicit
159  *          setter functions, but again values may only be set once so that the
160  *          Other has immutable properties.
161  * @param   keyname       name of the key.
162  * @returns pointer to the newly manufactured ::EVEL_INTERNAL_KEY.
163  *          not used (i.e. posted) it must be released using ::evel_free_internal_key.
164  * @retval  NULL  Failed to create the internal key.
165  *****************************************************************************/
166 EVEL_INTERNAL_KEY * evel_new_internal_key(char *keyname)
167 {
168   EVEL_INTERNAL_KEY *keyinst = NULL;
169   EVEL_ENTER();
170
171   /***************************************************************************/
172   /* Check preconditions.                                                    */
173   /***************************************************************************/
174   assert(keyname != NULL);
175
176   /***************************************************************************/
177   /* Allocate the key object.                                                     */
178   /***************************************************************************/
179   keyinst = malloc(sizeof(EVEL_INTERNAL_KEY));
180   if (keyinst == NULL)
181   {
182     log_error_state("Out of memory");
183     goto exit_label;
184   }
185   memset(keyinst, 0, sizeof(EVEL_INTERNAL_KEY));
186   keyinst->keyname = strdup(keyname);
187
188   /***************************************************************************/
189   /* Optional string values are  uninitialized (NULL).  */
190   /***************************************************************************/
191   evel_init_option_int(&keyinst->keyorder);
192   evel_init_option_string(&keyinst->keyvalue);
193
194 exit_label:
195   EVEL_EXIT();
196   return keyinst;
197 }
198
199 /**************************************************************************//**
200  * Set the keyorder  of the internal key instance.
201  *
202  * @note  The property is treated as immutable: it is only valid to call
203  *        the setter once.  However, we don't assert if the caller tries to
204  *        overwrite, just ignoring the update instead.
205  *
206  * @param int keyorder
207  *****************************************************************************/
208 void evel_internal_key_keyorder_set(EVEL_INTERNAL_KEY * pinst, const int keyorder)
209 {
210   assert (pinst != NULL);
211   evel_set_option_int(&pinst->keyorder,keyorder,"Key order");
212 }
213
214 /**************************************************************************//**
215  * Set the keyvalue  of the internal key instance.
216  *
217  * @note  The property is treated as immutable: it is only valid to call
218  *        the setter once.  However, we don't assert if the caller tries to
219  *        overwrite, just ignoring the update instead.
220  *
221  * @param string keyvalue
222  *****************************************************************************/
223 void evel_internal_key_keyvalue_set(EVEL_INTERNAL_KEY * pinst, const char * const keyval)
224 {
225   assert (pinst != NULL);
226   evel_set_option_string(&pinst->keyvalue,keyval,"Key Value");
227 }
228
229 /**************************************************************************//**
230  * Set the string values of json object
231  *
232  * @note  The property is treated as immutable: it is only valid to call
233  *        the setter once.  However, we don't assert if the caller tries to
234  *        overwrite, just ignoring the update instead.
235  *
236  * @param string object schema
237  *****************************************************************************/
238 void evel_jsonobject_objectschema_set(EVEL_JSON_OBJECT * pinst, const char * const objectschema)
239 {
240   assert (pinst != NULL);
241   evel_set_option_string(&pinst->objectschema,objectschema,"Object Schema");
242 }
243
244 /**************************************************************************//**
245  * Set the string values of json object
246  *
247  * @note  The property is treated as immutable: it is only valid to call
248  *        the setter once.  However, we don't assert if the caller tries to
249  *        overwrite, just ignoring the update instead.
250  *
251  * @param string object schema url
252  *****************************************************************************/
253 void evel_jsonobject_objectschemaurl_set(EVEL_JSON_OBJECT * pinst, const char * const objectschemaurl)
254 {
255   assert (pinst != NULL);
256   evel_set_option_string(&pinst->objectschemaurl,objectschemaurl,"Object Schema URL");
257 }
258
259 /**************************************************************************//**
260  * Set the string values of json object
261  *
262  * @note  The property is treated as immutable: it is only valid to call
263  *        the setter once.  However, we don't assert if the caller tries to
264  *        overwrite, just ignoring the update instead.
265  *
266  * @param string  NF Subscribed object name
267  *****************************************************************************/
268 void evel_jsonobject_nfsubscribedobjname_set(EVEL_JSON_OBJECT * pinst, const char * const nfsubscribedobjname)
269 {
270   assert (pinst != NULL);
271   evel_set_option_string(&pinst->nfsubscribedobjname,nfsubscribedobjname,"NF Subscribed Object Name");
272 }
273
274 /**************************************************************************//**
275  * Set the string values of json object
276  *
277  * @note  The property is treated as immutable: it is only valid to call
278  *        the setter once.  However, we don't assert if the caller tries to
279  *        overwrite, just ignoring the update instead.
280  *
281  * @param string  NF Subscription Id
282  *****************************************************************************/
283 void evel_jsonobject_nfsubscriptionid_set(EVEL_JSON_OBJECT * pinst, const char * const nfsubscriptionid)
284 {
285   assert (pinst != NULL);
286   evel_set_option_string(&pinst->nfsubscriptionid,nfsubscriptionid,"NF Subscription Id");
287 }
288
289 /**************************************************************************//**
290  * Set the Epoch time of the json object instance.
291  *
292  * @note  The property is treated as immutable: it is only valid to call
293  *        the setter once.  However, we don't assert if the caller tries to
294  *        overwrite, just ignoring the update instead.
295  *
296  * @param unsigned long long epoch time
297  *****************************************************************************/
298 void evel_epoch_microsec_set(EVEL_JSON_OBJECT_INSTANCE * pinst, const unsigned long long epmicrosec)
299 {
300   assert(epmicrosec != 0 );
301   evel_set_option_ull(&pinst->objinst_epoch_microsec , epmicrosec, "Json object instance microsec set");
302 }
303
304 /**************************************************************************//**
305  * Add a json object instance to jsonObject list.
306  *
307  * The name and value are null delimited ASCII strings.  The library takes
308  * a copy so the caller does not have to preserve values after the function
309  * returns.
310  *
311  * @param pobj         Pointer to the Other.
312  * @param jinst        Pointer to HashTable
313  *****************************************************************************/
314 void evel_jsonobject_add_jsoninstance(EVEL_JSON_OBJECT * pobj, EVEL_JSON_OBJECT_INSTANCE *jinst)
315 {
316   EVEL_ENTER();
317
318   /***************************************************************************/
319   /* Check preconditions.                                                    */
320   /***************************************************************************/
321   assert(pobj != NULL);
322   assert(jinst != NULL);
323
324   EVEL_DEBUG("Adding json object instance %p",jinst);
325
326   dlist_push_last(&pobj->jsonobjectinstances, jinst);
327
328   EVEL_EXIT();
329 }
330
331
332 /**************************************************************************//**
333  * Add a json object to jsonObject list.
334  *
335  * The name and value are null delimited ASCII strings.  The library takes
336  * a copy so the caller does not have to preserve values after the function
337  * returns.
338  *
339  * @param other     Pointer to the Other.
340  * @param jsonobj   Pointer to json object
341  *****************************************************************************/
342 void evel_jsonobjinst_add_objectkey(EVEL_JSON_OBJECT_INSTANCE * jsoninst, EVEL_INTERNAL_KEY *keyp)
343 {
344   EVEL_ENTER();
345
346   /***************************************************************************/
347   /* Check preconditions.                                                    */
348   /***************************************************************************/
349   assert(jsoninst != NULL);
350   assert(keyp != NULL);
351
352   EVEL_DEBUG("Adding jsonObject instance");
353
354   dlist_push_last(&jsoninst->object_keys, keyp);
355
356   EVEL_EXIT();
357 }
358
359 /**************************************************************************//**
360  * Free an internal key.
361  *
362  * Free off the internal key supplied.  Will free all the contained allocated memory.
363  *
364  *****************************************************************************/
365 void evel_free_internal_key(EVEL_INTERNAL_KEY * keyp)
366 {
367
368   EVEL_ENTER();
369
370   /***************************************************************************/
371   /* Check preconditions.  As an internal API we don't allow freeing NULL    */
372   /* events as we do on the public API.                                      */
373   /***************************************************************************/
374   assert(keyp != NULL);
375
376   free(keyp->keyname);
377   evel_free_option_string(&keyp->keyvalue);
378   free(keyp);
379   EVEL_EXIT();
380 }
381
382
383 /**************************************************************************//**
384  * Free an json object instance.
385  *
386  * Free off the json object instance supplied.
387  *  Will free all the contained allocated memory.
388  *
389  *****************************************************************************/
390 void evel_free_jsonobjinst(EVEL_JSON_OBJECT_INSTANCE * objinst)
391 {
392   EVEL_INTERNAL_KEY *other_field = NULL;
393
394   EVEL_ENTER();
395   assert(objinst != NULL);
396   assert(objinst->jsonstring != NULL);
397
398   free(objinst->jsonstring);
399
400   /***************************************************************************/
401   /* Free all internal internal keys */
402   /***************************************************************************/
403   other_field = dlist_pop_last(&objinst->object_keys);
404   while (other_field != NULL)
405   {
406     EVEL_DEBUG("Freeing Object Instance Field (%s)",
407                other_field->keyname);
408     evel_free_internal_key(other_field);
409     other_field = dlist_pop_last(&objinst->object_keys);
410   }
411   free(objinst);
412
413   EVEL_EXIT();
414 }
415
416 /**************************************************************************//**
417  * Free an json object.
418  *
419  * Free off the json object instance supplied.
420  *  Will free all the contained allocated memory.
421  *
422  *****************************************************************************/
423 void evel_free_jsonobject(EVEL_JSON_OBJECT * jsobj)
424 {
425   EVEL_JSON_OBJECT_INSTANCE *other_field = NULL;
426
427   EVEL_ENTER();
428   assert(jsobj != NULL);
429
430   EVEL_DEBUG("Freeing Json Object (%s)", jsobj->object_name);
431   free(jsobj->object_name);
432   evel_free_option_string(&jsobj->objectschema);
433   evel_free_option_string(&jsobj->objectschemaurl);
434   evel_free_option_string(&jsobj->nfsubscribedobjname);
435   evel_free_option_string(&jsobj->nfsubscriptionid);
436
437   /***************************************************************************/
438   /* Free all internal strings then the header itself.                       */
439   /***************************************************************************/
440   other_field = dlist_pop_last(&jsobj->jsonobjectinstances);
441   while (other_field != NULL)
442   {
443     EVEL_DEBUG("Freeing jsonObject Instance Field %p (%s)",
444                other_field,other_field->jsonstring);
445     evel_free_jsonobjinst(other_field);
446     other_field = dlist_pop_last(&jsobj->jsonobjectinstances);
447   }
448   free(jsobj);
449
450   EVEL_EXIT();
451 }