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