1 /*************************************************************************//**
3 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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
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.
16 ****************************************************************************/
18 /**************************************************************************//**
20 * Implementation of EVEL functions relating to the Measurement for VF
23 * @note This is an experimental event tytpe and does not form part of the
24 * currently approved AT&T event schema. It is intended to allow a
25 * less-onerous event reporting mechanism because it avoids having to
26 * return all the platform statistics which are mandatory in the
27 * **measurementsForVfScaling** event.
28 ****************************************************************************/
35 #include "evel_internal.h"
36 #include "evel_throttle.h"
38 /**************************************************************************//**
39 * Create a new Report event.
41 * @note The mandatory fields on the Report must be supplied to this
42 * factory function and are immutable once set. Optional fields have
43 * explicit setter functions, but again values may only be set once so
44 * that the Report has immutable properties.
46 * @param measurement_interval
47 * @param event_name Unique Event Name
48 * @param event_id A universal identifier of the event for analysis etc
49 * @returns pointer to the newly manufactured ::EVENT_REPORT. If the event is
50 * not used (i.e. posted) it must be released using ::evel_free_event.
51 * @retval NULL Failed to create the event.
52 *****************************************************************************/
53 EVENT_REPORT * evel_new_report(double measurement_interval,const char *ev_name, const char *ev_id)
55 EVENT_REPORT * report = NULL;
59 /***************************************************************************/
60 /* Check preconditions. */
61 /***************************************************************************/
62 assert(measurement_interval >= 0.0);
64 /***************************************************************************/
65 /* Allocate the report. */
66 /***************************************************************************/
67 report = malloc(sizeof(EVENT_REPORT));
70 log_error_state("Out of memory for Report");
73 memset(report, 0, sizeof(EVENT_REPORT));
74 EVEL_DEBUG("New report is at %lp", report);
76 /***************************************************************************/
77 /* Initialize the header & the report fields. */
78 /***************************************************************************/
79 evel_init_header_nameid(&report->header,ev_name,ev_id);
80 report->header.event_domain = EVEL_DOMAIN_REPORT;
81 report->measurement_interval = measurement_interval;
83 dlist_initialize(&report->feature_usage);
84 dlist_initialize(&report->measurement_groups);
85 report->major_version = EVEL_REPORT_MAJOR_VERSION;
86 report->minor_version = EVEL_REPORT_MINOR_VERSION;
93 /**************************************************************************//**
94 * Set the Event Type property of the Report.
96 * @note The property is treated as immutable: it is only valid to call
97 * the setter once. However, we don't assert if the caller tries to
98 * overwrite, just ignoring the update instead.
100 * @param report Pointer to the Report.
101 * @param type The Event Type to be set. ASCIIZ string. The caller
102 * does not need to preserve the value once the function
104 *****************************************************************************/
105 void evel_report_type_set(EVENT_REPORT * report,
106 const char * const type)
110 /***************************************************************************/
111 /* Check preconditions and call evel_header_type_set. */
112 /***************************************************************************/
113 assert(report != NULL);
114 assert(report->header.event_domain == EVEL_DOMAIN_REPORT);
115 evel_header_type_set(&report->header, type);
120 /**************************************************************************//**
121 * Add a Feature usage value name/value pair to the Report.
123 * The name is null delimited ASCII string. The library takes
124 * a copy so the caller does not have to preserve values after the function
127 * @param report Pointer to the report.
128 * @param feature ASCIIZ string with the feature's name.
129 * @param utilization Utilization of the feature.
130 *****************************************************************************/
131 void evel_report_feature_use_add(EVENT_REPORT * report,
135 MEASUREMENT_FEATURE_USE * feature_use = NULL;
138 /***************************************************************************/
139 /* Check assumptions. */
140 /***************************************************************************/
141 assert(report != NULL);
142 assert(report->header.event_domain == EVEL_DOMAIN_REPORT);
143 assert(feature != NULL);
144 assert(utilization >= 0);
146 /***************************************************************************/
147 /* Allocate a container for the value and push onto the list. */
148 /***************************************************************************/
149 EVEL_DEBUG("Adding Feature=%s Use=%d", feature, utilization);
150 feature_use = malloc(sizeof(MEASUREMENT_FEATURE_USE));
151 assert(feature_use != NULL);
152 memset(feature_use, 0, sizeof(MEASUREMENT_FEATURE_USE));
153 feature_use->feature_id = strdup(feature);
154 assert(feature_use->feature_id != NULL);
155 feature_use->feature_utilization = utilization;
157 dlist_push_last(&report->feature_usage, feature_use);
162 /**************************************************************************//**
163 * Add a Additional Measurement value name/value pair to the Report.
165 * The name is null delimited ASCII string. The library takes
166 * a copy so the caller does not have to preserve values after the function
169 * @param report Pointer to the report.
170 * @param group ASCIIZ string with the measurement group's name.
171 * @param name ASCIIZ string containing the measurement's name.
172 * @param value ASCIIZ string containing the measurement's value.
173 *****************************************************************************/
174 void evel_report_custom_measurement_add(EVENT_REPORT * report,
175 const char * const group,
176 const char * const name,
177 const char * const value)
179 MEASUREMENT_GROUP * measurement_group = NULL;
180 CUSTOM_MEASUREMENT * measurement = NULL;
181 DLIST_ITEM * item = NULL;
184 /***************************************************************************/
185 /* Check assumptions. */
186 /***************************************************************************/
187 assert(report != NULL);
188 assert(report->header.event_domain == EVEL_DOMAIN_REPORT);
189 assert(group != NULL);
190 assert(name != NULL);
191 assert(value != NULL);
193 /***************************************************************************/
194 /* Allocate a container for the name/value pair. */
195 /***************************************************************************/
196 EVEL_DEBUG("Adding Measurement Group=%s Name=%s Value=%s",
198 measurement = malloc(sizeof(CUSTOM_MEASUREMENT));
199 assert(measurement != NULL);
200 memset(measurement, 0, sizeof(CUSTOM_MEASUREMENT));
201 measurement->name = strdup(name);
202 assert(measurement->name != NULL);
203 measurement->value = strdup(value);
204 assert(measurement->value != NULL);
206 /***************************************************************************/
207 /* See if we have that group already. */
208 /***************************************************************************/
209 item = dlist_get_first(&report->measurement_groups);
212 measurement_group = (MEASUREMENT_GROUP *) item->item;
213 assert(measurement_group != NULL);
215 EVEL_DEBUG("Got measurement group %s", measurement_group->name);
216 if (strcmp(group, measurement_group->name) == 0)
218 EVEL_DEBUG("Found existing Measurement Group");
221 item = dlist_get_next(item);
224 /***************************************************************************/
225 /* If we didn't have the group already, create it. */
226 /***************************************************************************/
229 EVEL_DEBUG("Creating new Measurement Group");
230 measurement_group = malloc(sizeof(MEASUREMENT_GROUP));
231 assert(measurement_group != NULL);
232 memset(measurement_group, 0, sizeof(MEASUREMENT_GROUP));
233 measurement_group->name = strdup(group);
234 assert(measurement_group->name != NULL);
235 dlist_initialize(&measurement_group->measurements);
236 dlist_push_last(&report->measurement_groups, measurement_group);
239 /***************************************************************************/
240 /* If we didn't have the group already, create it. */
241 /***************************************************************************/
242 dlist_push_last(&measurement_group->measurements, measurement);
247 /**************************************************************************//**
248 * Encode the report as a JSON report.
250 * @param jbuf Pointer to the ::EVEL_JSON_BUFFER to encode into.
251 * @param event Pointer to the ::EVENT_HEADER to encode.
252 *****************************************************************************/
253 void evel_json_encode_report(EVEL_JSON_BUFFER * jbuf,
254 EVENT_REPORT * event)
256 MEASUREMENT_FEATURE_USE * feature_use = NULL;
257 MEASUREMENT_GROUP * measurement_group = NULL;
258 CUSTOM_MEASUREMENT * custom_measurement = NULL;
259 DLIST_ITEM * item = NULL;
260 DLIST_ITEM * nested_item = NULL;
264 /***************************************************************************/
265 /* Check preconditions. */
266 /***************************************************************************/
267 assert(event != NULL);
268 assert(event->header.event_domain == EVEL_DOMAIN_REPORT);
270 evel_json_encode_header(jbuf, &event->header);
271 evel_json_open_named_object(jbuf, "measurementsForVfReportingFields");
272 evel_enc_kv_double(jbuf, "measurementInterval", event->measurement_interval);
274 /***************************************************************************/
275 /* Feature Utilization list. */
276 /***************************************************************************/
277 evel_json_checkpoint(jbuf);
278 if (evel_json_open_opt_named_list(jbuf, "featureUsageArray"))
280 bool item_added = false;
282 item = dlist_get_first(&event->feature_usage);
285 feature_use = (MEASUREMENT_FEATURE_USE*) item->item;
286 assert(feature_use != NULL);
288 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
290 feature_use->feature_id))
292 evel_json_open_object(jbuf);
293 evel_enc_kv_string(jbuf, "featureIdentifier", feature_use->feature_id);
295 jbuf, "featureUtilization", feature_use->feature_utilization);
296 evel_json_close_object(jbuf);
299 item = dlist_get_next(item);
301 evel_json_close_list(jbuf);
303 /*************************************************************************/
304 /* If we've not written anything, rewind to before we opened the list. */
305 /*************************************************************************/
308 evel_json_rewind(jbuf);
312 /***************************************************************************/
313 /* Additional Measurement Groups list. */
314 /***************************************************************************/
315 evel_json_checkpoint(jbuf);
316 if (evel_json_open_opt_named_list(jbuf, "additionalMeasurements"))
318 bool item_added = false;
320 item = dlist_get_first(&event->measurement_groups);
323 measurement_group = (MEASUREMENT_GROUP *) item->item;
324 assert(measurement_group != NULL);
326 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
327 "additionalMeasurements",
328 measurement_group->name))
330 evel_json_open_object(jbuf);
331 evel_enc_kv_string(jbuf, "name", measurement_group->name);
332 evel_json_open_named_list(jbuf, "arrayOfFields");
334 /*********************************************************************/
335 /* Measurements list. */
336 /*********************************************************************/
337 nested_item = dlist_get_first(&measurement_group->measurements);
338 while (nested_item != NULL)
340 custom_measurement = (CUSTOM_MEASUREMENT *) nested_item->item;
341 assert(custom_measurement != NULL);
343 evel_json_open_object(jbuf);
344 evel_enc_kv_string(jbuf, "name", custom_measurement->name);
345 evel_enc_kv_string(jbuf, "value", custom_measurement->value);
346 evel_json_close_object(jbuf);
347 nested_item = dlist_get_next(nested_item);
349 evel_json_close_list(jbuf);
350 evel_json_close_object(jbuf);
353 item = dlist_get_next(item);
355 evel_json_close_list(jbuf);
357 /*************************************************************************/
358 /* If we've not written anything, rewind to before we opened the list. */
359 /*************************************************************************/
362 evel_json_rewind(jbuf);
366 /***************************************************************************/
367 /* Although optional, we always generate the version. Note that this */
368 /* closes the object, too. */
369 /***************************************************************************/
370 evel_enc_version(jbuf,
371 "measurementFieldsVersion",
372 event->major_version,
373 event->minor_version);
374 evel_json_close_object(jbuf);
379 /**************************************************************************//**
382 * Free off the Report supplied. Will free all the contained allocated memory.
384 * @note It does not free the Report itself, since that may be part of a
386 *****************************************************************************/
387 void evel_free_report(EVENT_REPORT * event)
389 MEASUREMENT_FEATURE_USE * feature_use = NULL;
390 MEASUREMENT_GROUP * measurement_group = NULL;
391 CUSTOM_MEASUREMENT * custom_measurement = NULL;
395 /***************************************************************************/
396 /* Check preconditions. As an internal API we don't allow freeing NULL */
397 /* events as we do on the public API. */
398 /***************************************************************************/
399 assert(event != NULL);
400 assert(event->header.event_domain == EVEL_DOMAIN_REPORT);
402 /***************************************************************************/
403 /* Free all internal strings then the header itself. */
404 /***************************************************************************/
405 feature_use = dlist_pop_last(&event->feature_usage);
406 while (feature_use != NULL)
408 EVEL_DEBUG("Freeing Feature use Info (%s)", feature_use->feature_id);
409 free(feature_use->feature_id);
411 feature_use = dlist_pop_last(&event->feature_usage);
413 measurement_group = dlist_pop_last(&event->measurement_groups);
414 while (measurement_group != NULL)
416 EVEL_DEBUG("Freeing Measurement Group (%s)", measurement_group->name);
418 custom_measurement = dlist_pop_last(&measurement_group->measurements);
419 while (custom_measurement != NULL)
421 EVEL_DEBUG("Freeing mesaurement (%s)", custom_measurement->name);
423 free(custom_measurement->name);
424 free(custom_measurement->value);
425 free(custom_measurement);
426 custom_measurement = dlist_pop_last(&measurement_group->measurements);
429 free(measurement_group->name);
430 free(measurement_group);
431 measurement_group = dlist_pop_last(&event->measurement_groups);
434 evel_free_header(&event->header);