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 path The optional path (may be NULL).
68 * @param topic The optional topic part of the URL (may be NULL).
69 * @param secure Whether to use HTTPS (0=HTTP, 1=HTTPS)
70 * @param username Username for Basic Authentication of requests.
71 * @param password Password for Basic Authentication of requests.
72 * @param source_type The kind of node we represent.
73 * @param role The role this node undertakes.
74 * @param verbosity 0 for normal operation, positive values for chattier
77 * @returns Status code
78 * @retval EVEL_SUCCESS On success
79 * @retval ::EVEL_ERR_CODES On failure.
80 *****************************************************************************/
81 EVEL_ERR_CODES evel_initialize(const char * const fqdn,
83 const char * const path,
84 const char * const topic,
86 const char * const username,
87 const char * const password,
88 EVEL_SOURCE_TYPES source_type,
89 const char * const role,
93 EVEL_ERR_CODES rc = EVEL_SUCCESS;
94 char base_api_url[EVEL_MAX_URL_LEN + 1] = {0};
95 char event_api_url[EVEL_MAX_URL_LEN + 1] = {0};
96 char throt_api_url[EVEL_MAX_URL_LEN + 1] = {0};
97 char path_url[EVEL_MAX_URL_LEN + 1] = {0};
98 char topic_url[EVEL_MAX_URL_LEN + 1] = {0};
99 char version_string[10] = {0};
102 /***************************************************************************/
103 /* Check assumptions. */
104 /***************************************************************************/
105 assert(fqdn != NULL);
106 assert(port > 0 && port <= 65535);
107 assert(source_type < EVEL_MAX_SOURCE_TYPES);
108 assert(role != NULL);
110 /***************************************************************************/
111 /* Start logging so we can report on progress. */
112 /***************************************************************************/
113 log_initialize(verbosity == 0 ? EVEL_LOG_INFO : EVEL_LOG_DEBUG, "EVEL");
114 EVEL_INFO("EVEL started");
115 EVEL_INFO("API server is: %s", fqdn);
116 EVEL_INFO("API port is: %d", port);
120 EVEL_INFO("API path is: %s", path);
124 EVEL_INFO("No API path");
129 EVEL_INFO("API topic is: %s", topic);
133 EVEL_INFO("No API topic");
136 EVEL_INFO("API transport is: %s", secure ? "HTTPS" : "HTTP");
137 EVEL_INFO("Event Source Type is: %d", source_type);
138 EVEL_INFO("Functional Role is: %s", role);
139 EVEL_INFO("Log verbosity is: %d", verbosity);
141 /***************************************************************************/
142 /* Initialize event throttling to the default state. */
143 /***************************************************************************/
144 evel_throttle_initialize();
146 /***************************************************************************/
147 /* Save values we will need during operation. */
148 /***************************************************************************/
149 event_source_type = source_type;
150 functional_role = strdup(role);
152 /***************************************************************************/
153 /* Ensure there are no trailing zeroes and unnecessary decimal points in */
155 /***************************************************************************/
156 offset = sprintf(version_string, "%d", EVEL_API_MAJOR_VERSION);
158 if (EVEL_API_MINOR_VERSION != 0)
160 sprintf(version_string + offset, ".%d", EVEL_API_MINOR_VERSION);
163 /***************************************************************************/
164 /* Build a common base of the API URLs. */
165 /***************************************************************************/
166 strcpy(path_url, "/");
167 snprintf(base_api_url,
169 "%s://%s:%d%s/eventListener/v%s",
170 secure ? "https" : "http",
173 (((path != NULL) && (strlen(path) > 0)) ?
174 strncat(path_url, path, EVEL_MAX_URL_LEN) : ""),
177 /***************************************************************************/
178 /* Build the URL to the event API. */
179 /***************************************************************************/
180 strcpy(topic_url, "/");
181 snprintf(event_api_url,
185 (((topic != NULL) && (strlen(topic) > 0)) ?
186 strncat(topic_url, topic, EVEL_MAX_URL_LEN) : ""));
187 EVEL_INFO("Vendor Event Listener API is located at: %s", event_api_url);
189 /***************************************************************************/
190 /* Build the URL to the throttling API. */
191 /***************************************************************************/
192 snprintf(throt_api_url,
194 "%s/clientThrottlingState",
196 EVEL_INFO("Vendor Event Throttling API is located at: %s", throt_api_url);
198 /***************************************************************************/
199 /* Spin-up the event-handler, which gets cURL readied for use. */
200 /***************************************************************************/
201 rc = event_handler_initialize(event_api_url,
206 if (rc != EVEL_SUCCESS)
208 log_error_state("Failed to initialize event handler (including cURL)");
212 /***************************************************************************/
213 /* Extract the metadata from OpenStack. If we fail to extract it, we */
214 /* record that in the logs, but carry on, assuming we're in a test */
215 /* without a metadata service. */
216 /***************************************************************************/
217 rc = openstack_metadata(verbosity);
218 if (rc != EVEL_SUCCESS)
220 EVEL_INFO("Failed to load OpenStack metadata - assuming test environment");
224 /***************************************************************************/
225 /* Start the event handler thread. */
226 /***************************************************************************/
227 rc = event_handler_run();
228 if (rc != EVEL_SUCCESS)
230 log_error_state("Failed to start event handler thread. "
231 "Error code=%d", rc);
239 /**************************************************************************//**
240 * Clean up the EVEL library.
242 * @note that at present don't expect Init/Term cycling not to leak memory!
244 * @returns Status code
245 * @retval EVEL_SUCCESS On success
246 * @retval "One of ::EVEL_ERR_CODES" On failure.
247 *****************************************************************************/
248 EVEL_ERR_CODES evel_terminate(void)
250 int rc = EVEL_SUCCESS;
252 /***************************************************************************/
253 /* First terminate any pending transactions in the event-posting thread. */
254 /***************************************************************************/
255 rc = event_handler_terminate();
256 if (rc != EVEL_SUCCESS)
258 log_error_state("Failed to terminate EVEL library cleanly!");
261 /***************************************************************************/
262 /* Shut down the Event Handler library in a tidy manner. */
263 /***************************************************************************/
264 curl_global_cleanup();
266 /***************************************************************************/
267 /* Clean up allocated memory. */
268 /***************************************************************************/
269 free(functional_role);
271 /***************************************************************************/
272 /* Clean up event throttling. */
273 /***************************************************************************/
274 evel_throttle_terminate();
276 EVEL_INFO("EVEL stopped");
280 /**************************************************************************//**
283 * Free off the event supplied. Will free all the contained allocated memory.
285 * @note It is safe to free a NULL pointer.
286 *****************************************************************************/
287 void evel_free_event(void * event)
289 EVENT_HEADER * evt_ptr = event;
294 /*************************************************************************/
295 /* Work out what kind of event we're dealing with so we can cast it */
297 /*************************************************************************/
298 switch (evt_ptr->event_domain)
300 case EVEL_DOMAIN_INTERNAL:
301 EVEL_DEBUG("Event is an Internal event at %lp", evt_ptr);
302 evel_free_internal_event((EVENT_INTERNAL *) evt_ptr);
303 memset(evt_ptr, 0, sizeof(EVENT_INTERNAL));
307 case EVEL_DOMAIN_HEARTBEAT:
308 EVEL_DEBUG("Event is a Heartbeat at %lp", evt_ptr);
309 evel_free_header(evt_ptr);
310 memset(evt_ptr, 0, sizeof(EVENT_HEADER));
314 case EVEL_DOMAIN_FAULT:
315 EVEL_DEBUG("Event is a Fault at %lp", evt_ptr);
316 evel_free_fault((EVENT_FAULT *)evt_ptr);
317 memset(evt_ptr, 0, sizeof(EVENT_FAULT));
321 case EVEL_DOMAIN_MEASUREMENT:
322 EVEL_DEBUG("Event is a Measurement at %lp", evt_ptr);
323 evel_free_measurement((EVENT_MEASUREMENT *)evt_ptr);
324 memset(evt_ptr, 0, sizeof(EVENT_MEASUREMENT));
328 case EVEL_DOMAIN_MOBILE_FLOW:
329 EVEL_DEBUG("Event is a Mobile Flow at %lp", evt_ptr);
330 evel_free_mobile_flow((EVENT_MOBILE_FLOW *)evt_ptr);
331 memset(evt_ptr, 0, sizeof(EVENT_MOBILE_FLOW));
335 case EVEL_DOMAIN_REPORT:
336 EVEL_DEBUG("Event is a Report at %lp", evt_ptr);
337 evel_free_report((EVENT_REPORT *)evt_ptr);
338 memset(evt_ptr, 0, sizeof(EVENT_REPORT));
342 case EVEL_DOMAIN_HEARTBEAT_FIELD:
343 EVEL_DEBUG("Event is a Heartbeat Field Event at %lp", evt_ptr);
344 evel_free_hrtbt_field((EVENT_HEARTBEAT_FIELD *)evt_ptr);
345 memset(evt_ptr, 0, sizeof(EVENT_HEARTBEAT_FIELD));
349 case EVEL_DOMAIN_SIPSIGNALING:
350 EVEL_DEBUG("Event is a Signaling at %lp", evt_ptr);
351 evel_free_signaling((EVENT_SIGNALING *)evt_ptr);
352 memset(evt_ptr, 0, sizeof(EVENT_SIGNALING));
356 case EVEL_DOMAIN_STATE_CHANGE:
357 EVEL_DEBUG("Event is a State Change at %lp", evt_ptr);
358 evel_free_state_change((EVENT_STATE_CHANGE *)evt_ptr);
359 memset(evt_ptr, 0, sizeof(EVENT_STATE_CHANGE));
363 case EVEL_DOMAIN_SYSLOG:
364 EVEL_DEBUG("Event is a Syslog at %lp", evt_ptr);
365 evel_free_syslog((EVENT_SYSLOG *)evt_ptr);
366 memset(evt_ptr, 0, sizeof(EVENT_SYSLOG));
370 case EVEL_DOMAIN_OTHER:
371 EVEL_DEBUG("Event is an Other at %lp", evt_ptr);
372 evel_free_other((EVENT_OTHER *)evt_ptr);
373 memset(evt_ptr, 0, sizeof(EVENT_OTHER));
377 case EVEL_DOMAIN_VOICE_QUALITY:
378 EVEL_DEBUG("Event is an VoiceQuality at %lp", evt_ptr);
379 evel_free_voice_quality((EVENT_VOICE_QUALITY *)evt_ptr);
380 memset(evt_ptr, 0, sizeof(EVENT_VOICE_QUALITY));
384 case EVEL_DOMAIN_THRESHOLD_CROSS:
385 EVEL_DEBUG("Event is a Threshold crossing at %lp", evt_ptr);
386 evel_free_threshold_cross((EVENT_THRESHOLD_CROSS *)evt_ptr);
387 memset(evt_ptr, 0, sizeof(EVENT_THRESHOLD_CROSS));
391 case EVEL_DOMAIN_BATCH:
392 EVEL_DEBUG("Event is a Batch at %lp", evt_ptr);
393 evel_free_batch((EVENT_HEADER *)evt_ptr);
394 memset(evt_ptr, 0, sizeof(EVENT_HEADER));
399 EVEL_ERROR("Unexpected event domain (%d)", evt_ptr->event_domain);