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