1 /**************************************************************************//**
3 * Implementation of EVEL functions relating to the State Change.
8 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *****************************************************************************/
26 #include "evel_throttle.h"
28 /**************************************************************************//**
29 * Create a new State Change event.
31 * @note The mandatory fields on the State Change must be supplied to this
32 * factory function and are immutable once set. Optional fields have
33 * explicit setter functions, but again values may only be set once
34 * so that the State Change has immutable properties.
36 * @param new_state The new state of the reporting entity.
37 * @param old_state The old state of the reporting entity.
38 * @param interface The card or port name of the reporting entity.
40 * @returns pointer to the newly manufactured ::EVENT_STATE_CHANGE. If the
41 * event is not used it must be released using
42 * ::evel_free_state_change
43 * @retval NULL Failed to create the event.
44 *****************************************************************************/
45 EVENT_STATE_CHANGE * evel_new_state_change(const EVEL_ENTITY_STATE new_state,
46 const EVEL_ENTITY_STATE old_state,
47 const char * const interface)
49 EVENT_STATE_CHANGE * state_change = NULL;
52 /***************************************************************************/
53 /* Check preconditions. */
54 /***************************************************************************/
55 assert(new_state < EVEL_MAX_ENTITY_STATES);
56 assert(old_state < EVEL_MAX_ENTITY_STATES);
57 assert(interface != NULL);
59 /***************************************************************************/
60 /* Allocate the State Change. */
61 /***************************************************************************/
62 state_change = malloc(sizeof(EVENT_STATE_CHANGE));
63 if (state_change == NULL)
65 log_error_state("Out of memory");
68 memset(state_change, 0, sizeof(EVENT_STATE_CHANGE));
69 EVEL_DEBUG("New State Change is at %lp", state_change);
71 /***************************************************************************/
72 /* Initialize the header & the State Change fields. Optional string */
73 /* values are uninitialized (NULL). */
74 /***************************************************************************/
75 evel_init_header(&state_change->header);
76 state_change->header.event_domain = EVEL_DOMAIN_STATE_CHANGE;
77 state_change->major_version = EVEL_STATE_CHANGE_MAJOR_VERSION;
78 state_change->minor_version = EVEL_STATE_CHANGE_MINOR_VERSION;
79 state_change->new_state = new_state;
80 state_change->old_state = old_state;
81 state_change->state_interface = strdup(interface);
82 dlist_initialize(&state_change->additional_fields);
89 /**************************************************************************//**
90 * Free a State Change.
92 * Free off the State Change supplied. Will free all the contained allocated
95 * @note It does not free the State Change itself, since that may be part of a
97 *****************************************************************************/
98 void evel_free_state_change(EVENT_STATE_CHANGE * const state_change)
100 STATE_CHANGE_ADDL_FIELD * addl_field = NULL;
104 /***************************************************************************/
105 /* Check preconditions. As an internal API we don't allow freeing NULL */
106 /* events as we do on the public API. */
107 /***************************************************************************/
108 assert(state_change != NULL);
109 assert(state_change->header.event_domain == EVEL_DOMAIN_STATE_CHANGE);
111 /***************************************************************************/
112 /* Free all internal strings then the header itself. */
113 /***************************************************************************/
114 addl_field = dlist_pop_last(&state_change->additional_fields);
115 while (addl_field != NULL)
117 EVEL_DEBUG("Freeing Additional Field (%s, %s)",
120 free(addl_field->name);
121 free(addl_field->value);
123 addl_field = dlist_pop_last(&state_change->additional_fields);
125 free(state_change->state_interface);
126 evel_free_header(&state_change->header);
131 /**************************************************************************//**
132 * Set the Event Type property of the State Change.
134 * @note The property is treated as immutable: it is only valid to call
135 * the setter once. However, we don't assert if the caller tries to
136 * overwrite, just ignoring the update instead.
138 * @param state_change Pointer to the ::EVENT_STATE_CHANGE.
139 * @param type The Event Type to be set. ASCIIZ string. The caller
140 * does not need to preserve the value once the function
142 *****************************************************************************/
143 void evel_state_change_type_set(EVENT_STATE_CHANGE * const state_change,
144 const char * const type)
148 /***************************************************************************/
149 /* Check preconditions and call evel_header_type_set. */
150 /***************************************************************************/
151 assert(state_change != NULL);
152 assert(state_change->header.event_domain == EVEL_DOMAIN_STATE_CHANGE);
153 evel_header_type_set(&state_change->header, type);
158 /**************************************************************************//**
159 * Add an additional field name/value pair to the State Change.
161 * The name and value are null delimited ASCII strings. The library takes
162 * a copy so the caller does not have to preserve values after the function
165 * @param state_change Pointer to the ::EVENT_STATE_CHANGE.
166 * @param name ASCIIZ string with the attribute's name. The caller
167 * does not need to preserve the value once the function
169 * @param value ASCIIZ string with the attribute's value. The caller
170 * does not need to preserve the value once the function
172 *****************************************************************************/
173 void evel_state_change_addl_field_add(EVENT_STATE_CHANGE * const state_change,
174 const char * const name,
175 const char * const value)
177 STATE_CHANGE_ADDL_FIELD * addl_field = NULL;
180 /***************************************************************************/
181 /* Check preconditions. */
182 /***************************************************************************/
183 assert(state_change != NULL);
184 assert(state_change->header.event_domain == EVEL_DOMAIN_STATE_CHANGE);
185 assert(name != NULL);
186 assert(value != NULL);
188 EVEL_DEBUG("Adding name=%s value=%s", name, value);
189 addl_field = malloc(sizeof(STATE_CHANGE_ADDL_FIELD));
190 assert(addl_field != NULL);
191 memset(addl_field, 0, sizeof(STATE_CHANGE_ADDL_FIELD));
192 addl_field->name = strdup(name);
193 addl_field->value = strdup(value);
194 assert(addl_field->name != NULL);
195 assert(addl_field->value != NULL);
197 dlist_push_last(&state_change->additional_fields, addl_field);
202 /**************************************************************************//**
203 * Encode the state change as a JSON state change.
205 * @param jbuf Pointer to the ::EVEL_JSON_BUFFER to encode into.
206 * @param state_change Pointer to the ::EVENT_STATE_CHANGE to encode.
207 *****************************************************************************/
208 void evel_json_encode_state_change(EVEL_JSON_BUFFER * jbuf,
209 EVENT_STATE_CHANGE * state_change)
211 STATE_CHANGE_ADDL_FIELD * addl_field = NULL;
212 DLIST_ITEM * addl_field_item = NULL;
218 /***************************************************************************/
219 /* Check preconditions. */
220 /***************************************************************************/
221 assert(state_change != NULL);
222 assert(state_change->header.event_domain == EVEL_DOMAIN_STATE_CHANGE);
224 new_state = evel_entity_state(state_change->new_state);
225 old_state = evel_entity_state(state_change->old_state);
227 evel_json_encode_header(jbuf, &state_change->header);
228 evel_json_open_named_object(jbuf, "stateChangeFields");
230 /***************************************************************************/
231 /* Mandatory fields. */
232 /***************************************************************************/
233 evel_enc_kv_string(jbuf, "newState", new_state);
234 evel_enc_kv_string(jbuf, "oldState", old_state);
235 evel_enc_kv_string(jbuf, "stateInterface", state_change->state_interface);
237 /***************************************************************************/
238 /* Optional fields. */
239 /***************************************************************************/
240 evel_json_checkpoint(jbuf);
241 if (evel_json_open_opt_named_list(jbuf, "additionalFields"))
243 bool item_added = false;
245 addl_field_item = dlist_get_first(&state_change->additional_fields);
246 while (addl_field_item != NULL)
248 addl_field = (STATE_CHANGE_ADDL_FIELD *) addl_field_item->item;
249 assert(addl_field != NULL);
251 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
255 evel_json_open_object(jbuf);
256 evel_enc_kv_string(jbuf, "name", addl_field->name);
257 evel_enc_kv_string(jbuf, "value", addl_field->value);
258 evel_json_close_object(jbuf);
261 addl_field_item = dlist_get_next(addl_field_item);
263 evel_json_close_list(jbuf);
265 /*************************************************************************/
266 /* If we've not written anything, rewind to before we opened the list. */
267 /*************************************************************************/
270 evel_json_rewind(jbuf);
274 evel_enc_version(jbuf,
275 "stateChangeFieldsVersion",
276 state_change->major_version,
277 state_change->minor_version);
279 evel_json_close_object(jbuf);