1 /*************************************************************************//**
3 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5 * Unless otherwise specified, all software contained herein is
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
17 ****************************************************************************/
19 /**************************************************************************//**
21 * Implementation of EVEL functions relating to Heartbeat fields.
23 ****************************************************************************/
30 #include "evel_throttle.h"
32 /**************************************************************************//**
33 * Create a new Heartbeat fields event.
35 * @note The mandatory fields on the Heartbeat fields must be supplied to
36 * this factory function and are immutable once set. Optional fields
37 * have explicit setter functions, but again values may only be set
38 * once so that the event has immutable properties.
39 * @param event_name Unique Event Name confirming Domain AsdcModel Description
40 * @param event_id A universal identifier of the event for: troubleshooting correlation, analysis, etc
41 * @param vendor_id The vendor id to encode in the event instance id.
42 * @param event_id The vendor event id to encode in the event instance id.
43 * @returns pointer to the newly manufactured ::EVENT_HEARTBEAT_FIELD. If the event
44 * is not used (i.e. posted) it must be released using
45 * ::evel_free_hrtbt_field.
46 * @retval NULL Failed to create the event.
47 *****************************************************************************/
48 EVENT_HEARTBEAT_FIELD * evel_new_heartbeat_field(int interval,const char* ev_name, const char *ev_id)
50 EVENT_HEARTBEAT_FIELD * event = NULL;
54 /***************************************************************************/
55 /* Check preconditions. */
56 /***************************************************************************/
59 /***************************************************************************/
60 /* Allocate the Heartbeat fields event. */
61 /***************************************************************************/
62 event = malloc(sizeof(EVENT_HEARTBEAT_FIELD));
65 log_error_state("Out of memory");
68 memset(event, 0, sizeof(EVENT_HEARTBEAT_FIELD));
69 EVEL_DEBUG("New Heartbeat fields event is at %lp", event);
71 /***************************************************************************/
72 /* Initialize the header & the Heartbeat fields fields. */
73 /***************************************************************************/
74 evel_init_header_nameid(&event->header,ev_name,ev_id);
75 event->header.event_domain = EVEL_DOMAIN_HEARTBEAT_FIELD;
76 event->major_version = EVEL_HEARTBEAT_FIELD_MAJOR_VERSION;
77 event->minor_version = EVEL_HEARTBEAT_FIELD_MINOR_VERSION;
79 event->heartbeat_interval = interval;
80 dlist_initialize(&event->additional_info);
88 /**************************************************************************//**
89 * Add a name/value pair to the Heartbeat fields, under the additionalFields array.
91 * The name and value are null delimited ASCII strings. The library takes
92 * a copy so the caller does not have to preserve values after the function
95 * @param event Pointer to the Heartbeat fields event.
96 * @param name ASCIIZ string with the field's name. The caller does not
97 * need to preserve the value once the function returns.
98 * @param value ASCIIZ string with the field's value. The caller does not
99 * need to preserve the value once the function returns.
100 *****************************************************************************/
101 void evel_hrtbt_field_addl_field_add(EVENT_HEARTBEAT_FIELD * const event,
102 const char * const name,
103 const char * const value)
105 OTHER_FIELD * nv_pair = NULL;
109 /***************************************************************************/
110 /* Check preconditions. */
111 /***************************************************************************/
112 assert(event != NULL);
113 assert(event->header.event_domain == EVEL_DOMAIN_HEARTBEAT_FIELD);
114 assert(name != NULL);
115 assert(value != NULL);
117 EVEL_DEBUG("Adding name=%s value=%s", name, value);
118 nv_pair = malloc(sizeof(OTHER_FIELD));
119 assert(nv_pair != NULL);
120 nv_pair->name = strdup(name);
121 nv_pair->value = strdup(value);
122 assert(nv_pair->name != NULL);
123 assert(nv_pair->value != NULL);
125 dlist_push_last(&event->additional_info, nv_pair);
130 /**************************************************************************//**
131 * Set the Interval property of the Heartbeat fields event.
133 * @note The property is treated as immutable: it is only valid to call
134 * the setter once. However, we don't assert if the caller tries to
135 * overwrite, just ignoring the update instead.
137 * @param event Pointer to the Heartbeat fields event.
138 * @param product_id The vendor product id to be set. ASCIIZ string. The
139 * caller does not need to preserve the value once the
141 *****************************************************************************/
142 void evel_hrtbt_interval_set(EVENT_HEARTBEAT_FIELD * const event,
147 /***************************************************************************/
148 /* Check preconditions and call evel_set_option_string. */
149 /***************************************************************************/
150 assert(event != NULL);
151 assert(event->header.event_domain == EVEL_DOMAIN_HEARTBEAT_FIELD);
152 assert(interval > 0);
154 event->heartbeat_interval = interval;
159 /**************************************************************************//**
160 * Encode the Heartbeat fields in JSON according to AT&T's schema for the
163 * @param jbuf Pointer to the ::EVEL_JSON_BUFFER to encode into.
164 * @param event Pointer to the ::EVENT_HEADER to encode.
165 *****************************************************************************/
166 void evel_json_encode_hrtbt_field(EVEL_JSON_BUFFER * const jbuf,
167 EVENT_HEARTBEAT_FIELD * const event)
169 OTHER_FIELD * nv_pair = NULL;
170 DLIST_ITEM * dlist_item = NULL;
174 /***************************************************************************/
175 /* Check preconditions. */
176 /***************************************************************************/
177 assert(event != NULL);
178 assert(event->header.event_domain == EVEL_DOMAIN_HEARTBEAT_FIELD);
180 evel_json_encode_header(jbuf, &event->header);
181 evel_json_open_named_object(jbuf, "heartbeatField");
183 /***************************************************************************/
184 /* Mandatory fields */
185 /***************************************************************************/
186 evel_enc_version(jbuf, "heartbeatFieldsVersion", event->major_version,event->minor_version);
187 evel_enc_kv_int(jbuf, "heartbeatInterval", event->heartbeat_interval);
189 /***************************************************************************/
190 /* Optional fields */
191 /***************************************************************************/
193 /***************************************************************************/
194 /* Checkpoint, so that we can wind back if all fields are suppressed. */
195 /***************************************************************************/
196 evel_json_checkpoint(jbuf);
197 if (evel_json_open_opt_named_list(jbuf, "additionalFields"))
201 dlist_item = dlist_get_first(&event->additional_info);
202 while (dlist_item != NULL)
204 nv_pair = (OTHER_FIELD *) dlist_item->item;
205 assert(nv_pair != NULL);
207 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
211 evel_json_open_object(jbuf);
212 evel_enc_kv_string(jbuf, "name", nv_pair->name);
213 evel_enc_kv_string(jbuf, "value", nv_pair->value);
214 evel_json_close_object(jbuf);
217 dlist_item = dlist_get_next(dlist_item);
219 evel_json_close_list(jbuf);
221 /*************************************************************************/
222 /* If we've not written anything, rewind to before we opened the list. */
223 /*************************************************************************/
226 evel_json_rewind(jbuf);
230 evel_json_close_object(jbuf);
235 /**************************************************************************//**
236 * Free a Heartbeat fields event.
238 * Free off the event supplied. Will free all the contained allocated memory.
240 * @note It does not free the event itself, since that may be part of a larger
242 *****************************************************************************/
243 void evel_free_hrtbt_field(EVENT_HEARTBEAT_FIELD * const event)
245 OTHER_FIELD * nv_pair = NULL;
249 /***************************************************************************/
250 /* Check preconditions. */
251 /***************************************************************************/
252 assert(event != NULL);
253 assert(event->header.event_domain == EVEL_DOMAIN_HEARTBEAT_FIELD);
255 /***************************************************************************/
256 /* Free all internal strings then the header itself. */
257 /***************************************************************************/
258 nv_pair = dlist_pop_last(&event->additional_info);
259 while (nv_pair != NULL)
261 EVEL_DEBUG("Freeing Other Field (%s, %s)", nv_pair->name, nv_pair->value);
263 free(nv_pair->value);
265 nv_pair = dlist_pop_last(&event->additional_info);
267 evel_free_header(&event->header);