Modified hardcoded eventname id to user defined
[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  * @param event_name    Unique Event Name
48  * @param event_id    A universal identifier of the event for analysis etc
49  * @returns pointer to the newly manufactured ::EVENT_REPORT.  If the event is
50  *          not used (i.e. posted) it must be released using ::evel_free_event.
51  * @retval  NULL  Failed to create the event.
52  *****************************************************************************/
53 EVENT_REPORT * evel_new_report(double measurement_interval,const char *ev_name, const char *ev_id)
54 {
55   EVENT_REPORT * report = NULL;
56
57   EVEL_ENTER();
58
59   /***************************************************************************/
60   /* Check preconditions.                                                    */
61   /***************************************************************************/
62   assert(measurement_interval >= 0.0);
63
64   /***************************************************************************/
65   /* Allocate the report.                                                    */
66   /***************************************************************************/
67   report = malloc(sizeof(EVENT_REPORT));
68   if (report == NULL)
69   {
70     log_error_state("Out of memory for Report");
71     goto exit_label;
72   }
73   memset(report, 0, sizeof(EVENT_REPORT));
74   EVEL_DEBUG("New report is at %lp", report);
75
76   /***************************************************************************/
77   /* Initialize the header & the report fields.                              */
78   /***************************************************************************/
79   evel_init_header_nameid(&report->header,ev_name,ev_id);
80   report->header.event_domain = EVEL_DOMAIN_REPORT;
81   report->measurement_interval = measurement_interval;
82
83   dlist_initialize(&report->feature_usage);
84   dlist_initialize(&report->measurement_groups);
85   report->major_version = EVEL_REPORT_MAJOR_VERSION;
86   report->minor_version = EVEL_REPORT_MINOR_VERSION;
87
88 exit_label:
89   EVEL_EXIT();
90   return report;
91 }
92
93 /**************************************************************************//**
94  * Set the Event Type property of the Report.
95  *
96  * @note  The property is treated as immutable: it is only valid to call
97  *        the setter once.  However, we don't assert if the caller tries to
98  *        overwrite, just ignoring the update instead.
99  *
100  * @param report Pointer to the Report.
101  * @param type        The Event Type to be set. ASCIIZ string. The caller
102  *                    does not need to preserve the value once the function
103  *                    returns.
104  *****************************************************************************/
105 void evel_report_type_set(EVENT_REPORT * report,
106                           const char * const type)
107 {
108   EVEL_ENTER();
109
110   /***************************************************************************/
111   /* Check preconditions and call evel_header_type_set.                      */
112   /***************************************************************************/
113   assert(report != NULL);
114   assert(report->header.event_domain == EVEL_DOMAIN_REPORT);
115   evel_header_type_set(&report->header, type);
116
117   EVEL_EXIT();
118 }
119
120 /**************************************************************************//**
121  * Add a Feature usage value name/value pair to the Report.
122  *
123  * The name is null delimited ASCII string.  The library takes
124  * a copy so the caller does not have to preserve values after the function
125  * returns.
126  *
127  * @param report          Pointer to the report.
128  * @param feature         ASCIIZ string with the feature's name.
129  * @param utilization     Utilization of the feature.
130  *****************************************************************************/
131 void evel_report_feature_use_add(EVENT_REPORT * report,
132                                  char * feature,
133                                  int utilization)
134 {
135   MEASUREMENT_FEATURE_USE * feature_use = NULL;
136   EVEL_ENTER();
137
138   /***************************************************************************/
139   /* Check assumptions.                                                      */
140   /***************************************************************************/
141   assert(report != NULL);
142   assert(report->header.event_domain == EVEL_DOMAIN_REPORT);
143   assert(feature != NULL);
144   assert(utilization >= 0);
145
146   /***************************************************************************/
147   /* Allocate a container for the value and push onto the list.              */
148   /***************************************************************************/
149   EVEL_DEBUG("Adding Feature=%s Use=%d", feature, utilization);
150   feature_use = malloc(sizeof(MEASUREMENT_FEATURE_USE));
151   assert(feature_use != NULL);
152   memset(feature_use, 0, sizeof(MEASUREMENT_FEATURE_USE));
153   feature_use->feature_id = strdup(feature);
154   assert(feature_use->feature_id != NULL);
155   feature_use->feature_utilization = utilization;
156
157   dlist_push_last(&report->feature_usage, feature_use);
158
159   EVEL_EXIT();
160 }
161
162 /**************************************************************************//**
163  * Add a Additional Measurement value name/value pair to the Report.
164  *
165  * The name is null delimited ASCII string.  The library takes
166  * a copy so the caller does not have to preserve values after the function
167  * returns.
168  *
169  * @param report   Pointer to the report.
170  * @param group    ASCIIZ string with the measurement group's name.
171  * @param name     ASCIIZ string containing the measurement's name.
172  * @param value    ASCIIZ string containing the measurement's value.
173  *****************************************************************************/
174 void evel_report_custom_measurement_add(EVENT_REPORT * report,
175                                         const char * const group,
176                                         const char * const name,
177                                         const char * const value)
178 {
179   MEASUREMENT_GROUP * measurement_group = NULL;
180   CUSTOM_MEASUREMENT * measurement = NULL;
181   DLIST_ITEM * item = NULL;
182   EVEL_ENTER();
183
184   /***************************************************************************/
185   /* Check assumptions.                                                      */
186   /***************************************************************************/
187   assert(report != NULL);
188   assert(report->header.event_domain == EVEL_DOMAIN_REPORT);
189   assert(group != NULL);
190   assert(name != NULL);
191   assert(value != NULL);
192
193   /***************************************************************************/
194   /* Allocate a container for the name/value pair.                           */
195   /***************************************************************************/
196   EVEL_DEBUG("Adding Measurement Group=%s Name=%s Value=%s",
197               group, name, value);
198   measurement = malloc(sizeof(CUSTOM_MEASUREMENT));
199   assert(measurement != NULL);
200   memset(measurement, 0, sizeof(CUSTOM_MEASUREMENT));
201   measurement->name = strdup(name);
202   assert(measurement->name != NULL);
203   measurement->value = strdup(value);
204   assert(measurement->value != NULL);
205
206   /***************************************************************************/
207   /* See if we have that group already.                                      */
208   /***************************************************************************/
209   item = dlist_get_first(&report->measurement_groups);
210   while (item != NULL)
211   {
212     measurement_group = (MEASUREMENT_GROUP *) item->item;
213     assert(measurement_group != NULL);
214
215     EVEL_DEBUG("Got measurement group %s", measurement_group->name);
216     if (strcmp(group, measurement_group->name) == 0)
217     {
218       EVEL_DEBUG("Found existing Measurement Group");
219       break;
220     }
221     item = dlist_get_next(item);
222   }
223
224   /***************************************************************************/
225   /* If we didn't have the group already, create it.                         */
226   /***************************************************************************/
227   if (item == NULL)
228   {
229     EVEL_DEBUG("Creating new Measurement Group");
230     measurement_group = malloc(sizeof(MEASUREMENT_GROUP));
231     assert(measurement_group != NULL);
232     memset(measurement_group, 0, sizeof(MEASUREMENT_GROUP));
233     measurement_group->name = strdup(group);
234     assert(measurement_group->name != NULL);
235     dlist_initialize(&measurement_group->measurements);
236     dlist_push_last(&report->measurement_groups, measurement_group);
237   }
238
239   /***************************************************************************/
240   /* If we didn't have the group already, create it.                         */
241   /***************************************************************************/
242   dlist_push_last(&measurement_group->measurements, measurement);
243
244   EVEL_EXIT();
245 }
246
247 /**************************************************************************//**
248  * Encode the report as a JSON report.
249  *
250  * @param jbuf          Pointer to the ::EVEL_JSON_BUFFER to encode into.
251  * @param event         Pointer to the ::EVENT_HEADER to encode.
252  *****************************************************************************/
253 void evel_json_encode_report(EVEL_JSON_BUFFER * jbuf,
254                              EVENT_REPORT * event)
255 {
256   MEASUREMENT_FEATURE_USE * feature_use = NULL;
257   MEASUREMENT_GROUP * measurement_group = NULL;
258   CUSTOM_MEASUREMENT * custom_measurement = NULL;
259   DLIST_ITEM * item = NULL;
260   DLIST_ITEM * nested_item = NULL;
261
262   EVEL_ENTER();
263
264   /***************************************************************************/
265   /* Check preconditions.                                                    */
266   /***************************************************************************/
267   assert(event != NULL);
268   assert(event->header.event_domain == EVEL_DOMAIN_REPORT);
269
270   evel_json_encode_header(jbuf, &event->header);
271   evel_json_open_named_object(jbuf, "measurementsForVfReportingFields");
272   evel_enc_kv_double(jbuf, "measurementInterval", event->measurement_interval);
273
274   /***************************************************************************/
275   /* Feature Utilization list.                                               */
276   /***************************************************************************/
277   evel_json_checkpoint(jbuf);
278   if (evel_json_open_opt_named_list(jbuf, "featureUsageArray"))
279   {
280     bool item_added = false;
281
282     item = dlist_get_first(&event->feature_usage);
283     while (item != NULL)
284     {
285       feature_use = (MEASUREMENT_FEATURE_USE*) item->item;
286       assert(feature_use != NULL);
287
288       if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
289                                           "featureUsageArray",
290                                           feature_use->feature_id))
291       {
292         evel_json_open_object(jbuf);
293         evel_enc_kv_string(jbuf, "featureIdentifier", feature_use->feature_id);
294         evel_enc_kv_int(
295           jbuf, "featureUtilization", feature_use->feature_utilization);
296         evel_json_close_object(jbuf);
297         item_added = true;
298       }
299       item = dlist_get_next(item);
300     }
301     evel_json_close_list(jbuf);
302
303     /*************************************************************************/
304     /* If we've not written anything, rewind to before we opened the list.   */
305     /*************************************************************************/
306     if (!item_added)
307     {
308       evel_json_rewind(jbuf);
309     }
310   }
311
312   /***************************************************************************/
313   /* Additional Measurement Groups list.                                     */
314   /***************************************************************************/
315   evel_json_checkpoint(jbuf);
316   if (evel_json_open_opt_named_list(jbuf, "additionalMeasurements"))
317   {
318     bool item_added = false;
319
320     item = dlist_get_first(&event->measurement_groups);
321     while (item != NULL)
322     {
323       measurement_group = (MEASUREMENT_GROUP *) item->item;
324       assert(measurement_group != NULL);
325
326       if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
327                                           "additionalMeasurements",
328                                           measurement_group->name))
329       {
330         evel_json_open_object(jbuf);
331         evel_enc_kv_string(jbuf, "name", measurement_group->name);
332         evel_json_open_named_list(jbuf, "measurements");
333
334         /*********************************************************************/
335         /* Measurements list.                                                */
336         /*********************************************************************/
337         nested_item = dlist_get_first(&measurement_group->measurements);
338         while (nested_item != NULL)
339         {
340           custom_measurement = (CUSTOM_MEASUREMENT *) nested_item->item;
341           assert(custom_measurement != NULL);
342
343           evel_json_open_object(jbuf);
344           evel_enc_kv_string(jbuf, "name", custom_measurement->name);
345           evel_enc_kv_string(jbuf, "value", custom_measurement->value);
346           evel_json_close_object(jbuf);
347           nested_item = dlist_get_next(nested_item);
348         }
349         evel_json_close_list(jbuf);
350         evel_json_close_object(jbuf);
351         item_added = true;
352       }
353       item = dlist_get_next(item);
354     }
355     evel_json_close_list(jbuf);
356
357     /*************************************************************************/
358     /* If we've not written anything, rewind to before we opened the list.   */
359     /*************************************************************************/
360     if (!item_added)
361     {
362       evel_json_rewind(jbuf);
363     }
364   }
365
366   /***************************************************************************/
367   /* Although optional, we always generate the version.  Note that this      */
368   /* closes the object, too.                                                 */
369   /***************************************************************************/
370   evel_enc_version(jbuf,
371                    "measurementFieldsVersion",
372                    event->major_version,
373                    event->minor_version);
374   evel_json_close_object(jbuf);
375
376   EVEL_EXIT();
377 }
378
379 /**************************************************************************//**
380  * Free a Report.
381  *
382  * Free off the Report supplied.  Will free all the contained allocated memory.
383  *
384  * @note It does not free the Report itself, since that may be part of a
385  * larger structure.
386  *****************************************************************************/
387 void evel_free_report(EVENT_REPORT * event)
388 {
389   MEASUREMENT_FEATURE_USE * feature_use = NULL;
390   MEASUREMENT_GROUP * measurement_group = NULL;
391   CUSTOM_MEASUREMENT * custom_measurement = NULL;
392
393   EVEL_ENTER();
394
395   /***************************************************************************/
396   /* Check preconditions.  As an internal API we don't allow freeing NULL    */
397   /* events as we do on the public API.                                      */
398   /***************************************************************************/
399   assert(event != NULL);
400   assert(event->header.event_domain == EVEL_DOMAIN_REPORT);
401
402   /***************************************************************************/
403   /* Free all internal strings then the header itself.                       */
404   /***************************************************************************/
405   feature_use = dlist_pop_last(&event->feature_usage);
406   while (feature_use != NULL)
407   {
408     EVEL_DEBUG("Freeing Feature use Info (%s)", feature_use->feature_id);
409     free(feature_use->feature_id);
410     free(feature_use);
411     feature_use = dlist_pop_last(&event->feature_usage);
412   }
413   measurement_group = dlist_pop_last(&event->measurement_groups);
414   while (measurement_group != NULL)
415   {
416     EVEL_DEBUG("Freeing Measurement Group (%s)", measurement_group->name);
417
418     custom_measurement = dlist_pop_last(&measurement_group->measurements);
419     while (custom_measurement != NULL)
420     {
421       EVEL_DEBUG("Freeing mesaurement (%s)", custom_measurement->name);
422
423       free(custom_measurement->name);
424       free(custom_measurement->value);
425       free(custom_measurement);
426       custom_measurement = dlist_pop_last(&measurement_group->measurements);
427     }
428
429     free(measurement_group->name);
430     free(measurement_group);
431     measurement_group = dlist_pop_last(&event->measurement_groups);
432   }
433
434   evel_free_header(&event->header);
435
436   EVEL_EXIT();
437 }