Implement batching of VES events
[demo.git] / vnfs / VES5.0 / evel / evel-library / code / evel_library / evel_event.c
index 9fbf392..c9bb434 100644 (file)
@@ -1,39 +1,26 @@
+/*************************************************************************//**
+ *
+ * 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 Event Headers - since
  * Heartbeats only contain the Event Header, the Heartbeat factory function is
  * here too.
- *
- * License
- * -------
- *
- * Copyright(c) <2016>, AT&T Intellectual Property.  All other rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- *    this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- *    this list of conditions and the following disclaimer in the documentation
- *    and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:  This product includes
- *    software developed by the AT&T.
- * 4. Neither the name of AT&T nor the names of its contributors may be used to
- *    endorse or promote products derived from this software without specific
- *    prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY AT&T INTELLECTUAL PROPERTY ''AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL AT&T INTELLECTUAL PROPERTY BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *****************************************************************************/
 
 #include <string.h>
@@ -66,6 +53,51 @@ void evel_set_next_event_sequence(const int sequence)
   EVEL_EXIT();
 }
 
+
+/**************************************************************************//**
+ * Create a new heartbeat event of given name and type.
+ *
+ * @note that the heartbeat is just a "naked" commonEventHeader!
+ *
+ * @param event_name    Unique Event Name: in format
+ * {DomainAbbreviation}_{AsdcModel or ApplicationPlatform}_{DescriptionOfInfoBeingConveyed}
+ * @param event_id     Uniquely identify event for correlation and analysis
+ *
+ * @returns pointer to the newly manufactured ::EVENT_HEADER.  If the event is
+ *          not used it must be released using ::evel_free_event
+ * @retval  NULL  Failed to create the event.
+ *****************************************************************************/
+EVENT_HEADER * evel_new_heartbeat_nameid(const char* ev_name, const char *ev_id)
+{
+  EVENT_HEADER * heartbeat = NULL;
+  EVEL_ENTER();
+
+  assert(ev_name != NULL);
+  assert(ev_id != NULL);
+
+  /***************************************************************************/
+  /* Allocate the header.                                                    */
+  /***************************************************************************/
+  heartbeat = malloc(sizeof(EVENT_HEADER));
+  if (heartbeat == NULL)
+  {
+    log_error_state("Out of memory");
+    goto exit_label;
+  }
+  memset(heartbeat, 0, sizeof(EVENT_HEADER));
+
+  /***************************************************************************/
+  /* Initialize the header.  Get a new event sequence number.  Note that if  */
+  /* any memory allocation fails in here we will fail gracefully because     */
+  /* everything downstream can cope with NULLs.                              */
+  /***************************************************************************/
+  evel_init_header_nameid(heartbeat,ev_name,ev_id);
+
+exit_label:
+  EVEL_EXIT();
+  return heartbeat;
+}
+
 /**************************************************************************//**
  * Create a new heartbeat event.
  *
@@ -151,6 +183,60 @@ void evel_init_header(EVENT_HEADER * const header,const char *const eventname)
   evel_force_option_string(&header->reporting_entity_id, openstack_vm_uuid());
   evel_force_option_string(&header->source_id, openstack_vm_uuid());
   evel_init_option_intheader(&header->internal_field);
+  dlist_initialize(&header->batch_events);
+
+  EVEL_EXIT();
+}
+
+
+/**************************************************************************//**
+ * Initialize a newly created event header.
+ *
+ * @param header  Pointer to the header being initialized.
+ * @param eventname Eventname string
+ * @param eventid   Event id : unique id for classification and analysis
+ * @param header  Pointer to the header being initialized.
+ *****************************************************************************/
+void evel_init_header_nameid(EVENT_HEADER * const header,const char *const eventname, const char *eventid)
+{
+  struct timeval tv;
+
+  EVEL_ENTER();
+
+  assert(header != NULL);
+  assert(eventname != NULL);
+  assert(eventid != NULL);
+
+  gettimeofday(&tv, NULL);
+
+  /***************************************************************************/
+  /* Initialize the header.  Get a new event sequence number.  Note that if  */
+  /* any memory allocation fails in here we will fail gracefully because     */
+  /* everything downstream can cope with NULLs.                              */
+  /***************************************************************************/
+  header->event_domain = EVEL_DOMAIN_HEARTBEAT;
+  header->event_id = strdup(eventid);
+  header->event_name = strdup(eventname);
+  header->last_epoch_microsec = tv.tv_usec + 1000000 * tv.tv_sec;
+  header->priority = EVEL_PRIORITY_NORMAL;
+  header->reporting_entity_name = strdup(openstack_vm_name());
+  header->source_name = strdup(openstack_vm_name());
+  header->sequence = event_sequence;
+  header->start_epoch_microsec = header->last_epoch_microsec;
+  header->major_version = EVEL_HEADER_MAJOR_VERSION;
+  header->minor_version = EVEL_HEADER_MINOR_VERSION;
+  event_sequence++;
+
+  /***************************************************************************/
+  /* Optional parameters.                                                    */
+  /***************************************************************************/
+  evel_init_option_string(&header->event_type);
+  evel_init_option_string(&header->nfcnaming_code);
+  evel_init_option_string(&header->nfnaming_code);
+  evel_force_option_string(&header->reporting_entity_id, openstack_vm_uuid());
+  evel_force_option_string(&header->source_id, openstack_vm_uuid());
+  evel_init_option_intheader(&header->internal_field);
+  dlist_initialize(&header->batch_events);
 
   EVEL_EXIT();
 }
