Add License to VES library
[demo.git] / vnfs / VES / code / evel_library / evel_reporting_measurement.c
1 /**************************************************************************//**
2  * @file
3  * Implementation of EVEL functions relating to the Measurement for VF
4  * Reporting event.
5  *
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.
11  *
12  * License
13  * -------
14  *
15  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
16  *
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
21  *
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  *****************************************************************************/
28
29 #include <string.h>
30 #include <assert.h>
31 #include <stdlib.h>
32
33 #include "evel.h"
34 #include "evel_internal.h"
35 #include "evel_throttle.h"
36
37 /**************************************************************************//**
38  * Create a new Report event.
39  *
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.
44  *
45  * @param   measurement_interval
46
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)
52 {
53   EVENT_REPORT * report = NULL;
54
55   EVEL_ENTER();
56
57   /***************************************************************************/
58   /* Check preconditions.                                                    */
59   /***************************************************************************/
60   assert(measurement_interval >= 0.0);
61
62   /***************************************************************************/
63   /* Allocate the report.                                                    */
64   /***************************************************************************/
65   report = malloc(sizeof(EVENT_REPORT));
66   if (report == NULL)
67   {
68     log_error_state("Out of memory for Report");
69     goto exit_label;
70   }
71   memset(report, 0, sizeof(EVENT_REPORT));
72   EVEL_DEBUG("New report is at %lp", report);
73
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;
80
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;
85
86 exit_label:
87   EVEL_EXIT();
88   return report;
89 }
90
91 /**************************************************************************//**
92  * Set the Event Type property of the Report.
93  *
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.
97  *
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
101  *                    returns.
102  *****************************************************************************/
103 void evel_report_type_set(EVENT_REPORT * report,
104                           const char * const type)
105 {
106   EVEL_ENTER();
107
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);
114
115   EVEL_EXIT();
116 }
117
118 /**************************************************************************//**
119  * Add a Feature usage value name/value pair to the Report.
120  *
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
123  * returns.
124  *
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,
130                                  char * feature,
131                                  int utilization)
132 {
133   MEASUREMENT_FEATURE_USE * feature_use = NULL;
134   EVEL_ENTER();
135
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);
143
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;
154
155   dlist_push_last(&report->feature_usage, feature_use);
156
157   EVEL_EXIT();
158 }
159
160 /**************************************************************************//**
161  * Add a Additional Measurement value name/value pair to the Report.
162  *
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
165  * returns.
166  *
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)
176 {
177   MEASUREMENT_GROUP * measurement_group = NULL;
178   CUSTOM_MEASUREMENT * measurement = NULL;
179   DLIST_ITEM * item = NULL;
180   EVEL_ENTER();
181
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);
190
191   /***************************************************************************/
192   /* Allocate a container for the name/value pair.                           */
193   /***************************************************************************/
194   EVEL_DEBUG("Adding Measurement Group=%s Name=%s Value=%s",
195               group, name, value);
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);
203
204   /***************************************************************************/
205   /* See if we have that group already.                                      */
206   /***************************************************************************/
207   item = dlist_get_first(&report->measurement_groups);
208   while (item != NULL)
209   {
210     measurement_group = (MEASUREMENT_GROUP *) item->item;
211     assert(measurement_group != NULL);
212
213     EVEL_DEBUG("Got measurement group %s", measurement_group->name);
214     if (strcmp(group, measurement_group->name) == 0)
215     {
216       EVEL_DEBUG("Found existing Measurement Group");
217       break;
218     }
219     item = dlist_get_next(item);
220   }
221
222   /***************************************************************************/
223   /* If we didn't have the group already, create it.                         */
224   /***************************************************************************/
225   if (item == NULL)
226   {
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);
235   }
236
237   /***************************************************************************/
238   /* If we didn't have the group already, create it.                         */
239   /***************************************************************************/
240   dlist_push_last(&measurement_group->measurements, measurement);
241
242   EVEL_EXIT();
243 }
244
245 /**************************************************************************//**
246  * Encode the report as a JSON report.
247  *
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)
253 {
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;
259
260   EVEL_ENTER();
261
262   /***************************************************************************/
263   /* Check preconditions.                                                    */
264   /***************************************************************************/
265   assert(event != NULL);
266   assert(event->header.event_domain == EVEL_DOMAIN_REPORT);
267
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);
271
272   /***************************************************************************/
273   /* Feature Utilization list.                                               */
274   /***************************************************************************/
275   evel_json_checkpoint(jbuf);
276   if (evel_json_open_opt_named_list(jbuf, "featureUsageArray"))
277   {
278     bool item_added = false;
279
280     item = dlist_get_first(&event->feature_usage);
281     while (item != NULL)
282     {
283       feature_use = (MEASUREMENT_FEATURE_USE*) item->item;
284       assert(feature_use != NULL);
285
286       if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
287                                           "featureUsageArray",
288                                           feature_use->feature_id))
289       {
290         evel_json_open_object(jbuf);
291         evel_enc_kv_string(jbuf, "featureIdentifier", feature_use->feature_id);
292         evel_enc_kv_int(
293           jbuf, "featureUtilization", feature_use->feature_utilization);
294         evel_json_close_object(jbuf);
295         item_added = true;
296       }
297       item = dlist_get_next(item);
298     }
299     evel_json_close_list(jbuf);
300
301     /*************************************************************************/
302     /* If we've not written anything, rewind to before we opened the list.   */
303     /*************************************************************************/
304     if (!item_added)
305     {
306       evel_json_rewind(jbuf);
307     }
308   }
309
310   /***************************************************************************/
311   /* Additional Measurement Groups list.                                     */
312   /***************************************************************************/
313   evel_json_checkpoint(jbuf);
314   if (evel_json_open_opt_named_list(jbuf, "additionalMeasurements"))
315   {
316     bool item_added = false;
317
318     item = dlist_get_first(&event->measurement_groups);
319     while (item != NULL)
320     {
321       measurement_group = (MEASUREMENT_GROUP *) item->item;
322       assert(measurement_group != NULL);
323
324       if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
325                                           "additionalMeasurements",
326                                           measurement_group->name))
327       {
328         evel_json_open_object(jbuf);
329         evel_enc_kv_string(jbuf, "name", measurement_group->name);
330         evel_json_open_named_list(jbuf, "measurements");
331
332         /*********************************************************************/
333         /* Measurements list.                                                */
334         /*********************************************************************/
335         nested_item = dlist_get_first(&measurement_group->measurements);
336         while (nested_item != NULL)
337         {
338           custom_measurement = (CUSTOM_MEASUREMENT *) nested_item->item;
339           assert(custom_measurement != NULL);
340
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);
346         }
347         evel_json_close_list(jbuf);
348         evel_json_close_object(jbuf);
349         item_added = true;
350       }
351       item = dlist_get_next(item);
352     }
353     evel_json_close_list(jbuf);
354
355     /*************************************************************************/
356     /* If we've not written anything, rewind to before we opened the list.   */
357     /*************************************************************************/
358     if (!item_added)
359     {
360       evel_json_rewind(jbuf);
361     }
362   }
363
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);
373
374   EVEL_EXIT();
375 }
376
377 /**************************************************************************//**
378  * Free a Report.
379  *
380  * Free off the Report supplied.  Will free all the contained allocated memory.
381  *
382  * @note It does not free the Report itself, since that may be part of a
383  * larger structure.
384  *****************************************************************************/
385 void evel_free_report(EVENT_REPORT * event)
386 {
387   MEASUREMENT_FEATURE_USE * feature_use = NULL;
388   MEASUREMENT_GROUP * measurement_group = NULL;
389   CUSTOM_MEASUREMENT * custom_measurement = NULL;
390
391   EVEL_ENTER();
392
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);
399
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)
405   {
406     EVEL_DEBUG("Freeing Feature use Info (%s)", feature_use->feature_id);
407     free(feature_use->feature_id);
408     free(feature_use);
409     feature_use = dlist_pop_last(&event->feature_usage);
410   }
411   measurement_group = dlist_pop_last(&event->measurement_groups);
412   while (measurement_group != NULL)
413   {
414     EVEL_DEBUG("Freeing Measurement Group (%s)", measurement_group->name);
415
416     custom_measurement = dlist_pop_last(&measurement_group->measurements);
417     while (custom_measurement != NULL)
418     {
419       EVEL_DEBUG("Freeing mesaurement (%s)", custom_measurement->name);
420
421       free(custom_measurement->name);
422       free(custom_measurement->value);
423       free(custom_measurement);
424       custom_measurement = dlist_pop_last(&measurement_group->measurements);
425     }
426
427     free(measurement_group->name);
428     free(measurement_group);
429     measurement_group = dlist_pop_last(&event->measurement_groups);
430   }
431
432   evel_free_header(&event->header);
433
434   EVEL_EXIT();
435 }