Initial VES for DANOS vRouter
[demo.git] / vnfs / VESreporting_vFW5.0_DANOS / evel / evel-library / code / evel_library / evel_batch.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  *
17  ****************************************************************************/
18
19 /**************************************************************************//**
20  * @file
21  * Source module implementing EVEL Batch API.
22  *
23  * This file implements the EVEL Batch API which is intended to provide a
24  * simple wrapper around packaging multiple EVEL messages into single HTTP(S) package
25  * This is implemented per VES 5.3 standards. Currently max size of package is 160K
26  *
27  ****************************************************************************/
28
29 #include <string.h>
30 #include <assert.h>
31 #include <stdlib.h>
32
33 #include "evel.h"
34 #include "evel_internal.h"
35
36 /**************************************************************************//**
37  * Create a new empty Batch event.
38  *
39  * @note    The mandatory fields on the Batch must be supplied to this factory
40  *          function and are immutable once set.  Optional fields have explicit
41  *          setter functions, but again values may only be set once so that the
42  *          Batch has immutable properties.
43  * @params  Event name and Event id are dummy strings. Not encoded into JSON
44  * @returns pointer to the newly manufactured ::EVENT_HEADER.  If the event is
45  *          not used (i.e. posted) it must be released using ::evel_free_batch.
46  * @retval  NULL  Failed to create the event.
47  *****************************************************************************/
48 EVENT_HEADER * evel_new_batch(const char* ev_name, const char *ev_id)
49 {
50   EVENT_HEADER * other = NULL;
51   EVEL_ENTER();
52
53   /***************************************************************************/
54   /* Check preconditions.                                                    */
55   /***************************************************************************/
56
57   /***************************************************************************/
58   /* Allocate the Batch.                                                     */
59   /***************************************************************************/
60   other = malloc(sizeof(EVENT_HEADER));
61   if (other == NULL)
62   {
63     log_error_state("Out of memory");
64     goto exit_label;
65   }
66   memset(other, 0, sizeof(EVENT_HEADER));
67   EVEL_DEBUG("New Batch is at %lp", other);
68
69   /***************************************************************************/
70   /* Initialize the header & the Batch fields.  Optional string values are   */
71   /* uninitialized (NULL).                                                   */
72   /***************************************************************************/
73   evel_init_header_nameid(other,ev_name,ev_id);
74   other->event_domain = EVEL_DOMAIN_BATCH;
75   other->major_version = EVEL_BATCH_MAJOR_VERSION;
76   other->minor_version = EVEL_BATCH_MINOR_VERSION;
77
78   dlist_initialize(&other->batch_events);
79
80 exit_label:
81   EVEL_EXIT();
82   return other;
83 }
84
85
86 /**************************************************************************//**
87  * Add an additional VES Message into Batch Event
88  *
89  * The function may be called as many times without reaching 160K max json size
90  * limit.
91  * The max limit is only checked at encoding time and error generated
92  *
93  * @param batchev     Pointer to  already created new Batch Event.
94  * @param child       Pointer to  additional VES Event
95  *****************************************************************************/
96 void evel_batch_add_event(EVENT_HEADER * batchev, EVENT_HEADER *child)
97 {
98   EVEL_ENTER();
99
100   /***************************************************************************/
101   /* Check preconditions.                                                    */
102   /***************************************************************************/
103   assert(batchev != NULL);
104   assert(batchev->event_domain == EVEL_DOMAIN_BATCH);
105   assert(child != NULL);
106
107   EVEL_DEBUG("Adding Batch event");
108
109   dlist_push_last(&batchev->batch_events, child);
110
111   EVEL_EXIT();
112 }
113
114
115 /**************************************************************************//**
116  * Free a Batch Event.
117  *
118  * Free off the Batch supplied.  Will free all the contained VES message memory.
119  *
120  * @note It does not free the Batch itself, since that may be part of a
121  * larger structure.
122  *****************************************************************************/
123 void evel_free_batch(EVENT_HEADER * event)
124 {
125   EVENT_HEADER * batch_field = NULL;
126
127   EVEL_ENTER();
128
129   /***************************************************************************/
130   /* Check preconditions.  As an internal API we don't allow freeing NULL    */
131   /* events as we do on the public API.                                      */
132   /***************************************************************************/
133   assert(event != NULL);
134   assert(event->event_domain == EVEL_DOMAIN_BATCH);
135
136   /***************************************************************************/
137   /* Free all internal strings then the header itself.                       */
138   /***************************************************************************/
139   batch_field = dlist_pop_last(&event->batch_events);
140   while (batch_field != NULL)
141   {
142     EVEL_DEBUG("Freeing Batch Event (%s, %s)",
143                batch_field->event_name,
144                batch_field->event_id);
145     evel_free_event(batch_field);
146     batch_field = dlist_pop_last(&event->batch_events);
147   }
148   evel_free_header(event); 
149
150   EVEL_EXIT();
151 }