1 /*************************************************************************//**
3 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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
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 ****************************************************************************/
19 /**************************************************************************//**
21 * Implementation of EVEL functions relating to the Fault.
23 ****************************************************************************/
30 #include "evel_internal.h"
31 #include "evel_throttle.h"
33 /**************************************************************************//**
34 * Create a new fault event.
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,
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)
62 EVENT_FAULT * fault = NULL;
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);
73 /***************************************************************************/
74 /* Allocate the fault. */
75 /***************************************************************************/
76 fault = malloc(sizeof(EVENT_FAULT));
79 log_error_state("Out of memory");
82 memset(fault, 0, sizeof(EVENT_FAULT));
83 EVEL_DEBUG("New fault is at %lp", fault);
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);
108 /**************************************************************************//**
109 * Add an additional value name/value pair to the Fault.
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
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
119 * @param value ASCIIZ string with the attribute's value. The caller
120 * does not need to preserve the value once the function
122 *****************************************************************************/
123 void evel_fault_addl_info_add(EVENT_FAULT * fault, char * name, char * value)
125 FAULT_ADDL_INFO * addl_info = NULL;
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);
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);
145 dlist_push_last(&fault->additional_info, addl_info);
150 /**************************************************************************//**
151 * Set the Fault Category property of the Fault.
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.
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
162 *****************************************************************************/
163 void evel_fault_category_set(EVENT_FAULT * fault,
164 const char * const category)
168 /***************************************************************************/
169 /* Check preconditions. */
170 /***************************************************************************/
171 assert(fault != NULL);
172 assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
173 assert(category != NULL);
175 evel_set_option_string(&fault->category,
177 "Fault Category set");
181 /**************************************************************************//**
182 * Set the Alarm Interface A property of the Fault.
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.
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
192 *****************************************************************************/
193 void evel_fault_interface_set(EVENT_FAULT * fault,
194 const char * const interface)
198 /***************************************************************************/
199 /* Check preconditions. */
200 /***************************************************************************/
201 assert(fault != NULL);
202 assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
203 assert(interface != NULL);
205 evel_set_option_string(&fault->alarm_interface_a,
207 "Alarm Interface A");
211 /**************************************************************************//**
212 * Set the Event Type property of the Fault.
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.
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
222 *****************************************************************************/
223 void evel_fault_type_set(EVENT_FAULT * fault, const char * const type)
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);
237 /**************************************************************************//**
238 * Encode the fault in JSON according to AT&T's schema for the fault type.
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,
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;
254 /***************************************************************************/
255 /* Check preconditions. */
256 /***************************************************************************/
257 assert(event != NULL);
258 assert(event->header.event_domain == EVEL_DOMAIN_FAULT);
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);
264 evel_json_encode_header(jbuf, &event->header);
265 evel_json_open_named_object(jbuf, "faultFields");
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);
277 jbuf, "faultFieldsVersion", event->major_version, event->minor_version);
279 /***************************************************************************/
280 /* Optional fields. */
281 /***************************************************************************/
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"))
289 bool item_added = false;
291 addl_info_item = dlist_get_first(&event->additional_info);
292 while (addl_info_item != NULL)
294 addl_info = (FAULT_ADDL_INFO*) addl_info_item->item;
295 assert(addl_info != NULL);
297 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
298 "alarmAdditionalInformation",
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);
307 addl_info_item = dlist_get_next(addl_info_item);
309 evel_json_close_list(jbuf);
311 /*************************************************************************/
312 /* If we've not written anything, rewind to before we opened the list. */
313 /*************************************************************************/
316 evel_json_rewind(jbuf);
319 evel_enc_kv_opt_string(jbuf, "alarmInterfaceA", &event->alarm_interface_a);
321 evel_json_close_object(jbuf);
326 /**************************************************************************//**
329 * Free off the Fault supplied. Will free all the contained allocated memory.
331 * @note It does not free the Fault itself, since that may be part of a
333 *****************************************************************************/
334 void evel_free_fault(EVENT_FAULT * event)
336 FAULT_ADDL_INFO * addl_info = NULL;
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);
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)
353 EVEL_DEBUG("Freeing Additional Info (%s, %s)",
356 free(addl_info->name);
357 free(addl_info->value);
359 addl_info = dlist_pop_last(&event->additional_info);
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);