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.
17 ****************************************************************************/
18 /**************************************************************************//**
20 * Implementation of EVEL functions relating to the State Change.
22 ****************************************************************************/
28 #include "evel_throttle.h"
30 /**************************************************************************//**
31 * Create a new State Change event.
33 * @note The mandatory fields on the State Change must be supplied to this
34 * factory function and are immutable once set. Optional fields have
35 * explicit setter functions, but again values may only be set once
36 * so that the State Change has immutable properties.
38 * @param event_name Unique Event Name confirming Domain AsdcModel Description
39 * @param event_id A universal identifier of the event for: troubleshooting correlation, analysis, etc
40 * @param new_state The new state of the reporting entity.
41 * @param old_state The old state of the reporting entity.
42 * @param interface The card or port name of the reporting entity.
44 * @returns pointer to the newly manufactured ::EVENT_STATE_CHANGE. If the
45 * event is not used it must be released using
46 * ::evel_free_state_change
47 * @retval NULL Failed to create the event.
48 *****************************************************************************/
49 EVENT_STATE_CHANGE * evel_new_state_change(const char* ev_name,
51 const EVEL_ENTITY_STATE new_state,
52 const EVEL_ENTITY_STATE old_state,
53 const char * const interface)
55 EVENT_STATE_CHANGE * state_change = NULL;
58 /***************************************************************************/
59 /* Check preconditions. */
60 /***************************************************************************/
61 assert(new_state < EVEL_MAX_ENTITY_STATES);
62 assert(old_state < EVEL_MAX_ENTITY_STATES);
63 assert(interface != NULL);
65 /***************************************************************************/
66 /* Allocate the State Change. */
67 /***************************************************************************/
68 state_change = malloc(sizeof(EVENT_STATE_CHANGE));
69 if (state_change == NULL)
71 log_error_state("Out of memory");
74 memset(state_change, 0, sizeof(EVENT_STATE_CHANGE));
75 EVEL_DEBUG("New State Change is at %lp", state_change);
77 /***************************************************************************/
78 /* Initialize the header & the State Change fields. Optional string */
79 /* values are uninitialized (NULL). */
80 /***************************************************************************/
81 evel_init_header_nameid(&state_change->header,ev_name,ev_id);
82 state_change->header.event_domain = EVEL_DOMAIN_STATE_CHANGE;
83 state_change->major_version = EVEL_STATE_CHANGE_MAJOR_VERSION;
84 state_change->minor_version = EVEL_STATE_CHANGE_MINOR_VERSION;
85 state_change->new_state = new_state;
86 state_change->old_state = old_state;
87 state_change->state_interface = strdup(interface);
88 dlist_initialize(&state_change->additional_fields);
95 /**************************************************************************//**
96 * Free a State Change.
98 * Free off the State Change supplied. Will free all the contained allocated
101 * @note It does not free the State Change itself, since that may be part of a
103 *****************************************************************************/
104 void evel_free_state_change(EVENT_STATE_CHANGE * const state_change)
106 STATE_CHANGE_ADDL_FIELD * addl_field = NULL;
110 /***************************************************************************/
111 /* Check preconditions. As an internal API we don't allow freeing NULL */
112 /* events as we do on the public API. */
113 /***************************************************************************/
114 assert(state_change != NULL);
115 assert(state_change->header.event_domain == EVEL_DOMAIN_STATE_CHANGE);
117 /***************************************************************************/
118 /* Free all internal strings then the header itself. */
119 /***************************************************************************/
120 addl_field = dlist_pop_last(&state_change->additional_fields);
121 while (addl_field != NULL)
123 EVEL_DEBUG("Freeing Additional Field (%s, %s)",
126 free(addl_field->name);
127 free(addl_field->value);
129 addl_field = dlist_pop_last(&state_change->additional_fields);
131 free(state_change->state_interface);
132 evel_free_header(&state_change->header);
137 /**************************************************************************//**
138 * Set the Event Type property of the State Change.
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 state_change Pointer to the ::EVENT_STATE_CHANGE.
145 * @param type The Event Type to be set. ASCIIZ string. The caller
146 * does not need to preserve the value once the function
148 *****************************************************************************/
149 void evel_state_change_type_set(EVENT_STATE_CHANGE * const state_change,
150 const char * const type)
154 /***************************************************************************/
155 /* Check preconditions and call evel_header_type_set. */
156 /***************************************************************************/
157 assert(state_change != NULL);
158 assert(state_change->header.event_domain == EVEL_DOMAIN_STATE_CHANGE);
159 evel_header_type_set(&state_change->header, type);
164 /**************************************************************************//**
165 * Add an additional field name/value pair to the State Change.
167 * The name and value are null delimited ASCII strings. The library takes
168 * a copy so the caller does not have to preserve values after the function
171 * @param state_change Pointer to the ::EVENT_STATE_CHANGE.
172 * @param name ASCIIZ string with the attribute's name. The caller
173 * does not need to preserve the value once the function
175 * @param value ASCIIZ string with the attribute's value. The caller
176 * does not need to preserve the value once the function
178 *****************************************************************************/
179 void evel_state_change_addl_field_add(EVENT_STATE_CHANGE * const state_change,
180 const char * const name,
181 const char * const value)
183 STATE_CHANGE_ADDL_FIELD * addl_field = NULL;
186 /***************************************************************************/
187 /* Check preconditions. */
188 /***************************************************************************/
189 assert(state_change != NULL);
190 assert(state_change->header.event_domain == EVEL_DOMAIN_STATE_CHANGE);
191 assert(name != NULL);
192 assert(value != NULL);
194 EVEL_DEBUG("Adding name=%s value=%s", name, value);
195 addl_field = malloc(sizeof(STATE_CHANGE_ADDL_FIELD));
196 assert(addl_field != NULL);
197 memset(addl_field, 0, sizeof(STATE_CHANGE_ADDL_FIELD));
198 addl_field->name = strdup(name);
199 addl_field->value = strdup(value);
200 assert(addl_field->name != NULL);
201 assert(addl_field->value != NULL);
203 dlist_push_last(&state_change->additional_fields, addl_field);
208 /**************************************************************************//**
209 * Encode the state change as a JSON state change.
211 * @param jbuf Pointer to the ::EVEL_JSON_BUFFER to encode into.
212 * @param state_change Pointer to the ::EVENT_STATE_CHANGE to encode.
213 *****************************************************************************/
214 void evel_json_encode_state_change(EVEL_JSON_BUFFER * jbuf,
215 EVENT_STATE_CHANGE * state_change)
217 STATE_CHANGE_ADDL_FIELD * addl_field = NULL;
218 DLIST_ITEM * addl_field_item = NULL;
224 /***************************************************************************/
225 /* Check preconditions. */
226 /***************************************************************************/
227 assert(state_change != NULL);
228 assert(state_change->header.event_domain == EVEL_DOMAIN_STATE_CHANGE);
230 new_state = evel_entity_state(state_change->new_state);
231 old_state = evel_entity_state(state_change->old_state);
233 evel_json_encode_header(jbuf, &state_change->header);
234 evel_json_open_named_object(jbuf, "stateChangeFields");
236 /***************************************************************************/
237 /* Mandatory fields. */
238 /***************************************************************************/
239 evel_enc_kv_string(jbuf, "newState", new_state);
240 evel_enc_kv_string(jbuf, "oldState", old_state);
241 evel_enc_kv_string(jbuf, "stateInterface", state_change->state_interface);
243 /***************************************************************************/
244 /* Optional fields. */
245 /***************************************************************************/
246 evel_json_checkpoint(jbuf);
247 if (evel_json_open_opt_named_list(jbuf, "additionalFields"))
249 bool item_added = false;
251 addl_field_item = dlist_get_first(&state_change->additional_fields);
252 while (addl_field_item != NULL)
254 addl_field = (STATE_CHANGE_ADDL_FIELD *) addl_field_item->item;
255 assert(addl_field != NULL);
257 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
261 evel_json_open_object(jbuf);
262 evel_enc_kv_string(jbuf, "name", addl_field->name);
263 evel_enc_kv_string(jbuf, "value", addl_field->value);
264 evel_json_close_object(jbuf);
267 addl_field_item = dlist_get_next(addl_field_item);
269 evel_json_close_list(jbuf);
271 /*************************************************************************/
272 /* If we've not written anything, rewind to before we opened the list. */
273 /*************************************************************************/
276 evel_json_rewind(jbuf);
280 evel_enc_version(jbuf,
281 "stateChangeFieldsVersion",
282 state_change->major_version,state_change->minor_version);
284 evel_json_close_object(jbuf);