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 © 2017 AT&T Intellectual Property. All rights reserved.
17 * Licensed under the Apache License, Version 2.0 (the "License");
18 * you may not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 * http://www.apache.org/licenses/LICENSE-2.0
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 *****************************************************************************/
34 #include "evel_internal.h"
35 #include "evel_throttle.h"
37 /**************************************************************************//**
38 * Create a new Report event.
40 * @note The mandatory fields on the Report must be supplied to this
41 * factory function and are immutable once set. Optional fields have
42 * explicit setter functions, but again values may only be set once so
43 * that the Report has immutable properties.
45 * @param measurement_interval
47 * @returns pointer to the newly manufactured ::EVENT_REPORT. If the event is
48 * not used (i.e. posted) it must be released using ::evel_free_event.
49 * @retval NULL Failed to create the event.
50 *****************************************************************************/
51 EVENT_REPORT * evel_new_report(double measurement_interval)
53 EVENT_REPORT * report = NULL;
57 /***************************************************************************/
58 /* Check preconditions. */
59 /***************************************************************************/
60 assert(measurement_interval >= 0.0);
62 /***************************************************************************/
63 /* Allocate the report. */
64 /***************************************************************************/
65 report = malloc(sizeof(EVENT_REPORT));
68 log_error_state("Out of memory for Report");
71 memset(report, 0, sizeof(EVENT_REPORT));
72 EVEL_DEBUG("New report is at %lp", report);
74 /***************************************************************************/
75 /* Initialize the header & the report fields. */
76 /***************************************************************************/
77 evel_init_header(&report->header);
78 report->header.event_domain = EVEL_DOMAIN_REPORT;
79 report->measurement_interval = measurement_interval;
81 dlist_initialize(&report->feature_usage);
82 dlist_initialize(&report->measurement_groups);
83 report->major_version = EVEL_REPORT_MAJOR_VERSION;
84 report->minor_version = EVEL_REPORT_MINOR_VERSION;
91 /**************************************************************************//**
92 * Set the Event Type property of the Report.
94 * @note The property is treated as immutable: it is only valid to call
95 * the setter once. However, we don't assert if the caller tries to
96 * overwrite, just ignoring the update instead.
98 * @param report Pointer to the Report.
99 * @param type The Event Type to be set. ASCIIZ string. The caller
100 * does not need to preserve the value once the function
102 *****************************************************************************/
103 void evel_report_type_set(EVENT_REPORT * report,
104 const char * const type)
108 /***************************************************************************/
109 /* Check preconditions and call evel_header_type_set. */
110 /***************************************************************************/
111 assert(report != NULL);
112 assert(report->header.event_domain == EVEL_DOMAIN_REPORT);
113 evel_header_type_set(&report->header, type);
118 /**************************************************************************//**
119 * Add a Feature usage value name/value pair to the Report.
121 * The name is null delimited ASCII string. The library takes
122 * a copy so the caller does not have to preserve values after the function
125 * @param report Pointer to the report.
126 * @param feature ASCIIZ string with the feature's name.
127 * @param utilization Utilization of the feature.
128 *****************************************************************************/
129 void evel_report_feature_use_add(EVENT_REPORT * report,
133 MEASUREMENT_FEATURE_USE * feature_use = NULL;
136 /***************************************************************************/
137 /* Check assumptions. */
138 /***************************************************************************/
139 assert(report != NULL);
140 assert(report->header.event_domain == EVEL_DOMAIN_REPORT);
141 assert(feature != NULL);
142 assert(utilization >= 0);
144 /***************************************************************************/
145 /* Allocate a container for the value and push onto the list. */
146 /***************************************************************************/
147 EVEL_DEBUG("Adding Feature=%s Use=%d", feature, utilization);
148 feature_use = malloc(sizeof(MEASUREMENT_FEATURE_USE));
149 assert(feature_use != NULL);
150 memset(feature_use, 0, sizeof(MEASUREMENT_FEATURE_USE));
151 feature_use->feature_id = strdup(feature);
152 assert(feature_use->feature_id != NULL);
153 feature_use->feature_utilization = utilization;
155 dlist_push_last(&report->feature_usage, feature_use);
160 /**************************************************************************//**
161 * Add a Additional Measurement value name/value pair to the Report.
163 * The name is null delimited ASCII string. The library takes
164 * a copy so the caller does not have to preserve values after the function
167 * @param report Pointer to the report.
168 * @param group ASCIIZ string with the measurement group's name.
169 * @param name ASCIIZ string containing the measurement's name.
170 * @param value ASCIIZ string containing the measurement's value.
171 *****************************************************************************/
172 void evel_report_custom_measurement_add(EVENT_REPORT * report,
173 const char * const group,
174 const char * const name,
175 const char * const value)
177 MEASUREMENT_GROUP * measurement_group = NULL;
178 CUSTOM_MEASUREMENT * measurement = NULL;
179 DLIST_ITEM * item = NULL;
182 /***************************************************************************/
183 /* Check assumptions. */
184 /***************************************************************************/
185 assert(report != NULL);
186 assert(report->header.event_domain == EVEL_DOMAIN_REPORT);
187 assert(group != NULL);
188 assert(name != NULL);
189 assert(value != NULL);
191 /***************************************************************************/
192 /* Allocate a container for the name/value pair. */
193 /***************************************************************************/
194 EVEL_DEBUG("Adding Measurement Group=%s Name=%s Value=%s",
196 measurement = malloc(sizeof(CUSTOM_MEASUREMENT));
197 assert(measurement != NULL);
198 memset(measurement, 0, sizeof(CUSTOM_MEASUREMENT));
199 measurement->name = strdup(name);
200 assert(measurement->name != NULL);
201 measurement->value = strdup(value);
202 assert(measurement->value != NULL);
204 /***************************************************************************/
205 /* See if we have that group already. */
206 /***************************************************************************/
207 item = dlist_get_first(&report->measurement_groups);
210 measurement_group = (MEASUREMENT_GROUP *) item->item;
211 assert(measurement_group != NULL);
213 EVEL_DEBUG("Got measurement group %s", measurement_group->name);
214 if (strcmp(group, measurement_group->name) == 0)
216 EVEL_DEBUG("Found existing Measurement Group");
219 item = dlist_get_next(item);
222 /***************************************************************************/
223 /* If we didn't have the group already, create it. */
224 /***************************************************************************/
227 EVEL_DEBUG("Creating new Measurement Group");
228 measurement_group = malloc(sizeof(MEASUREMENT_GROUP));
229 assert(measurement_group != NULL);
230 memset(measurement_group, 0, sizeof(MEASUREMENT_GROUP));
231 measurement_group->name = strdup(group);
232 assert(measurement_group->name != NULL);
233 dlist_initialize(&measurement_group->measurements);
234 dlist_push_last(&report->measurement_groups, measurement_group);
237 /***************************************************************************/
238 /* If we didn't have the group already, create it. */
239 /***************************************************************************/
240 dlist_push_last(&measurement_group->measurements, measurement);
245 /**************************************************************************//**
246 * Encode the report as a JSON report.
248 * @param jbuf Pointer to the ::EVEL_JSON_BUFFER to encode into.
249 * @param event Pointer to the ::EVENT_HEADER to encode.
250 *****************************************************************************/
251 void evel_json_encode_report(EVEL_JSON_BUFFER * jbuf,
252 EVENT_REPORT * event)
254 MEASUREMENT_FEATURE_USE * feature_use = NULL;
255 MEASUREMENT_GROUP * measurement_group = NULL;
256 CUSTOM_MEASUREMENT * custom_measurement = NULL;
257 DLIST_ITEM * item = NULL;
258 DLIST_ITEM * nested_item = NULL;
262 /***************************************************************************/
263 /* Check preconditions. */
264 /***************************************************************************/
265 assert(event != NULL);
266 assert(event->header.event_domain == EVEL_DOMAIN_REPORT);
268 evel_json_encode_header(jbuf, &event->header);
269 evel_json_open_named_object(jbuf, "measurementsForVfReportingFields");
270 evel_enc_kv_double(jbuf, "measurementInterval", event->measurement_interval);
272 /***************************************************************************/
273 /* Feature Utilization list. */
274 /***************************************************************************/
275 evel_json_checkpoint(jbuf);
276 if (evel_json_open_opt_named_list(jbuf, "featureUsageArray"))
278 bool item_added = false;
280 item = dlist_get_first(&event->feature_usage);
283 feature_use = (MEASUREMENT_FEATURE_USE*) item->item;
284 assert(feature_use != NULL);
286 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
288 feature_use->feature_id))
290 evel_json_open_object(jbuf);
291 evel_enc_kv_string(jbuf, "featureIdentifier", feature_use->feature_id);
293 jbuf, "featureUtilization", feature_use->feature_utilization);
294 evel_json_close_object(jbuf);
297 item = dlist_get_next(item);
299 evel_json_close_list(jbuf);
301 /*************************************************************************/
302 /* If we've not written anything, rewind to before we opened the list. */
303 /*************************************************************************/
306 evel_json_rewind(jbuf);
310 /***************************************************************************/
311 /* Additional Measurement Groups list. */
312 /***************************************************************************/
313 evel_json_checkpoint(jbuf);
314 if (evel_json_open_opt_named_list(jbuf, "additionalMeasurements"))
316 bool item_added = false;
318 item = dlist_get_first(&event->measurement_groups);
321 measurement_group = (MEASUREMENT_GROUP *) item->item;
322 assert(measurement_group != NULL);
324 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
325 "additionalMeasurements",
326 measurement_group->name))
328 evel_json_open_object(jbuf);
329 evel_enc_kv_string(jbuf, "name", measurement_group->name);
330 evel_json_open_named_list(jbuf, "measurements");
332 /*********************************************************************/
333 /* Measurements list. */
334 /*********************************************************************/
335 nested_item = dlist_get_first(&measurement_group->measurements);
336 while (nested_item != NULL)
338 custom_measurement = (CUSTOM_MEASUREMENT *) nested_item->item;
339 assert(custom_measurement != NULL);
341 evel_json_open_object(jbuf);
342 evel_enc_kv_string(jbuf, "name", custom_measurement->name);
343 evel_enc_kv_string(jbuf, "value", custom_measurement->value);
344 evel_json_close_object(jbuf);
345 nested_item = dlist_get_next(nested_item);
347 evel_json_close_list(jbuf);
348 evel_json_close_object(jbuf);
351 item = dlist_get_next(item);
353 evel_json_close_list(jbuf);
355 /*************************************************************************/
356 /* If we've not written anything, rewind to before we opened the list. */
357 /*************************************************************************/
360 evel_json_rewind(jbuf);
364 /***************************************************************************/
365 /* Although optional, we always generate the version. Note that this */
366 /* closes the object, too. */
367 /***************************************************************************/
368 evel_enc_version(jbuf,
369 "measurementFieldsVersion",
370 event->major_version,
371 event->major_version);
372 evel_json_close_object(jbuf);
377 /**************************************************************************//**
380 * Free off the Report supplied. Will free all the contained allocated memory.
382 * @note It does not free the Report itself, since that may be part of a
384 *****************************************************************************/
385 void evel_free_report(EVENT_REPORT * event)
387 MEASUREMENT_FEATURE_USE * feature_use = NULL;
388 MEASUREMENT_GROUP * measurement_group = NULL;
389 CUSTOM_MEASUREMENT * custom_measurement = NULL;
393 /***************************************************************************/
394 /* Check preconditions. As an internal API we don't allow freeing NULL */
395 /* events as we do on the public API. */
396 /***************************************************************************/
397 assert(event != NULL);
398 assert(event->header.event_domain == EVEL_DOMAIN_REPORT);
400 /***************************************************************************/
401 /* Free all internal strings then the header itself. */
402 /***************************************************************************/
403 feature_use = dlist_pop_last(&event->feature_usage);
404 while (feature_use != NULL)
406 EVEL_DEBUG("Freeing Feature use Info (%s)", feature_use->feature_id);
407 free(feature_use->feature_id);
409 feature_use = dlist_pop_last(&event->feature_usage);
411 measurement_group = dlist_pop_last(&event->measurement_groups);
412 while (measurement_group != NULL)
414 EVEL_DEBUG("Freeing Measurement Group (%s)", measurement_group->name);
416 custom_measurement = dlist_pop_last(&measurement_group->measurements);
417 while (custom_measurement != NULL)
419 EVEL_DEBUG("Freeing mesaurement (%s)", custom_measurement->name);
421 free(custom_measurement->name);
422 free(custom_measurement->value);
423 free(custom_measurement);
424 custom_measurement = dlist_pop_last(&measurement_group->measurements);
427 free(measurement_group->name);
428 free(measurement_group);
429 measurement_group = dlist_pop_last(&event->measurement_groups);
432 evel_free_header(&event->header);