Initial VES for DANOS vRouter
[demo.git] / vnfs / VESreporting_vFW5.0_DANOS / evel / evel-library / code / evel_library / evel_fault.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 the Fault.
22  *
23  ****************************************************************************/
24
25 #include <string.h>
26 #include <assert.h>
27 #include <stdlib.h>
28
29 #include "evel.h"
30 #include "evel_internal.h"
31 #include "evel_throttle.h"
32
33 /**************************************************************************//**
34  * Create a new fault event.
35  *
36  * @note    The mandatory fields on the Fault must be supplied to this factory
37  *          function and are immutable once set.  Optional fields have explicit
38  *          setter functions, but again values may only be set once so that the
39  *          Fault has immutable properties.
40  * @param event_name  Unique Event Name confirming Domain AsdcModel Description
41  * @param event_id    A universal identifier of the event for: troubleshooting correlation, analysis, etc
42  * @param   condition   The condition indicated by the Fault.
43  * @param   specific_problem  The specific problem triggering the fault.
44  * @param   priority    The priority of the event.
45  * @param   severity    The severity of the Fault.
46  * @param   ev_source_type    Source of Alarm event
47  * @param   version     fault version
48  * @param   status      status of Virtual Function
49  * @returns pointer to the newly manufactured ::EVENT_FAULT.  If the event is
50  *          not used (i.e. posted) it must be released using ::evel_free_fault.
51  * @retval  NULL  Failed to create the event.
52  *****************************************************************************/
53 EVENT_FAULT * evel_new_fault(const char * ev_name,
54                              const char * ev_id,
55                              const char * const condition,
56                              const char * const specific_problem,
57                              EVEL_EVENT_PRIORITIES priority,
58                              EVEL_SEVERITIES severity,
59                              EVEL_SOURCE_TYPES ev_source_type,
60                              EVEL_VF_STATUSES status)
61 {
62   EVENT_FAULT * fault = NULL;
63   EVEL_ENTER();
64
65   /***************************************************************************/
66   /* Check preconditions.                                                    */
67   /***************************************************************************/
68   assert(condition != NULL);
69   assert(specific_problem != NULL);
70   assert(priority < EVEL_MAX_PRIORITIES);
71   assert(severity < EVEL_MAX_SEVERITIES);
72
73   /***************************************************************************/
74   /* Allocate the fault.                                                     */
75   /***************************************************************************/
76   fault = malloc(sizeof(EVENT_FAULT));
77   if (fault == NULL)
78   {
79     log_error_state("Out of memory");
80     goto exit_label;
81   }
82   memset(fault, 0, sizeof(EVENT_FAULT));
83   EVEL_DEBUG("New fault is at %lp", fault);
84
85   /***************************************************************************/
86   /* Initialize the header & the fault fields.  Optional string values are   */
87   /* uninitialized (NULL).                                                   */
88   /***************************************************************************/
89   evel_init_header_nameid(&fault->header,ev_name,ev_id);
90   fault->header.event_domain = EVEL_DOMAIN_FAULT;
91   fault->header.priority = priority;
92   fault->major_version = EVEL_FAULT_MAJOR_VERSION;
93   fault->minor_version = EVEL_FAULT_MINOR_VERSION;
94   fault->event_severity = severity;
95   if( severity == EVEL_SEVERITY_NORMAL )
96      evel_event_sequence_set(&fault->header,0);
97   else
98      evel_event_sequence_set(&fault->header,1);
99   fault->event_source_type = ev_source_type;
100   fault->vf_status = status;
101   fault->alarm_condition = strdup(condition);
102   fault->specific_problem = strdup(specific_problem);
103   evel_init_option_string(&fault->category);
104   evel_init_option_string(&fault->alarm_interface_a);
105   dlist_initialize(&fault->additional_info);
106
107 exit_label:
108   EVEL_EXIT();
109   return fault;
110 }
111
112 /**************************************************************************//**
113  * Add an additional value name/value pair to the Fault.
114  *
115  * The name and value are null delimited ASCII strings.  The library takes
116  * a copy so the caller does not have to preserve values after the function
117  * returns.
118  *
119  * @param fault     Pointer to the fault.
120  * @param name      ASCIIZ string with the attribute's name.  The caller
121  *                  does not need to preserve the value once the function
122  *                  returns.
123  * @param value     ASCIIZ string with the attribute's value.  The caller
124  *                  does not need to preserve the value once the function
125  *                  returns.
126  *****************************************************************************/
127 void evel_fault_addl_info_add(EVENT_FAULT * fault, char * name, char * value)
128 {
129   FAULT_ADDL_INFO * addl_info = NULL;
130   EVEL_ENTER();
131
132   /***************************************************************************/
133   /* Check preconditions.                                                    */
134   /***************************************************************************/
135   assert(fault != NULL);
136   assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
137   assert(name != NULL);
138   assert(value != NULL);
139
140   EVEL_DEBUG("Adding name=%s value=%s", name, value);
141   addl_info = malloc(sizeof(FAULT_ADDL_INFO));
142   assert(addl_info != NULL);
143   memset(addl_info, 0, sizeof(FAULT_ADDL_INFO));
144   addl_info->name = strdup(name);
145   addl_info->value = strdup(value);
146   assert(addl_info->name != NULL);
147   assert(addl_info->value != NULL);
148
149   dlist_push_last(&fault->additional_info, addl_info);
150
151   EVEL_EXIT();
152 }
153
154 /**************************************************************************//**
155  * Set the Fault Category property of the Fault.
156  *
157  * @note  The property is treated as immutable: it is only valid to call
158  *        the setter once.  However, we don't assert if the caller tries to
159  *        overwrite, just ignoring the update instead.
160  *
161  * @param fault      Pointer to the fault.
162  * @param category   Category : license, link, routing, security, signaling.
163  *                       ASCIIZ string. The caller
164  *                   does not need to preserve the value once the function
165  *                   returns.
166  *****************************************************************************/
167 void evel_fault_category_set(EVENT_FAULT * fault,
168                               const char * const category)
169 {
170   EVEL_ENTER();
171
172   /***************************************************************************/
173   /* Check preconditions.                                                    */
174   /***************************************************************************/
175   assert(fault != NULL);
176   assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
177   assert(category != NULL);
178
179   evel_set_option_string(&fault->category,
180                          category,
181                          "Fault Category set");
182   EVEL_EXIT();
183 }
184
185 /**************************************************************************//**
186  * Set the Alarm Interface A property of the Fault.
187  *
188  * @note  The property is treated as immutable: it is only valid to call
189  *        the setter once.  However, we don't assert if the caller tries to
190  *        overwrite, just ignoring the update instead.
191  *
192  * @param fault      Pointer to the fault.
193  * @param interface  The Alarm Interface A to be set. ASCIIZ string. The caller
194  *                   does not need to preserve the value once the function
195  *                   returns.
196  *****************************************************************************/
197 void evel_fault_interface_set(EVENT_FAULT * fault,
198                               const char * const interface)
199 {
200   EVEL_ENTER();
201
202   /***************************************************************************/
203   /* Check preconditions.                                                    */
204   /***************************************************************************/
205   assert(fault != NULL);
206   assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
207   assert(interface != NULL);
208
209   evel_set_option_string(&fault->alarm_interface_a,
210                          interface,
211                          "Alarm Interface A");
212   EVEL_EXIT();
213 }
214
215 /**************************************************************************//**
216  * Set the Event Type property of the Fault.
217  *
218  * @note  The property is treated as immutable: it is only valid to call
219  *        the setter once.  However, we don't assert if the caller tries to
220  *        overwrite, just ignoring the update instead.
221  *
222  * @param fault      Pointer to the fault.
223  * @param type       The Event Type to be set. ASCIIZ string. The caller
224  *                   does not need to preserve the value once the function
225  *                   returns.
226  *****************************************************************************/
227 void evel_fault_type_set(EVENT_FAULT * fault, const char * const type)
228 {
229   EVEL_ENTER();
230
231   /***************************************************************************/
232   /* Check preconditions and call evel_header_type_set.                      */
233   /***************************************************************************/
234   assert(fault != NULL);
235   assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
236   evel_header_type_set(&fault->header, type);
237
238   EVEL_EXIT();
239 }
240
241 /**************************************************************************//**
242  * Encode the fault in JSON according to AT&T's schema for the fault type.
243  *
244  * @param jbuf          Pointer to the ::EVEL_JSON_BUFFER to encode into.
245  * @param event         Pointer to the ::EVENT_HEADER to encode.
246  *****************************************************************************/
247 void evel_json_encode_fault(EVEL_JSON_BUFFER * jbuf,
248                             EVENT_FAULT * event)
249 {
250   FAULT_ADDL_INFO * addl_info = NULL;
251   DLIST_ITEM * addl_info_item = NULL;
252   char * fault_severity;
253   char * fault_source_type;
254   char * fault_vf_status;
255
256   EVEL_ENTER();
257
258   /***************************************************************************/
259   /* Check preconditions.                                                    */
260   /***************************************************************************/
261   assert(event != NULL);
262   assert(event->header.event_domain == EVEL_DOMAIN_FAULT);
263
264   fault_severity = evel_severity(event->event_severity);
265   fault_source_type = evel_source_type(event->event_source_type);
266   fault_vf_status = evel_vf_status(event->vf_status);
267
268   evel_json_encode_header(jbuf, &event->header);
269   evel_json_open_named_object(jbuf, "faultFields");
270
271   /***************************************************************************/
272   /* Mandatory fields.                                                       */
273   /***************************************************************************/
274   evel_enc_kv_string(jbuf, "alarmCondition", event->alarm_condition);
275   evel_enc_kv_opt_string(jbuf, "eventCategory", &event->category);
276   evel_enc_kv_string(jbuf, "eventSeverity", fault_severity);
277   evel_enc_kv_string(jbuf, "eventSourceType", fault_source_type);
278   evel_enc_kv_string(jbuf, "specificProblem", event->specific_problem);
279   evel_enc_kv_string(jbuf, "vfStatus", fault_vf_status);
280   evel_enc_version(
281     jbuf, "faultFieldsVersion", event->major_version, event->minor_version);
282
283   /***************************************************************************/
284   /* Optional fields.                                                        */
285   /***************************************************************************/
286
287   /***************************************************************************/
288   /* Checkpoint, so that we can wind back if all fields are suppressed.      */
289   /***************************************************************************/
290   evel_json_checkpoint(jbuf);
291   if (evel_json_open_opt_named_list(jbuf, "alarmAdditionalInformation"))
292   {
293     bool item_added = false;
294
295     addl_info_item = dlist_get_first(&event->additional_info);
296     while (addl_info_item != NULL)
297     {
298       addl_info = (FAULT_ADDL_INFO*) addl_info_item->item;
299       assert(addl_info != NULL);
300
301       if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
302                                           "alarmAdditionalInformation",
303                                           addl_info->name))
304       {
305         evel_json_open_object(jbuf);
306         evel_enc_kv_string(jbuf, "name", addl_info->name);
307         evel_enc_kv_string(jbuf, "value", addl_info->value);
308         evel_json_close_object(jbuf);
309         item_added = true;
310       }
311       addl_info_item = dlist_get_next(addl_info_item);
312     }
313     evel_json_close_list(jbuf);
314
315     /*************************************************************************/
316     /* If we've not written anything, rewind to before we opened the list.   */
317     /*************************************************************************/
318     if (!item_added)
319     {
320       evel_json_rewind(jbuf);
321     }
322   }
323   evel_enc_kv_opt_string(jbuf, "alarmInterfaceA", &event->alarm_interface_a);
324
325   evel_json_close_object(jbuf);
326
327   EVEL_EXIT();
328 }
329
330 /**************************************************************************//**
331  * Free a Fault.
332  *
333  * Free off the Fault supplied.  Will free all the contained allocated memory.
334  *
335  * @note It does not free the Fault itself, since that may be part of a
336  * larger structure.
337  *****************************************************************************/
338 void evel_free_fault(EVENT_FAULT * event)
339 {
340   FAULT_ADDL_INFO * addl_info = NULL;
341
342   EVEL_ENTER();
343
344   /***************************************************************************/
345   /* Check preconditions.  As an internal API we don't allow freeing NULL    */
346   /* events as we do on the public API.                                      */
347   /***************************************************************************/
348   assert(event != NULL);
349   assert(event->header.event_domain == EVEL_DOMAIN_FAULT);
350
351   /***************************************************************************/
352   /* Free all internal strings then the header itself.                       */
353   /***************************************************************************/
354   addl_info = dlist_pop_last(&event->additional_info);
355   while (addl_info != NULL)
356   {
357     EVEL_DEBUG("Freeing Additional Info (%s, %s)",
358                addl_info->name,
359                addl_info->value);
360     free(addl_info->name);
361     free(addl_info->value);
362     free(addl_info);
363     addl_info = dlist_pop_last(&event->additional_info);
364   }
365   free(event->alarm_condition);
366   free(event->specific_problem);
367   evel_free_option_string(&event->category);
368   evel_free_option_string(&event->alarm_interface_a);
369   evel_free_header(&event->header);
370
371   EVEL_EXIT();
372 }