summaryrefslogtreecommitdiffstats
path: root/vnfs/VES5.0/evel/evel-library/code/evel_library/evel.c
blob: 3678d7d0899770a4e80758955a31276aa9c177be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/**************************************************************************//**
 * @file
 * Source module isolating the ECOMP Vendor Event Listener (EVEL) API.
 *
 * This file implements the EVEL library which is intended to provide a
 * simple wrapper around the complexity of AT&T's Vendor Event Listener API so
 * that VNFs can use it without worrying about details of:
 *
 *  *   The API's encoding into JSON.
 *  *   The API's transport over HTTP/HTTPS.
 *
 * License
 * -------
 *
 * Copyright(c) <2016>, AT&T Intellectual Property.  All other rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:  This product includes
 *    software developed by the AT&T.
 * 4. Neither the name of AT&T nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY AT&T INTELLECTUAL PROPERTY ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL AT&T INTELLECTUAL PROPERTY BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/

#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <sys/time.h>
#include <curl/curl.h>

#include "evel.h"
#include "evel_internal.h"
#include "evel_throttle.h"
#include "metadata.h"

/**************************************************************************//**
 * The type of equipment represented by this VNF.
 *****************************************************************************/
EVEL_SOURCE_TYPES event_source_type = EVEL_SOURCE_OTHER;

/**************************************************************************//**
 * The Functional Role of the equipment represented by this VNF.
 *****************************************************************************/
char *functional_role = NULL;

/**************************************************************************//**
 * Library initialization.
 *
 * Initialize the EVEL library.
 *
 * @note  This function initializes the cURL library.  Applications making use
 *        of libcurl may need to pull the initialization out of here.  Note
 *        also that this function is not threadsafe as a result - refer to
 *        libcurl's API documentation for relevant warnings.
 *
 * @sa  Matching Term function.
 *
 * @param   fqdn    The API's FQDN or IP address.
 * @param   port    The API's port.
 * @param   path    The optional path (may be NULL).
 * @param   topic   The optional topic part of the URL (may be NULL).
 * @param   secure  Whether to use HTTPS (0=HTTP, 1=HTTPS)
 * @param   username  Username for Basic Authentication of requests.
 * @param   password  Password for Basic Authentication of requests.
 * @param   source_type The kind of node we represent.
 * @param   role    The role this node undertakes.
 * @param   verbosity  0 for normal operation, positive values for chattier
 *                        logs.
 *
 * @returns Status code
 * @retval  EVEL_SUCCESS      On success
 * @retval  ::EVEL_ERR_CODES  On failure.
 *****************************************************************************/
