38b07a74030ce4b4399c4fd8c3065775184fc445
[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  * 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   fault->event_source_type = ev_source_type;
96   fault->vf_status = status;
97   fault->alarm_condition = strdup(condition);
98   fault->specific_problem = strdup(specific_problem);
99   evel_init_option_string(&fault->category);
100   evel_init_option_string(&fault->alarm_interface_a);
101   dlist_initialize(&fault->additional_info);
102
103 exit_label:
104   EVEL_EXIT();
105   return fault;
106 }
107
108 /**************************************************************************//**
109  * Add an additional value name/value pair to the Fault.
110  *
111  * The name and value are null delimited ASCII strings.  The library takes
112  * a copy so the caller does not have to preserve values after the function
113  * returns.
114  *
115  * @param fault     Pointer to the fault.
116  * @param name      ASCIIZ string with the attribute's name.  The caller
117  *                  does not need to preserve the value once the function
118  *                  returns.
119  * @param value     ASCIIZ string with the attribute's value.  The caller
120  *                  does not need to preserve the value once the function
121  *                  returns.
122  *****************************************************************************/
123 void evel_fault_addl_info_add(EVENT_FAULT * fault, char * name, char * value)
124 {
125   FAULT_ADDL_INFO * addl_info = NULL;
126   EVEL_ENTER();
127
128   /***************************************************************************/
129   /* Check preconditions.                                                    */
130   /***************************************************************************/
131   assert(fault != NULL);
132   assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
133   assert(name != NULL);
134   assert(value != NULL);
135
136   EVEL_DEBUG("Adding name=%s value=%s", name, value);
137   addl_info = malloc(sizeof(FAULT_ADDL_INFO));
138   assert(addl_info != NULL);
139   memset(addl_info, 0, sizeof(FAULT_ADDL_INFO));
140   addl_info->name = strdup(name);
141   addl_info->value = strdup(value);
142   assert(addl_info->name != NULL);
143   assert(addl_info->value != NULL);
144
145   dlist_push_last(&fault->additional_info, addl_info);
146
147   EVEL_EXIT();
148 }
149
150 /**************************************************************************//**
151  * Set the Fault Category property of the Fault.
152  *
153  * @note  The property is treated as immutable: it is only valid to call
154  *        the setter once.  However, we don't assert if the caller tries to
155  *        overwrite, just ignoring the update instead.
156  *
157  * @param fault      Pointer to the fault.
158  * @param category   Category : license, link, routing, security, signaling.
159  *                       ASCIIZ string. The caller
160  *                   does not need to preserve the value once the function
161  *                   returns.
162  *****************************************************************************/
163 void evel_fault_category_set(EVENT_FAULT * fault,
164                               const char * const category)
165 {
166   EVEL_ENTER();
167
168   /***************************************************************************/
169   /* Check preconditions.                                                    */
170   /***************************************************************************/
171   assert(fault != NULL);
172   assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
173   assert(category != NULL);
174
175   evel_set_option_string(&fault->category,
176                          category,
177                          "Fault Category set");
178   EVEL_EXIT();
179 }
180
181 /**************************************************************************//**
182  * Set the Alarm Interface A property of the Fault.
183  *
184  * @note  The property is treated as immutable: it is only valid to call
185  *        the setter once.  However, we don't assert if the caller tries to
186  *        overwrite, just ignoring the update instead.
187  *
188  * @param fault      Pointer to the fault.
189  * @param interface  The Alarm Interface A to be set. ASCIIZ string. The caller
190  *                   does not need to preserve the value once the function
191  *                   returns.
192  *****************************************************************************/
193 void evel_fault_interface_set(EVENT_FAULT * fault,
194                               const char * const interface)
195 {
196   EVEL_ENTER();
197
198   /***************************************************************************/
199   /* Check preconditions.                                                    */
200   /***************************************************************************/
201   assert(fault != NULL);
202   assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
203   assert(interface != NULL);
204
205   evel_set_option_string(&fault->alarm_interface_a,
206                          interface,
207                          "Alarm Interface A");
208   EVEL_EXIT();
209 }
210
211 /**************************************************************************//**
212  * Set the Event Type property of the Fault.
213  *
214  * @note  The property is treated as immutable: it is only valid to call
215  *        the setter once.  However, we don't assert if the caller tries to
216  *        overwrite, just ignoring the update instead.
217  *
218  * @param fault      Pointer to the fault.
219  * @param type       The Event Type to be set. ASCIIZ string. The caller
220  *                   does not need to preserve the value once the function
221  *                   returns.
222  *****************************************************************************/
223 void evel_fault_type_set(EVENT_FAULT * fault, const char * const type)
224 {
225   EVEL_ENTER();
226
227   /***************************************************************************/
228   /* Check preconditions and call evel_header_type_set.                      */
229   /***************************************************************************/
230   assert(fault != NULL);
231   assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
232   evel_header_type_set(&fault->header, type);
233
234   EVEL_EXIT();
235 }
236
237 /**************************************************************************//**
238  * Encode the fault in JSON according to AT&T's schema for the fault type.
239  *
240  * @param jbuf          Pointer to the ::EVEL_JSON_BUFFER to encode into.
241  * @param event         Pointer to the ::EVENT_HEADER to encode.
242  *****************************************************************************/
243 void evel_json_encode_fault(EVEL_JSON_BUFFER * jbuf,
244                             EVENT_FAULT * event)
245 {
246   FAULT_ADDL_INFO * addl_info = NULL;
247   DLIST_ITEM * addl_info_item = NULL;
248   char * fault_severity;
249   char * fault_source_type;
250   char * fault_vf_status;
251
252   EVEL_ENTER();
253
254   /***************************************************************************/
255   /* Check preconditions.                                                    */
256   /***************************************************************************/
257   assert(event != NULL);
258   assert(event->header.event_domain == EVEL_DOMAIN_FAULT);
259
260   fault_severity = evel_severity(event->event_severity);
261   fault_source_type = evel_source_type(event->event_source_type);
262   fault_vf_status = evel_vf_status(event->vf_status);
263
264   evel_json_encode_header(jbuf, &event->header);
265   evel_json_open_named_object(jbuf, "faultFields");
266
267   /***************************************************************************/
268   /* Mandatory fields.                                                       */
269   /***************************************************************************/
270   evel_enc_kv_string(jbuf, "alarmCondition", event->alarm_condition);
271   evel_enc_kv_opt_string(jbuf, "eventCategory", &event->category);
272   evel_enc_kv_string(jbuf, "eventSeverity", fault_severity);
273   evel_enc_kv_string(jbuf, "eventSourceType", fault_source_type);
274   evel_enc_kv_string(jbuf, "specificProblem", event->specific_problem);
275   evel_enc_kv_string(jbuf, "vfStatus", fault_vf_status);
276   evel_enc_version(
277     jbuf, "faultFieldsVersion", event->major_version, event->minor_version);
278
279   /***************************************************************************/
280   /* Optional fields.                                                        */
281   /***************************************************************************/
282
283   /***************************************************************************/
284   /* Checkpoint, so that we can wind back if all fields are suppressed.      */
285   /***************************************************************************/
286   evel_json_checkpoint(jbuf);
287   if (evel_json_open_opt_named_list(jbuf, "alarmAdditionalInformation"))
288   {
289     bool item_added = false;
290
291     addl_info_item = dlist_get_first(&event->additional_info);
292     while (addl_info_item != NULL)
293     {
294       addl_info = (FAULT_ADDL_INFO*) addl_info_item->item;
295       assert(addl_info != NULL);
296
297       if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
298                                           "alarmAdditionalInformation",
299                                           addl_info->name))
300       {
301         evel_json_open_object(jbuf);
302         evel_enc_kv_string(jbuf, "name", addl_info->name);
303         evel_enc_kv_string(jbuf, "value", addl_info->value);
304         evel_json_close_object(jbuf);
305         item_added = true;
306       }
307       addl_info_item = dlist_get_next(addl_info_item);
308     }
309     evel_json_close_list(jbuf);
310
311     /*************************************************************************/
312     /* If we've not written anything, rewind to before we opened the list.   */
313     /*************************************************************************/
314     if (!item_added)
315     {
316       evel_json_rewind(jbuf);
317     }
318   }
319   evel_enc_kv_opt_string(jbuf, "alarmInterfaceA", &event->alarm_interface_a);
320
321   evel_json_close_object(jbuf);
322
323   EVEL_EXIT();
324 }
325
326 /**************************************************************************//**
327  * Free a Fault.
328  *
329  * Free off the Fault supplied.  Will free all the contained allocated memory.
330  *
331  * @note It does not free the Fault itself, since that may be part of a
332  * larger structure.
333  *****************************************************************************/
334 void evel_free_fault(EVENT_FAULT * event)
335 {
336   FAULT_ADDL_INFO * addl_info = NULL;
337
338   EVEL_ENTER();
339
340   /***************************************************************************/
341   /* Check preconditions.  As an internal API we don't allow freeing NULL    */
342   /* events as we do on the public API.                                      */
343   /***************************************************************************/
344   assert(event != NULL);
345   assert(event->header.event_domain == EVEL_DOMAIN_FAULT);
346
347   /***************************************************************************/
348   /* Free all internal strings then the header itself.                       */
349   /***************************************************************************/
350   addl_info = dlist_pop_last(&event->additional_info);
351   while (addl_info != NULL)
352   {
353     EVEL_DEBUG("Freeing Additional Info (%s, %s)",
354                addl_info->name,
355                addl_info->value);
356     free(addl_info->name);
357     free(addl_info->value);
358     free(addl_info);
359     addl_info = dlist_pop_last(&event->additional_info);
360   }
361   free(event->alarm_condition);
362   free(event->specific_problem);
363   evel_free_option_string(&event->category);
364   evel_free_option_string(&event->alarm_interface_a);
365   evel_free_header(&event->header);
366
367   EVEL_EXIT();
368 }