@@ -424,6 +510,78 @@ void evel_free_header(EVENT_HEADER * const event)
   EVEL_EXIT();
 }
 
+
+/**************************************************************************//**
+ * Encode the event as a JSON event object according to AT&T's schema.
+ *
+ * @param json      Pointer to where to store the JSON encoded data.
+ * @param max_size  Size of storage available in json_body.
+ * @param event     Pointer to the ::EVENT_HEADER to encode.
+ * @returns Number of bytes actually written.
+ *****************************************************************************/
+void evel_json_encode_eventtype(
+                          EVEL_JSON_BUFFER * jbuf,
+                           EVENT_HEADER * event)
+{
+      switch (event->event_domain)
+      {
+        case EVEL_DOMAIN_HEARTBEAT:
+          evel_json_encode_header(jbuf, event);
+          break;
+
+        case EVEL_DOMAIN_FAULT:
+          evel_json_encode_fault(jbuf, (EVENT_FAULT *)event);
+          break;
+
+        case EVEL_DOMAIN_MEASUREMENT:
+          evel_json_encode_measurement(jbuf, (EVENT_MEASUREMENT *)event);
+          break;
+
+        case EVEL_DOMAIN_MOBILE_FLOW:
+          evel_json_encode_mobile_flow(jbuf, (EVENT_MOBILE_FLOW *)event);
+          break;
+
+        case EVEL_DOMAIN_REPORT:
+          evel_json_encode_report(jbuf, (EVENT_REPORT *)event);
+          break;
+
+        case EVEL_DOMAIN_HEARTBEAT_FIELD:
+          evel_json_encode_hrtbt_field(jbuf, (EVENT_HEARTBEAT_FIELD *)event);
+          break;
+
+        case EVEL_DOMAIN_SIPSIGNALING:
+          evel_json_encode_signaling(jbuf, (EVENT_SIGNALING *)event);
+          break;
+
+        case EVEL_DOMAIN_STATE_CHANGE:
+          evel_json_encode_state_change(jbuf, (EVENT_STATE_CHANGE *)event);
+          break;
+
+        case EVEL_DOMAIN_SYSLOG:
+          evel_json_encode_syslog(jbuf, (EVENT_SYSLOG *)event);
+          break;
+
+        case EVEL_DOMAIN_OTHER:
+          evel_json_encode_other(jbuf, (EVENT_OTHER *)event);
+          break;
+
+        case EVEL_DOMAIN_VOICE_QUALITY:
+          evel_json_encode_voice_quality(jbuf, (EVENT_VOICE_QUALITY *)event);
+          break;
+
+        case EVEL_DOMAIN_THRESHOLD_CROSS:
+          evel_json_encode_threshold_cross(jbuf, (EVENT_THRESHOLD_CROSS *)event);
+          break;
+
+        case EVEL_DOMAIN_INTERNAL:
+        default:
+          EVEL_ERROR("Unexpected domain %d", event->event_domain);
+          assert(0);
+      }
+}
+
+
+
 /**************************************************************************//**
  * Encode the event as a JSON event object according to AT&T's schema.
  *
@@ -454,57 +612,7 @@ int evel_json_encode_event(char * json,
   evel_json_open_object(jbuf);
   evel_json_open_named_object(jbuf, "event");
 
-  switch (event->event_domain)
-  {
-    case EVEL_DOMAIN_HEARTBEAT:
-      evel_json_encode_header(jbuf, event);
-      break;
-
-    case EVEL_DOMAIN_FAULT:
-      evel_json_encode_fault(jbuf, (EVENT_FAULT *)event);
-      break;
-
-    case EVEL_DOMAIN_MEASUREMENT:
-      evel_json_encode_measurement(jbuf, (EVENT_MEASUREMENT *)event);
-      break;
-
-    case EVEL_DOMAIN_MOBILE_FLOW:
-      evel_json_encode_mobile_flow(jbuf, (EVENT_MOBILE_FLOW *)event);
-      break;
-
-    case EVEL_DOMAIN_REPORT:
-      evel_json_encode_report(jbuf, (EVENT_REPORT *)event);
-      break;
-
-    case EVEL_DOMAIN_HEARTBEAT_FIELD:
-      evel_json_encode_hrtbt_field(jbuf, (EVENT_HEARTBEAT_FIELD *)event);
-      break;
-
-    case EVEL_DOMAIN_SIPSIGNALING:
-      evel_json_encode_signaling(jbuf, (EVENT_SIGNALING *)event);
-      break;
-
-    case EVEL_DOMAIN_STATE_CHANGE:
-      evel_json_encode_state_change(jbuf, (EVENT_STATE_CHANGE *)event);
-      break;
-
-    case EVEL_DOMAIN_SYSLOG:
-      evel_json_encode_syslog(jbuf, (EVENT_SYSLOG *)event);
-      break;
-
-    case EVEL_DOMAIN_OTHER:
-      evel_json_encode_other(jbuf, (EVENT_OTHER *)event);
-      break;
-
-    case EVEL_DOMAIN_VOICE_QUALITY:
-      evel_json_encode_other(jbuf, (EVENT_VOICE_QUALITY *)event);
-      break;
-
-    case EVEL_DOMAIN_INTERNAL:
-    default:
-      EVEL_ERROR("Unexpected domain %d", event->event_domain);
-      assert(0);
-  }
+  evel_json_encode_eventtype(jbuf, event);
 
   evel_json_close_object(jbuf);
   evel_json_close_object(jbuf);
@@ -518,6 +626,75 @@ int evel_json_encode_event(char * json,
 
   return jbuf->offset;
 }
+/**************************************************************************//**
+ * Encode the event as a JSON event object according to AT&T's schema.
+ *
+ * @param json      Pointer to where to store the JSON encoded data.
+ * @param max_size  Size of storage available in json_body.
+ * @param event     Pointer to the ::EVENT_HEADER to encode.
+ * @returns Number of bytes actually written.
+ *****************************************************************************/
+int evel_json_encode_batch_event(char * json,
+                           int max_size,
+                           EVENT_HEADER * event)
+{
+  EVEL_JSON_BUFFER json_buffer;
+  EVEL_JSON_BUFFER *jbuf = &json_buffer;
+  EVEL_THROTTLE_SPEC * throttle_spec;
+  int tot_size = 0;
+  EVENT_HEADER * batch_field = NULL;
+  DLIST_ITEM * batch_field_item = NULL;
+
+  EVEL_ENTER();
+
+  /***************************************************************************/
+  /* Get the latest throttle specification for the domain.                   */
+  /***************************************************************************/
+  throttle_spec = evel_get_throttle_spec(event->event_domain);
+
+  /***************************************************************************/
+  /* Initialize the JSON_BUFFER and open the top-level objects.              */
+  /***************************************************************************/
+  if (event->event_domain == EVEL_DOMAIN_BATCH){
+      evel_json_buffer_init(jbuf, json, max_size, throttle_spec);
+
+  if(dlist_count(&event->batch_events) > 0)
+  {
+    evel_json_open_object(jbuf);
+    evel_json_open_named_list(jbuf, "eventList");
+    batch_field_item = dlist_get_first(&event->batch_events);
+    while (batch_field_item != NULL)
+    {
+     batch_field = (EVENT_HEADER *) batch_field_item->item;
+     if(batch_field != NULL){
+       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);
+       evel_json_open_object(jbuf);
+       evel_json_encode_eventtype(jbuf, batch_field);
+       evel_json_close_object(jbuf);
+
+       tot_size += jbuf->offset;
+       EVEL_DEBUG("Batch Event result size %d offset %d depth %d check %d", tot_size,jbuf->offset,jbuf->depth,jbuf->checkpoint);
+       if( tot_size >= max_size ){
+          EVEL_ERROR("Batch Event exceeded size limit %d", tot_size);
+          assert(0);
+       }
+       batch_field_item = dlist_get_next(batch_field_item);
+     }
+    }
+    evel_json_close_list(jbuf);
+    evel_json_close_object(jbuf);
+  }
+
+  }
+  /***************************************************************************/
+  /* Sanity check.                                                           */
+  /***************************************************************************/
+  //assert(jbuf->depth == 0);
+
+  EVEL_EXIT();
+
+  return jbuf->offset;
+}
 
 
 /**************************************************************************//**
@@ -652,7 +829,7 @@ void evel_json_encode_vendor_field(EVEL_JSON_BUFFER * jbuf,
   assert(vfield != NULL);
   assert(vfield->vendorname != NULL);
 
-  evel_json_open_named_object(jbuf, "vendorVnfNamedFields");
+  evel_json_open_named_object(jbuf, "vendorVnfNameFields");
 
   /***************************************************************************/
   /* Mandatory fields.                                                       */