EVEL_ERR_CODES evel_initialize(const char * const fqdn,
                               int port,
                               const char * const path,
                               const char * const topic,
                               int secure,
                               const char * const username,
                               const char * const password,
                               EVEL_SOURCE_TYPES source_type,
                               const char * const role,
                               int verbosity
                               )
{
  EVEL_ERR_CODES rc = EVEL_SUCCESS;
  char base_api_url[EVEL_MAX_URL_LEN + 1] = {0};
  char event_api_url[EVEL_MAX_URL_LEN + 1] = {0};
  char throt_api_url[EVEL_MAX_URL_LEN + 1] = {0};
  char path_url[EVEL_MAX_URL_LEN + 1] = {0};
  char topic_url[EVEL_MAX_URL_LEN + 1] = {0};
  char version_string[10] = {0};
  int offset;

  /***************************************************************************/
  /* Check assumptions.                                                      */
  /***************************************************************************/
  assert(fqdn != NULL);
  assert(port > 0 && port <= 65535);
  assert(source_type < EVEL_MAX_SOURCE_TYPES);
  assert(role != NULL);

  /***************************************************************************/
  /* Start logging so we can report on progress.                             */
  /***************************************************************************/
  log_initialize(verbosity == 0 ? EVEL_LOG_INFO : EVEL_LOG_DEBUG, "EVEL");
  EVEL_INFO("EVEL started");
  EVEL_INFO("API server is: %s", fqdn);
  EVEL_INFO("API port is: %d", port);

  if (path != NULL)
  {
    EVEL_INFO("API path is: %s", path);
  }
  else
  {
    EVEL_INFO("No API path");
  }

  if (topic != NULL)
  {
    EVEL_INFO("API topic is: %s", topic);
  }
  else
  {
    EVEL_INFO("No API topic");
  }

  EVEL_INFO("API transport is: %s", secure ? "HTTPS" : "HTTP");
  EVEL_INFO("Event Source Type is: %d", source_type);
  EVEL_INFO("Functional Role is: %s", role);
  EVEL_INFO("Log verbosity is: %d", verbosity);

  /***************************************************************************/
  /* Initialize event throttling to the default state.                       */
  /***************************************************************************/
  evel_throttle_initialize();

  /***************************************************************************/
  /* Save values we will need during operation.                              */
  /***************************************************************************/
  event_source_type = source_type;
  functional_role = strdup(role);

  /***************************************************************************/
  /* Ensure there are no trailing zeroes and unnecessary decimal points in   */
  /* the version.                                                            */
  /***************************************************************************/
  offset = sprintf(version_string, "%d", EVEL_API_MAJOR_VERSION);

  if (EVEL_API_MINOR_VERSION != 0)
  {
    sprintf(version_string + offset, ".%d", EVEL_API_MINOR_VERSION);
  }

  /***************************************************************************/
  /* Build a common base of the API URLs.                                    */
  /***************************************************************************/
  strcpy(path_url, "/");
  snprintf(base_api_url,
           EVEL_MAX_URL_LEN,
           "%s://%s:%d%s/eventListener/v%s",
           secure ? "https" : "http",
           fqdn,
           port,
           (((path != NULL) && (strlen(path) > 0)) ?
            strncat(path_url, path, EVEL_MAX_URL_LEN) : ""),
           version_string);

  /***************************************************************************/
  /* Build the URL to the event API.                                         */
  /***************************************************************************/
  strcpy(topic_url, "/");
  snprintf(event_api_url,
           EVEL_MAX_URL_LEN,
           "%s%s",
           base_api_url,
           (((topic != NULL) && (strlen(topic) > 0)) ?
            strncat(topic_url, topic, EVEL_MAX_URL_LEN) : ""));
  EVEL_INFO("Vendor Event Listener API is located at: %s", event_api_url);

  /***************************************************************************/
  /* Build the URL to the throttling API.                                    */
  /***************************************************************************/
  snprintf(throt_api_url,
           EVEL_MAX_URL_LEN,
           "%s/clientThrottlingState",
           base_api_url);
  EVEL_INFO("Vendor Event Throttling API is located at: %s", throt_api_url);

  /***************************************************************************/
  /* Spin-up the event-handler, which gets cURL readied for use.             */
  /***************************************************************************/
  rc = event_handler_initialize(event_api_url,
                                throt_api_url,
                                username,
                                password,
                                verbosity);
  if (rc != EVEL_SUCCESS)
  {
    log_error_state("Failed to initialize event handler (including cURL)");
    goto exit_label;
  }

  /***************************************************************************/
  /* Extract the metadata from OpenStack. If we fail to extract it, we       */
  /* record that in the logs, but carry on, assuming we're in a test         */
  /* without a metadata service.                                             */
  /***************************************************************************/
  rc = openstack_metadata(verbosity);
  if (rc != EVEL_SUCCESS)
  {
    EVEL_INFO("Failed to load OpenStack metadata - assuming test environment");
    rc = EVEL_SUCCESS;
  }

  /***************************************************************************/
  /* Start the event handler thread.                                         */
  /***************************************************************************/
  rc = event_handler_run();
  if (rc != EVEL_SUCCESS)
  {
    log_error_state("Failed to start event handler thread. "
                    "Error code=%d", rc);
    goto exit_label;
  }

exit_label:
  return(rc);
}

/**************************************************************************//**
 * Clean up the EVEL library.
 *
 * @note that at present don't expect Init/Term cycling not to leak memory!
 *
 * @returns Status code
 * @retval  EVEL_SUCCESS On success
 * @retval  "One of ::EVEL_ERR_CODES" On failure.
 *****************************************************************************/
EVEL_ERR_CODES evel_terminate(void)
{
  int rc = EVEL_SUCCESS;

  /***************************************************************************/
  /* First terminate any pending transactions in the event-posting thread.   */
  /***************************************************************************/
  rc = event_handler_terminate();
  if (rc != EVEL_SUCCESS)
  {
    log_error_state("Failed to terminate EVEL library cleanly!");
  }

  /***************************************************************************/
  /* Shut down the Event Handler library in a tidy manner.                   */
  /***************************************************************************/
  curl_global_cleanup();

  /***************************************************************************/
  /* Clean up allocated memory.                                              */
  /***************************************************************************/
  free(functional_role);

  /***************************************************************************/
  /* Clean up event throttling.                                              */
  /***************************************************************************/
  evel_throttle_terminate();

  EVEL_INFO("EVEL stopped");
  return(rc);
}

/**************************************************************************//**
 * Free an event.
 *
 * Free off the event supplied.  Will free all the contained allocated memory.
 *
 * @note  It is safe to free a NULL pointer.
 *****************************************************************************/
