aboutsummaryrefslogtreecommitdiffstats
path: root/vnfs/VESreporting_vFW5.0_DANOS/evel/evel-library/code/evel_library/evel_jsonobject.c
blob: df0b03cceffbddf4973e13d6786ac8365c4f0222 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*************************************************************************//**
 *
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 *
 * Unless otherwise specified, all software contained herein is
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 ****************************************************************************/

/**************************************************************************//**
 * @file
 * Implementation of EVEL functions relating to json_object.
 *
 ****************************************************************************/

#include <string.h>
#include <assert.h>
#include <stdlib.h>

#include "jsmn.h"
#include "evel.h"
#include "evel_internal.h"

/**************************************************************************//**
 * Create a new json object.
 *
 * @note    The mandatory fields on the Other must be supplied to this factory
 *          function and are immutable once set.  Optional fields have explicit
 *          setter functions, but again values may only be set once so that the
 *          Other has immutable properties.
 * @param name       name of the object.
 * @returns pointer to the newly manufactured ::EVEL_JSON_OBJECT.
 *          not used (i.e. posted) it must be released using ::evel_free_jsonobject.
 * @retval  NULL  Failed to create the json object.
 *****************************************************************************/
EVEL_JSON_OBJECT * evel_new_jsonobject(const char *const name)
{
  EVEL_JSON_OBJECT *jobj = NULL;
  EVEL_ENTER();

  /***************************************************************************/
  /* Check preconditions.                                                    */
  /***************************************************************************/
  assert(name != NULL);

  /***************************************************************************/
  /* Allocate the json object.                                                     */
  /***************************************************************************/
  jobj = malloc(sizeof(EVEL_JSON_OBJECT));
  if (jobj == NULL)
  {
    log_error_state("Out of memory");
    goto exit_label;
  }
  memset(jobj, 0, sizeof(EVEL_JSON_OBJECT));
  EVEL_DEBUG("New json object is at %lp", jobj);

  /***************************************************************************/
  /* Initialize the fields.  Optional string values are   */
  /* uninitialized (NULL).                                                   */
  /***************************************************************************/
  jobj->object_name = strdup(name);
  evel_init_option_string(&jobj->objectschema);
  evel_init_option_string(&jobj->objectschemaurl);
  evel_init_option_string(&jobj->nfsubscribedobjname);
  evel_init_option_string(&jobj->nfsubscriptionid);
  dlist_initialize(&jobj->jsonobjectinstances);

exit_label:
  EVEL_EXIT();
  return jobj;
}


/**************************************************************************//**
 * Create a new json object instance.
 *
 * @note    The mandatory fields on the Other must be supplied to this factory
 *          function and are immutable once set.  Optional fields have explicit
 *          setter functions, but again values may only be set once so that the
 *          Other has immutable properties.
 * @param   yourjson       json string.
 * @returns pointer to the newly manufactured ::EVEL_JSON_OBJECT_INSTANCE.
 *          not used (i.e. posted) it must be released using ::evel_free_jsonobjectinstance.
 * @retval  NULL  Failed to create the json object instance.
 *****************************************************************************/
EVEL_JSON_OBJECT_INSTANCE * evel_new_jsonobjinstance(const char *const yourjson)
{
  EVEL_JSON_OBJECT_INSTANCE *jobjinst = NULL;
  jsmntok_t *key;
  int resultCode;
  jsmn_parser p;
  jsmntok_t tokens[MAX_JSON_TOKENS]; // a number >= total number of tokens
  int len=0;


  EVEL_ENTER();

  /***************************************************************************/
  /* Check preconditions.                                                    */
  /***************************************************************************/
  assert(yourjson != NULL);
  len = strlen(yourjson)+1;
  assert(len > 0);

  /***************************************************************************/
  /*  Validate JSON for json object */
  /***************************************************************************/
  jsmn_init(&p);
  resultCode = jsmn_parse(&p, yourjson, len, tokens, sizeof(tokens)/sizeof(tokens[0]));
  if( resultCode < 0 ){
    log_error_state("Failed to parse json for object");
    goto exit_label;
  }

  if (resultCode < 1 || tokens[0].type != JSMN_OBJECT) {
    log_error_state("Error json object expected");
    goto exit_label;
  }

  /***************************************************************************/
  /* Allocate the json object.                                                     */
  /***************************************************************************/
  jobjinst = malloc(sizeof(EVEL_JSON_OBJECT_INSTANCE));
  if (jobjinst == NULL)
  {
    log_error_state("Out of memory");
    goto exit_label;
  }
  memset(jobjinst, 0, sizeof(EVEL_JSON_OBJECT_INSTANCE));

  /***************************************************************************/
  /* Initialize the fields.  Optional key values are   */
  /* uninitialized (NULL).                                                   */
  /***************************************************************************/
  jobjinst->jsonstring = strdup(yourjson);
  dlist_initialize(&jobjinst->object_keys);

exit_label:
  EVEL_EXIT();
  return jobjinst;
}


