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