1 /**************************************************************************//**
3 * Implementation of EVEL functions relating to the Measurement for VF
6 * @note This is an experimental event tytpe and does not form part of the
7 * currently approved AT&T event schema. It is intended to allow a
8 * less-onerous event reporting mechanism because it avoids having to
9 * return all the platform statistics which are mandatory in the
10 * **measurementsForVfScaling** event.
15 * Copyright(c) <2016>, AT&T Intellectual Property. All other rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are met:
20 * 1. Redistributions of source code must retain the above copyright notice,
21 * this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright notice,
23 * this list of conditions and the following disclaimer in the documentation
24 * and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement: This product includes
27 * software developed by the AT&T.
28 * 4. Neither the name of AT&T nor the names of its contributors may be used to
29 * endorse or promote products derived from this software without specific
30 * prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY AT&T INTELLECTUAL PROPERTY ''AS IS'' AND ANY
33 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL AT&T INTELLECTUAL PROPERTY BE LIABLE FOR ANY
36 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
41 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 *****************************************************************************/
49 #include "evel_internal.h"
50 #include "evel_throttle.h"
52 /**************************************************************************//**
53 * Create a new Report event.
55 * @note The mandatory fields on the Report must be supplied to this
56 * factory function and are immutable once set. Optional fields have
57 * explicit setter functions, but again values may only be set once so
58 * that the Report has immutable properties.
60 * @param measurement_interval
62 * @returns pointer to the newly manufactured ::EVENT_REPORT. If the event is
63 * not used (i.e. posted) it must be released using ::evel_free_event.
64 * @retval NULL Failed to create the event.
65 *****************************************************************************/
66 EVENT_REPORT * evel_new_report(double measurement_interval)
68 EVENT_REPORT * report = NULL;
72 /***************************************************************************/
73 /* Check preconditions. */
74 /***************************************************************************/
75 assert(measurement_interval >= 0.0);
77 /***************************************************************************/
78 /* Allocate the report. */
79 /***************************************************************************/
80 report = malloc(sizeof(EVENT_REPORT));
83 log_error_state("Out of memory for Report");
86 memset(report, 0, sizeof(EVENT_REPORT));
87 EVEL_DEBUG("New report is at %lp", report);
89 /***************************************************************************/
90 /* Initialize the header & the report fields. */
91 /***************************************************************************/
92 evel_init_header(&report->header);
93 report->header.event_domain = EVEL_DOMAIN_REPORT;
94 report->measurement_interval = measurement_interval;
96 dlist_initialize(&report->feature_usage);
97 dlist_initialize(&report->measurement_groups);
98 report->major_version = EVEL_REPORT_MAJOR_VERSION;
99 report->minor_version = EVEL_REPORT_MINOR_VERSION;
106 /**************************************************************************//**
107 * Set the Event Type property of the Report.
109 * @note The property is treated as immutable: it is only valid to call
110 * the setter once. However, we don't assert if the caller tries to
111 * overwrite, just ignoring the update instead.
113 * @param report Pointer to the Report.
114 * @param type The Event Type to be set. ASCIIZ string. The caller
115 * does not need to preserve the value once the function
117 *****************************************************************************/
118 void evel_report_type_set(EVENT_REPORT * report,
119 const char * const type)
123 /***************************************************************************/
124 /* Check preconditions and call evel_header_type_set. */
125 /***************************************************************************/
126 assert(report != NULL);
127 assert(report->header.event_domain == EVEL_DOMAIN_REPORT);
128 evel_header_type_set(&report->header, type);
133 /**************************************************************************//**
134 * Add a Feature usage value name/value pair to the Report.
136 * The name is null delimited ASCII string. The library takes
137 * a copy so the caller does not have to preserve values after the function
140 * @param report Pointer to the report.
141 * @param feature ASCIIZ string with the feature's name.
142 * @param utilization Utilization of the feature.
143 *****************************************************************************/
144 void evel_report_feature_use_add(EVENT_REPORT * report,
148 MEASUREMENT_FEATURE_USE * feature_use = NULL;
151 /***************************************************************************/
152 /* Check assumptions. */
153 /***************************************************************************/
154 assert(report != NULL);
155 assert(report->header.event_domain == EVEL_DOMAIN_REPORT);
156 assert(feature != NULL);
157 assert(utilization >= 0);
159 /***************************************************************************/
160 /* Allocate a container for the value and push onto the list. */
161 /***************************************************************************/
162 EVEL_DEBUG("Adding Feature=%s Use=%d", feature, utilization);
163 feature_use = malloc(sizeof(MEASUREMENT_FEATURE_USE));
164 assert(feature_use != NULL);
165 memset(feature_use, 0, sizeof(MEASUREMENT_FEATURE_USE));
166 feature_use->feature_id = strdup(feature);
167 assert(feature_use->feature_id != NULL);
168 feature_use->feature_utilization = utilization;
170 dlist_push_last(&report->feature_usage, feature_use);
175 /**************************************************************************//**
176 * Add a Additional Measurement value name/value pair to the Report.
178 * The name is null delimited ASCII string. The library takes
179 * a copy so the caller does not have to preserve values after the function
182 * @param report Pointer to the report.
183 * @param group ASCIIZ string with the measurement group's name.
184 * @param name ASCIIZ string containing the measurement's name.
185 * @param value ASCIIZ string containing the measurement's value.
186 *****************************************************************************/
187 void evel_report_custom_measurement_add(EVENT_REPORT * report,
188 const char * const group,
189 const char * const name,
190 const char * const value)
192 MEASUREMENT_GROUP * measurement_group = NULL;
193 CUSTOM_MEASUREMENT * measurement = NULL;
194 DLIST_ITEM * item = NULL;
197 /***************************************************************************/
198 /* Check assumptions. */
199 /***************************************************************************/
200 assert(report != NULL);
201 assert(report->header.event_domain == EVEL_DOMAIN_REPORT);
202 assert(group != NULL);
203 assert(name != NULL);
204 assert(value != NULL);
206 /***************************************************************************/
207 /* Allocate a container for the name/value pair. */
208 /***************************************************************************/
209 EVEL_DEBUG("Adding Measurement Group=%s Name=%s Value=%s",
211 measurement = malloc(sizeof(CUSTOM_MEASUREMENT));
212 assert(measurement != NULL);
213 memset(measurement, 0, sizeof(CUSTOM_MEASUREMENT));
214 measurement->name = strdup(name);
215 assert(measurement->name != NULL);
216 measurement->value = strdup(value);
217 assert(measurement->value != NULL);
219 /***************************************************************************/
220 /* See if we have that group already. */
221 /***************************************************************************/
222 item = dlist_get_first(&report->measurement_groups);
225 measurement_group = (MEASUREMENT_GROUP *) item->item;
226 assert(measurement_group != NULL);
228 EVEL_DEBUG("Got measurement group %s", measurement_group->name);
229 if (strcmp(group, measurement_group->name) == 0)
231 EVEL_DEBUG("Found existing Measurement Group");
234 item = dlist_get_next(item);
237 /***************************************************************************/
238 /* If we didn't have the group already, create it. */
239 /***************************************************************************/
242 EVEL_DEBUG("Creating new Measurement Group");
243 measurement_group = malloc(sizeof(MEASUREMENT_GROUP));
244 assert(measurement_group != NULL);
245 memset(measurement_group, 0, sizeof(MEASUREMENT_GROUP));
246 measurement_group->name = strdup(group);
247 assert(measurement_group->name != NULL);
248 dlist_initialize(&measurement_group->measurements);
249 dlist_push_last(&report->measurement_groups, measurement_group);
252 /***************************************************************************/
253 /* If we didn't have the group already, create it. */
254 /***************************************************************************/
255 dlist_push_last(&measurement_group->measurements, measurement);
260 /**************************************************************************//**
261 * Encode the report as a JSON report.
263 * @param jbuf Pointer to the ::EVEL_JSON_BUFFER to encode into.
264 * @param event Pointer to the ::EVENT_HEADER to encode.
265 *****************************************************************************/
266 void evel_json_encode_report(EVEL_JSON_BUFFER * jbuf,
267 EVENT_REPORT * event)
269 MEASUREMENT_FEATURE_USE * feature_use = NULL;
270 MEASUREMENT_GROUP * measurement_group = NULL;
271 CUSTOM_MEASUREMENT * custom_measurement = NULL;
272 DLIST_ITEM * item = NULL;
273 DLIST_ITEM * nested_item = NULL;
277 /***************************************************************************/
278 /* Check preconditions. */
279 /***************************************************************************/
280 assert(event != NULL);
281 assert(event->header.event_domain == EVEL_DOMAIN_REPORT);
283 evel_json_encode_header(jbuf, &event->header);
284 evel_json_open_named_object(jbuf, "measurementsForVfReportingFields");
285 evel_enc_kv_double(jbuf, "measurementInterval", event->measurement_interval);
287 /***************************************************************************/
288 /* Feature Utilization list. */
289 /***************************************************************************/
290 evel_json_checkpoint(jbuf);
291 if (evel_json_open_opt_named_list(jbuf, "featureUsageArray"))
293 bool item_added = false;
295 item = dlist_get_first(&event->feature_usage);
298 feature_use = (MEASUREMENT_FEATURE_USE*) item->item;
299 assert(feature_use != NULL);
301 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
303 feature_use->feature_id))
305 evel_json_open_object(jbuf);
306 evel_enc_kv_string(jbuf, "featureIdentifier", feature_use->feature_id);
308 jbuf, "featureUtilization", feature_use->feature_utilization);
309 evel_json_close_object(jbuf);
312 item = dlist_get_next(item);
314 evel_json_close_list(jbuf);
316 /*************************************************************************/
317 /* If we've not written anything, rewind to before we opened the list. */
318 /*************************************************************************/
321 evel_json_rewind(jbuf);
325 /***************************************************************************/
326 /* Additional Measurement Groups list. */
327 /***************************************************************************/
328 evel_json_checkpoint(jbuf);
329 if (evel_json_open_opt_named_list(jbuf, "additionalMeasurements"))
331 bool item_added = false;
333 item = dlist_get_first(&event->measurement_groups);
336 measurement_group = (MEASUREMENT_GROUP *) item->item;
337 assert(measurement_group != NULL);
339 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
340 "additionalMeasurements",
341 measurement_group->name))
343 evel_json_open_object(jbuf);
344 evel_enc_kv_string(jbuf, "name", measurement_group->name);
345 evel_json_open_named_list(jbuf, "measurements");
347 /*********************************************************************/
348 /* Measurements list. */
349 /*********************************************************************/
350 nested_item = dlist_get_first(&measurement_group->measurements);
351 while (nested_item != NULL)
353 custom_measurement = (CUSTOM_MEASUREMENT *) nested_item->item;
354 assert(custom_measurement != NULL);
356 evel_json_open_object(jbuf);
357 evel_enc_kv_string(jbuf, "name", custom_measurement->name);
358 evel_enc_kv_string(jbuf, "value", custom_measurement->value);
359 evel_json_close_object(jbuf);
360 nested_item = dlist_get_next(nested_item);
362 evel_json_close_list(jbuf);
363 evel_json_close_object(jbuf);
366 item = dlist_get_next(item);
368 evel_json_close_list(jbuf);
370 /*************************************************************************/
371 /* If we've not written anything, rewind to before we opened the list. */
372 /*************************************************************************/
375 evel_json_rewind(jbuf);
379 /***************************************************************************/
380 /* Although optional, we always generate the version. Note that this */
381 /* closes the object, too. */
382 /***************************************************************************/
383 evel_enc_version(jbuf,
384 "measurementFieldsVersion",
385 event->major_version,
386 event->major_version);
387 evel_json_close_object(jbuf);
392 /**************************************************************************//**
395 * Free off the Report supplied. Will free all the contained allocated memory.
397 * @note It does not free the Report itself, since that may be part of a
399 *****************************************************************************/
400 void evel_free_report(EVENT_REPORT * event)
402 MEASUREMENT_FEATURE_USE * feature_use = NULL;
403 MEASUREMENT_GROUP * measurement_group = NULL;
404 CUSTOM_MEASUREMENT * custom_measurement = NULL;
408 /***************************************************************************/
409 /* Check preconditions. As an internal API we don't allow freeing NULL */
410 /* events as we do on the public API. */
411 /***************************************************************************/
412 assert(event != NULL);
413 assert(event->header.event_domain == EVEL_DOMAIN_REPORT);
415 /***************************************************************************/
416 /* Free all internal strings then the header itself. */
417 /***************************************************************************/
418 feature_use = dlist_pop_last(&event->feature_usage);
419 while (feature_use != NULL)
421 EVEL_DEBUG("Freeing Feature use Info (%s)", feature_use->feature_id);
422 free(feature_use->feature_id);
424 feature_use = dlist_pop_last(&event->feature_usage);
426 measurement_group = dlist_pop_last(&event->measurement_groups);
427 while (measurement_group != NULL)
429 EVEL_DEBUG("Freeing Measurement Group (%s)", measurement_group->name);
431 custom_measurement = dlist_pop_last(&measurement_group->measurements);
432 while (custom_measurement != NULL)
434 EVEL_DEBUG("Freeing mesaurement (%s)", custom_measurement->name);
436 free(custom_measurement->name);
437 free(custom_measurement->value);
438 free(custom_measurement);
439 custom_measurement = dlist_pop_last(&measurement_group->measurements);
442 free(measurement_group->name);
443 free(measurement_group);
444 measurement_group = dlist_pop_last(&event->measurement_groups);
447 evel_free_header(&event->header);