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 ****************************************************************************/
17 /**************************************************************************//**
19 * Implementation of EVEL functions relating to the Threshold Cross Alerts.
21 *****************************************************************************/
26 #include "evel_internal.h"
27 #include "evel_throttle.h"
30 /**************************************************************************//**
31 * Create a new Threshold Crossing Alert event.
33 * @note The mandatory fields on the TCA 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 * TCA has immutable properties.
38 * @param event_name Unique Event Name confirming Domain AsdcVnfModel Description
39 * @param event_id A universal identifier of the event for: troubleshooting correlation, analysis, etc
40 * @param char* tcriticality Performance Counter Criticality MAJ MIN,
41 * @param char* tname Performance Counter Threshold name
42 * @param char* tthresholdCrossed Counter Threshold crossed value
43 * @param char* tvalue Counter actual value
44 * @param EVEL_EVENT_ACTION talertAction Alert set continue or clear
45 * @param char* talertDescription
46 * @param EVEL_ALERT_TYPE talertType Kind of anamoly
47 * @param unsigned long long tcollectionTimestamp time at which alert was collected
48 * @param EVEL_SEVERITIES teventSeverity Severity of Alert
49 * @param unsigned long long teventStartTimestamp Time when this alert started
51 * @returns pointer to the newly manufactured ::EVENT_THRESHOLD_CROSS. If the
52 * event is not used it must be released using
53 * ::evel_free_threshold_cross
54 * @retval NULL Failed to create the event.
55 *****************************************************************************/
56 EVENT_THRESHOLD_CROSS * evel_new_threshold_cross(const char * ev_name, const char * ev_id,
59 char * tthresholdCrossed,
61 EVEL_EVENT_ACTION talertAction,
62 char * talertDescription,
63 EVEL_ALERT_TYPE talertType,
64 unsigned long long tcollectionTimestamp,
65 EVEL_SEVERITIES teventSeverity,
66 unsigned long long teventStartTimestamp )
68 EVENT_THRESHOLD_CROSS * event = NULL;
71 assert( tcriticality!= NULL );
72 assert( tname!= NULL );
73 assert( tthresholdCrossed != NULL );
74 assert( tvalue!= NULL );
75 assert( talertDescription != NULL );
78 /***************************************************************************/
79 /* Allocate the Threshold crossing event. */
80 /***************************************************************************/
81 event = malloc(sizeof(EVENT_THRESHOLD_CROSS));
84 log_error_state("Out of memory");
87 memset(event, 0, sizeof(EVENT_THRESHOLD_CROSS));
88 EVEL_DEBUG("New Threshold Cross event is at %lp", event);
90 /***************************************************************************/
91 /* Initialize the header & the threshold crossing fields. */
92 /***************************************************************************/
93 evel_init_header_nameid(&event->header,ev_name,ev_id);
94 event->header.event_domain = EVEL_DOMAIN_THRESHOLD_CROSS;
95 event->major_version = EVEL_THRESHOLD_CROSS_MAJOR_VERSION;
96 event->minor_version = EVEL_THRESHOLD_CROSS_MINOR_VERSION;
99 event->additionalParameters.criticality = strdup(tcriticality);
100 event->additionalParameters.name = strdup(tname);
101 event->additionalParameters.thresholdCrossed = strdup(tthresholdCrossed);
102 event->additionalParameters.value = strdup(tvalue);
103 event->alertAction = talertAction;
104 event->alertDescription = strdup(talertDescription);
105 event->alertType = talertType;
106 event->collectionTimestamp = tcollectionTimestamp;
107 event->eventSeverity = teventSeverity;
108 event->eventStartTimestamp = teventStartTimestamp;
110 evel_init_option_string(&event->alertValue);
111 evel_init_option_string(&event->dataCollector);
112 evel_init_option_string(&event->elementType);
113 evel_init_option_string(&event->interfaceName);
114 evel_init_option_string(&event->networkService);
115 evel_init_option_string(&event->possibleRootCause);
116 dlist_initialize(&event->additional_info);
117 dlist_initialize(&event->alertidList);
127 /**************************************************************************//**
128 * Set the Event Type property of the TC Alert.
130 * @note The property is treated as immutable: it is only valid to call
131 * the setter once. However, we don't assert if the caller tries to
132 * overwrite, just ignoring the update instead.
134 * @param type The Event Type to be set. ASCIIZ string. The caller
135 * does not need to preserve the value once the function
137 *****************************************************************************/
138 void evel_threshold_cross_type_set(EVENT_THRESHOLD_CROSS * const event,char * type)
142 /***************************************************************************/
143 /* Check preconditions and call evel_header_type_set. */
144 /***************************************************************************/
146 assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
147 evel_header_type_set(&event->header, type);
152 /**************************************************************************//**
153 * Add an optional additional alertid value to Alert.
155 *****************************************************************************/
156 void evel_threshold_cross_alertid_add(EVENT_THRESHOLD_CROSS * const event,char * alertid)
161 /***************************************************************************/
162 /* Check preconditions. */
163 /***************************************************************************/
164 assert(event != NULL);
165 assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
166 assert(alertid != NULL);
168 EVEL_DEBUG("Adding AlertId=%s", alertid);
169 alid = strdup(alertid);
170 assert(alid != NULL);
172 dlist_push_last(&event->alertidList, alid);
177 /**************************************************************************//**
178 * Add an optional additional value name/value pair to the Alert.
180 * The name and value are NULL delimited ASCII strings. The library takes
181 * a copy so the caller does not have to preserve values after the function
183 * @param name ASCIIZ string with the attribute's name. The caller
184 * does not need to preserve the value once the function
186 * @param value ASCIIZ string with the attribute's value. The caller
187 * does not need to preserve the value once the function
189 *****************************************************************************/
190 void evel_threshold_cross_addl_info_add(EVENT_THRESHOLD_CROSS * const event, const char * name, const char * value)
192 OTHER_FIELD * nv_pair = NULL;
196 /***************************************************************************/
197 /* Check preconditions. */
198 /***************************************************************************/
199 assert(event != NULL);
200 assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
201 assert(name != NULL);
202 assert(value != NULL);
204 EVEL_DEBUG("Adding name=%s value=%s", name, value);
205 nv_pair = malloc(sizeof(OTHER_FIELD));
206 assert(nv_pair != NULL);
207 nv_pair->name = strdup(name);
208 nv_pair->value = strdup(value);
209 assert(nv_pair->name != NULL);
210 assert(nv_pair->value != NULL);
212 dlist_push_last(&event->additional_info, nv_pair);
218 /**************************************************************************//**
219 * Free a Signaling event.
221 * Free off the event supplied. Will free all the contained allocated memory.
223 * @note It does not free the event itself, since that may be part of a larger
225 *****************************************************************************/
226 void evel_free_threshold_cross(EVENT_THRESHOLD_CROSS * const event)
228 OTHER_FIELD * addl_info = NULL;
232 /***************************************************************************/
233 /* Check preconditions. As an internal API we don't allow freeing NULL */
234 /* events as we do on the API. */
235 /***************************************************************************/
236 assert(event != NULL);
237 assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
239 /***************************************************************************/
240 /* Free all internal strings then the header itself. */
241 /***************************************************************************/
242 addl_info = dlist_pop_last(&event->additional_info);
243 while (addl_info != NULL)
245 EVEL_DEBUG("Freeing Additional Info (%s, %s)",
248 free(addl_info->name);
249 free(addl_info->value);
251 addl_info = dlist_pop_last(&event->additional_info);
253 ptr = dlist_pop_last(&event->alertidList);
257 ptr = dlist_pop_last(&event->alertidList);
260 free(event->additionalParameters.criticality);
261 free(event->additionalParameters.name);
262 free(event->additionalParameters.thresholdCrossed);
263 free(event->additionalParameters.value);
264 free(event->alertDescription);
266 evel_free_option_string(&event->alertValue);
267 evel_free_option_string(&event->dataCollector);
268 evel_free_option_string(&event->elementType);
269 evel_free_option_string(&event->interfaceName);
270 evel_free_option_string(&event->networkService);
271 evel_free_option_string(&event->possibleRootCause);
272 evel_free_header(&event->header);
277 /**************************************************************************//**
278 * Set the TCA probable Root cause.
280 * @param sheader Possible root cause to Threshold
281 *****************************************************************************/
282 void evel_threshold_cross_possible_rootcause_set(EVENT_THRESHOLD_CROSS * const event, char * sheader)
286 /***************************************************************************/
287 /* Check preconditions. */
288 /***************************************************************************/
289 assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
290 assert(sheader != NULL);
292 evel_set_option_string(&event->possibleRootCause,
299 /**************************************************************************//**
300 * Set the TCA networking cause.
302 * @param sheader Possible networking service value to Threshold
303 *****************************************************************************/
304 void evel_threshold_cross_networkservice_set(EVENT_THRESHOLD_CROSS * const event, char * sheader)
308 /***************************************************************************/
309 /* Check preconditions. */
310 /***************************************************************************/
311 assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
312 assert(sheader != NULL);
314 evel_set_option_string(&event->networkService,
316 "Networking service value");
321 /**************************************************************************//**
322 * Set the TCA Interface name.
324 * @param sheader Interface name to threshold
325 *****************************************************************************/
326 void evel_threshold_cross_interfacename_set(EVENT_THRESHOLD_CROSS * const event,char * sheader)
330 /***************************************************************************/
331 /* Check preconditions. */
332 /***************************************************************************/
333 assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
334 assert(sheader != NULL);
336 evel_set_option_string(&event->interfaceName,
338 "TCA Interface name");
342 /**************************************************************************//**
343 * Set the TCA Data element type.
345 * @param sheader element type of Threshold
346 *****************************************************************************/
347 void evel_threshold_cross_data_elementtype_set(EVENT_THRESHOLD_CROSS * const event,char * sheader)
351 /***************************************************************************/
352 /* Check preconditions. */
353 /***************************************************************************/
354 assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
355 assert(sheader != NULL);
357 evel_set_option_string(&event->elementType,
359 "TCA Element type value");
363 /**************************************************************************//**
364 * Set the TCA Data collector value.
366 * @param sheader Data collector value
367 *****************************************************************************/
368 void evel_threshold_cross_data_collector_set(EVENT_THRESHOLD_CROSS * const event,char * sheader)
372 /***************************************************************************/
373 /* Check preconditions. */
374 /***************************************************************************/
375 assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
376 assert(sheader != NULL);
378 evel_set_option_string(&event->dataCollector,
380 "Datacollector value");
386 /**************************************************************************//**
387 * Set the TCA alert value.
389 * @param sheader Possible alert value
390 *****************************************************************************/
391 void evel_threshold_cross_alertvalue_set(EVENT_THRESHOLD_CROSS * const event,char * sheader)
395 /***************************************************************************/
396 /* Check preconditions. */
397 /***************************************************************************/
398 assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
399 assert(sheader != NULL);
401 evel_set_option_string(&event->alertValue,
407 /**************************************************************************//**
408 * Encode the Mobile Flow GTP Per Flow Metrics as a JSON object.
410 * @param jbuf Pointer to working ::EVEL_JSON_BUFFER.
411 * @param metrics Pointer to the ::EVENT_MOBILE_FLOW to encode.
412 * @returns Number of bytes actually written.
413 *****************************************************************************/
414 void evel_json_encode_perf_counter( EVEL_JSON_BUFFER * jbuf, PERF_COUNTER *pcounter)
418 /***************************************************************************/
419 /* Check preconditions. */
420 /***************************************************************************/
421 assert(jbuf != NULL);
422 assert(pcounter != NULL);
424 evel_json_open_named_object(jbuf, "additionalParameters");
426 /***************************************************************************/
427 /* Mandatory parameters. */
428 /***************************************************************************/
429 evel_enc_kv_string(jbuf, "criticality", pcounter->criticality);
430 evel_enc_kv_string(jbuf, "name", pcounter->name);
431 evel_enc_kv_string(jbuf, "thresholdCrossed", pcounter->name);
432 evel_enc_kv_string(jbuf, "value", pcounter->value);
434 evel_json_close_object(jbuf);
439 /**************************************************************************//**
440 * Encode the Signaling in JSON according to AT&T's schema for the
443 * @param jbuf Pointer to the ::EVEL_JSON_BUFFER to encode into.
444 * @param event Pointer to the ::EVENT_HEADER to encode.
445 *****************************************************************************/
446 void evel_json_encode_threshold_cross(EVEL_JSON_BUFFER * const jbuf,
447 EVENT_THRESHOLD_CROSS * const event)
449 OTHER_FIELD * nv_pair = NULL;
450 DLIST_ITEM * dlist_item = NULL;
454 /***************************************************************************/
455 /* Check preconditions. */
456 /***************************************************************************/
457 assert(event != NULL);
458 assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
460 evel_json_encode_header(jbuf, &event->header);
461 evel_json_open_named_object(jbuf, "thresholdCrossingAlert");
463 /***************************************************************************/
464 /* Mandatory fields */
465 /***************************************************************************/
466 evel_json_encode_perf_counter(jbuf, &event->additionalParameters);
467 evel_enc_kv_int(jbuf, "alertAction", event->alertAction);
468 evel_enc_kv_string(jbuf, "alertDescription", event->alertDescription);
469 evel_enc_kv_int(jbuf, "alertType", event->alertType);
471 jbuf, "collectionTimestamp", event->collectionTimestamp);
472 evel_enc_kv_int(jbuf, "eventSeverity", event->eventSeverity);
474 jbuf, "eventStartTimestamp", event->eventStartTimestamp);
476 /***************************************************************************/
477 /* Optional fields */
478 /***************************************************************************/
479 evel_enc_kv_opt_string(jbuf, "alertValue", &event->alertValue);
480 evel_enc_kv_opt_string(jbuf, "dataCollector", &event->dataCollector);
481 evel_enc_kv_opt_string(jbuf, "elementType", &event->elementType);
482 evel_enc_kv_opt_string(jbuf, "interfaceName", &event->interfaceName);
483 evel_enc_kv_opt_string(jbuf, "networkService", &event->networkService);
484 evel_enc_kv_opt_string(jbuf, "possibleRootCause", &event->possibleRootCause);
486 /***************************************************************************/
487 /* Checkpoint, so that we can wind back if all fields are suppressed. */
488 /***************************************************************************/
489 evel_json_checkpoint(jbuf);
490 if (evel_json_open_opt_named_list(jbuf, "additionalFields"))
494 dlist_item = dlist_get_first(&event->additional_info);
495 while (dlist_item != NULL)
497 nv_pair = (OTHER_FIELD *) dlist_item->item;
498 assert(nv_pair != NULL);
500 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
504 evel_json_open_object(jbuf);
505 evel_enc_kv_string(jbuf, "name", nv_pair->name);
506 evel_enc_kv_string(jbuf, "value", nv_pair->value);
507 evel_json_close_object(jbuf);
510 dlist_item = dlist_get_next(dlist_item);
512 evel_json_close_list(jbuf);
514 /*************************************************************************/
515 /* If we've not written anything, rewind to before we opened the list. */
516 /*************************************************************************/
519 evel_json_rewind(jbuf);
522 evel_enc_version(jbuf,
523 "thresholdCrossingFieldsVersion",
524 event->major_version,
525 event->minor_version);
527 evel_json_close_object(jbuf);