1 /**************************************************************************//**
3 * Implementation of EVEL functions relating to the Fault.
8 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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
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 *****************************************************************************/
27 #include "evel_internal.h"
28 #include "evel_throttle.h"
30 /**************************************************************************//**
31 * Create a new fault event.
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)
50 EVENT_FAULT * fault = NULL;
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);
61 /***************************************************************************/
62 /* Allocate the fault. */
63 /***************************************************************************/
64 fault = malloc(sizeof(EVENT_FAULT));
67 log_error_state("Out of memory");
70 memset(fault, 0, sizeof(EVENT_FAULT));
71 EVEL_DEBUG("New fault is at %lp", fault);
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);
95 /**************************************************************************//**
96 * Add an additional value name/value pair to the Fault.
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
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
106 * @param value ASCIIZ string with the attribute's value. The caller
107 * does not need to preserve the value once the function
109 *****************************************************************************/
110 void evel_fault_addl_info_add(EVENT_FAULT * fault, char * name, char * value)
112 FAULT_ADDL_INFO * addl_info = NULL;
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);
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);
132 dlist_push_last(&fault->additional_info, addl_info);
137 /**************************************************************************//**
138 * Set the Alarm Interface A property of the Fault.
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.
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
148 *****************************************************************************/
149 void evel_fault_interface_set(EVENT_FAULT * fault,
150 const char * const interface)
154 /***************************************************************************/
155 /* Check preconditions. */
156 /***************************************************************************/
157 assert(fault != NULL);
158 assert(fault->header.event_domain == EVEL_DOMAIN_FAULT);
159 assert(interface != NULL);
161 evel_set_option_string(&fault->alarm_interface_a,
163 "Alarm Interface A");
167 /**************************************************************************//**
168 * Set the Event Type property of the Fault.
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.
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
178 *****************************************************************************/
179 void evel_fault_type_set(EVENT_FAULT * fault, const char * const type)
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);
193 /**************************************************************************//**
194 * Encode the fault in JSON according to AT&T's schema for the fault type.
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,
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;
210 /***************************************************************************/
211 /* Check preconditions. */
212 /***************************************************************************/
213 assert(event != NULL);
214 assert(event->header.event_domain == EVEL_DOMAIN_FAULT);
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);
220 evel_json_encode_header(jbuf, &event->header);
221 evel_json_open_named_object(jbuf, "faultFields");
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);
232 jbuf, "faultFieldsVersion", event->major_version, event->minor_version);
234 /***************************************************************************/
235 /* Optional fields. */
236 /***************************************************************************/
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"))
244 bool item_added = false;
246 addl_info_item = dlist_get_first(&event->additional_info);
247 while (addl_info_item != NULL)
249 addl_info = (FAULT_ADDL_INFO*) addl_info_item->item;
250 assert(addl_info != NULL);
252 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
253 "alarmAdditionalInformation",
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);
262 addl_info_item = dlist_get_next(addl_info_item);
264 evel_json_close_list(jbuf);
266 /*************************************************************************/
267 /* If we've not written anything, rewind to before we opened the list. */
268 /*************************************************************************/
271 evel_json_rewind(jbuf);
274 evel_enc_kv_opt_string(jbuf, "alarmInterfaceA", &event->alarm_interface_a);
276 evel_json_close_object(jbuf);
281 /**************************************************************************//**
284 * Free off the Fault supplied. Will free all the contained allocated memory.
286 * @note It does not free the Fault itself, since that may be part of a
288 *****************************************************************************/
289 void evel_free_fault(EVENT_FAULT * event)
291 FAULT_ADDL_INFO * addl_info = NULL;
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);
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)
308 EVEL_DEBUG("Freeing Additional Info (%s, %s)",
311 free(addl_info->name);
312 free(addl_info->value);
314 addl_info = dlist_pop_last(&event->additional_info);
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);