1 /*************************************************************************//**
3 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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
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.
16 ****************************************************************************/
18 /**************************************************************************//**
20 * Implementation of EVEL functions relating to the Fault.
22 ****************************************************************************/
29 #include "evel_internal.h"
30 #include "evel_throttle.h"
32 /**************************************************************************//**
33 * Create a new fault event.
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,
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)
61 EVENT_FAULT * fault = NULL;
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);
72 /***************************************************************************/
73 /* Allocate the fault. */
74 /***************************************************************************/
75 fault = malloc(sizeof(EVENT_FAULT));
78 log_error_state("Out of memory");
81 memset(fault, 0, sizeof(EVENT_FAULT));
82 EVEL_DEBUG("New fault is at %lp", fault);
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);
107 /**************************************************************************//**
108 * Add an additional value name/value pair to the Fault.
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
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
118 * @param value ASCIIZ string with the attribute's value. The caller
119 * does not need to preserve the value once the function
121 *****************************************************************************/
122 void evel_fault_addl_info_add(EVENT_FAULT * fault, char * name, char * value)
124 FAULT_ADDL_INFO * addl_info = NULL;
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);
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);
144 dlist_push_last(&fault->additional_info, addl_info);
149 /**************************************************************************//**
150 * Set the Fault Category property of the Fault.
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.
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
161 *****************************************************************************/
162 void evel_fault_category_set(EVENT_FAULT * fault,
163 const char * const category)
167 /***************************************************************************/
168 /* Check preconditions. */
169 /***************************************************************************/
170 assert(fault != NULL);
171 assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
172 assert(category != NULL);
174 evel_set_option_string(&fault->category,
176 "Fault Category set");
180 /**************************************************************************//**
181 * Set the Alarm Interface A property of the Fault.
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.
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
191 *****************************************************************************/
192 void evel_fault_interface_set(EVENT_FAULT * fault,
193 const char * const interface)
197 /***************************************************************************/
198 /* Check preconditions. */
199 /***************************************************************************/
200 assert(fault != NULL);
201 assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
202 assert(interface != NULL);
204 evel_set_option_string(&fault->alarm_interface_a,
206 "Alarm Interface A");
210 /**************************************************************************//**
211 * Set the Event Type property of the Fault.
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.
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
221 *****************************************************************************/
222 void evel_fault_type_set(EVENT_FAULT * fault, const char * const type)
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);
236 /**************************************************************************//**
237 * Encode the fault in JSON according to AT&T's schema for the fault type.
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,
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;
253 /***************************************************************************/
254 /* Check preconditions. */
255 /***************************************************************************/
256 assert(event != NULL);
257 assert(event->header.event_domain == EVEL_DOMAIN_FAULT);
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);
263 evel_json_encode_header(jbuf, &event->header);
264 evel_json_open_named_object(jbuf, "faultFields");
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);
276 jbuf, "faultFieldsVersion", event->major_version, event->minor_version);
278 /***************************************************************************/
279 /* Optional fields. */
280 /***************************************************************************/
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"))
288 bool item_added = false;
290 addl_info_item = dlist_get_first(&event->additional_info);
291 while (addl_info_item != NULL)
293 addl_info = (FAULT_ADDL_INFO*) addl_info_item->item;
294 assert(addl_info != NULL);
296 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
297 "alarmAdditionalInformation",
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);
306 addl_info_item = dlist_get_next(addl_info_item);
308 evel_json_close_list(jbuf);
310 /*************************************************************************/
311 /* If we've not written anything, rewind to before we opened the list. */
312 /*************************************************************************/
315 evel_json_rewind(jbuf);
318 evel_enc_kv_opt_string(jbuf, "alarmInterfaceA", &event->alarm_interface_a);
320 evel_json_close_object(jbuf);
325 /**************************************************************************//**
328 * Free off the Fault supplied. Will free all the contained allocated memory.
330 * @note It does not free the Fault itself, since that may be part of a
332 *****************************************************************************/
333 void evel_free_fault(EVENT_FAULT * event)
335 FAULT_ADDL_INFO * addl_info = NULL;
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);
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)
352 EVEL_DEBUG("Freeing Additional Info (%s, %s)",
355 free(addl_info->name);
356 free(addl_info->value);
358 addl_info = dlist_pop_last(&event->additional_info);
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);