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 Other.
23 ****************************************************************************/
30 #include "evel_internal.h"
32 /**************************************************************************//**
33 * Create a new Other event.
35 * @note The mandatory fields on the Other must be supplied to this factory
36 * function and are immutable once set. Optional fields have explicit
37 * setter functions, but again values may only be set once so that the
38 * Other has immutable properties.
39 * @returns pointer to the newly manufactured ::EVENT_OTHER. If the event is
40 * not used (i.e. posted) it must be released using ::evel_free_other.
41 * @retval NULL Failed to create the event.
42 *****************************************************************************/
43 EVENT_OTHER * evel_new_other(const char *ev_name, const char *ev_id)
45 EVENT_OTHER * other = NULL;
48 /***************************************************************************/
49 /* Check preconditions. */
50 /***************************************************************************/
52 /***************************************************************************/
53 /* Allocate the Other. */
54 /***************************************************************************/
55 other = malloc(sizeof(EVENT_OTHER));
58 log_error_state("Out of memory");
61 memset(other, 0, sizeof(EVENT_OTHER));
62 EVEL_DEBUG("New Other is at %lp", other);
64 /***************************************************************************/
65 /* Initialize the header & the Other fields. Optional string values are */
66 /* uninitialized (NULL). */
67 /***************************************************************************/
68 evel_init_header_nameid(&other->header,ev_name,ev_id);
69 other->header.event_domain = EVEL_DOMAIN_OTHER;
70 other->major_version = EVEL_OTHER_EVENT_MAJOR_VERSION;
71 other->minor_version = EVEL_OTHER_EVENT_MINOR_VERSION;
73 other->namedarrays = NULL;
74 dlist_initialize(&other->jsonobjects);
75 dlist_initialize(&other->namedvalues);
82 /**************************************************************************//**
83 * Set the Event Type property of the Other.
85 * @note The property is treated as immutable: it is only valid to call
86 * the setter once. However, we don't assert if the caller tries to
87 * overwrite, just ignoring the update instead.
89 * @param other Pointer to the Other.
90 * @param type The Event Type to be set. ASCIIZ string. The caller
91 * does not need to preserve the value once the function
93 *****************************************************************************/
94 void evel_other_type_set(EVENT_OTHER * other,
95 const char * const type)
99 /***************************************************************************/
100 /* Check preconditions and call evel_header_type_set. */
101 /***************************************************************************/
102 assert(other != NULL);
103 assert(other->header.event_domain == EVEL_DOMAIN_OTHER);
104 evel_header_type_set(&other->header, type);
109 /**************************************************************************//**
110 * Add a json object to jsonObject list.
112 * The name and value are null delimited ASCII strings. The library takes
113 * a copy so the caller does not have to preserve values after the function
116 * @param other Pointer to the Other.
117 * @param size size of hashtable
118 *****************************************************************************/
119 void evel_other_field_set_namedarraysize(EVENT_OTHER * other, const int size)
123 /***************************************************************************/
124 /* Check preconditions. */
125 /***************************************************************************/
126 assert(other != NULL);
127 assert(other->header.event_domain == EVEL_DOMAIN_OTHER);
128 assert(other->namedarrays == NULL);
131 EVEL_DEBUG("Adding Named array");
133 other->namedarrays = ht_create(size);
139 /**************************************************************************//**
140 * Add a json object to jsonObject list.
142 * The name and value are null delimited ASCII strings. The library takes
143 * a copy so the caller does not have to preserve values after the function
146 * @param other Pointer to the Other.
147 * @param size size of hashtable
148 *****************************************************************************/
149 void evel_other_field_add_namedarray(EVENT_OTHER * other, const char *hashname, char * name, char *value)
151 OTHER_FIELD * other_field = NULL;
155 /***************************************************************************/
156 /* Check preconditions. */
157 /***************************************************************************/
158 assert(other != NULL);
159 assert(other->header.event_domain == EVEL_DOMAIN_OTHER);
160 assert(other->namedarrays != NULL);
162 EVEL_DEBUG("Adding values to Named array");
164 EVEL_DEBUG("Adding name=%s value=%s", name, value);
165 other_field = malloc(sizeof(OTHER_FIELD));
166 assert(other_field != NULL);
167 memset(other_field, 0, sizeof(OTHER_FIELD));
168 other_field->name = strdup(name);
169 other_field->value = strdup(value);
170 assert(other_field->name != NULL);
171 assert(other_field->value != NULL);
174 list = ht_get(other->namedarrays, hashname);
177 DLIST * nlist = malloc(sizeof(DLIST));
178 dlist_initialize(nlist);
179 dlist_push_last(nlist, other_field);
180 ht_set(other->namedarrays, hashname, nlist);
181 EVEL_DEBUG("Created to new table table");
185 dlist_push_last(list, other_field);
186 EVEL_DEBUG("Adding to existing table");
193 /**************************************************************************//**
194 * Add a json object to jsonObject list.
196 * The name and value are null delimited ASCII strings. The library takes
197 * a copy so the caller does not have to preserve values after the function
200 * @param other Pointer to the Other.
201 * @param jsonobj Pointer to json object
202 *****************************************************************************/
203 void evel_other_field_add_jsonobj(EVENT_OTHER * other, EVEL_JSON_OBJECT *jsonobj)
205 OTHER_FIELD * other_field = NULL;
208 /***************************************************************************/
209 /* Check preconditions. */
210 /***************************************************************************/
211 assert(other != NULL);
212 assert(other->header.event_domain == EVEL_DOMAIN_OTHER);
213 assert(jsonobj != NULL);
215 EVEL_DEBUG("Adding jsonObject");
217 dlist_push_last(&other->jsonobjects, jsonobj);
222 /**************************************************************************//**
223 * Add a field name/value pair to the Other.
225 * The name and value are null delimited ASCII strings. The library takes
226 * a copy so the caller does not have to preserve values after the function
229 * @param other Pointer to the Other.
230 * @param name ASCIIZ string with the field's name. The caller does not
231 * need to preserve the value once the function returns.
232 * @param value ASCIIZ string with the field's value. The caller does not
233 * need to preserve the value once the function returns.
234 *****************************************************************************/
235 void evel_other_field_add(EVENT_OTHER * other, char * name, char * value)
237 OTHER_FIELD * other_field = NULL;
240 /***************************************************************************/
241 /* Check preconditions. */
242 /***************************************************************************/
243 assert(other != NULL);
244 assert(other->header.event_domain == EVEL_DOMAIN_OTHER);
245 assert(name != NULL);
246 assert(value != NULL);
248 EVEL_DEBUG("Adding name=%s value=%s", name, value);
249 other_field = malloc(sizeof(OTHER_FIELD));
250 assert(other_field != NULL);
251 memset(other_field, 0, sizeof(OTHER_FIELD));
252 other_field->name = strdup(name);
253 other_field->value = strdup(value);
254 assert(other_field->name != NULL);
255 assert(other_field->value != NULL);
257 dlist_push_last(&other->namedvalues, other_field);
262 /**************************************************************************//**
263 * Encode the Other in JSON according to AT&T's schema for the event type.
265 * @param jbuf Pointer to the ::EVEL_JSON_BUFFER to encode into.
266 * @param event Pointer to the ::EVENT_HEADER to encode.
267 *****************************************************************************/
268 void evel_json_encode_other(EVEL_JSON_BUFFER * jbuf,
271 OTHER_FIELD * other_field = NULL;
272 EVEL_JSON_OBJECT * jsonobjp = NULL;
273 DLIST_ITEM * other_field_item = NULL;
274 EVEL_JSON_OBJECT_INSTANCE * jsonobjinst = NULL;
275 DLIST_ITEM * jsobj_field_item = NULL;
276 EVEL_INTERNAL_KEY * keyinst = NULL;
277 DLIST_ITEM * keyinst_field_item = NULL;
278 HASHTABLE_T *ht = NULL;
280 bool itm_added = false;
281 DLIST *itm_list = NULL;
285 /***************************************************************************/
286 /* Check preconditions. */
287 /***************************************************************************/
288 assert(event != NULL);
289 assert(event->header.event_domain == EVEL_DOMAIN_OTHER);
291 evel_json_encode_header(jbuf, &event->header);
292 evel_json_open_named_object(jbuf, "otherFields");
294 // iterate through hashtable and print DLIST for each entry
296 ht = event->namedarrays;
301 for( i = 0; i < ht->size; i++ ) {
302 if( ht->table[i] != NULL)
307 if( itm_added == true)
310 if (evel_json_open_opt_named_list(jbuf, "hashOfNameValuePairArrays"))
312 for( i = 0; i < ht->size; i++ ) {
313 if( ht->table[i] != NULL)
315 itm_list = ht->table[i];
317 if(evel_json_open_opt_named_list(jbuf, ht->table[i]->key))
319 other_field_item = dlist_get_first(&itm_list);
320 while (other_field_item != NULL)
322 other_field = (OTHER_FIELD *) other_field_item->item;
323 if(other_field != NULL){
324 evel_json_open_object(jbuf);
325 evel_enc_kv_string(jbuf, "name", other_field->name);
326 evel_enc_kv_string(jbuf, "value", other_field->value);
327 evel_json_close_object(jbuf);
328 other_field_item = dlist_get_next(other_field_item);
331 evel_json_close_list(jbuf);
337 evel_json_close_list(jbuf);
345 evel_json_checkpoint(jbuf);
346 if(evel_json_open_opt_named_list(jbuf, "jsonObjects"))
348 bool item_added = false;
349 other_field_item = dlist_get_first(&event->jsonobjects);
350 while (other_field_item != NULL)
352 jsonobjp = (EVEL_JSON_OBJECT *) other_field_item->item;
353 if(jsonobjp != NULL);
355 evel_json_open_object(jbuf);
357 if( evel_json_open_opt_named_list(jbuf, "objectInstances"))
359 bool item_added2 = false;
360 jsobj_field_item = dlist_get_first(&jsonobjp->jsonobjectinstances);
361 while (jsobj_field_item != NULL)
363 jsonobjinst = (EVEL_JSON_OBJECT_INSTANCE *) jsobj_field_item->item;
364 if( jsonobjinst != NULL )
366 evel_json_open_object(jbuf);
367 evel_enc_kv_object(jbuf, "objectInstance", jsonobjinst->jsonstring);
368 evel_enc_kv_ull(jbuf, "objectInstanceEpochMicrosec", jsonobjinst->objinst_epoch_microsec);
369 //evel_json_checkpoint(jbuf);
370 if (evel_json_open_opt_named_list(jbuf, "objectKeys"))
372 bool item_added3 = false;
374 keyinst_field_item = dlist_get_first(&jsonobjinst->object_keys);
375 while (keyinst_field_item != NULL)
377 keyinst = (EVEL_INTERNAL_KEY *)keyinst_field_item->item;
380 evel_json_open_object(jbuf);
381 evel_enc_kv_string(jbuf, "keyName", keyinst->keyname);
382 evel_enc_kv_opt_int(jbuf, "keyOrder", &keyinst->keyorder);
383 evel_enc_kv_opt_string(jbuf, "keyValue", &keyinst->keyvalue);
384 evel_json_close_object(jbuf);
387 keyinst_field_item = dlist_get_next(keyinst_field_item);
389 evel_json_close_list(jbuf);
391 /*************************************************************************/
392 /* If we've not written anything, rewind to before we opened the list. */
393 /*************************************************************************/
396 // evel_json_rewind(jbuf);
399 evel_json_close_object(jbuf);
402 jsobj_field_item = dlist_get_next(jsobj_field_item);
404 evel_json_close_list(jbuf);
407 evel_json_rewind(jbuf);
411 evel_enc_kv_string(jbuf, "objectName", jsonobjp->object_name);
412 evel_enc_kv_opt_string(jbuf, "objectSchema", &jsonobjp->objectschema);
413 evel_enc_kv_opt_string(jbuf, "objectSchemaUrl", &jsonobjp->objectschemaurl);
414 evel_enc_kv_opt_string(jbuf, "nfSubscribedObjectName", &jsonobjp->nfsubscribedobjname);
415 evel_enc_kv_opt_string(jbuf, "nfSubscriptionId", &jsonobjp->nfsubscriptionid);
416 evel_json_close_object(jbuf);
419 other_field_item = dlist_get_next(other_field_item);
421 evel_json_close_list(jbuf);
425 evel_json_rewind(jbuf);
430 if( evel_json_open_opt_named_list(jbuf, "nameValuePairs"))
432 other_field_item = dlist_get_first(&event->namedvalues);
433 while (other_field_item != NULL)
435 other_field = (OTHER_FIELD *) other_field_item->item;
436 assert(other_field != NULL);
438 evel_json_open_object(jbuf);
439 evel_enc_kv_string(jbuf, "name", other_field->name);
440 evel_enc_kv_string(jbuf, "value", other_field->value);
441 evel_json_close_object(jbuf);
442 other_field_item = dlist_get_next(other_field_item);
445 evel_json_close_list(jbuf);
447 evel_enc_version(jbuf, "otherFieldsVersion", event->major_version,event->minor_version);
449 evel_json_close_object(jbuf);
454 /**************************************************************************//**
457 * Free off the Other supplied. Will free all the contained allocated memory.
459 * @note It does not free the Other itself, since that may be part of a
461 *****************************************************************************/
462 void evel_free_other(EVENT_OTHER * event)
464 OTHER_FIELD * other_field = NULL;
468 /***************************************************************************/
469 /* Check preconditions. As an internal API we don't allow freeing NULL */
470 /* events as we do on the public API. */
471 /***************************************************************************/
472 assert(event != NULL);
473 assert(event->header.event_domain == EVEL_DOMAIN_OTHER);
475 /***************************************************************************/
476 /* Free all internal strings then the header itself. */
477 /***************************************************************************/
478 other_field = dlist_pop_last(&event->namedvalues);
479 while (other_field != NULL)
481 EVEL_DEBUG("Freeing Other Field (%s, %s)",
484 free(other_field->name);
485 free(other_field->value);
487 other_field = dlist_pop_last(&event->namedvalues);
489 evel_free_header(&event->header);