/**************************************************************************//**
 * Create a new internal key.
 *
 * @note    The mandatory fields on the Other must be supplied to this factory
 *          function and are immutable once set.  Optional fields have explicit
 *          setter functions, but again values may only be set once so that the
 *          Other has immutable properties.
 * @param   keyname       name of the key.
 * @returns pointer to the newly manufactured ::EVEL_INTERNAL_KEY.
 *          not used (i.e. posted) it must be released using ::evel_free_internal_key.
 * @retval  NULL  Failed to create the internal key.
 *****************************************************************************/
EVEL_INTERNAL_KEY * evel_new_internal_key(char *keyname)
{
  EVEL_INTERNAL_KEY *keyinst = NULL;
  EVEL_ENTER();

  /***************************************************************************/
  /* Check preconditions.                                                    */
  /***************************************************************************/
  assert(keyname != NULL);

  /***************************************************************************/
  /* Allocate the key object.                                                     */
  /***************************************************************************/
  keyinst = malloc(sizeof(EVEL_INTERNAL_KEY));
  if (keyinst == NULL)
  {
    log_error_state("Out of memory");
    goto exit_label;
  }
  memset(keyinst, 0, sizeof(EVEL_INTERNAL_KEY));
  keyinst->keyname = strdup(keyname);

  /***************************************************************************/
  /* Optional string values are  uninitialized (NULL).  */
  /***************************************************************************/
  evel_init_option_int(&keyinst->keyorder);
  evel_init_option_string(&keyinst->keyvalue);

exit_label:
  EVEL_EXIT();
  return keyinst;
}

/**************************************************************************//**
 * Set the keyorder  of the internal key instance.
 *
 * @note  The property is treated as immutable: it is only valid to call
 *        the setter once.  However, we don't assert if the caller tries to
 *        overwrite, just ignoring the update instead.
 *
 * @param int keyorder
 *****************************************************************************/
void evel_internal_key_keyorder_set(EVEL_INTERNAL_KEY * pinst, const int keyorder)
{
  assert (pinst != NULL);
  evel_set_option_int(&pinst->keyorder,keyorder,"Key order");
}

/**************************************************************************//**
 * Set the keyvalue  of the internal key instance.
 *
 * @note  The property is treated as immutable: it is only valid to call
 *        the setter once.  However, we don't assert if the caller tries to
 *        overwrite, just ignoring the update instead.
 *
 * @param string keyvalue
 *****************************************************************************/
void evel_internal_key_keyvalue_set(EVEL_INTERNAL_KEY * pinst, const char * const keyval)
{
  assert (pinst != NULL);
  evel_set_option_string(&pinst->keyvalue,keyval,"Key Value");
}

/**************************************************************************//**
 * Set the string values of json object
 *
 * @note  The property is treated as immutable: it is only valid to call
 *        the setter once.  However, we don't assert if the caller tries to
 *        overwrite, just ignoring the update instead.
 *
 * @param string object schema
 *****************************************************************************/
void evel_jsonobject_objectschema_set(EVEL_JSON_OBJECT * pinst, const char * const objectschema)
{
  assert (pinst != NULL);
  evel_set_option_string(&pinst->objectschema,objectschema,"Object Schema");
}

/**************************************************************************//**
 * Set the string values of json object
 *
 * @note  The property is treated as immutable: it is only valid to call
 *        the setter once.  However, we don't assert if the caller tries to
 *        overwrite, just ignoring the update instead.
 *
 * @param string object schema url
 *****************************************************************************/
void evel_jsonobject_objectschemaurl_set(EVEL_JSON_OBJECT * pinst, const char * const objectschemaurl)
{
  assert (pinst != NULL);
  evel_set_option_string(&pinst->objectschemaurl,objectschemaurl,"Object Schema URL");
}

/**************************************************************************//**
 * Set the string values of json object
 *
 * @note  The property is treated as immutable: it is only valid to call
 *        the setter once.  However, we don't assert if the caller tries to
 *        overwrite, just ignoring the update instead.
 *
 * @param string  NF Subscribed object name
 *****************************************************************************/
void evel_jsonobject_nfsubscribedobjname_set(EVEL_JSON_OBJECT * pinst, const char * const nfsubscribedobjname)
{
  assert (pinst != NULL);
  evel_set_option_string(&pinst->nfsubscribedobjname,nfsubscribedobjname,"NF Subscribed Object Name");
}

/**************************************************************************//**
 * Set the string values of json object
 *
 * @note  The property is treated as immutable: it is only valid to call
 *        the setter once.  However, we don't assert if the caller tries to
 *        overwrite, just ignoring the update instead.
 *
 * @param string  NF Subscription Id
 *****************************************************************************/
void evel_jsonobject_nfsubscriptionid_set(EVEL_JSON_OBJECT * pinst, const char * const nfsubscriptionid)
{
  assert (pinst != NULL);
  evel_set_option_string(&pinst->nfsubscriptionid,nfsubscriptionid,"NF Subscription Id");
}

/**************************************************************************//**
 * Set the Epoch time of the json object instance.
 *
 * @note  The property is treated as immutable: it is only valid to call
 *        the setter once.  However, we don't assert if the caller tries to
 *        overwrite, just ignoring the update instead.
 *
 * @param unsigned long long epoch time
 *****************************************************************************/
