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
48 * @returns pointer to the newly manufactured ::EVENT_REPORT. If the event is
49 * not used (i.e. posted) it must be released using ::evel_free_event.
50 * @retval NULL Failed to create the event.
51 *****************************************************************************/
52 EVENT_REPORT * evel_new_report(double measurement_interval)
54 EVENT_REPORT * report = NULL;
58 /***************************************************************************/
59 /* Check preconditions. */
60 /***************************************************************************/
61 assert(measurement_interval >= 0.0);
63 /***************************************************************************/
64 /* Allocate the report. */
65 /***************************************************************************/
66 report = malloc(sizeof(EVENT_REPORT));
69 log_error_state("Out of memory for Report");
72 memset(report, 0, sizeof(EVENT_REPORT));
73 EVEL_DEBUG("New report is at %lp", report);
75 /***************************************************************************/
76 /* Initialize the header & the report fields. */
77 /***************************************************************************/
78 evel_init_header(&report->header,"Report");
79 report->header.event_domain = EVEL_DOMAIN_REPORT;
80 report->measurement_interval = measurement_interval;
82 dlist_initialize(&report->feature_usage);
83 dlist_initialize(&report->measurement_groups);
84 report->major_version = EVEL_REPORT_MAJOR_VERSION;
85 report->minor_version = EVEL_REPORT_MINOR_VERSION;
92 /**************************************************************************//**
93 * Set the Event Type property of the Report.
95 * @note The property is treated as immutable: it is only valid to call
96 * the setter once. However, we don't assert if the caller tries to
97 * overwrite, just ignoring the update instead.
99 * @param report Pointer to the Report.
100 * @param type The Event Type to be set. ASCIIZ string. The caller
101 * does not need to preserve the value once the function
103 *****************************************************************************/
104 void evel_report_type_set(EVENT_REPORT * report,
105 const char * const type)
109 /***************************************************************************/
110 /* Check preconditions and call evel_header_type_set. */
111 /***************************************************************************/
112 assert(report != NULL);
113 assert(report->header.event_domain == EVEL_DOMAIN_REPORT);
114 evel_header_type_set(&report->header, type);
119 /**************************************************************************//**
120 * Add a Feature usage value name/value pair to the Report.
122 * The name is null delimited ASCII string. The library takes
123 * a copy so the caller does not have to preserve values after the function
126 * @param report Pointer to the report.
127 * @param feature ASCIIZ string with the feature's name.
128 * @param utilization Utilization of the feature.
129 *****************************************************************************/
130 void evel_report_feature_use_add(EVENT_REPORT * report,
134 MEASUREMENT_FEATURE_USE * feature_use = NULL;
137 /***************************************************************************/
138 /* Check assumptions. */
139 /***************************************************************************/
140 assert(report != NULL);
141 assert(report->header.event_domain == EVEL_DOMAIN_REPORT);
142 assert(feature != NULL);
143 assert(utilization >= 0);
145 /***************************************************************************/
146 /* Allocate a container for the value and push onto the list. */
147 /***************************************************************************/
148 EVEL_DEBUG("Adding Feature=%s Use=%d", feature, utilization);
149 feature_use = malloc(sizeof(MEASUREMENT_FEATURE_USE));
150 assert(feature_use != NULL);
151 memset(feature_use, 0, sizeof(MEASUREMENT_FEATURE_USE));
152 feature_use->feature_id = strdup(feature);
153 assert(feature_use->feature_id != NULL);
154 feature_use->feature_utilization = utilization;
156 dlist_push_last(&report->feature_usage, feature_use);
161 /**************************************************************************//**
162 * Add a Additional Measurement value name/value pair to the Report.
164 * The name is null delimited ASCII string. The library takes
165 * a copy so the caller does not have to preserve values after the function
168 * @param report Pointer to the report.
169 * @param group ASCIIZ string with the measurement group's name.
170 * @param name ASCIIZ string containing the measurement's name.
171 * @param value ASCIIZ string containing the measurement's value.
172 *****************************************************************************/
173 void evel_report_custom_measurement_add(EVENT_REPORT * report,
174 const char * const group,
175 const char * const name,
176 const char * const value)
178 MEASUREMENT_GROUP * measurement_group = NULL;
179 CUSTOM_MEASUREMENT * measurement = NULL;
180 DLIST_ITEM * item = NULL;
183 /***************************************************************************/
184 /* Check assumptions. */
185 /***************************************************************************/
186 assert(report != NULL);
187 assert(report->header.event_domain == EVEL_DOMAIN_REPORT);
188 assert(group != NULL);
189 assert(name != NULL);
190 assert(value != NULL);
192 /***************************************************************************/
193 /* Allocate a container for the name/value pair. */
194 /***************************************************************************/
195 EVEL_DEBUG("Adding Measurement Group=%s Name=%s Value=%s",
197 measurement = malloc(sizeof(CUSTOM_MEASUREMENT));
198 assert(measurement != NULL);
199 memset(measurement, 0, sizeof(CUSTOM_MEASUREMENT));
200 measurement->name = strdup(name);
201 assert(measurement->name != NULL);
202 measurement->value = strdup(value);
203 assert(measurement->value != NULL);
205 /***************************************************************************/
206 /* See if we have that group already. */
207 /***************************************************************************/
208 item = dlist_get_first(&report->measurement_groups);
211 measurement_group = (MEASUREMENT_GROUP *) item->item;
212 assert(measurement_group != NULL);
214 EVEL_DEBUG("Got measurement group %s", measurement_group->name);
215 if (strcmp(group, measurement_group->name) == 0)
217 EVEL_DEBUG("Found existing Measurement Group");
220 item = dlist_get_next(item);
223 /***************************************************************************/
224 /* If we didn't have the group already, create it. */
225 /***************************************************************************/
228 EVEL_DEBUG("Creating new Measurement Group");
229 measurement_group = malloc(sizeof(MEASUREMENT_GROUP));
230 assert(measurement_group != NULL);
231 memset(measurement_group, 0, sizeof(MEASUREMENT_GROUP));
232 measurement_group->name = strdup(group);
233 assert(measurement_group->name != NULL);
234 dlist_initialize(&measurement_group->measurements);
235 dlist_push_last(&report->measurement_groups, measurement_group);
238 /***************************************************************************/
239 /* If we didn't have the group already, create it. */
240 /***************************************************************************/
241 dlist_push_last(&measurement_group->measurements, measurement);
246 /**************************************************************************//**
247 * Encode the report as a JSON report.
249 * @param jbuf Pointer to the ::EVEL_JSON_BUFFER to encode into.
250 * @param event Pointer to the ::EVENT_HEADER to encode.
251 *****************************************************************************/
252 void evel_json_encode_report(EVEL_JSON_BUFFER * jbuf,
253 EVENT_REPORT * event)
255 MEASUREMENT_FEATURE_USE * feature_use = NULL;
256 MEASUREMENT_GROUP * measurement_group = NULL;
257 CUSTOM_MEASUREMENT * custom_measurement = NULL;
258 DLIST_ITEM * item = NULL;
259 DLIST_ITEM * nested_item = NULL;
263 /***************************************************************************/
264 /* Check preconditions. */
265 /***************************************************************************/
266 assert(event != NULL);
267 assert(event->header.event_domain == EVEL_DOMAIN_REPORT);
269 evel_json_encode_header(jbuf, &event->header);
270 evel_json_open_named_object(jbuf, "measurementsForVfReportingFields");
271 evel_enc_kv_double(jbuf, "measurementInterval", event->measurement_interval);
273 /***************************************************************************/
274 /* Feature Utilization list. */
275 /***************************************************************************/
276 evel_json_checkpoint(jbuf);
277 if (evel_json_open_opt_named_list(jbuf, "featureUsageArray"))
279 bool item_added = false;
281 item = dlist_get_first(&event->feature_usage);
284 feature_use = (MEASUREMENT_FEATURE_USE*) item->item;
285 assert(feature_use != NULL);
287 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
289 feature_use->feature_id))
291 evel_json_open_object(jbuf);
292 evel_enc_kv_string(jbuf, "featureIdentifier", feature_use->feature_id);
294 jbuf, "featureUtilization", feature_use->feature_utilization);
295 evel_json_close_object(jbuf);
298 item = dlist_get_next(item);
300 evel_json_close_list(jbuf);
302 /*************************************************************************/
303 /* If we've not written anything, rewind to before we opened the list. */
304 /*************************************************************************/
307 evel_json_rewind(jbuf);
311 /***************************************************************************/
312 /* Additional Measurement Groups list. */
313 /***************************************************************************/
314 evel_json_checkpoint(jbuf);
315 if (evel_json_open_opt_named_list(jbuf, "additionalMeasurements"))
317 bool item_added = false;
319 item = dlist_get_first(&event->measurement_groups);
322 measurement_group = (MEASUREMENT_GROUP *) item->item;
323 assert(measurement_group != NULL);
325 if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
326 "additionalMeasurements",
327 measurement_group->name))
329 evel_json_open_object(jbuf);
330 evel_enc_kv_string(jbuf, "name", measurement_group->name);
331 evel_json_open_named_list(jbuf, "measurements");
333 /*********************************************************************/
334 /* Measurements list. */
335 /*********************************************************************/
336 nested_item = dlist_get_first(&measurement_group->measurements);
337 while (nested_item != NULL)
339 custom_measurement = (CUSTOM_MEASUREMENT *) nested_item->item;
340 assert(custom_measurement != NULL);
342 evel_json_open_object(jbuf);
343 evel_enc_kv_string(jbuf, "name", custom_measurement->name);
344 evel_enc_kv_string(jbuf, "value", custom_measurement->value);
345 evel_json_close_object(jbuf);
346 nested_item = dlist_get_next(nested_item);
348 evel_json_close_list(jbuf);
349 evel_json_close_object(jbuf);
352 item = dlist_get_next(item);
354 evel_json_close_list(jbuf);
356 /*************************************************************************/
357 /* If we've not written anything, rewind to before we opened the list. */
358 /*************************************************************************/
361 evel_json_rewind(jbuf);
365 /***************************************************************************/
366 /* Although optional, we always generate the version. Note that this */
367 /* closes the object, too. */
368 /***************************************************************************/
369 evel_enc_version(jbuf,
370 "measurementFieldsVersion",
371 event->major_version,
372 event->minor_version);
373 evel_json_close_object(jbuf);
378 /**************************************************************************//**
381 * Free off the Report supplied. Will free all the contained allocated memory.
383 * @note It does not free the Report itself, since that may be part of a
385 *****************************************************************************/
386 void evel_free_report(EVENT_REPORT * event)
388 MEASUREMENT_FEATURE_USE * feature_use = NULL;
389 MEASUREMENT_GROUP * measurement_group = NULL;
390 CUSTOM_MEASUREMENT * custom_measurement = NULL;
394 /***************************************************************************/
395 /* Check preconditions. As an internal API we don't allow freeing NULL */
396 /* events as we do on the public API. */
397 /***************************************************************************/
398 assert(event != NULL);
399 assert(event->header.event_domain == EVEL_DOMAIN_REPORT);
401 /***************************************************************************/
402 /* Free all internal strings then the header itself. */
403 /***************************************************************************/
404 feature_use = dlist_pop_last(&event->feature_usage);
405 while (feature_use != NULL)
407 EVEL_DEBUG("Freeing Feature use Info (%s)", feature_use->feature_id);
408 free(feature_use->feature_id);
410 feature_use = dlist_pop_last(&event->feature_usage);
412 measurement_group = dlist_pop_last(&event->measurement_groups);
413 while (measurement_group != NULL)
415 EVEL_DEBUG("Freeing Measurement Group (%s)", measurement_group->name);
417 custom_measurement = dlist_pop_last(&measurement_group->measurements);
418 while (custom_measurement != NULL)
420 EVEL_DEBUG("Freeing mesaurement (%s)", custom_measurement->name);
422 free(custom_measurement->name);
423 free(custom_measurement->value);
424 free(custom_measurement);
425 custom_measurement = dlist_pop_last(&measurement_group->measurements);
428 free(measurement_group->name);
429 free(measurement_group);
430 measurement_group = dlist_pop_last(&event->measurement_groups);
433 evel_free_header(&event->header);