void evel_free_event(void * event)
{
  EVENT_HEADER * evt_ptr = event;
  EVEL_ENTER();

  if (event != NULL)
  {
    /*************************************************************************/
    /* Work out what kind of event we're dealing with so we can cast it      */
    /* appropriately.                                                        */
    /*************************************************************************/
    switch (evt_ptr->event_domain)
    {
    case EVEL_DOMAIN_INTERNAL:
      EVEL_DEBUG("Event is an Internal event at %lp", evt_ptr);
      evel_free_internal_event((EVENT_INTERNAL *) evt_ptr);
      memset(evt_ptr, 0, sizeof(EVENT_INTERNAL));
      free(evt_ptr);
      break;

    case EVEL_DOMAIN_HEARTBEAT:
      EVEL_DEBUG("Event is a Heartbeat at %lp", evt_ptr);
      evel_free_header(evt_ptr);
      memset(evt_ptr, 0, sizeof(EVENT_HEADER));
      free(evt_ptr);
      break;

    case EVEL_DOMAIN_FAULT:
      EVEL_DEBUG("Event is a Fault at %lp", evt_ptr);
      evel_free_fault((EVENT_FAULT *)evt_ptr);
      memset(evt_ptr, 0, sizeof(EVENT_FAULT));
      free(evt_ptr);
      break;

    case EVEL_DOMAIN_MEASUREMENT:
      EVEL_DEBUG("Event is a Measurement at %lp", evt_ptr);
      evel_free_measurement((EVENT_MEASUREMENT *)evt_ptr);
      memset(evt_ptr, 0, sizeof(EVENT_MEASUREMENT));
      free(evt_ptr);
      break;

    case EVEL_DOMAIN_MOBILE_FLOW:
      EVEL_DEBUG("Event is a Mobile Flow at %lp", evt_ptr);
      evel_free_mobile_flow((EVENT_MOBILE_FLOW *)evt_ptr);
      memset(evt_ptr, 0, sizeof(EVENT_MOBILE_FLOW));
      free(evt_ptr);
      break;

    case EVEL_DOMAIN_REPORT:
      EVEL_DEBUG("Event is a Report at %lp", evt_ptr);
      evel_free_report((EVENT_REPORT *)evt_ptr);
      memset(evt_ptr, 0, sizeof(EVENT_REPORT));
      free(evt_ptr);
      break;

    case EVEL_DOMAIN_HEARTBEAT_FIELD:
      EVEL_DEBUG("Event is a Heartbeat Field Event at %lp", evt_ptr);
      evel_free_hrtbt_field((EVENT_HEARTBEAT_FIELD *)evt_ptr);
      memset(evt_ptr, 0, sizeof(EVENT_HEARTBEAT_FIELD));
      free(evt_ptr);
      break;

    case EVEL_DOMAIN_SIPSIGNALING:
      EVEL_DEBUG("Event is a Signaling at %lp", evt_ptr);
      evel_free_signaling((EVENT_SIGNALING *)evt_ptr);
      memset(evt_ptr, 0, sizeof(EVENT_SIGNALING));
      free(evt_ptr);
      break;

    case EVEL_DOMAIN_STATE_CHANGE:
      EVEL_DEBUG("Event is a State Change at %lp", evt_ptr);
      evel_free_state_change((EVENT_STATE_CHANGE *)evt_ptr);
      memset(evt_ptr, 0, sizeof(EVENT_STATE_CHANGE));
      free(evt_ptr);
      break;

    case EVEL_DOMAIN_SYSLOG:
      EVEL_DEBUG("Event is a Syslog at %lp", evt_ptr);
      evel_free_syslog((EVENT_SYSLOG *)evt_ptr);
      memset(evt_ptr, 0, sizeof(EVENT_SYSLOG));
      free(evt_ptr);
      break;

    case EVEL_DOMAIN_OTHER:
      EVEL_DEBUG("Event is an Other at %lp", evt_ptr);
      evel_free_other((EVENT_OTHER *)evt_ptr);
      memset(evt_ptr, 0, sizeof(EVENT_OTHER));
      free(evt_ptr);
      break;

    case EVEL_DOMAIN_VOICE_QUALITY:
      EVEL_DEBUG("Event is an VoiceQuality at %lp", evt_ptr);
      evel_free_voice_quality((EVENT_VOICE_QUALITY *)evt_ptr);
      memset(evt_ptr, 0, sizeof(EVENT_VOICE_QUALITY));
      free(evt_ptr);
      break;

    default:
      EVEL_ERROR("Unexpected event domain (%d)", evt_ptr->event_domain);
      assert(0);
    }
  }
  EVEL_EXIT();
}