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 if( severity == EVEL_SEVERITY_NORMAL )
96 evel_event_sequence_set(&fault->header,0);
98 evel_event_sequence_set(&fault->header,1);
99 fault->event_source_type = ev_source_type;
100 fault->vf_status = status;
101 fault->alarm_condition = strdup(condition);
102 fault->specific_problem = strdup(specific_problem);
103 evel_init_option_string(&fault->category);
104 evel_init_option_string(&fault->alarm_interface_a);
105 dlist_initialize(&fault->additional_info);
112 /**************************************************************************//**
113 * Add an additional value name/value pair to the Fault.
115 * The name and value are null delimited ASCII strings. The library takes
116 * a copy so the caller does not have to preserve values after the function
119 * @param fault Pointer to the fault.
120 * @param name ASCIIZ string with the attribute's name. The caller
121 * does not need to preserve the value once the function
123 * @param value ASCIIZ string with the attribute's value. The caller
124 * does not need to preserve the value once the function
126 *****************************************************************************/
127 void evel_fault_addl_info_add(EVENT_FAULT * fault, char * name, char * value)
129 FAULT_ADDL_INFO * addl_info = NULL;
132 /***************************************************************************/
133 /* Check preconditions. */
134 /***************************************************************************/
135 assert(fault != NULL);
136 assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
137 assert(name != NULL);
138 assert(value != NULL);
140 EVEL_DEBUG("Adding name=%s value=%s", name, value);
141 addl_info = malloc(sizeof(FAULT_ADDL_INFO));
142 assert(addl_info != NULL);
143 memset(addl_info, 0, sizeof(FAULT_ADDL_INFO));
144 addl_info->name = strdup(name);
145 addl_info->value = strdup(value);
146 assert(addl_info->name != NULL);
147 assert(addl_info->value != NULL);
149 dlist_push_last(&fault->additional_info, addl_info);
154 /**************************************************************************//**
155 * Set the Fault Category property of the Fault.
157 * @note The property is treated as immutable: it is only valid to call
158 * the setter once. However, we don't assert if the caller tries to
159 * overwrite, just ignoring the update instead.
161 * @param fault Pointer to the fault.
162 * @param category Category : license, link, routing, security, signaling.
163 * ASCIIZ string. The caller
164 * does not need to preserve the value once the function
166 *****************************************************************************/
167 void evel_fault_category_set(EVENT_FAULT * fault,
168 const char * const category)
172 /***************************************************************************/
173 /* Check preconditions. */
174 /***************************************************************************/
175 assert(fault != NULL);
176 assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
177 assert(category != NULL);
179 evel_set_option_string(&fault->category,
181 "Fault Category set");
185 /**************************************************************************//**
186 * Set the Alarm Interface A property of the Fault.
188 * @note The property is treated as immutable: it is only valid to call
189 * the setter once. However, we don't assert if the caller tries to
190 * overwrite, just ignoring the update instead.
192 * @param fault Pointer to the fault.
193 * @param interface The Alarm Interface A to be set. ASCIIZ string. The caller
194 * does not need to preserve the value once the function
196 *****************************************************************************/
197 void evel_fault_interface_set(EVENT_FAULT * fault,
198 const char * const interface)
202 /***************************************************************************/
203 /* Check preconditions. */
204 /***************************************************************************/
205 assert(fault != NULL);
206 assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
207 assert(interface != NULL);
209 evel_set_option_string(&fault->alarm_interface_a,
211 "Alarm Interface A");
215 /**************************************************************************//**
216 * Set the Event Type property of the Fault.
218 * @note The property is treated as immutable: it is only valid to call
219 * the setter once. However, we don't assert if the caller tries to
220 * overwrite, just ignoring the update instead.
222 * @param fault Pointer to the fault.
223 * @param type The Event Type to be set. ASCIIZ string. The caller
224 * does not need to preserve the value once the function
226 *****************************************************************************/
227 void evel_fault_type_set(EVENT_FAULT * fault, const char * const type)
231 /***************************************************************************/
232 /* Check preconditions and call evel_header_type_set. */
233 /***************************************************************************/
234 assert(fault != NULL);
235 assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
236 evel_header_type_set(&fault->header, type);
241 /**************************************************************************//**
242 * Encode the fault in JSON according to AT&T's schema for the fault type.
244 * @param jbuf Pointer to the ::EVEL_JSON_BUFFER to encode into.
245 * @param event Pointer to the ::EVENT_HEADER to encode.
246 *****************************************************************************/
247 void evel_json_encode_fault(EVEL_JSON_BUFFER * jbuf,
250 FAULT_ADDL_INFO * addl_info = NULL;
251 DLIST_ITEM * addl_info_item = NULL;
252 char * fault_severity;
253 char * fault_source_type;
254 char * fault_vf_status;
258 /***************************************************************************/
259 /* Check preconditions. */
260 /***************************************************************************/
261 assert(event != NULL);
262 assert(event->header.event_domain == EVEL_DOMAIN_FAULT);
264 fault_severity = evel_severity(event->event_severity);
265 fault_source_type = evel_source_type(event->event_source_type);
266 fault_vf_status = evel_vf_status(event->vf_status);
268 evel_json_encode_header(jbuf, &event->header);
269 evel_json_open_named_object(jbuf, "faultFields");
271 /***************************************************************************/
272 /* Mandatory fields. */
273 /***************************************************************************/
274 evel_enc_kv_string(jbuf, "alarmCondition", event->alarm_condition);
275 evel_enc_kv_opt_string(jbuf, "eventCategory", &event->category);
276 evel_enc_kv_string(jbuf, "eventSeverity", fault_severity);
277 evel_enc_kv_string(jbuf, "eventSourceType", fault_source_type);
278 evel_enc_kv_string(jbuf, "specificProblem", event->specific_problem);
279 evel_enc_kv_string(jbuf, "vfStatus", fault_vf_status);
281 jbuf, "faultFieldsVersion", event->major_version, event->minor_version);
283 /***************************************************************************/
284 /* Optional fields. */
285 /***************************************************************************/
287 /***************************************************************************/
288 /* Checkpoint, so that we can wind back if all fields are suppressed. */
289 /***************************************************************************/
290 evel_json_checkpoint(jbuf);
291 if (evel_json_open_opt_named_list(jbuf, "alarmAdditionalInformation"))
293 bool item_added = false;
295 addl_info_item = dlist_get_first(&event->additional_info);
296 while (addl_info_item != NULL)
298 addl_info = (FAULT_ADDL_INFO*) addl_info_item->item;
299 assert(addl_info != NULL);
301 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
302 "alarmAdditionalInformation",
305 evel_json_open_object(jbuf);
306 evel_enc_kv_string(jbuf, "name", addl_info->name);
307 evel_enc_kv_string(jbuf, "value", addl_info->value);
308 evel_json_close_object(jbuf);
311 addl_info_item = dlist_get_next(addl_info_item);
313 evel_json_close_list(jbuf);
315 /*************************************************************************/
316 /* If we've not written anything, rewind to before we opened the list. */
317 /*************************************************************************/
320 evel_json_rewind(jbuf);
323 evel_enc_kv_opt_string(jbuf, "alarmInterfaceA", &event->alarm_interface_a);
325 evel_json_close_object(jbuf);
330 /**************************************************************************//**
333 * Free off the Fault supplied. Will free all the contained allocated memory.
335 * @note It does not free the Fault itself, since that may be part of a
337 *****************************************************************************/
338 void evel_free_fault(EVENT_FAULT * event)
340 FAULT_ADDL_INFO * addl_info = NULL;
344 /***************************************************************************/
345 /* Check preconditions. As an internal API we don't allow freeing NULL */
346 /* events as we do on the public API. */
347 /***************************************************************************/
348 assert(event != NULL);
349 assert(event->header.event_domain == EVEL_DOMAIN_FAULT);
351 /***************************************************************************/
352 /* Free all internal strings then the header itself. */
353 /***************************************************************************/
354 addl_info = dlist_pop_last(&event->additional_info);
355 while (addl_info != NULL)
357 EVEL_DEBUG("Freeing Additional Info (%s, %s)",
360 free(addl_info->name);
361 free(addl_info->value);
363 addl_info = dlist_pop_last(&event->additional_info);
365 free(event->alarm_condition);
366 free(event->specific_problem);
367 evel_free_option_string(&event->category);
368 evel_free_option_string(&event->alarm_interface_a);
369 evel_free_header(&event->header);