Modified hardcoded eventname id to user defined
[demo.git] / vnfs / VES5.0 / evel / evel-library / code / evel_library / evel_threshold_cross.c
1 /*************************************************************************//**
2  *
3  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
4  *
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
9  *
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.
15  *
16  ****************************************************************************/
17 /**************************************************************************//**
18  * @file
19  * Implementation of EVEL functions relating to the Threshold Cross Alerts.
20  *
21  *****************************************************************************/
22 #include <string.h>
23 #include <assert.h>
24 #include <stdlib.h>
25 #include "evel.h"       
26 #include "evel_internal.h"      
27 #include "evel_throttle.h"      
28
29
30 /**************************************************************************//**
31  * Create a new Threshold Crossing Alert event.
32  *
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.
37  *
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
50  *
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,
57                            char *  tcriticality,
58                            char *  tname,
59                            char *  tthresholdCrossed,
60                            char *  tvalue,
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 )
67 {
68         EVENT_THRESHOLD_CROSS * event = NULL;
69         EVEL_ENTER();
70
71         assert( tcriticality!= NULL );
72         assert( tname!= NULL );
73         assert( tthresholdCrossed != NULL );
74         assert( tvalue!= NULL );
75         assert( talertDescription != NULL );
76                 
77
78         /***************************************************************************/
79         /* Allocate the Threshold crossing event.                                  */
80         /***************************************************************************/
81         event = malloc(sizeof(EVENT_THRESHOLD_CROSS));
82         if (event == NULL)
83         {
84             log_error_state("Out of memory");
85             goto exit_label;
86         }
87         memset(event, 0, sizeof(EVENT_THRESHOLD_CROSS));
88         EVEL_DEBUG("New Threshold Cross event is at %lp", event);
89
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;
97
98
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;
109
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);
118
119 exit_label:
120
121   EVEL_EXIT();
122   return event;
123
124 }
125
126
127 /**************************************************************************//**
128  * Set the Event Type property of the TC Alert.
129  *
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.
133  *
134  * @param type        The Event Type to be set. ASCIIZ string. The caller
135  *                    does not need to preserve the value once the function
136  *                    returns.
137  *****************************************************************************/
138 void evel_threshold_cross_type_set(EVENT_THRESHOLD_CROSS * const event,char *  type)
139 {
140   EVEL_ENTER();
141
142   /***************************************************************************/
143   /* Check preconditions and call evel_header_type_set.                      */
144   /***************************************************************************/
145   assert(type!=NULL);
146   assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
147   evel_header_type_set(&event->header, type);
148
149   EVEL_EXIT();
150 }
151
152 /**************************************************************************//**
153  * Add an optional additional alertid value to Alert.
154  *
155  *****************************************************************************/
156 void evel_threshold_cross_alertid_add(EVENT_THRESHOLD_CROSS * const event,char *  alertid)
157 {
158   char *alid=NULL;
159   EVEL_ENTER();
160
161   /***************************************************************************/
162   /* Check preconditions.                                                    */
163   /***************************************************************************/
164   assert(event != NULL);
165   assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
166   assert(alertid != NULL);
167
168   EVEL_DEBUG("Adding AlertId=%s", alertid);
169   alid = strdup(alertid);
170   assert(alid != NULL);
171
172   dlist_push_last(&event->alertidList, alid);
173
174   EVEL_EXIT();
175 }
176         
177 /**************************************************************************//**
178  * Add an optional additional value name/value pair to the Alert.
179  *
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
182  * returns.
183  * @param name      ASCIIZ string with the attribute's name.  The caller
184  *                  does not need to preserve the value once the function
185  *                  returns.
186  * @param value     ASCIIZ string with the attribute's value.  The caller
187  *                  does not need to preserve the value once the function
188  *                  returns.
189  *****************************************************************************/
190 void evel_threshold_cross_addl_info_add(EVENT_THRESHOLD_CROSS * const event, const char *  name, const char *  value)
191 {
192   OTHER_FIELD * nv_pair = NULL;
193
194   EVEL_ENTER();
195
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);
203
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);
211
212   dlist_push_last(&event->additional_info, nv_pair);
213
214   EVEL_EXIT();
215 }
216
217
218 /**************************************************************************//**
219  * Free a Signaling event.
220  *
221  * Free off the event supplied.  Will free all the contained allocated memory.
222  *
223  * @note It does not free the event itself, since that may be part of a larger
224  * structure.
225  *****************************************************************************/
226 void evel_free_threshold_cross(EVENT_THRESHOLD_CROSS * const event)
227 {
228   OTHER_FIELD * addl_info = NULL;
229   char *ptr;
230   EVEL_ENTER();
231
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);
238
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)
244   {
245     EVEL_DEBUG("Freeing Additional Info (%s, %s)",
246                addl_info->name,
247                addl_info->value);
248     free(addl_info->name);
249     free(addl_info->value);
250     free(addl_info);
251     addl_info = dlist_pop_last(&event->additional_info);
252   }
253   ptr = dlist_pop_last(&event->alertidList);
254   while (ptr != NULL)
255   {
256     free(ptr);
257     ptr = dlist_pop_last(&event->alertidList);
258   }
259
260   free(event->additionalParameters.criticality);
261   free(event->additionalParameters.name);
262   free(event->additionalParameters.thresholdCrossed);
263   free(event->additionalParameters.value);
264   free(event->alertDescription); 
265
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);
273
274   EVEL_EXIT();
275 }
276
277   /**************************************************************************//**
278    * Set the TCA probable Root cause.
279    *
280    * @param sheader     Possible root cause to Threshold
281    *****************************************************************************/
282   void evel_threshold_cross_possible_rootcause_set(EVENT_THRESHOLD_CROSS * const event, char *  sheader)
283   {
284     EVEL_ENTER();
285
286     /***************************************************************************/
287     /* Check preconditions.                                                    */
288     /***************************************************************************/
289     assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
290     assert(sheader != NULL);
291
292     evel_set_option_string(&event->possibleRootCause,
293                          sheader,
294                          "Rootcause value");
295
296     EVEL_EXIT();
297   }
298     
299   /**************************************************************************//**
300    * Set the TCA networking cause.
301    *
302    * @param sheader     Possible networking service value to Threshold
303    *****************************************************************************/
304   void evel_threshold_cross_networkservice_set(EVENT_THRESHOLD_CROSS * const event, char *  sheader)
305   {
306             EVEL_ENTER();
307
308     /***************************************************************************/
309     /* Check preconditions.                                                    */
310     /***************************************************************************/
311     assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
312     assert(sheader != NULL);
313
314     evel_set_option_string(&event->networkService,
315                          sheader,
316                          "Networking service value");
317
318             EVEL_EXIT();
319   }
320     
321   /**************************************************************************//**
322    * Set the TCA Interface name.
323    *
324    * @param sheader     Interface name to threshold
325    *****************************************************************************/
326   void evel_threshold_cross_interfacename_set(EVENT_THRESHOLD_CROSS * const event,char *  sheader)
327   {
328             EVEL_ENTER();
329
330             /***************************************************************************/
331             /* Check preconditions.                                                    */
332             /***************************************************************************/
333             assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
334             assert(sheader != NULL);
335
336             evel_set_option_string(&event->interfaceName,
337                                    sheader,
338                                    "TCA Interface name");
339             EVEL_EXIT();
340   }
341     
342   /**************************************************************************//**
343    * Set the TCA Data element type.
344    *
345    * @param sheader     element type of Threshold
346    *****************************************************************************/
347   void evel_threshold_cross_data_elementtype_set(EVENT_THRESHOLD_CROSS * const event,char *  sheader)
348   {
349             EVEL_ENTER();
350
351             /***************************************************************************/
352             /* Check preconditions.                                                    */
353             /***************************************************************************/
354             assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
355             assert(sheader != NULL);
356
357             evel_set_option_string(&event->elementType,
358                                    sheader,
359                                    "TCA Element type value");
360             EVEL_EXIT();
361   }
362
363   /**************************************************************************//**
364    * Set the TCA Data collector value.
365    *
366    * @param sheader     Data collector value
367    *****************************************************************************/
368   void evel_threshold_cross_data_collector_set(EVENT_THRESHOLD_CROSS * const event,char *  sheader)
369   {
370             EVEL_ENTER();
371
372             /***************************************************************************/
373             /* Check preconditions.                                                    */
374             /***************************************************************************/
375             assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
376             assert(sheader != NULL);
377
378             evel_set_option_string(&event->dataCollector,
379                                    sheader,
380                                    "Datacollector value");
381             EVEL_EXIT();
382   }
383     
384     
385     
386   /**************************************************************************//**
387    * Set the TCA alert value.
388    *
389    * @param sheader     Possible alert value
390    *****************************************************************************/
391   void evel_threshold_cross_alertvalue_set(EVENT_THRESHOLD_CROSS * const event,char *  sheader)
392   {
393             EVEL_ENTER();
394
395             /***************************************************************************/
396             /* Check preconditions.                                                    */
397             /***************************************************************************/
398             assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
399             assert(sheader != NULL);
400
401             evel_set_option_string(&event->alertValue,
402                                    sheader,
403                                    "Alert value");
404             EVEL_EXIT();
405   }
406
407 /**************************************************************************//**
408  * Encode the Mobile Flow GTP Per Flow Metrics as a JSON object.
409  *
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)
415 {
416   EVEL_ENTER();
417
418   /***************************************************************************/
419   /* Check preconditions.                                                    */
420   /***************************************************************************/
421   assert(jbuf != NULL);
422   assert(pcounter != NULL);
423
424   evel_json_open_named_object(jbuf, "additionalParameters");
425
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);
433
434   evel_json_close_object(jbuf);
435
436   EVEL_EXIT();
437 }
438
439 /**************************************************************************//**
440  * Encode the Signaling in JSON according to AT&T's schema for the
441  * event type.
442  *
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)
448 {
449   OTHER_FIELD * nv_pair = NULL;
450   DLIST_ITEM * dlist_item = NULL;
451
452   EVEL_ENTER();
453
454   /***************************************************************************/
455   /* Check preconditions.                                                    */
456   /***************************************************************************/
457   assert(event != NULL);
458   assert(event->header.event_domain == EVEL_DOMAIN_THRESHOLD_CROSS);
459
460   evel_json_encode_header(jbuf, &event->header);
461   evel_json_open_named_object(jbuf, "thresholdCrossingAlert");
462
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);
470   evel_enc_kv_ull(
471     jbuf, "collectionTimestamp", event->collectionTimestamp);
472   evel_enc_kv_int(jbuf, "eventSeverity", event->eventSeverity);
473   evel_enc_kv_ull(
474     jbuf, "eventStartTimestamp", event->eventStartTimestamp);
475
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);
485
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"))
491   {
492     bool added = false;
493
494     dlist_item = dlist_get_first(&event->additional_info);
495     while (dlist_item != NULL)
496     {
497       nv_pair = (OTHER_FIELD *) dlist_item->item;
498       assert(nv_pair != NULL);
499
500       if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
501                                           "additionalFields",
502                                           nv_pair->name))
503       {
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);
508         added = true;
509       }
510       dlist_item = dlist_get_next(dlist_item);
511     }
512     evel_json_close_list(jbuf);
513
514     /*************************************************************************/
515     /* If we've not written anything, rewind to before we opened the list.   */
516     /*************************************************************************/
517     if (!added)
518     {
519       evel_json_rewind(jbuf);
520     }
521   }
522   evel_enc_version(jbuf,
523                    "thresholdCrossingFieldsVersion",
524                    event->major_version,
525                    event->minor_version);
526
527   evel_json_close_object(jbuf);
528
529   EVEL_EXIT();
530 }
531