Initial VES for DANOS vRouter
[demo.git] / vnfs / VESreporting_vFW5.0_DANOS / evel / evel-library / code / evel_library / evel_event.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 Event Headers - since
22  * Heartbeats only contain the Event Header, the Heartbeat factory function is
23  * here too.
24  *****************************************************************************/
25
26 #include <string.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <sys/time.h>
30
31 #include "evel.h"
32 #include "evel_internal.h"
33 #include "evel_throttle.h"
34 #include "metadata.h"
35
36 /**************************************************************************//**
37  * Unique sequence number for events from this VNF.
38  *****************************************************************************/
39 static int event_sequence = 1;
40
41 /**************************************************************************//**
42  * Set the next event_sequence to use.
43  *
44  * @param sequence      The next sequence number to use.
45  *****************************************************************************/
46 void evel_set_global_event_sequence(const int sequence)
47 {
48   EVEL_ENTER();
49
50   EVEL_INFO("Setting event sequence to %d, was %d ", sequence, event_sequence);
51   event_sequence = sequence;
52
53   EVEL_EXIT();
54 }
55
56
57 /**************************************************************************//**
58  * Create a new heartbeat event of given name and type.
59  *
60  * @note that the heartbeat is just a "naked" commonEventHeader!
61  *
62  * @param event_name    Unique Event Name: in format
63  * {DomainAbbreviation}_{AsdcModel or ApplicationPlatform}_{DescriptionOfInfoBeingConveyed}
64  * @param event_id     Uniquely identify event for correlation and analysis
65  *
66  * @returns pointer to the newly manufactured ::EVENT_HEADER.  If the event is
67  *          not used it must be released using ::evel_free_event
68  * @retval  NULL  Failed to create the event.
69  *****************************************************************************/
70 EVENT_HEADER * evel_new_heartbeat_nameid(const char* ev_name, const char *ev_id)
71 {
72   EVENT_HEADER * heartbeat = NULL;
73   EVEL_ENTER();
74
75   assert(ev_name != NULL);
76   assert(ev_id != NULL);
77
78   /***************************************************************************/
79   /* Allocate the header.                                                    */
80   /***************************************************************************/
81   heartbeat = malloc(sizeof(EVENT_HEADER));
82   if (heartbeat == NULL)
83   {
84     log_error_state("Out of memory");
85     goto exit_label;
86   }
87   memset(heartbeat, 0, sizeof(EVENT_HEADER));
88
89   /***************************************************************************/
90   /* Initialize the header.  Get a new event sequence number.  Note that if  */
91   /* any memory allocation fails in here we will fail gracefully because     */
92   /* everything downstream can cope with NULLs.                              */
93   /***************************************************************************/
94   evel_init_header_nameid(heartbeat,ev_name,ev_id);
95
96 exit_label:
97   EVEL_EXIT();
98   return heartbeat;
99 }
100
101 /**************************************************************************//**
102  * Create a new heartbeat event.
103  *
104  * @note that the heartbeat is just a "naked" commonEventHeader!
105  *
106  * @returns pointer to the newly manufactured ::EVENT_HEADER.  If the event is
107  *          not used it must be released using ::evel_free_event
108  * @retval  NULL  Failed to create the event.
109  *****************************************************************************/
110 EVENT_HEADER * evel_new_heartbeat()
111 {
112   EVENT_HEADER * heartbeat = NULL;
113   EVEL_ENTER();
114
115   /***************************************************************************/
116   /* Allocate the header.                                                    */
117   /***************************************************************************/
118   heartbeat = malloc(sizeof(EVENT_HEADER));
119   if (heartbeat == NULL)
120   {
121     log_error_state("Out of memory");
122     goto exit_label;
123   }
124   memset(heartbeat, 0, sizeof(EVENT_HEADER));
125
126   /***************************************************************************/
127   /* Initialize the header.  Get a new event sequence number.  Note that if  */
128   /* any memory allocation fails in here we will fail gracefully because     */
129   /* everything downstream can cope with NULLs.                              */
130   /***************************************************************************/
131   evel_init_header(heartbeat,"Heartbeat");
132   evel_force_option_string(&heartbeat->event_type, "Autonomous heartbeat");
133
134 exit_label:
135   EVEL_EXIT();
136   return heartbeat;
137 }
138
139 /**************************************************************************//**
140  * Initialize a newly created event header.
141  *
142  * @param header  Pointer to the header being initialized.
143  *****************************************************************************/
144 void evel_init_header(EVENT_HEADER * const header,const char *const eventname)
145 {
146   char scratchpad[EVEL_MAX_STRING_LEN + 1] = {0};
147   struct timeval tv;
148
149   EVEL_ENTER();
150
151   assert(header != NULL);
152
153   gettimeofday(&tv, NULL);
154
155   /***************************************************************************/
156   /* Initialize the header.  Get a new event sequence number.  Note that if  */
157   /* any memory allocation fails in here we will fail gracefully because     */
158   /* everything downstream can cope with NULLs.                              */
159   /***************************************************************************/
160   header->event_domain = EVEL_DOMAIN_HEARTBEAT;
161   snprintf(scratchpad, EVEL_MAX_STRING_LEN, "%d", event_sequence);
162   header->event_id = strdup(scratchpad);
163   if( eventname == NULL )
164      header->event_name = strdup(functional_role);
165   else
166      header->event_name = strdup(eventname);
167   header->last_epoch_microsec = tv.tv_usec + 1000000 * tv.tv_sec;
168   header->priority = EVEL_PRIORITY_NORMAL;
169   header->reporting_entity_name = strdup(openstack_vm_name());
170   header->source_name = strdup(openstack_vm_name());
171   header->sequence = 0;
172   header->start_epoch_microsec = header->last_epoch_microsec;
173   header->major_version = EVEL_HEADER_MAJOR_VERSION;
174   header->minor_version = EVEL_HEADER_MINOR_VERSION;
175   event_sequence++;
176
177   /***************************************************************************/
178   /* Optional parameters.                                                    */
179   /***************************************************************************/
180   evel_init_option_string(&header->event_type);
181   evel_init_option_string(&header->nfcnaming_code);
182   evel_init_option_string(&header->nfnaming_code);
183   evel_init_option_string(&header->reporting_entity_id);
184   evel_init_option_string(&header->source_id);
185   evel_init_option_intheader(&header->internal_field);
186   dlist_initialize(&header->batch_events);
187
188   EVEL_EXIT();
189 }
190
191
192 /**************************************************************************//**
193  * Initialize a newly created event header.
194  *
195  * @param header  Pointer to the header being initialized.
196  * @param eventname Eventname string
197  * @param eventid   Event id : unique id for classification and analysis
198  * @param header  Pointer to the header being initialized.
199  *****************************************************************************/
200 void evel_init_header_nameid(EVENT_HEADER * const header,const char *const eventname, const char *eventid)
201 {
202   struct timeval tv;
203
204   EVEL_ENTER();
205
206   assert(header != NULL);
207   assert(eventname != NULL);
208   assert(eventid != NULL);
209
210   gettimeofday(&tv, NULL);
211
212   /***************************************************************************/
213   /* Initialize the header.  Reset event sequence number.  Note that if      */
214   /* any memory allocation fails in here we will fail gracefully because     */
215   /* everything downstream can cope with NULLs.                              */
216   /***************************************************************************/
217   header->event_domain = EVEL_DOMAIN_HEARTBEAT;
218   header->event_id = strdup(eventid);
219   header->event_name = strdup(eventname);
220   header->last_epoch_microsec = tv.tv_usec + 1000000 * tv.tv_sec;
221   header->priority = EVEL_PRIORITY_NORMAL;
222   header->reporting_entity_name = strdup(openstack_vm_name());
223   header->source_name = strdup(openstack_vm_name());
224   header->sequence = 0;
225   header->start_epoch_microsec = header->last_epoch_microsec;
226   header->major_version = EVEL_HEADER_MAJOR_VERSION;
227   header->minor_version = EVEL_HEADER_MINOR_VERSION;
228
229   /***************************************************************************/
230   /* Optional parameters.                                                    */
231   /***************************************************************************/
232   evel_init_option_string(&header->event_type);
233   evel_init_option_string(&header->nfcnaming_code);
234   evel_init_option_string(&header->nfnaming_code);
235   evel_init_option_string(&header->reporting_entity_id);
236   evel_init_option_string(&header->source_id);
237   evel_init_option_intheader(&header->internal_field);
238   dlist_initialize(&header->batch_events);
239
240   EVEL_EXIT();
241 }
242
243 /**************************************************************************//**
244  * Set the Event Type property of the event header.
245  *
246  * @note  The property is treated as immutable: it is only valid to call
247  *        the setter once.  However, we don't assert if the caller tries to
248  *        overwrite, just ignoring the update instead.
249  *
250  * @param header        Pointer to the ::EVENT_HEADER.
251  * @param type          The Event Type to be set. ASCIIZ string. The caller
252  *                      does not need to preserve the value once the function
253  *                      returns.
254  *****************************************************************************/
255 void evel_header_type_set(EVENT_HEADER * const header,
256                           const char * const type)
257 {
258   EVEL_ENTER();
259
260   /***************************************************************************/
261   /* Check preconditions.                                                    */
262   /***************************************************************************/
263   assert(header != NULL);
264   assert(type != NULL);
265
266   evel_set_option_string(&header->event_type, type, "Event Type");
267
268   EVEL_EXIT();
269 }
270
271 /**************************************************************************//**
272  * Set the Event Sequence property of the event header.
273  *
274  * @note The Start Epoch defaults to the time of event creation.
275  *
276  * @param header        Pointer to the ::EVENT_HEADER.
277  * @param start_epoch_microsec
278  *                      The start epoch to set, in microseconds.
279  *****************************************************************************/
280 void evel_event_sequence_set(EVENT_HEADER * const header,const int sequence_number)
281 {
282   EVEL_ENTER();
283
284   /***************************************************************************/
285   /* Check preconditions and assign the new value.                           */
286   /***************************************************************************/
287   assert(header != NULL);
288   header->sequence = sequence_number;
289
290   EVEL_EXIT();
291 }
292
293
294 /**************************************************************************//**
295  * Set the Start Epoch property of the event header.
296  *
297  * @note The Start Epoch defaults to the time of event creation.
298  *
299  * @param header        Pointer to the ::EVENT_HEADER.
300  * @param start_epoch_microsec
301  *                      The start epoch to set, in microseconds.
302  *****************************************************************************/
303 void evel_start_epoch_set(EVENT_HEADER * const header,
304                           const unsigned long long start_epoch_microsec)
305 {
306   EVEL_ENTER();
307
308   /***************************************************************************/
309   /* Check preconditions and assign the new value.                           */
310   /***************************************************************************/
311   assert(header != NULL);
312   header->start_epoch_microsec = start_epoch_microsec;
313
314   EVEL_EXIT();
315 }
316
317 /**************************************************************************//**
318  * Set the Last Epoch property of the event header.
319  *
320  * @note The Last Epoch defaults to the time of event creation.
321  *
322  * @param header        Pointer to the ::EVENT_HEADER.
323  * @param last_epoch_microsec
324  *                      The last epoch to set, in microseconds.
325  *****************************************************************************/
326 void evel_last_epoch_set(EVENT_HEADER * const header,
327                          const unsigned long long last_epoch_microsec)
328 {
329   EVEL_ENTER();
330
331   /***************************************************************************/
332   /* Check preconditions and assign the new value.                           */
333   /***************************************************************************/
334   assert(header != NULL);
335   header->last_epoch_microsec = last_epoch_microsec;
336
337   EVEL_EXIT();
338 }
339
340 /**************************************************************************//**
341  * Set the NFC Naming code property of the event header.
342  *
343  * @param header        Pointer to the ::EVENT_HEADER.
344  * @param nfcnamingcode String
345  *****************************************************************************/
346 void evel_nfcnamingcode_set(EVENT_HEADER * const header,
347                          const char * const nfcnam)
348 {
349   EVEL_ENTER();
350
351   /***************************************************************************/
352   /* Check preconditions and assign the new value.                           */
353   /***************************************************************************/
354   assert(header != NULL);
355   assert(nfcnam != NULL);
356   evel_set_option_string(&header->nfcnaming_code, nfcnam, "NFC Naming Code");
357
358   EVEL_EXIT();
359 }
360
361 /**************************************************************************//**
362  * Set the NF Naming code property of the event header.
363  *
364  * @param header        Pointer to the ::EVENT_HEADER.
365  * @param nfnamingcode String
366  *****************************************************************************/
367 void evel_nfnamingcode_set(EVENT_HEADER * const header,
368                          const char * const nfnam)
369 {
370   EVEL_ENTER();
371
372   /***************************************************************************/
373   /* Check preconditions and assign the new value.                           */
374   /***************************************************************************/
375   assert(header != NULL);
376   assert(nfnam != NULL);
377   evel_set_option_string(&header->nfnaming_code, nfnam, "NF Naming Code");
378
379   EVEL_EXIT();
380 }
381
382
383 /**************************************************************************//**
384  * Set the Reporting Entity Name property of the event header.
385  *
386  * @note The Reporting Entity Name defaults to the OpenStack VM Name.
387  *
388  * @param header        Pointer to the ::EVENT_HEADER.
389  * @param entity_name   The entity name to set.
390  *****************************************************************************/
391 void evel_reporting_entity_name_set(EVENT_HEADER * const header,
392                                     const char * const entity_name)
393 {
394   EVEL_ENTER();
395
396   /***************************************************************************/
397   /* Check preconditions and assign the new value.                           */
398   /***************************************************************************/
399   assert(header != NULL);
400   assert(entity_name != NULL);
401   assert(header->reporting_entity_name != NULL);
402
403   /***************************************************************************/
404   /* Free the previously allocated memory and replace it with a copy of the  */
405   /* provided one.                                                           */
406   /***************************************************************************/
407   free(header->reporting_entity_name);
408   header->reporting_entity_name = strdup(entity_name);
409
410   EVEL_EXIT();
411 }
412
413 /**************************************************************************//**
414  * Set the Source Name property of the event header.
415  *
416  * @note The Source Name defaults to the OpenStack VM Name.
417  *
418  * @param header        Pointer to the ::EVENT_HEADER.
419  * @param entity_name   The source name to set.
420  *****************************************************************************/
421 void evel_source_name_set(EVENT_HEADER * const header,
422                                     const char * const source_name)
423 {
424   EVEL_ENTER();
425
426   /***************************************************************************/
427   /* Check preconditions and assign the new value.                           */
428   /***************************************************************************/
429   assert(header != NULL);
430   assert(source_name != NULL);
431
432   /***************************************************************************/
433   /* Free the previously allocated memory and replace it with a copy of the  */
434   /* provided one.                                                           */
435   /***************************************************************************/
436   free(header->source_name);
437   header->source_name = strdup(source_name);
438
439   EVEL_EXIT();
440 }
441
442 /**************************************************************************//**
443  * Set the Reporting Entity Id property of the event header.
444  *
445  * @note The Reporting Entity Id defaults to the OpenStack VM UUID.
446  *
447  * @param header        Pointer to the ::EVENT_HEADER.
448  * @param entity_id     The entity id to set.
449  *****************************************************************************/
450 void evel_reporting_entity_id_set(EVENT_HEADER * const header,
451                                   const char * const entity_id)
452 {
453   EVEL_ENTER();
454
455   /***************************************************************************/
456   /* Check preconditions and assign the new value.                           */
457   /***************************************************************************/
458   assert(header != NULL);
459   assert(entity_id != NULL);
460
461   /***************************************************************************/
462   /* Free the previously allocated memory and replace it with a copy of the  */
463   /* provided one.  Note that evel_force_option_string strdups entity_id.    */
464   /***************************************************************************/
465   evel_free_option_string(&header->reporting_entity_id);
466   evel_force_option_string(&header->reporting_entity_id, entity_id);
467
468   EVEL_EXIT();
469 }
470
471 /**************************************************************************//**
472  * Set the Source Id property of the event header.
473  *
474  * @note The Source Id defaults to the OpenStack VM UUID.
475  *
476  * @param header        Pointer to the ::EVENT_HEADER.
477  * @param entity_id     The Source id to set.
478  *****************************************************************************/
479 void evel_source_id_set(EVENT_HEADER * const header,
480                         const char * const source_id)
481 {
482   EVEL_ENTER();
483
484   /***************************************************************************/
485   /* Check preconditions and assign the new value.                           */
486   /***************************************************************************/
487   assert(header != NULL);
488   assert(source_id != NULL);
489
490   /***************************************************************************/
491   /* Free the previously allocated memory and replace it with a copy of the  */
492   /* provided one.  Note that evel_force_option_string strdups entity_id.    */
493   /***************************************************************************/
494   evel_free_option_string(&header->source_id);
495   evel_force_option_string(&header->source_id, source_id);
496
497   EVEL_EXIT();
498 }
499
500 /**************************************************************************//**
501  * Encode the event as a JSON event object according to AT&T's schema.
502  *
503  * @param jbuf          Pointer to the ::EVEL_JSON_BUFFER to encode into.
504  * @param event         Pointer to the ::EVENT_HEADER to encode.
505  *****************************************************************************/
506 void evel_json_encode_header(EVEL_JSON_BUFFER * jbuf,
507                              EVENT_HEADER * event)
508 {
509   char * domain;
510   char * priority;
511
512   EVEL_ENTER();
513
514   /***************************************************************************/
515   /* Check preconditions.                                                    */
516   /***************************************************************************/
517   assert(jbuf != NULL);
518   assert(jbuf->json != NULL);
519   assert(jbuf->max_size > 0);
520   assert(event != NULL);
521
522   domain = evel_event_domain(event->event_domain);
523   priority = evel_event_priority(event->priority);
524   evel_json_open_named_object(jbuf, "commonEventHeader");
525
526   /***************************************************************************/
527   /* Mandatory fields.                                                       */
528   /***************************************************************************/
529   evel_enc_kv_string(jbuf, "domain", domain);
530   evel_enc_kv_string(jbuf, "eventId", event->event_id);
531   evel_enc_kv_string(jbuf, "eventName", event->event_name);
532   evel_enc_kv_ull(jbuf, "lastEpochMicrosec", event->last_epoch_microsec);
533   evel_enc_kv_string(jbuf, "priority", priority);
534   evel_enc_kv_string(
535     jbuf, "reportingEntityName", event->reporting_entity_name);
536   evel_enc_kv_int(jbuf, "sequence", event->sequence);
537   evel_enc_kv_string(jbuf, "sourceName", event->source_name);
538   evel_enc_kv_ull(jbuf, "startEpochMicrosec", event->start_epoch_microsec);
539   evel_enc_version(
540     jbuf, "version", event->major_version, event->minor_version);
541
542   /***************************************************************************/
543   /* Optional fields.                                                        */
544   /***************************************************************************/
545   evel_enc_kv_opt_string(jbuf, "eventType", &event->event_type);
546   evel_enc_kv_opt_string(
547     jbuf, "reportingEntityId", &event->reporting_entity_id);
548   evel_enc_kv_opt_string(jbuf, "sourceId", &event->source_id);
549   evel_enc_kv_opt_string(jbuf, "nfcNamingCode", &event->nfcnaming_code);
550   evel_enc_kv_opt_string(jbuf, "nfNamingCode", &event->nfnaming_code);
551
552   evel_json_close_object(jbuf);
553
554   EVEL_EXIT();
555 }
556
557 /**************************************************************************//**
558  * Free an event header.
559  *
560  * Free off the event header supplied.  Will free all the contained allocated
561  * memory.
562  *
563  * @note It does not free the header itself, since that may be part of a
564  * larger structure.
565  *****************************************************************************/
566 void evel_free_header(EVENT_HEADER * const event)
567 {
568   EVEL_ENTER();
569
570   /***************************************************************************/
571   /* Check preconditions.  As an internal API we don't allow freeing NULL    */
572   /* events as we do on the public API.                                      */
573   /***************************************************************************/
574   assert(event != NULL);
575
576   /***************************************************************************/
577   /* Free all internal strings.                                              */
578   /***************************************************************************/
579   free(event->event_id);
580   evel_free_option_string(&event->event_type);
581   free(event->event_name);
582   evel_free_option_string(&event->reporting_entity_id);
583   free(event->reporting_entity_name);
584   evel_free_option_string(&event->source_id);
585   evel_free_option_string(&event->nfcnaming_code);
586   evel_free_option_string(&event->nfnaming_code);
587   evel_free_option_intheader(&event->internal_field);
588   free(event->source_name);
589
590   EVEL_EXIT();
591 }
592
593
594 /**************************************************************************//**
595  * Encode the event as a JSON event object according to AT&T's schema.
596  *
597  * @param json      Pointer to where to store the JSON encoded data.
598  * @param max_size  Size of storage available in json_body.
599  * @param event     Pointer to the ::EVENT_HEADER to encode.
600  * @returns Number of bytes actually written.
601  *****************************************************************************/
602 void evel_json_encode_eventtype(
603                            EVEL_JSON_BUFFER * jbuf,
604                            EVENT_HEADER * event)
605 {
606       switch (event->event_domain)
607       {
608         case EVEL_DOMAIN_HEARTBEAT:
609           evel_json_encode_header(jbuf, event);
610           break;
611
612         case EVEL_DOMAIN_FAULT:
613           evel_json_encode_fault(jbuf, (EVENT_FAULT *)event);
614           break;
615
616         case EVEL_DOMAIN_MEASUREMENT:
617           evel_json_encode_measurement(jbuf, (EVENT_MEASUREMENT *)event);
618           break;
619
620         case EVEL_DOMAIN_MOBILE_FLOW:
621           evel_json_encode_mobile_flow(jbuf, (EVENT_MOBILE_FLOW *)event);
622           break;
623
624         case EVEL_DOMAIN_REPORT:
625           evel_json_encode_report(jbuf, (EVENT_REPORT *)event);
626           break;
627
628         case EVEL_DOMAIN_HEARTBEAT_FIELD:
629           evel_json_encode_hrtbt_field(jbuf, (EVENT_HEARTBEAT_FIELD *)event);
630           break;
631
632         case EVEL_DOMAIN_SIPSIGNALING:
633           evel_json_encode_signaling(jbuf, (EVENT_SIGNALING *)event);
634           break;
635
636         case EVEL_DOMAIN_STATE_CHANGE:
637           evel_json_encode_state_change(jbuf, (EVENT_STATE_CHANGE *)event);
638           break;
639
640         case EVEL_DOMAIN_SYSLOG:
641           evel_json_encode_syslog(jbuf, (EVENT_SYSLOG *)event);
642           break;
643
644         case EVEL_DOMAIN_OTHER:
645           evel_json_encode_other(jbuf, (EVENT_OTHER *)event);
646           break;
647
648         case EVEL_DOMAIN_VOICE_QUALITY:
649           evel_json_encode_voice_quality(jbuf, (EVENT_VOICE_QUALITY *)event);
650           break;
651
652         case EVEL_DOMAIN_THRESHOLD_CROSS:
653           evel_json_encode_threshold_cross(jbuf, (EVENT_THRESHOLD_CROSS *)event);
654           break;
655
656         case EVEL_DOMAIN_INTERNAL:
657         default:
658           EVEL_ERROR("Unexpected domain %d", event->event_domain);
659           assert(0);
660       }
661 }
662
663
664
665 /**************************************************************************//**
666  * Encode the event as a JSON event object according to AT&T's schema.
667  *
668  * @param json      Pointer to where to store the JSON encoded data.
669  * @param max_size  Size of storage available in json_body.
670  * @param event     Pointer to the ::EVENT_HEADER to encode.
671  * @returns Number of bytes actually written.
672  *****************************************************************************/
673 int evel_json_encode_event(char * json,
674                            int max_size,
675                            EVENT_HEADER * event)
676 {
677   EVEL_JSON_BUFFER json_buffer;
678   EVEL_JSON_BUFFER * jbuf = &json_buffer;
679   EVEL_THROTTLE_SPEC * throttle_spec;
680
681   EVEL_ENTER();
682
683   /***************************************************************************/
684   /* Get the latest throttle specification for the domain.                   */
685   /***************************************************************************/
686   throttle_spec = evel_get_throttle_spec(event->event_domain);
687
688   /***************************************************************************/
689   /* Initialize the JSON_BUFFER and open the top-level objects.              */
690   /***************************************************************************/
691   evel_json_buffer_init(jbuf, json, max_size, throttle_spec);
692   evel_json_open_object(jbuf);
693   evel_json_open_named_object(jbuf, "event");
694
695   evel_json_encode_eventtype(jbuf, event);
696
697   evel_json_close_object(jbuf);
698   evel_json_close_object(jbuf);
699
700   /***************************************************************************/
701   /* Sanity check.                                                           */
702   /***************************************************************************/
703   assert(jbuf->depth == 0);
704   if( jbuf->offset >= max_size ){
705           EVEL_ERROR("Event exceeded size limit %d", max_size);
706           assert(0);
707   }
708
709   EVEL_EXIT();
710
711   return jbuf->offset;
712 }
713 /**************************************************************************//**
714  * Encode the event as a JSON event object according to AT&T's schema.
715  *
716  * @param json      Pointer to where to store the JSON encoded data.
717  * @param max_size  Size of storage available in json_body.
718  * @param event     Pointer to the ::EVENT_HEADER to encode.
719  * @returns Number of bytes actually written.
720  *****************************************************************************/
721 int evel_json_encode_batch_event(char * json,
722                            int max_size,
723                            EVENT_HEADER * event)
724 {
725   EVEL_JSON_BUFFER json_buffer;
726   EVEL_JSON_BUFFER *jbuf = &json_buffer;
727   EVEL_THROTTLE_SPEC * throttle_spec;
728   int tot_size = 0;
729   EVENT_HEADER * batch_field = NULL;
730   DLIST_ITEM * batch_field_item = NULL;
731
732   EVEL_ENTER();
733
734   /***************************************************************************/
735   /* Get the latest throttle specification for the domain.                   */
736   /***************************************************************************/
737   throttle_spec = evel_get_throttle_spec(event->event_domain);
738
739   /***************************************************************************/
740   /* Initialize the JSON_BUFFER and open the top-level objects.              */
741   /***************************************************************************/
742   if (event->event_domain == EVEL_DOMAIN_BATCH){
743       evel_json_buffer_init(jbuf, json, max_size, throttle_spec);
744
745   if(dlist_count(&event->batch_events) > 0)
746   {
747     evel_json_open_object(jbuf);
748     evel_json_open_named_list(jbuf, "eventList");
749     batch_field_item = dlist_get_first(&event->batch_events);
750     while (batch_field_item != NULL)
751     {
752      batch_field = (EVENT_HEADER *) batch_field_item->item;
753      if(batch_field != NULL){
754        EVEL_DEBUG("Batch Event %p %p added curr fsize %d offset %d depth %d check %d", batch_field_item->item, batch_field, tot_size,jbuf->offset,jbuf->depth,jbuf->checkpoint);
755        evel_json_open_object(jbuf);
756        evel_json_encode_eventtype(jbuf, batch_field);
757        evel_json_close_object(jbuf);
758
759        tot_size += jbuf->offset;
760        EVEL_DEBUG("Batch Event result size %d offset %d depth %d check %d", tot_size,jbuf->offset,jbuf->depth,jbuf->checkpoint);
761        if( tot_size >= max_size ){
762           EVEL_ERROR("Batch Event exceeded size limit %d", tot_size);
763           assert(0);
764        }
765        batch_field_item = dlist_get_next(batch_field_item);
766      }
767     }
768     evel_json_close_list(jbuf);
769     evel_json_close_object(jbuf);
770   }
771
772   }
773   /***************************************************************************/
774   /* Sanity check.                                                           */
775   /***************************************************************************/
776   //assert(jbuf->depth == 0);
777
778   EVEL_EXIT();
779
780   return jbuf->offset;
781 }
782
783
784 /**************************************************************************//**
785  * Initialize an event instance id.
786  *
787  * @param vfield        Pointer to the event vnfname field being initialized.
788  * @param vendor_id     The vendor id to encode in the event instance id.
789  * @param event_id      The event id to encode in the event instance id.
790  *****************************************************************************/
791 void evel_init_vendor_field(VENDOR_VNFNAME_FIELD * const vfield,
792                                  const char * const vendor_name)
793 {
794   EVEL_ENTER();
795
796   /***************************************************************************/
797   /* Check preconditions.                                                    */
798   /***************************************************************************/
799   assert(vfield != NULL);
800   assert(vendor_name != NULL);
801
802   /***************************************************************************/
803   /* Store the mandatory parts.                                              */
804   /***************************************************************************/
805   vfield->vendorname = strdup(vendor_name);
806   evel_init_option_string(&vfield->vfmodule);
807   evel_init_option_string(&vfield->vnfname);
808
809   /***************************************************************************/
810   /* Initialize the optional parts.                                          */
811   /***************************************************************************/
812
813   EVEL_EXIT();
814 }
815
816 /**************************************************************************//**
817  * Set the Vendor module property of the Vendor.
818  *
819  * @note  The property is treated as immutable: it is only valid to call
820  *        the setter once.  However, we don't assert if the caller tries to
821  *        overwrite, just ignoring the update instead.
822  *
823  * @param vfield        Pointer to the Vendor field.
824  * @param module_name   The module name to be set. ASCIIZ string. The caller
825  *                      does not need to preserve the value once the function
826  *                      returns.
827  *****************************************************************************/
828 void evel_vendor_field_module_set(VENDOR_VNFNAME_FIELD * const vfield,
829                                     const char * const module_name)
830 {
831   EVEL_ENTER();
832
833   /***************************************************************************/
834   /* Check preconditions.                                                    */
835   /***************************************************************************/
836   assert(vfield != NULL);
837   assert(module_name != NULL);
838
839   evel_set_option_string(&vfield->vfmodule, module_name, "Module name set");
840
841   EVEL_EXIT();
842 }
843
844 /**************************************************************************//**
845  * Set the Vendor module property of the Vendor.
846  *
847  * @note  The property is treated as immutable: it is only valid to call
848  *        the setter once.  However, we don't assert if the caller tries to
849  *        overwrite, just ignoring the update instead.
850  *
851  * @param vfield        Pointer to the Vendor field.
852  * @param module_name   The module name to be set. ASCIIZ string. The caller
853  *                      does not need to preserve the value once the function
854  *                      returns.
855  *****************************************************************************/
856 void evel_vendor_field_vnfname_set(VENDOR_VNFNAME_FIELD * const vfield,
857                                     const char * const vnfname)
858 {
859   EVEL_ENTER();
860
861   /***************************************************************************/
862   /* Check preconditions.                                                    */
863   /***************************************************************************/
864   assert(vfield != NULL);
865   assert(vnfname != NULL);
866
867   evel_set_option_string(&vfield->vnfname, vnfname, "Virtual Network Function name set");
868
869   EVEL_EXIT();
870 }
871
872 /**************************************************************************//**
873  * Free an event instance id.
874  *
875  * @param vfield   Pointer to the event vnfname_field being freed.
876  *****************************************************************************/
877 void evel_free_event_vendor_field(VENDOR_VNFNAME_FIELD * const vfield)
878 {
879   EVEL_ENTER();
880
881   /***************************************************************************/
882   /* Check preconditions.                                                    */
883   /***************************************************************************/
884   assert(vfield->vendorname != NULL);
885
886   /***************************************************************************/
887   /* Free everything.                                                        */
888   /***************************************************************************/
889   evel_free_option_string(&vfield->vfmodule);
890   evel_free_option_string(&vfield->vnfname);
891   free(vfield->vendorname);
892
893   EVEL_EXIT();
894 }
895
896 /**************************************************************************//**
897  * Encode the instance id as a JSON object according to AT&T's schema.
898  *
899  * @param jbuf          Pointer to the ::EVEL_JSON_BUFFER to encode into.
900  * @param vfield        Pointer to the ::VENDOR_VNFNAME_FIELD to encode.
901  *****************************************************************************/
902 void evel_json_encode_vendor_field(EVEL_JSON_BUFFER * jbuf,
903                                   VENDOR_VNFNAME_FIELD * vfield)
904 {
905   EVEL_ENTER();
906
907   /***************************************************************************/
908   /* Check preconditions.                                                    */
909   /***************************************************************************/
910   assert(jbuf != NULL);
911   assert(jbuf->json != NULL);
912   assert(jbuf->max_size > 0);
913   assert(vfield != NULL);
914   assert(vfield->vendorname != NULL);
915
916   evel_json_open_named_object(jbuf, "vendorVnfNameFields");
917
918   /***************************************************************************/
919   /* Mandatory fields.                                                       */
920   /***************************************************************************/
921   evel_enc_kv_string(jbuf, "vendorName", vfield->vendorname);
922   evel_enc_kv_opt_string(jbuf, "vfModuleName", &vfield->vfmodule);
923   evel_enc_kv_opt_string(jbuf, "vnfName", &vfield->vnfname);
924
925   /***************************************************************************/
926   /* Optional fields.                                                        */
927   /***************************************************************************/
928
929   evel_json_close_object(jbuf);
930
931   EVEL_EXIT();
932 }