1 /*************************************************************************//**
3 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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
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 ****************************************************************************/
19 /**************************************************************************//**
21 * Implementation of EVEL functions relating to json_object.
23 ****************************************************************************/
31 #include "evel_internal.h"
33 /**************************************************************************//**
34 * Create a new json object.
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)
47 EVEL_JSON_OBJECT *jobj = NULL;
50 /***************************************************************************/
51 /* Check preconditions. */
52 /***************************************************************************/
55 /***************************************************************************/
56 /* Allocate the json object. */
57 /***************************************************************************/
58 jobj = malloc(sizeof(EVEL_JSON_OBJECT));
61 log_error_state("Out of memory");
64 memset(jobj, 0, sizeof(EVEL_JSON_OBJECT));
65 EVEL_DEBUG("New json object is at %lp", jobj);
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);
84 /**************************************************************************//**
85 * Create a new json object instance.
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)
98 EVEL_JSON_OBJECT_INSTANCE *jobjinst = NULL;
102 jsmntok_t tokens[MAX_JSON_TOKENS]; // a number >= total number of tokens
108 /***************************************************************************/
109 /* Check preconditions. */
110 /***************************************************************************/
111 assert(yourjson != NULL);
112 len = strlen(yourjson)+1;
115 /***************************************************************************/
116 /* Validate JSON for json object */
117 /***************************************************************************/
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");
125 if (resultCode < 1 || tokens[0].type != JSMN_OBJECT) {
126 log_error_state("Error json object expected");
130 /***************************************************************************/
131 /* Allocate the json object. */
132 /***************************************************************************/
133 jobjinst = malloc(sizeof(EVEL_JSON_OBJECT_INSTANCE));
134 if (jobjinst == NULL)
136 log_error_state("Out of memory");
139 memset(jobjinst, 0, sizeof(EVEL_JSON_OBJECT_INSTANCE));
141 /***************************************************************************/
142 /* Initialize the fields. Optional key values are */
143 /* uninitialized (NULL). */
144 /***************************************************************************/
145 jobjinst->jsonstring = strdup(yourjson);
146 dlist_initialize(&jobjinst->object_keys);
154 /**************************************************************************//**
155 * Create a new internal key.
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)
168 EVEL_INTERNAL_KEY *keyinst = NULL;
171 /***************************************************************************/
172 /* Check preconditions. */
173 /***************************************************************************/
174 assert(keyname != NULL);
176 /***************************************************************************/
177 /* Allocate the key object. */
178 /***************************************************************************/
179 keyinst = malloc(sizeof(EVEL_INTERNAL_KEY));
182 log_error_state("Out of memory");
185 memset(keyinst, 0, sizeof(EVEL_INTERNAL_KEY));
186 keyinst->keyname = strdup(keyname);
188 /***************************************************************************/
189 /* Optional string values are uninitialized (NULL). */
190 /***************************************************************************/
191 evel_init_option_int(&keyinst->keyorder);
192 evel_init_option_string(&keyinst->keyvalue);
199 /**************************************************************************//**
200 * Set the keyorder of the internal key instance.
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.
206 * @param int keyorder
207 *****************************************************************************/
208 void evel_internal_key_keyorder_set(EVEL_INTERNAL_KEY * pinst, const int keyorder)
210 assert (pinst != NULL);
211 evel_set_option_int(&pinst->keyorder,keyorder,"Key order");
214 /**************************************************************************//**
215 * Set the keyvalue of the internal key instance.
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.
221 * @param string keyvalue
222 *****************************************************************************/
223 void evel_internal_key_keyvalue_set(EVEL_INTERNAL_KEY * pinst, const char * const keyval)
225 assert (pinst != NULL);
226 evel_set_option_string(&pinst->keyvalue,keyval,"Key Value");
229 /**************************************************************************//**
230 * Set the string values of json object
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.
236 * @param string object schema
237 *****************************************************************************/
238 void evel_jsonobject_objectschema_set(EVEL_JSON_OBJECT * pinst, const char * const objectschema)
240 assert (pinst != NULL);
241 evel_set_option_string(&pinst->objectschema,objectschema,"Object Schema");
244 /**************************************************************************//**
245 * Set the string values of json object
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.
251 * @param string object schema url
252 *****************************************************************************/
253 void evel_jsonobject_objectschemaurl_set(EVEL_JSON_OBJECT * pinst, const char * const objectschemaurl)
255 assert (pinst != NULL);
256 evel_set_option_string(&pinst->objectschemaurl,objectschemaurl,"Object Schema URL");
259 /**************************************************************************//**
260 * Set the string values of json object
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.
266 * @param string NF Subscribed object name
267 *****************************************************************************/
268 void evel_jsonobject_nfsubscribedobjname_set(EVEL_JSON_OBJECT * pinst, const char * const nfsubscribedobjname)
270 assert (pinst != NULL);
271 evel_set_option_string(&pinst->nfsubscribedobjname,nfsubscribedobjname,"NF Subscribed Object Name");
274 /**************************************************************************//**
275 * Set the string values of json object
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.
281 * @param string NF Subscription Id
282 *****************************************************************************/
283 void evel_jsonobject_nfsubscriptionid_set(EVEL_JSON_OBJECT * pinst, const char * const nfsubscriptionid)
285 assert (pinst != NULL);
286 evel_set_option_string(&pinst->nfsubscriptionid,nfsubscriptionid,"NF Subscription Id");
289 /**************************************************************************//**
290 * Set the Epoch time of the json object instance.
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.
296 * @param unsigned long long epoch time
297 *****************************************************************************/
298 void evel_epoch_microsec_set(EVEL_JSON_OBJECT_INSTANCE * pinst, const unsigned long long epmicrosec)
300 assert(epmicrosec != 0 );
301 evel_set_option_ull(&pinst->objinst_epoch_microsec , epmicrosec, "Json object instance microsec set");
304 /**************************************************************************//**
305 * Add a json object instance to jsonObject list.
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
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)
318 /***************************************************************************/
319 /* Check preconditions. */
320 /***************************************************************************/
321 assert(pobj != NULL);
322 assert(jinst != NULL);
324 EVEL_DEBUG("Adding json object instance %p",jinst);
326 dlist_push_last(&pobj->jsonobjectinstances, jinst);
332 /**************************************************************************//**
333 * Add a json object to jsonObject list.
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
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)
346 /***************************************************************************/
347 /* Check preconditions. */
348 /***************************************************************************/
349 assert(jsoninst != NULL);
350 assert(keyp != NULL);
352 EVEL_DEBUG("Adding jsonObject instance");
354 dlist_push_last(&jsoninst->object_keys, keyp);
359 /**************************************************************************//**
360 * Free an internal key.
362 * Free off the internal key supplied. Will free all the contained allocated memory.
364 *****************************************************************************/
365 void evel_free_internal_key(EVEL_INTERNAL_KEY * keyp)
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);
377 evel_free_option_string(&keyp->keyvalue);
383 /**************************************************************************//**
384 * Free an json object instance.
386 * Free off the json object instance supplied.
387 * Will free all the contained allocated memory.
389 *****************************************************************************/
390 void evel_free_jsonobjinst(EVEL_JSON_OBJECT_INSTANCE * objinst)
392 EVEL_INTERNAL_KEY *other_field = NULL;
395 assert(objinst != NULL);
396 assert(objinst->jsonstring != NULL);
398 free(objinst->jsonstring);
400 /***************************************************************************/
401 /* Free all internal internal keys */
402 /***************************************************************************/
403 other_field = dlist_pop_last(&objinst->object_keys);
404 while (other_field != NULL)
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);
416 /**************************************************************************//**
417 * Free an json object.
419 * Free off the json object instance supplied.
420 * Will free all the contained allocated memory.
422 *****************************************************************************/
423 void evel_free_jsonobject(EVEL_JSON_OBJECT * jsobj)
425 EVEL_JSON_OBJECT_INSTANCE *other_field = NULL;
428 assert(jsobj != NULL);
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);
437 /***************************************************************************/
438 /* Free all internal strings then the header itself. */
439 /***************************************************************************/
440 other_field = dlist_pop_last(&jsobj->jsonobjectinstances);
441 while (other_field != NULL)
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);