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