031632a3c75780675a78374e8036342a53a0c393
[demo.git] / vnfs / VES5.0 / evel / evel-library / code / evel_library / evel_heartbeat_fields.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 Heartbeat fields.
22  *
23  ****************************************************************************/
24
25 #include <string.h>
26 #include <assert.h>
27 #include <stdlib.h>
28
29 #include "evel.h"
30 #include "evel_throttle.h"
31
32 /**************************************************************************//**
33  * Create a new Heartbeat fields event.
34  *
35  * @note    The mandatory fields on the Heartbeat fields must be supplied to
36  *          this factory function and are immutable once set.  Optional fields
37  *          have explicit setter functions, but again values may only be set
38  *          once so that the event has immutable properties.
39  * @param event_name  Unique Event Name confirming Domain AsdcModel Description
40  * @param event_id    A universal identifier of the event for: troubleshooting correlation, analysis, etc
41  * @param vendor_id     The vendor id to encode in the event instance id.
42  * @param event_id      The vendor event id to encode in the event instance id.
43  * @returns pointer to the newly manufactured ::EVENT_HEARTBEAT_FIELD.  If the event
44  *          is not used (i.e. posted) it must be released using
45  *          ::evel_free_hrtbt_field.
46  * @retval  NULL  Failed to create the event.
47  *****************************************************************************/
48 EVENT_HEARTBEAT_FIELD * evel_new_heartbeat_field(int interval,const char* ev_name, const char *ev_id)
49 {
50   EVENT_HEARTBEAT_FIELD * event = NULL;
51
52   EVEL_ENTER();
53
54   /***************************************************************************/
55   /* Check preconditions.                                                    */
56   /***************************************************************************/
57   assert(interval > 0);
58
59   /***************************************************************************/
60   /* Allocate the Heartbeat fields event.                                           */
61   /***************************************************************************/
62   event = malloc(sizeof(EVENT_HEARTBEAT_FIELD));
63   if (event == NULL)
64   {
65     log_error_state("Out of memory");
66     goto exit_label;
67   }
68   memset(event, 0, sizeof(EVENT_HEARTBEAT_FIELD));
69   EVEL_DEBUG("New Heartbeat fields event is at %lp", event);
70
71   /***************************************************************************/
72   /* Initialize the header & the Heartbeat fields fields.                           */
73   /***************************************************************************/
74   evel_init_header_nameid(&event->header,ev_name,ev_id);
75   event->header.event_domain = EVEL_DOMAIN_HEARTBEAT_FIELD;
76   event->major_version = EVEL_HEARTBEAT_FIELD_MAJOR_VERSION;
77   event->minor_version = EVEL_HEARTBEAT_FIELD_MINOR_VERSION;
78
79   event->heartbeat_interval = interval;
80   dlist_initialize(&event->additional_info);
81
82 exit_label:
83
84   EVEL_EXIT();
85   return event;
86 }
87
88 /**************************************************************************//**
89  * Add a name/value pair to the Heartbeat fields, under the additionalFields array.
90  *
91  * The name and value are null delimited ASCII strings.  The library takes
92  * a copy so the caller does not have to preserve values after the function
93  * returns.
94  *
95  * @param event     Pointer to the Heartbeat fields event.
96  * @param name      ASCIIZ string with the field's name.  The caller does not
97  *                  need to preserve the value once the function returns.
98  * @param value     ASCIIZ string with the field's value.  The caller does not
99  *                  need to preserve the value once the function returns.
100  *****************************************************************************/
101 void evel_hrtbt_field_addl_field_add(EVENT_HEARTBEAT_FIELD * const event,
102                                  const char * const name,
103                                  const char * const value)
104 {
105   OTHER_FIELD * nv_pair = NULL;
106
107   EVEL_ENTER();
108
109   /***************************************************************************/
110   /* Check preconditions.                                                    */
111   /***************************************************************************/
112   assert(event != NULL);
113   assert(event->header.event_domain == EVEL_DOMAIN_HEARTBEAT_FIELD);
114   assert(name != NULL);
115   assert(value != NULL);
116
117   EVEL_DEBUG("Adding name=%s value=%s", name, value);
118   nv_pair = malloc(sizeof(OTHER_FIELD));
119   assert(nv_pair != NULL);
120   nv_pair->name = strdup(name);
121   nv_pair->value = strdup(value);
122   assert(nv_pair->name != NULL);
123   assert(nv_pair->value != NULL);
124
125   dlist_push_last(&event->additional_info, nv_pair);
126
127   EVEL_EXIT();
128 }
129
130 /**************************************************************************//**
131  * Set the Interval property of the Heartbeat fields event.
132  *
133  * @note  The property is treated as immutable: it is only valid to call
134  *        the setter once.  However, we don't assert if the caller tries to
135  *        overwrite, just ignoring the update instead.
136  *
137  * @param event         Pointer to the Heartbeat fields event.
138  * @param product_id    The vendor product id to be set. ASCIIZ string. The
139  *                      caller does not need to preserve the value once the
140  *                      function returns.
141  *****************************************************************************/
142 void evel_hrtbt_interval_set(EVENT_HEARTBEAT_FIELD * const event,
143                                  const int interval)
144 {
145   EVEL_ENTER();
146
147   /***************************************************************************/
148   /* Check preconditions and call evel_set_option_string.                    */
149   /***************************************************************************/
150   assert(event != NULL);
151   assert(event->header.event_domain == EVEL_DOMAIN_HEARTBEAT_FIELD);
152   assert(interval > 0);
153
154   event->heartbeat_interval = interval;
155   EVEL_EXIT();
156 }
157
158
159 /**************************************************************************//**
160  * Encode the Heartbeat fields in JSON according to AT&T's schema for the
161  * event type.
162  *
163  * @param jbuf          Pointer to the ::EVEL_JSON_BUFFER to encode into.
164  * @param event         Pointer to the ::EVENT_HEADER to encode.
165  *****************************************************************************/
166 void evel_json_encode_hrtbt_field(EVEL_JSON_BUFFER * const jbuf,
167                                 EVENT_HEARTBEAT_FIELD * const event)
168 {
169   OTHER_FIELD * nv_pair = NULL;
170   DLIST_ITEM * dlist_item = NULL;
171
172   EVEL_ENTER();
173
174   /***************************************************************************/
175   /* Check preconditions.                                                    */
176   /***************************************************************************/
177   assert(event != NULL);
178   assert(event->header.event_domain == EVEL_DOMAIN_HEARTBEAT_FIELD);
179
180   evel_json_encode_header(jbuf, &event->header);
181   evel_json_open_named_object(jbuf, "heartbeatField");
182
183   /***************************************************************************/
184   /* Mandatory fields                                                        */
185   /***************************************************************************/
186   evel_enc_version(jbuf, "heartbeatFieldsVersion", event->major_version,event->minor_version);
187   evel_enc_kv_int(jbuf, "heartbeatInterval", event->heartbeat_interval);
188
189   /***************************************************************************/
190   /* Optional fields                                                         */
191   /***************************************************************************/
192
193   /***************************************************************************/
194   /* Checkpoint, so that we can wind back if all fields are suppressed.      */
195   /***************************************************************************/
196   evel_json_checkpoint(jbuf);
197   if (evel_json_open_opt_named_list(jbuf, "additionalFields"))
198   {
199     bool added = false;
200
201     dlist_item = dlist_get_first(&event->additional_info);
202     while (dlist_item != NULL)
203     {
204       nv_pair = (OTHER_FIELD *) dlist_item->item;
205       assert(nv_pair != NULL);
206
207       if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
208                                           "additionalFields",
209                                           nv_pair->name))
210       {
211         evel_json_open_object(jbuf);
212         evel_enc_kv_string(jbuf, "name", nv_pair->name);
213         evel_enc_kv_string(jbuf, "value", nv_pair->value);
214         evel_json_close_object(jbuf);
215         added = true;
216       }
217       dlist_item = dlist_get_next(dlist_item);
218     }
219     evel_json_close_list(jbuf);
220
221     /*************************************************************************/
222     /* If we've not written anything, rewind to before we opened the list.   */
223     /*************************************************************************/
224     if (!added)
225     {
226       evel_json_rewind(jbuf);
227     }
228   }
229
230   evel_json_close_object(jbuf);
231
232   EVEL_EXIT();
233 }
234
235 /**************************************************************************//**
236  * Free a Heartbeat fields event.
237  *
238  * Free off the event supplied.  Will free all the contained allocated memory.
239  *
240  * @note It does not free the event itself, since that may be part of a larger
241  * structure.
242  *****************************************************************************/
243 void evel_free_hrtbt_field(EVENT_HEARTBEAT_FIELD * const event)
244 {
245   OTHER_FIELD * nv_pair = NULL;
246
247   EVEL_ENTER();
248
249   /***************************************************************************/
250   /* Check preconditions.                                                    */
251   /***************************************************************************/
252   assert(event != NULL);
253   assert(event->header.event_domain == EVEL_DOMAIN_HEARTBEAT_FIELD);
254
255   /***************************************************************************/
256   /* Free all internal strings then the header itself.                       */
257   /***************************************************************************/
258   nv_pair = dlist_pop_last(&event->additional_info);
259   while (nv_pair != NULL)
260   {
261     EVEL_DEBUG("Freeing Other Field (%s, %s)", nv_pair->name, nv_pair->value);
262     free(nv_pair->name);
263     free(nv_pair->value);
264     free(nv_pair);
265     nv_pair = dlist_pop_last(&event->additional_info);
266   }
267   evel_free_header(&event->header);
268
269   EVEL_EXIT();
270 }