1 /*************************************************************************//**
3 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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
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.
17 ****************************************************************************/
19 /**************************************************************************//**
21 * Source module isolating the ECOMP Vendor Event Listener (EVEL) API.
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:
27 * * The API's encoding into JSON.
28 * * The API's transport over HTTP/HTTPS.
30 ****************************************************************************/
36 #include <curl/curl.h>
39 #include "evel_internal.h"
40 #include "evel_throttle.h"
43 /**************************************************************************//**
44 * The type of equipment represented by this VNF.
45 *****************************************************************************/
46 EVEL_SOURCE_TYPES event_source_type = EVEL_SOURCE_OTHER;
48 /**************************************************************************//**
49 * The Functional Role of the equipment represented by this VNF.
50 *****************************************************************************/
51 char *functional_role = NULL;
53 /**************************************************************************//**
54 * Library initialization.
56 * Initialize the EVEL library.
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.
63 * @sa Matching Term function.
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
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,
96 const char * const bakup_fqdn,
98 const char * const path,
99 const char * const topic,
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,
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,
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};
128 char * bakup_coll = NULL;
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);
138 if( bakup_fqdn != NULL ) {
139 assert(bakup_port > 0 && bakup_port <= 65535);
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");
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);
155 EVEL_INFO("API path is: %s", path);
159 EVEL_INFO("No API path");
164 EVEL_INFO("API topic is: %s", topic);
168 EVEL_INFO("No API topic");
171 EVEL_INFO("API transport is: %s", secure ? "HTTPS" : "HTTP");
173 assert( verify_peer >= 0 );
174 assert( verify_host >= 0 );
175 if (cert_file_path != NULL)
177 EVEL_INFO("Client cert is: %s", cert_file_path);
181 EVEL_INFO("No Client cert");
183 if (key_file_path != NULL)
185 EVEL_INFO("Key file is: %s", key_file_path);
189 EVEL_INFO("No Key file");
191 if (ca_file_path != NULL)
193 EVEL_INFO("Client CA certs path is: %s", ca_file_path);
197 EVEL_INFO("No CA certs path");
201 EVEL_INFO("Client CA cert file is: %s", ca_info);
205 EVEL_INFO("No CA cert file");
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);
212 /***************************************************************************/
213 /* Initialize event throttling to the default state. */
214 /***************************************************************************/
215 evel_throttle_initialize();
217 /***************************************************************************/
218 /* Save values we will need during operation. */
219 /***************************************************************************/
220 event_source_type = source_type;
221 functional_role = strdup(role);
223 /***************************************************************************/
224 /* Ensure there are no trailing zeroes and unnecessary decimal points in */
226 /***************************************************************************/
227 offset = sprintf(version_string, "%d", EVEL_API_MAJOR_VERSION);
229 if (EVEL_API_MINOR_VERSION != 0)
231 sprintf(version_string + offset, ".%d", EVEL_API_MINOR_VERSION);
234 /***************************************************************************/
235 /* Build a common base of the API URLs. */
236 /***************************************************************************/
237 strcpy(path_url, "/");
238 snprintf(base_api_url,
240 "%s://%s:%d%s/eventListener/v%s",
241 secure ? "https" : "http",
244 (((path != NULL) && (strlen(path) > 0)) ?
245 strncat(path_url, path, EVEL_MAX_URL_LEN) : ""),
248 /***************************************************************************/
249 /* Build the URL to the event API. */
250 /***************************************************************************/
251 strcpy(topic_url, "/");
252 snprintf(event_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);
260 /***************************************************************************/
261 /* Build a common base of the Backup API URLs. */
262 /***************************************************************************/
263 if( bakup_fqdn != NULL )
265 strcpy(path_url, "/");
266 snprintf(base_api_url,
268 "%s://%s:%d%s/eventListener/v%s",
269 secure ? "https" : "http",
272 (((path != NULL) && (strlen(path) > 0)) ?
273 strncat(path_url, path, EVEL_MAX_URL_LEN) : ""),
276 /***************************************************************************/
277 /* Build the URL to the event API. */
278 /***************************************************************************/
279 strcpy(topic_url, "/");
280 snprintf(bakup_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;
290 /***************************************************************************/
291 /* Build the URL to the throttling API. */
292 /***************************************************************************/
293 snprintf(throt_api_url,
295 "%s/clientThrottlingState",
297 EVEL_INFO("Vendor Event Throttling API is located at: %s", throt_api_url);
299 /***************************************************************************/
300 /* Spin-up the event-handler, which gets cURL readied for use. */
301 /***************************************************************************/
302 rc = event_handler_initialize(event_api_url,
320 if (rc != EVEL_SUCCESS)
322 log_error_state("Failed to initialize event handler (including cURL)");
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)
334 EVEL_INFO("Failed to load OpenStack metadata - assuming test environment");
338 /***************************************************************************/
339 /* Start the event handler thread. */
340 /***************************************************************************/
341 rc = event_handler_run();
342 if (rc != EVEL_SUCCESS)
344 log_error_state("Failed to start event handler thread. "
345 "Error code=%d", rc);
353 /**************************************************************************//**
354 * Clean up the EVEL library.
356 * @note that at present don't expect Init/Term cycling not to leak memory!
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)
364 int rc = EVEL_SUCCESS;
366 /***************************************************************************/
367 /* First terminate any pending transactions in the event-posting thread. */
368 /***************************************************************************/
369 rc = event_handler_terminate();
370 if (rc != EVEL_SUCCESS)
372 log_error_state("Failed to terminate EVEL library cleanly!");
375 /***************************************************************************/
376 /* Shut down the Event Handler library in a tidy manner. */
377 /***************************************************************************/
378 curl_global_cleanup();
380 /***************************************************************************/
381 /* Clean up allocated memory. */
382 /***************************************************************************/
383 free(functional_role);
385 /***************************************************************************/
386 /* Clean up event throttling. */
387 /***************************************************************************/
388 evel_throttle_terminate();
390 EVEL_INFO("EVEL stopped");
394 /**************************************************************************//**
397 * Free off the event supplied. Will free all the contained allocated memory.
399 * @note It is safe to free a NULL pointer.
400 *****************************************************************************/
401 void evel_free_event(void * event)
403 EVENT_HEADER * evt_ptr = event;
408 /*************************************************************************/
409 /* Work out what kind of event we're dealing with so we can cast it */
411 /*************************************************************************/
412 switch (evt_ptr->event_domain)
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
513 EVEL_ERROR("Unexpected event domain (%d)", evt_ptr->event_domain);