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 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)
57 EVENT_FAULT * fault = NULL;
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);
68 /***************************************************************************/
69 /* Allocate the fault. */
70 /***************************************************************************/
71 fault = malloc(sizeof(EVENT_FAULT));
74 log_error_state("Out of memory");
77 memset(fault, 0, sizeof(EVENT_FAULT));
78 EVEL_DEBUG("New fault is at %lp", fault);
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);
103 /**************************************************************************//**
104 * Add an additional value name/value pair to the Fault.
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
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
114 * @param value ASCIIZ string with the attribute's value. The caller
115 * does not need to preserve the value once the function
117 *****************************************************************************/
118 void evel_fault_addl_info_add(EVENT_FAULT * fault, char * name, char * value)
120 FAULT_ADDL_INFO * addl_info = NULL;
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);
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);
140 dlist_push_last(&fault->additional_info, addl_info);
145 /**************************************************************************//**
146 * Set the Fault Category property of the Fault.
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.
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
157 *****************************************************************************/
158 void evel_fault_category_set(EVENT_FAULT * fault,
159 const char * const category)
163 /***************************************************************************/
164 /* Check preconditions. */
165 /***************************************************************************/
166 assert(fault != NULL);
167 assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
168 assert(category != NULL);
170 evel_set_option_string(&fault->category,
172 "Fault Category set");
176 /**************************************************************************//**
177 * Set the Alarm Interface A property of the Fault.
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.
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
187 *****************************************************************************/
188 void evel_fault_interface_set(EVENT_FAULT * fault,
189 const char * const interface)
193 /***************************************************************************/
194 /* Check preconditions. */
195 /***************************************************************************/
196 assert(fault != NULL);
197 assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
198 assert(interface != NULL);
200 evel_set_option_string(&fault->alarm_interface_a,
202 "Alarm Interface A");
206 /**************************************************************************//**
207 * Set the Event Type property of the Fault.
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.
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
217 *****************************************************************************/
218 void evel_fault_type_set(EVENT_FAULT * fault, const char * const type)
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);
232 /**************************************************************************//**
233 * Encode the fault in JSON according to AT&T's schema for the fault type.
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,
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;
249 /***************************************************************************/
250 /* Check preconditions. */
251 /***************************************************************************/
252 assert(event != NULL);
253 assert(event->header.event_domain == EVEL_DOMAIN_FAULT);
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);
259 evel_json_encode_header(jbuf, &event->header);
260 evel_json_open_named_object(jbuf, "faultFields");
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);
272 jbuf, "faultFieldsVersion", event->major_version, event->minor_version);
274 /***************************************************************************/
275 /* Optional fields. */
276 /***************************************************************************/
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"))
284 bool item_added = false;
286 addl_info_item = dlist_get_first(&event->additional_info);
287 while (addl_info_item != NULL)
289 addl_info = (FAULT_ADDL_INFO*) addl_info_item->item;
290 assert(addl_info != NULL);
292 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
293 "alarmAdditionalInformation",
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);
302 addl_info_item = dlist_get_next(addl_info_item);
304 evel_json_close_list(jbuf);
306 /*************************************************************************/
307 /* If we've not written anything, rewind to before we opened the list. */
308 /*************************************************************************/
311 evel_json_rewind(jbuf);
314 evel_enc_kv_opt_string(jbuf, "alarmInterfaceA", &event->alarm_interface_a);
316 evel_json_close_object(jbuf);
321 /**************************************************************************//**
324 * Free off the Fault supplied. Will free all the contained allocated memory.
326 * @note It does not free the Fault itself, since that may be part of a
328 *****************************************************************************/
329 void evel_free_fault(EVENT_FAULT * event)
331 FAULT_ADDL_INFO * addl_info = NULL;
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);
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)
348 EVEL_DEBUG("Freeing Additional Info (%s, %s)",
351 free(addl_info->name);
352 free(addl_info->value);
354 addl_info = dlist_pop_last(&event->additional_info);
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);