AT&T ECOMP Vendor Event Listener library  0.1
evel_event.c
Go to the documentation of this file.
1 /**************************************************************************/
39 #include <string.h>
40 #include <assert.h>
41 #include <stdlib.h>
42 #include <sys/time.h>
43 
44 #include "evel.h"
45 #include "evel_internal.h"
46 #include "evel_throttle.h"
47 #include "metadata.h"
48 
49 /**************************************************************************/
52 static int event_sequence = 1;
53 
54 /**************************************************************************/
59 void evel_set_next_event_sequence(const int sequence)
60 {
61  EVEL_ENTER();
62 
63  EVEL_INFO("Setting event sequence to %d, was %d ", sequence, event_sequence);
64  event_sequence = sequence;
65 
66  EVEL_EXIT();
67 }
68 
69 /**************************************************************************/
79 {
80  EVENT_HEADER * heartbeat = NULL;
81  EVEL_ENTER();
82 
83  /***************************************************************************/
84  /* Allocate the header. */
85  /***************************************************************************/
86  heartbeat = malloc(sizeof(EVENT_HEADER));
87  if (heartbeat == NULL)
88  {
89  log_error_state("Out of memory");
90  goto exit_label;
91  }
92  memset(heartbeat, 0, sizeof(EVENT_HEADER));
93 
94  /***************************************************************************/
95  /* Initialize the header. Get a new event sequence number. Note that if */
96  /* any memory allocation fails in here we will fail gracefully because */
97  /* everything downstream can cope with NULLs. */
98  /***************************************************************************/
99  evel_init_header(heartbeat);
100  evel_force_option_string(&heartbeat->event_type, "Autonomous heartbeat");
101 
102 exit_label:
103  EVEL_EXIT();
104  return heartbeat;
105 }
106 
107 /**************************************************************************/
112 void evel_init_header(EVENT_HEADER * const header)
113 {
114  char scratchpad[EVEL_MAX_STRING_LEN + 1] = {0};
115  struct timeval tv;
116 
117  EVEL_ENTER();
118 
119  assert(header != NULL);
120 
121  gettimeofday(&tv, NULL);
122 
123  /***************************************************************************/
124  /* Initialize the header. Get a new event sequence number. Note that if */
125  /* any memory allocation fails in here we will fail gracefully because */
126  /* everything downstream can cope with NULLs. */
127  /***************************************************************************/
129  snprintf(scratchpad, EVEL_MAX_STRING_LEN, "%d", event_sequence);
130  header->event_id = strdup(scratchpad);
131  header->functional_role = strdup(functional_role);
132  header->last_epoch_microsec = tv.tv_usec + 1000000 * tv.tv_sec;
133  header->priority = EVEL_PRIORITY_NORMAL;
134  header->reporting_entity_name = strdup(openstack_vm_name());
135  header->source_name = strdup(openstack_vm_name());
136  header->sequence = event_sequence;
137  header->start_epoch_microsec = header->last_epoch_microsec;
140  event_sequence++;
141 
142  /***************************************************************************/
143  /* Optional parameters. */
144  /***************************************************************************/
148 
149  EVEL_EXIT();
150 }
151 
152 /**************************************************************************/
164 void evel_header_type_set(EVENT_HEADER * const header,
165  const char * const type)
166 {
167  EVEL_ENTER();
168 
169  /***************************************************************************/
170  /* Check preconditions. */
171  /***************************************************************************/
172  assert(header != NULL);
173  assert(type != NULL);
174 
175  evel_set_option_string(&header->event_type, type, "Event Type");
176 
177  EVEL_EXIT();
178 }
179 
180 /**************************************************************************/
189 void evel_start_epoch_set(EVENT_HEADER * const header,
190  const unsigned long long start_epoch_microsec)
191 {
192  EVEL_ENTER();
193 
194  /***************************************************************************/
195  /* Check preconditions and assign the new value. */
196  /***************************************************************************/
197  assert(header != NULL);
198  header->start_epoch_microsec = start_epoch_microsec;
199 
200  EVEL_EXIT();
201 }
202 
203 /**************************************************************************/
212 void evel_last_epoch_set(EVENT_HEADER * const header,
213  const unsigned long long last_epoch_microsec)
214 {
215  EVEL_ENTER();
216 
217  /***************************************************************************/
218  /* Check preconditions and assign the new value. */
219  /***************************************************************************/
220  assert(header != NULL);
221  header->last_epoch_microsec = last_epoch_microsec;
222 
223  EVEL_EXIT();
224 }
225 
226 /**************************************************************************/
235  const char * const entity_name)
236 {
237  EVEL_ENTER();
238 
239  /***************************************************************************/
240  /* Check preconditions and assign the new value. */
241  /***************************************************************************/
242  assert(header != NULL);
243  assert(entity_name != NULL);
244  assert(header->reporting_entity_name != NULL);
245 
246  /***************************************************************************/
247  /* Free the previously allocated memory and replace it with a copy of the */
248  /* provided one. */
249  /***************************************************************************/
250  free(header->reporting_entity_name);
251  header->reporting_entity_name = strdup(entity_name);
252 
253  EVEL_EXIT();
254 }
255 
256 /**************************************************************************/
265  const char * const entity_id)
266 {
267  EVEL_ENTER();
268 
269  /***************************************************************************/
270  /* Check preconditions and assign the new value. */
271  /***************************************************************************/
272  assert(header != NULL);
273  assert(entity_id != NULL);
274 
275  /***************************************************************************/
276  /* Free the previously allocated memory and replace it with a copy of the */
277  /* provided one. Note that evel_force_option_string strdups entity_id. */
278  /***************************************************************************/
280  evel_force_option_string(&header->reporting_entity_id, entity_id);
281 
282  EVEL_EXIT();
283 }
284 
285 /**************************************************************************/
292  EVENT_HEADER * event)
293 {
294  char * domain;
295  char * priority;
296 
297  EVEL_ENTER();
298 
299  /***************************************************************************/
300  /* Check preconditions. */
301  /***************************************************************************/
302  assert(jbuf != NULL);
303  assert(jbuf->json != NULL);
304  assert(jbuf->max_size > 0);
305  assert(event != NULL);
306 
307  domain = evel_event_domain(event->event_domain);
308  priority = evel_event_priority(event->priority);
309  evel_json_open_named_object(jbuf, "commonEventHeader");
310 
311  /***************************************************************************/
312  /* Mandatory fields. */
313  /***************************************************************************/
314  evel_enc_kv_string(jbuf, "domain", domain);
315  evel_enc_kv_string(jbuf, "eventId", event->event_id);
316  evel_enc_kv_string(jbuf, "functionalRole", event->functional_role);
317  evel_enc_kv_ull(jbuf, "lastEpochMicrosec", event->last_epoch_microsec);
318  evel_enc_kv_string(jbuf, "priority", priority);
320  jbuf, "reportingEntityName", event->reporting_entity_name);
321  evel_enc_kv_int(jbuf, "sequence", event->sequence);
322  evel_enc_kv_string(jbuf, "sourceName", event->source_name);
323  evel_enc_kv_ull(jbuf, "startEpochMicrosec", event->start_epoch_microsec);
325  jbuf, "version", event->major_version, event->minor_version);
326 
327  /***************************************************************************/
328  /* Optional fields. */
329  /***************************************************************************/
330  evel_enc_kv_opt_string(jbuf, "eventType", &event->event_type);
332  jbuf, "reportingEntityId", &event->reporting_entity_id);
333  evel_enc_kv_opt_string(jbuf, "sourceId", &event->source_id);
334 
336 
337  EVEL_EXIT();
338 }
339 
340 /**************************************************************************/
349 void evel_free_header(EVENT_HEADER * const event)
350 {
351  EVEL_ENTER();
352 
353  /***************************************************************************/
354  /* Check preconditions. As an internal API we don't allow freeing NULL */
355  /* events as we do on the public API. */
356  /***************************************************************************/
357  assert(event != NULL);
358 
359  /***************************************************************************/
360  /* Free all internal strings. */
361  /***************************************************************************/
362  free(event->event_id);
364  free(event->functional_role);
366  free(event->reporting_entity_name);
368  free(event->source_name);
369 
370  EVEL_EXIT();
371 }
372 
373 /**************************************************************************/
381 int evel_json_encode_event(char * json,
382  int max_size,
383  EVENT_HEADER * event)
384 {
385  EVEL_JSON_BUFFER json_buffer;
386  EVEL_JSON_BUFFER * jbuf = &json_buffer;
387  EVEL_THROTTLE_SPEC * throttle_spec;
388 
389  EVEL_ENTER();
390 
391  /***************************************************************************/
392  /* Get the latest throttle specification for the domain. */
393  /***************************************************************************/
394  throttle_spec = evel_get_throttle_spec(event->event_domain);
395 
396  /***************************************************************************/
397  /* Initialize the JSON_BUFFER and open the top-level objects. */
398  /***************************************************************************/
399  evel_json_buffer_init(jbuf, json, max_size, throttle_spec);
400  evel_json_open_object(jbuf);
401  evel_json_open_named_object(jbuf, "event");
402 
403  switch (event->event_domain)
404  {
406  evel_json_encode_header(jbuf, event);
407  break;
408 
409  case EVEL_DOMAIN_FAULT:
410  evel_json_encode_fault(jbuf, (EVENT_FAULT *)event);
411  break;
412 
415  break;
416 
419  break;
420 
421  case EVEL_DOMAIN_REPORT:
422  evel_json_encode_report(jbuf, (EVENT_REPORT *)event);
423  break;
424 
425  case EVEL_DOMAIN_SERVICE:
426  evel_json_encode_service(jbuf, (EVENT_SERVICE *)event);
427  break;
428 
431  break;
432 
435  break;
436 
437  case EVEL_DOMAIN_SYSLOG:
438  evel_json_encode_syslog(jbuf, (EVENT_SYSLOG *)event);
439  break;
440 
441  case EVEL_DOMAIN_OTHER:
442  evel_json_encode_other(jbuf, (EVENT_OTHER *)event);
443  break;
444 
446  default:
447  EVEL_ERROR("Unexpected domain %d", event->event_domain);
448  assert(0);
449  }
450 
453 
454  /***************************************************************************/
455  /* Sanity check. */
456  /***************************************************************************/
457  assert(jbuf->depth == 0);
458 
459  EVEL_EXIT();
460 
461  return jbuf->offset;
462 }
463 
464 /**************************************************************************/
472  const char * const vendor_id,
473  const char * const event_id)
474 {
475  EVEL_ENTER();
476 
477  /***************************************************************************/
478  /* Check preconditions. */
479  /***************************************************************************/
480  assert(instance_id != NULL);
481  assert(vendor_id != NULL);
482  assert(event_id != NULL);
483 
484  /***************************************************************************/
485  /* Store the mandatory parts. */
486  /***************************************************************************/
487  instance_id->vendor_id = strdup(vendor_id);
488  instance_id->event_id = strdup(event_id);
489 
490  /***************************************************************************/
491  /* Initialize the optional parts. */
492  /***************************************************************************/
493  evel_init_option_string(&instance_id->product_id);
494  evel_init_option_string(&instance_id->subsystem_id);
496 
497  EVEL_EXIT();
498 }
499 
500 /**************************************************************************/
506 {
507  EVEL_ENTER();
508 
509  /***************************************************************************/
510  /* Check preconditions. */
511  /***************************************************************************/
512  assert(instance_id != NULL);
513  assert(instance_id->vendor_id != NULL);
514  assert(instance_id->event_id != NULL);
515 
516  /***************************************************************************/
517  /* Free everything. */
518  /***************************************************************************/
519  free(instance_id->vendor_id);
520  free(instance_id->event_id);
521  evel_free_option_string(&instance_id->product_id);
522  evel_free_option_string(&instance_id->subsystem_id);
524 
525  EVEL_EXIT();
526 }
527 
528 /**************************************************************************/
535  EVEL_EVENT_INSTANCE_ID * instance_id)
536 {
537  EVEL_ENTER();
538 
539  /***************************************************************************/
540  /* Check preconditions. */
541  /***************************************************************************/
542  assert(jbuf != NULL);
543  assert(jbuf->json != NULL);
544  assert(jbuf->max_size > 0);
545  assert(instance_id != NULL);
546  assert(instance_id->vendor_id != NULL);
547  assert(instance_id->event_id != NULL);
548 
549  evel_json_open_named_object(jbuf, "eventInstanceIdentifier");
550 
551  /***************************************************************************/
552  /* Mandatory fields. */
553  /***************************************************************************/
554  evel_enc_kv_string(jbuf, "vendorId", instance_id->vendor_id);
555  evel_enc_kv_string(jbuf, "eventId", instance_id->event_id);
556 
557  /***************************************************************************/
558  /* Optional fields. */
559  /***************************************************************************/
560  evel_enc_kv_opt_string(jbuf, "productId", &instance_id->product_id);
561  evel_enc_kv_opt_string(jbuf, "subsystemId", &instance_id->subsystem_id);
563  jbuf, "eventFriendlyName", &instance_id->event_friendly_name);
564 
566 
567  EVEL_EXIT();
568 }
EVENT_HEADER * evel_new_heartbeat()
Create a new heartbeat event.
Definition: evel_event.c:78
EVEL_OPTION_STRING reporting_entity_id
Definition: evel.h:435
char * evel_event_domain(const EVEL_EVENT_DOMAINS domain)
Map an EVEL_EVENT_DOMAINS enum value to the equivalent string.
Definition: evel_strings.c:199
void evel_set_option_string(EVEL_OPTION_STRING *const option, const char *const value, const char *const description)
Set the value of an EVEL_OPTION_STRING.
Definition: evel_option.c:94
void evel_json_buffer_init(EVEL_JSON_BUFFER *jbuf, char *const json, const int max_size, EVEL_THROTTLE_SPEC *throttle_spec)
Initialize a EVEL_JSON_BUFFER.
int minor_version
Definition: evel.h:415
char * functional_role
Definition: evel.h:423
void evel_reporting_entity_id_set(EVENT_HEADER *const header, const char *const entity_id)
Set the Reporting Entity Id property of the event header.
Definition: evel_event.c:264
unsigned long long start_epoch_microsec
Definition: evel.h:426
EVEL_OPTION_STRING source_id
Definition: evel.h:434
Report.
Definition: evel.h:650
Event Instance Identifier JSON equivalent field: eventInstanceIdentifier.
Definition: evel.h:812
#define EVEL_INFO(FMT,...)
Definition: evel.h:3622
char * reporting_entity_name
Definition: evel.h:424
EVEL_OPTION_STRING event_type
Definition: evel.h:433
Measurement.
Definition: evel.h:504
#define EVEL_HEADER_MINOR_VERSION
Definition: evel.h:404
Wrap the OpenStack metadata service.
void evel_header_type_set(EVENT_HEADER *const header, const char *const type)
Set the Event Type property of the event header.
Definition: evel_event.c:164
void evel_json_encode_mobile_flow(EVEL_JSON_BUFFER *jbuf, EVENT_MOBILE_FLOW *event)
Encode the Mobile Flow in JSON according to AT&T&#39;s schema for the event type.
A Fault event.
Definition: evel.h:134
void evel_json_encode_service(EVEL_JSON_BUFFER *const jbuf, EVENT_SERVICE *const event)
Encode the Service Event in JSON according to AT&T&#39;s schema for the event type.
#define EVEL_EXIT()
Definition: evel.h:3631
void evel_json_encode_state_change(EVEL_JSON_BUFFER *jbuf, EVENT_STATE_CHANGE *state_change)
Encode the state change as a JSON state change.
void evel_json_encode_fault(EVEL_JSON_BUFFER *jbuf, EVENT_FAULT *event)
Encode the fault in JSON according to AT&T&#39;s schema for the fault type.
Definition: evel_fault.c:214
void evel_init_option_string(EVEL_OPTION_STRING *const option)
Initialize an EVEL_OPTION_STRING to a not-set state.
Definition: evel_option.c:72
#define EVEL_ENTER()
Definition: evel.h:3626
A Measurement for VF Reporting event.
Definition: evel.h:137
Header for EVEL library.
char * evel_event_priority(const EVEL_EVENT_PRIORITIES priority)
Map an EVEL_EVENT_PRIORITIES enum value to the equivalent string.
Definition: evel_strings.c:264
void evel_json_encode_instance_id(EVEL_JSON_BUFFER *jbuf, EVEL_EVENT_INSTANCE_ID *instance_id)
Encode the instance id as a JSON object according to AT&T&#39;s schema.
Definition: evel_event.c:534
State Change.
Definition: evel.h:952
#define EVEL_MAX_STRING_LEN
Definition: evel.h:100
Syslog.
Definition: evel.h:993
void evel_force_option_string(EVEL_OPTION_STRING *const option, const char *const value)
Force the value of an EVEL_OPTION_STRING.
Definition: evel_option.c:128
EVEL_THROTTLE_SPEC * evel_get_throttle_spec(EVEL_EVENT_DOMAINS domain)
Return the EVEL_THROTTLE_SPEC for a given domain.
void evel_enc_kv_string(EVEL_JSON_BUFFER *jbuf, const char *const key, const char *const value)
Encode a string key and string value to a EVEL_JSON_BUFFER.
A Syslog event.
Definition: evel.h:141
void evel_json_close_object(EVEL_JSON_BUFFER *jbuf)
Add the closing bracket of an object to a JSON buffer.
Event Throttling Specification for a domain which is in a throttled state.
#define EVEL_HEADER_MAJOR_VERSION
Definition: evel.h:403
void evel_enc_kv_ull(EVEL_JSON_BUFFER *jbuf, const char *const key, const unsigned long long value)
Encode a string key and unsigned long long value to a EVEL_JSON_BUFFER.
void evel_enc_kv_int(EVEL_JSON_BUFFER *jbuf, const char *const key, const int value)
Encode a string key and integer value to a EVEL_JSON_BUFFER.
char * functional_role
The Functional Role of the equipment represented by this VNF.
Definition: evel.c:63
#define EVEL_ERROR(FMT,...)
Definition: evel.h:3624
void evel_reporting_entity_name_set(EVENT_HEADER *const header, const char *const entity_name)
Set the Reporting Entity Name property of the event header.
Definition: evel_event.c:234
A Signaling event.
Definition: evel.h:139
void evel_json_open_named_object(EVEL_JSON_BUFFER *jbuf, const char *const key)
Add the opening bracket of an object to a JSON buffer.
void evel_json_encode_signaling(EVEL_JSON_BUFFER *const jbuf, EVENT_SIGNALING *const event)
Encode the Signaling in JSON according to AT&T&#39;s schema for the event type.
void evel_json_open_object(EVEL_JSON_BUFFER *jbuf)
Add the opening bracket of an object to a JSON buffer.
char * source_name
Definition: evel.h:422
A Mobile Flow event.
Definition: evel.h:136
Mobile Flow.
Definition: evel.h:740
EVEL_OPTION_STRING event_friendly_name
Definition: evel.h:825
EVEL_EVENT_DOMAINS event_domain
Definition: evel.h:420
void evel_json_encode_report(EVEL_JSON_BUFFER *jbuf, EVENT_REPORT *event)
Encode the report as a JSON report.
EVEL_OPTION_STRING product_id
Definition: evel.h:823
void evel_json_encode_header(EVEL_JSON_BUFFER *jbuf, EVENT_HEADER *event)
Encode the event as a JSON event object according to AT&T&#39;s schema.
Definition: evel_event.c:291
void evel_set_next_event_sequence(const int sequence)
Set the next event_sequence to use.
Definition: evel_event.c:59
EVEL_OPTION_STRING subsystem_id
Definition: evel.h:824
Service Events.
Definition: evel.h:839
const char * openstack_vm_name()
Get the VM name provided by the metadata service.
Definition: metadata.c:594
A Heartbeat event (event header only).
Definition: evel.h:133
void evel_json_encode_measurement(EVEL_JSON_BUFFER *jbuf, EVENT_MEASUREMENT *event)
Encode the measurement as a JSON measurement.
void log_error_state(char *format,...)
Definition: evel_logging.c:98
A State Change event.
Definition: evel.h:140
void evel_start_epoch_set(EVENT_HEADER *const header, const unsigned long long start_epoch_microsec)
Set the Start Epoch property of the event header.
Definition: evel_event.c:189
Other.
Definition: evel.h:793
void evel_free_option_string(EVEL_OPTION_STRING *const option)
Free the underlying resources of an EVEL_OPTION_STRING.
Definition: evel_option.c:48
unsigned long long last_epoch_microsec
Definition: evel.h:427
A Measurement for VF Scaling event.
Definition: evel.h:135
bool evel_enc_kv_opt_string(EVEL_JSON_BUFFER *jbuf, const char *const key, const EVEL_OPTION_STRING *const option)
Encode a string key and string value to a EVEL_JSON_BUFFER.
Event header.
Definition: evel.h:410
int evel_json_encode_event(char *json, int max_size, EVENT_HEADER *event)
Encode the event as a JSON event object according to AT&T&#39;s schema.
Definition: evel_event.c:381
Internal event, not for external routing.
Definition: evel.h:132
EVEL_EVENT_PRIORITIES priority
Definition: evel.h:425
Signaling.
Definition: evel.h:916
const char * openstack_vm_uuid()
Get the VM UUID provided by the metadata service.
Definition: metadata.c:604
EVEL throttle definitions.
void evel_json_encode_syslog(EVEL_JSON_BUFFER *jbuf, EVENT_SYSLOG *event)
Encode the Syslog in JSON according to AT&T&#39;s schema for the event type.
Definition: evel_syslog.c:354
EVEL internal definitions.
void evel_init_event_instance_id(EVEL_EVENT_INSTANCE_ID *const instance_id, const char *const vendor_id, const char *const event_id)
Initialize an event instance id.
Definition: evel_event.c:471
char * event_id
Definition: evel.h:421
A Service event.
Definition: evel.h:138
void evel_enc_version(EVEL_JSON_BUFFER *jbuf, const char *const key, const int major_version, const int minor_version)
Encode a key and version.
void evel_free_event_instance_id(EVEL_EVENT_INSTANCE_ID *const instance_id)
Free an event instance id.
Definition: evel_event.c:505
void evel_json_encode_other(EVEL_JSON_BUFFER *jbuf, EVENT_OTHER *event)
Encode the Other in JSON according to AT&T&#39;s schema for the event type.
Definition: evel_other.c:160
void evel_free_header(EVENT_HEADER *const event)
Free an event header.
Definition: evel_event.c:349
int major_version
Definition: evel.h:414
Fault.
Definition: evel.h:449
int sequence
Definition: evel.h:428
void evel_last_epoch_set(EVENT_HEADER *const header, const unsigned long long last_epoch_microsec)
Set the Last Epoch property of the event header.
Definition: evel_event.c:212
void evel_init_header(EVENT_HEADER *const header)
Initialize a newly created event header.
Definition: evel_event.c:112