void evel_epoch_microsec_set(EVEL_JSON_OBJECT_INSTANCE * pinst, const unsigned long long epmicrosec)
{
  assert(epmicrosec != 0 );
  evel_set_option_ull(&pinst->objinst_epoch_microsec , epmicrosec, "Json object instance microsec set");
}

/**************************************************************************//**
 * Add a json object instance to jsonObject list.
 *
 * The name and value are null delimited ASCII strings.  The library takes
 * a copy so the caller does not have to preserve values after the function
 * returns.
 *
 * @param pobj         Pointer to the Other.
 * @param jinst        Pointer to HashTable
 *****************************************************************************/
void evel_jsonobject_add_jsoninstance(EVEL_JSON_OBJECT * pobj, EVEL_JSON_OBJECT_INSTANCE *jinst)
{
  EVEL_ENTER();

  /***************************************************************************/
  /* Check preconditions.                                                    */
  /***************************************************************************/
  assert(pobj != NULL);
  assert(jinst != NULL);

  EVEL_DEBUG("Adding json object instance %p",jinst);

  dlist_push_last(&pobj->jsonobjectinstances, jinst);

  EVEL_EXIT();
}


/**************************************************************************//**
 * Add a json object to jsonObject list.
 *
 * The name and value are null delimited ASCII strings.  The library takes
 * a copy so the caller does not have to preserve values after the function
 * returns.
 *
 * @param other     Pointer to the Other.
 * @param jsonobj   Pointer to json object
 *****************************************************************************/
void evel_jsonobjinst_add_objectkey(EVEL_JSON_OBJECT_INSTANCE * jsoninst, EVEL_INTERNAL_KEY *keyp)
{
  EVEL_ENTER();

  /***************************************************************************/
  /* Check preconditions.                                                    */
  /***************************************************************************/
  assert(jsoninst != NULL);
  assert(keyp != NULL);

  EVEL_DEBUG("Adding jsonObject instance");

  dlist_push_last(&jsoninst->object_keys, keyp);

  EVEL_EXIT();
}

/**************************************************************************//**
 * Free an internal key.
 *
 * Free off the internal key supplied.  Will free all the contained allocated memory.
 *
 *****************************************************************************/
void evel_free_internal_key(EVEL_INTERNAL_KEY * keyp)
{

  EVEL_ENTER();

  /***************************************************************************/
  /* Check preconditions.  As an internal API we don't allow freeing NULL    */
  /* events as we do on the public API.                                      */
  /***************************************************************************/
  assert(keyp != NULL);

  free(keyp->keyname);
  evel_free_option_string(&keyp->keyvalue);
  free(keyp);
  EVEL_EXIT();
}


/**************************************************************************//**
 * Free an json object instance.
 *
 * Free off the json object instance supplied.
 *  Will free all the contained allocated memory.
 *
 *****************************************************************************/
void evel_free_jsonobjinst(EVEL_JSON_OBJECT_INSTANCE * objinst)
{
  EVEL_INTERNAL_KEY *other_field = NULL;

  EVEL_ENTER();
  assert(objinst != NULL);
  assert(objinst->jsonstring != NULL);

  free(objinst->jsonstring);

  /***************************************************************************/
  /* Free all internal internal keys */
  /***************************************************************************/
  other_field = dlist_pop_last(&objinst->object_keys);
  while (other_field != NULL)
  {
    EVEL_DEBUG("Freeing Object Instance Field (%s)",
               other_field->keyname);
    evel_free_internal_key(other_field);
    other_field = dlist_pop_last(&objinst->object_keys);
  }
  free(objinst);

  EVEL_EXIT();
}

/**************************************************************************//**
 * Free an json object.
 *
 * Free off the json object instance supplied.
 *  Will free all the contained allocated memory.
 *
 *****************************************************************************/
void evel_free_jsonobject(EVEL_JSON_OBJECT * jsobj)
{
  EVEL_JSON_OBJECT_INSTANCE *other_field = NULL;

  EVEL_ENTER();
  assert(jsobj != NULL);

  EVEL_DEBUG("Freeing Json Object (%s)", jsobj->object_name);
  free(jsobj->object_name);
  evel_free_option_string(&jsobj->objectschema);
  evel_free_option_string(&jsobj->objectschemaurl);
  evel_free_option_string(&jsobj->nfsubscribedobjname);
  evel_free_option_string(&jsobj->nfsubscriptionid);

  /***************************************************************************/
  /* Free all internal strings then the header itself.                       */
  /***************************************************************************/
  other_field = dlist_pop_last(&jsobj->jsonobjectinstances);
  while (other_field != NULL)
  {
    EVEL_DEBUG("Freeing jsonObject Instance Field %p (%s)",
               other_field,other_field->jsonstring);
    evel_free_jsonobjinst(other_field);
    other_field = dlist_pop_last(&jsobj->jsonobjectinstances);
  }
  free(jsobj);

  EVEL_EXIT();
}