Initial VES for DANOS vRouter
[demo.git] / vnfs / VESreporting_vFW5.0_DANOS / evel / evel-library / code / evel_library / evel.c
1 /*************************************************************************//**
2  *
3  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
4  *
5  * Unless otherwise specified, all software contained herein is
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and 
15  * limitations under the License.
16  *
17  ****************************************************************************/
18
19 /**************************************************************************//**
20  * @file
21  * Source module isolating the ECOMP Vendor Event Listener (EVEL) API.
22  *
23  * This file implements the EVEL library which is intended to provide a
24  * simple wrapper around the complexity of AT&T's Vendor Event Listener API so
25  * that VNFs can use it without worrying about details of:
26  *
27  *  *   The API's encoding into JSON.
28  *  *   The API's transport over HTTP/HTTPS.
29  *
30  ****************************************************************************/
31
32 #include <string.h>
33 #include <assert.h>
34 #include <stdlib.h>
35 #include <sys/time.h>
36 #include <curl/curl.h>
37
38 #include "evel.h"
39 #include "evel_internal.h"
40 #include "evel_throttle.h"
41 #include "metadata.h"
42
43 /**************************************************************************//**
44  * The type of equipment represented by this VNF.
45  *****************************************************************************/
46 EVEL_SOURCE_TYPES event_source_type = EVEL_SOURCE_OTHER;
47
48 /**************************************************************************//**
49  * The Functional Role of the equipment represented by this VNF.
50  *****************************************************************************/
51 char *functional_role = NULL;
52
53 /**************************************************************************//**
54  * Library initialization.
55  *
56  * Initialize the EVEL library.
57  *
58  * @note  This function initializes the cURL library.  Applications making use
59  *        of libcurl may need to pull the initialization out of here.  Note
60  *        also that this function is not threadsafe as a result - refer to
61  *        libcurl's API documentation for relevant warnings.
62  *
63  * @sa  Matching Term function.
64  *
65  * @param   fqdn    The API's FQDN or IP address.
66  * @param   port    The API's port.
67  * @param   bakup_fqdn    The API's Backup FQDN or IP address.
68  * @param   bakup_port    The API's Backup port.
69  * @param   path    The optional path (may be NULL).
70  * @param   topic   The optional topic part of the URL (may be NULL).
71  * @param   ring buf size  Ring buffer size
72  * @param   secure  Whether to use HTTPS (0=HTTP, 1=HTTPS)
73  * @param   cert_file_path     Path to client certificate file
74  * @param   key_file_path      Path to client key file
75  * @param   ca_info            Path to CA cert file
76  * @param   ca_file_path       Path to CA cert files
77  * @param   verify_peer        SSL verification of peer 0 or 1
78  * @param   verify_host        SSL verification of host 0 or 1
79  * @param   username  Username for Basic Authentication of requests.
80  * @param   password  Password for Basic Authentication of requests.
81  * @param   bakup_username  Username for Basic Authentication of Bakup FQDN.
82  * @param   bakup_password  Password for Basic Authentication of Bakup FQDN.
83  * @param   source_ip       The ip of node we represent.(NULL for default ip)
84  * @param   bakup_source_ip The ip bakup fqdn interface.(NULL for default ip)
85  * @param   source_type The kind of node we represent.
86  * @param   role    The role this node undertakes.
87  * @param   verbosity  0 for normal operation, positive values for chattier
88  *                        logs.
89  *
90  * @returns Status code
91  * @retval  EVEL_SUCCESS      On success
92  * @retval  ::EVEL_ERR_CODES  On failure.
93  *****************************************************************************/
94 EVEL_ERR_CODES evel_initialize(const char * const fqdn,
95                                int port,
96                                const char * const bakup_fqdn,
97                                int bakup_port,
98                                const char * const path,
99                                const char * const topic,
100                                int ring_buf_size,
101                                int secure,
102                                const char * const cert_file_path,
103                                const char * const key_file_path,
104                                const char * const ca_info,
105                                const char * const ca_file_path,
106                                long verify_peer,
107                                long verify_host,
108                                const char * const username,
109                                const char * const password,
110                                const char * const bakup_username,
111                                const char * const bakup_password,
112                                const char * const source_ip,
113                                const char * const bakup_source_ip,
114                                EVEL_SOURCE_TYPES source_type,
115                                const char * const role,
116                                int verbosity
117                                )
118 {
119   EVEL_ERR_CODES rc = EVEL_SUCCESS;
120   char base_api_url[EVEL_MAX_URL_LEN + 1] = {0};
121   char event_api_url[EVEL_MAX_URL_LEN + 1] = {0};
122   char bakup_api_url[EVEL_MAX_URL_LEN + 1] = {0};
123   char throt_api_url[EVEL_MAX_URL_LEN + 1] = {0};
124   char path_url[EVEL_MAX_URL_LEN + 1] = {0};
125   char topic_url[EVEL_MAX_URL_LEN + 1] = {0};
126   char version_string[10] = {0};
127   int offset;
128   char * bakup_coll = NULL;
129
130   /***************************************************************************/
131   /* Check assumptions.                                                      */
132   /***************************************************************************/
133   assert(fqdn != NULL);
134   assert(port > 0 && port <= 65535);
135   assert(source_type < EVEL_MAX_SOURCE_TYPES);
136   assert(role != NULL);
137
138   if( bakup_fqdn != NULL ) {
139     assert(bakup_port > 0 && bakup_port <= 65535);
140   }
141
142   /***************************************************************************/
143   /* Start logging so we can report on progress.                             */
144   /***************************************************************************/
145   if( verbosity >= EVEL_LOG_MIN && verbosity <= EVEL_LOG_MAX)
146      log_initialize(verbosity, "EVEL");
147   else
148      log_initialize(EVEL_LOG_MIN, "EVEL");
149   EVEL_INFO("EVEL started");
150   EVEL_INFO("API server is: %s", fqdn);
151   EVEL_INFO("API port is: %d", port);
152
153   if (path != NULL)
154   {
155     EVEL_INFO("API path is: %s", path);
156   }
157   else
158   {
159     EVEL_INFO("No API path");
160   }
161
162   if (topic != NULL)
163   {
164     EVEL_INFO("API topic is: %s", topic);
165   }
166   else
167   {
168     EVEL_INFO("No API topic");
169   }
170
171   EVEL_INFO("API transport is: %s", secure ? "HTTPS" : "HTTP");
172   if( secure ) {
173     assert( verify_peer >= 0 );
174     assert( verify_host >= 0 );
175     if (cert_file_path != NULL)
176     {
177       EVEL_INFO("Client cert is: %s", cert_file_path);
178     }
179     else
180     {
181       EVEL_INFO("No Client cert");
182     }
183     if (key_file_path != NULL)
184     {
185       EVEL_INFO("Key file is: %s", key_file_path);
186     }
187     else
188     {
189       EVEL_INFO("No Key file");
190     }
191     if (ca_file_path != NULL)
192     {
193       EVEL_INFO("Client CA certs path is: %s", ca_file_path);
194     }
195     else
196     {
197       EVEL_INFO("No CA certs path");
198     }
199     if (ca_info != NULL)
200     {
201       EVEL_INFO("Client CA cert file is: %s", ca_info);
202     }
203     else
204     {
205       EVEL_INFO("No CA cert file");
206     }
207   }
208   EVEL_INFO("Event Source Type is: %d", source_type);
209   EVEL_INFO("Functional Role is: %s", role);
210   EVEL_INFO("Log verbosity is: %d", verbosity);
211
212   /***************************************************************************/
213   /* Initialize event throttling to the default state.                       */
214   /***************************************************************************/
215   evel_throttle_initialize();
216
217   /***************************************************************************/
218   /* Save values we will need during operation.                              */
219   /***************************************************************************/
220   event_source_type = source_type;
221   functional_role = strdup(role);
222
223   /***************************************************************************/
224   /* Ensure there are no trailing zeroes and unnecessary decimal points in   */
225   /* the version.                                                            */
226   /***************************************************************************/
227   offset = sprintf(version_string, "%d", EVEL_API_MAJOR_VERSION);
228
229   if (EVEL_API_MINOR_VERSION != 0)
230   {
231     sprintf(version_string + offset, ".%d", EVEL_API_MINOR_VERSION);
232   }
233
234   /***************************************************************************/
235   /* Build a common base of the API URLs.                                    */
236   /***************************************************************************/
237   strcpy(path_url, "/");
238   snprintf(base_api_url,
239            EVEL_MAX_URL_LEN,
240            "%s://%s:%d%s/eventListener/v%s",
241            secure ? "https" : "http",
242            fqdn,
243            port,
244            (((path != NULL) && (strlen(path) > 0)) ?
245             strncat(path_url, path, EVEL_MAX_URL_LEN) : ""),
246            version_string);
247
248   /***************************************************************************/
249   /* Build the URL to the event API.                                         */
250   /***************************************************************************/
251   strcpy(topic_url, "/");
252   snprintf(event_api_url,
253            EVEL_MAX_URL_LEN,
254            "%s%s",
255            base_api_url,
256            (((topic != NULL) && (strlen(topic) > 0)) ?
257             strncat(topic_url, topic, EVEL_MAX_URL_LEN) : ""));
258   EVEL_INFO("Vendor Event Listener API is located at: %s", event_api_url);
259
260   /***************************************************************************/
261   /* Build a common base of the Backup API URLs.                                    */
262   /***************************************************************************/
263   if( bakup_fqdn != NULL )
264   {
265     strcpy(path_url, "/");
266     snprintf(base_api_url,
267            EVEL_MAX_URL_LEN,
268            "%s://%s:%d%s/eventListener/v%s",
269            secure ? "https" : "http",
270            bakup_fqdn,
271            bakup_port,
272            (((path != NULL) && (strlen(path) > 0)) ?
273             strncat(path_url, path, EVEL_MAX_URL_LEN) : ""),
274            version_string);
275
276   /***************************************************************************/
277   /* Build the URL to the event API.                                         */
278   /***************************************************************************/
279     strcpy(topic_url, "/");
280     snprintf(bakup_api_url,
281            EVEL_MAX_URL_LEN,
282            "%s%s",
283            base_api_url,
284            (((topic != NULL) && (strlen(topic) > 0)) ?
285             strncat(topic_url, topic, EVEL_MAX_URL_LEN) : ""));
286     EVEL_INFO("Vendor Backup Event Listener API is located at: %s", bakup_api_url);
287     bakup_coll = bakup_api_url;
288   }
289
290   /***************************************************************************/
291   /* Build the URL to the throttling API.                                    */
292   /***************************************************************************/
293   snprintf(throt_api_url,
294            EVEL_MAX_URL_LEN,
295            "%s/clientThrottlingState",
296            base_api_url);
297   EVEL_INFO("Vendor Event Throttling API is located at: %s", throt_api_url);
298
299   /***************************************************************************/
300   /* Spin-up the event-handler, which gets cURL readied for use.             */
301   /***************************************************************************/
302   rc = event_handler_initialize(event_api_url,
303                                 bakup_coll,
304                                 throt_api_url,
305                                 source_ip,
306                                 bakup_source_ip,
307                                 ring_buf_size,
308                                 secure,
309                                 cert_file_path,
310                                 key_file_path,
311                                 ca_info,
312                                 ca_file_path,
313                                 verify_peer,
314                                 verify_host,
315                                 username,
316                                 password,
317                                 bakup_username,
318                                 bakup_password,
319                                 verbosity);
320   if (rc != EVEL_SUCCESS)
321   {
322     log_error_state("Failed to initialize event handler (including cURL)");
323     goto exit_label;
324   }
325
326   /***************************************************************************/
327   /* Extract the metadata from OpenStack. If we fail to extract it, we       */
328   /* record that in the logs, but carry on, assuming we're in a test         */
329   /* without a metadata service.                                             */
330   /***************************************************************************/
331   rc = openstack_metadata(verbosity);
332   if (rc != EVEL_SUCCESS)
333   {
334     EVEL_INFO("Failed to load OpenStack metadata - assuming test environment");
335     rc = EVEL_SUCCESS;
336   }
337
338   /***************************************************************************/
339   /* Start the event handler thread.                                         */
340   /***************************************************************************/
341   rc = event_handler_run();
342   if (rc != EVEL_SUCCESS)
343   {
344     log_error_state("Failed to start event handler thread. "
345                     "Error code=%d", rc);
346     goto exit_label;
347   }
348
349 exit_label:
350   return(rc);
351 }
352
353 /**************************************************************************//**
354  * Clean up the EVEL library.
355  *
356  * @note that at present don't expect Init/Term cycling not to leak memory!
357  *
358  * @returns Status code
359  * @retval  EVEL_SUCCESS On success
360  * @retval  "One of ::EVEL_ERR_CODES" On failure.
361  *****************************************************************************/
362 EVEL_ERR_CODES evel_terminate(void)
363 {
364   int rc = EVEL_SUCCESS;
365
366   /***************************************************************************/
367   /* First terminate any pending transactions in the event-posting thread.   */
368   /***************************************************************************/
369   rc = event_handler_terminate();
370   if (rc != EVEL_SUCCESS)
371   {
372     log_error_state("Failed to terminate EVEL library cleanly!");
373   }
374
375   /***************************************************************************/
376   /* Shut down the Event Handler library in a tidy manner.                   */
377   /***************************************************************************/
378   curl_global_cleanup();
379
380   /***************************************************************************/
381   /* Clean up allocated memory.                                              */
382   /***************************************************************************/
383   free(functional_role);
384
385   /***************************************************************************/
386   /* Clean up event throttling.                                              */
387   /***************************************************************************/
388   evel_throttle_terminate();
389
390   EVEL_INFO("EVEL stopped");
391   return(rc);
392 }
393
394 /**************************************************************************//**
395  * Free an event.
396  *
397  * Free off the event supplied.  Will free all the contained allocated memory.
398  *
399  * @note  It is safe to free a NULL pointer.
400  *****************************************************************************/
401 void evel_free_event(void * event)
402 {
403   EVENT_HEADER * evt_ptr = event;
404   EVEL_ENTER();
405
406   if (event != NULL)
407   {
408     /*************************************************************************/
409     /* Work out what kind of event we're dealing with so we can cast it      */
410     /* appropriately.                                                        */
411     /*************************************************************************/
412     switch (evt_ptr->event_domain)
413     {
414     case EVEL_DOMAIN_INTERNAL:
415       EVEL_DEBUG("Event is an Internal event at %lp", evt_ptr);
416       evel_free_internal_event((EVENT_INTERNAL *) evt_ptr);
417       memset(evt_ptr, 0, sizeof(EVENT_INTERNAL));
418       free(evt_ptr);
419       break;
420
421     case EVEL_DOMAIN_HEARTBEAT:
422       EVEL_DEBUG("Event is a Heartbeat at %lp", evt_ptr);
423       evel_free_header(evt_ptr);
424       memset(evt_ptr, 0, sizeof(EVENT_HEADER));
425       free(evt_ptr);
426       break;
427
428     case EVEL_DOMAIN_FAULT:
429       EVEL_DEBUG("Event is a Fault at %lp", evt_ptr);
430       evel_free_fault((EVENT_FAULT *)evt_ptr);
431       memset(evt_ptr, 0, sizeof(EVENT_FAULT));
432       free(evt_ptr);
433       break;
434
435     case EVEL_DOMAIN_MEASUREMENT:
436       EVEL_DEBUG("Event is a Measurement at %lp", evt_ptr);
437       evel_free_measurement((EVENT_MEASUREMENT *)evt_ptr);
438       memset(evt_ptr, 0, sizeof(EVENT_MEASUREMENT));
439       free(evt_ptr);
440       break;
441
442     case EVEL_DOMAIN_MOBILE_FLOW:
443       EVEL_DEBUG("Event is a Mobile Flow at %lp", evt_ptr);
444       evel_free_mobile_flow((EVENT_MOBILE_FLOW *)evt_ptr);
445       memset(evt_ptr, 0, sizeof(EVENT_MOBILE_FLOW));
446       free(evt_ptr);
447       break;
448
449     case EVEL_DOMAIN_REPORT:
450       EVEL_DEBUG("Event is a Report at %lp", evt_ptr);
451       evel_free_report((EVENT_REPORT *)evt_ptr);
452       memset(evt_ptr, 0, sizeof(EVENT_REPORT));
453       free(evt_ptr);
454       break;
455
456     case EVEL_DOMAIN_HEARTBEAT_FIELD:
457       EVEL_DEBUG("Event is a Heartbeat Field Event at %lp", evt_ptr);
458       evel_free_hrtbt_field((EVENT_HEARTBEAT_FIELD *)evt_ptr);
459       memset(evt_ptr, 0, sizeof(EVENT_HEARTBEAT_FIELD));
460       free(evt_ptr);
461       break;
462
463     case EVEL_DOMAIN_SIPSIGNALING:
464       EVEL_DEBUG("Event is a Signaling at %lp", evt_ptr);
465       evel_free_signaling((EVENT_SIGNALING *)evt_ptr);
466       memset(evt_ptr, 0, sizeof(EVENT_SIGNALING));
467       free(evt_ptr);
468       break;
469
470     case EVEL_DOMAIN_STATE_CHANGE:
471       EVEL_DEBUG("Event is a State Change at %lp", evt_ptr);
472       evel_free_state_change((EVENT_STATE_CHANGE *)evt_ptr);
473       memset(evt_ptr, 0, sizeof(EVENT_STATE_CHANGE));
474       free(evt_ptr);
475       break;
476
477     case EVEL_DOMAIN_SYSLOG:
478       EVEL_DEBUG("Event is a Syslog at %lp", evt_ptr);
479       evel_free_syslog((EVENT_SYSLOG *)evt_ptr);
480       memset(evt_ptr, 0, sizeof(EVENT_SYSLOG));
481       free(evt_ptr);
482       break;
483
484     case EVEL_DOMAIN_OTHER:
485       EVEL_DEBUG("Event is an Other at %lp", evt_ptr);
486       evel_free_other((EVENT_OTHER *)evt_ptr);
487       memset(evt_ptr, 0, sizeof(EVENT_OTHER));
488       free(evt_ptr);
489       break;
490
491     case EVEL_DOMAIN_VOICE_QUALITY:
492       EVEL_DEBUG("Event is an VoiceQuality at %lp", evt_ptr);
493       evel_free_voice_quality((EVENT_VOICE_QUALITY *)evt_ptr);
494       memset(evt_ptr, 0, sizeof(EVENT_VOICE_QUALITY));
495       free(evt_ptr);
496       break;
497
498     case EVEL_DOMAIN_THRESHOLD_CROSS:
499       EVEL_DEBUG("Event is a Threshold crossing at %lp", evt_ptr);
500       evel_free_threshold_cross((EVENT_THRESHOLD_CROSS *)evt_ptr);
501       memset(evt_ptr, 0, sizeof(EVENT_THRESHOLD_CROSS));
502       free(evt_ptr);
503       break;
504
505     case EVEL_DOMAIN_BATCH:
506       EVEL_DEBUG("Event is a Batch at %lp", evt_ptr);
507       evel_free_batch((EVENT_HEADER *)evt_ptr);
508       memset(evt_ptr, 0, sizeof(EVENT_HEADER));
509       free(evt_ptr);
510       break;
511
512     default:
513       EVEL_ERROR("Unexpected event domain (%d)", evt_ptr->event_domain);
514       assert(0);
515     }
516   }
517   EVEL_EXIT();
518 }