AT&T ECOMP Vendor Event Listener library  0.1
evel_test_control.c
Go to the documentation of this file.
1 /**************************************************************************/
41 #include <stdlib.h>
42 #include <string.h>
43 #include <curl/curl.h>
44 #include <assert.h>
45 
46 #include "evel_test_control.h"
47 #include "evel_internal.h" /* For MEMORY_CHUNK */
48 
49 /*****************************************************************************/
50 /* Local prototypes. */
51 /*****************************************************************************/
52 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp);
53 
54 /**************************************************************************/
65 void evel_test_control(char * const json_buffer,
66  const int json_size,
67  const int secure,
68  const char * fqdn,
69  const int port)
70 {
71  CURLcode curl_rc = CURLE_OK;
72  char curl_err_string[CURL_ERROR_SIZE] = "<NULL>";
73 
74  /***************************************************************************/
75  /* Get a curl handle. */
76  /***************************************************************************/
77  CURL * curl_handle = curl_easy_init();
78  assert(curl_handle != NULL);
79 
80  /***************************************************************************/
81  /* Prime the library to give friendly error codes. */
82  /***************************************************************************/
83  curl_rc = curl_easy_setopt(curl_handle,
84  CURLOPT_ERRORBUFFER,
85  curl_err_string);
86  assert(curl_rc == CURLE_OK);
87 
88  /***************************************************************************/
89  /* Build and set the testControl API URL. */
90  /***************************************************************************/
91  char version_string[10] = {0};
92  int offset = sprintf(version_string, "%d", EVEL_API_MAJOR_VERSION);
93  if (EVEL_API_MINOR_VERSION != 0)
94  {
95  sprintf(version_string + offset, ".%d", EVEL_API_MINOR_VERSION);
96  }
97  char test_control_url[EVEL_MAX_URL_LEN + 1] = {0};
98  snprintf(test_control_url,
100  "%s://%s:%d/testControl/v%s/commandList",
101  secure ? "https" : "http",
102  fqdn,
103  port,
104  version_string);
105  curl_rc = curl_easy_setopt(curl_handle, CURLOPT_URL, test_control_url);
106  assert(curl_rc == CURLE_OK);
107 
108  /***************************************************************************/
109  /* Some servers don't like requests that are made without a user-agent */
110  /* field, so we provide one. */
111  /***************************************************************************/
112  curl_rc = curl_easy_setopt(curl_handle,
113  CURLOPT_USERAGENT,
114  "libcurl-agent/1.0");
115  assert(curl_rc == CURLE_OK);
116 
117  /***************************************************************************/
118  /* Specify that we are going to POST data. */
119  /***************************************************************************/
120  curl_rc = curl_easy_setopt(curl_handle, CURLOPT_POST, 1L);
121  assert(curl_rc == CURLE_OK);
122 
123  /***************************************************************************/
124  /* We want to use our own read function. */
125  /***************************************************************************/
126  curl_rc = curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, read_callback);
127  assert(curl_rc == CURLE_OK);
128 
129  /***************************************************************************/
130  /* All of our events are JSON encoded. We also suppress the */
131  /* Expect: 100-continue header that we would otherwise get since it */
132  /* confuses some servers. */
133  /***************************************************************************/
134  static struct curl_slist * hdr_chunk = NULL;
135  hdr_chunk = curl_slist_append(hdr_chunk, "Content-type: application/json");
136  hdr_chunk = curl_slist_append(hdr_chunk, "Expect:");
137 
138  /***************************************************************************/
139  /* Set our custom set of headers. */
140  /***************************************************************************/
141  curl_rc = curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, hdr_chunk);
142  assert(curl_rc == CURLE_OK);
143 
144  /***************************************************************************/
145  /* Set the timeout for the operation. */
146  /***************************************************************************/
147  const int TEST_CTRL_TIMEOUT = 2;
148  curl_rc = curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, TEST_CTRL_TIMEOUT);
149  assert(curl_rc == CURLE_OK);
150 
151  /***************************************************************************/
152  /* Create a common pointer to pass to our read function, on stack. */
153  /***************************************************************************/
154  MEMORY_CHUNK tx_chunk = {json_buffer, json_size};
155  curl_rc = curl_easy_setopt(curl_handle, CURLOPT_READDATA, &tx_chunk);
156  assert(curl_rc == CURLE_OK);
157 
158  /***************************************************************************/
159  /* Set transmit size. */
160  /***************************************************************************/
161  curl_rc = curl_easy_setopt(curl_handle,
162  CURLOPT_POSTFIELDSIZE,
163  tx_chunk.size);
164  assert(curl_rc == CURLE_OK);
165 
166  /***************************************************************************/
167  /* Perform the POST. */
168  /***************************************************************************/
169  curl_rc = curl_easy_perform(curl_handle);
170  assert(curl_rc == CURLE_OK);
171 
172  /***************************************************************************/
173  /* Shut down the cURL library in a tidy manner. */
174  /***************************************************************************/
175  curl_easy_cleanup(curl_handle);
176 
177  EVEL_EXIT();
178 }
179 
180 /**************************************************************************/
188 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
189 {
190  size_t rtn = 0;
191  size_t bytes_to_write = 0;
192  MEMORY_CHUNK * tx_chunk = (MEMORY_CHUNK *)userp;
193 
194  EVEL_ENTER();
195 
196  bytes_to_write = min(size * nmemb, tx_chunk->size);
197 
198  if (bytes_to_write > 0)
199  {
200  strncpy((char *)ptr, tx_chunk->memory, bytes_to_write);
201  tx_chunk->memory += bytes_to_write;
202  tx_chunk->size -= bytes_to_write;
203  rtn = bytes_to_write;
204  }
205 
206  EVEL_EXIT();
207 
208  return rtn;
209 }
210 
211 /**************************************************************************/
223  const int secure,
224  const char * fqdn,
225  const int port)
226 {
227  const int MAX_JSON = 10000;
228  char json_buffer[MAX_JSON];
229  int json_size = 0;
230 
231  EVEL_ENTER();
232 
233  switch (scenario)
234  {
236  json_size += snprintf(
237  json_buffer + json_size,
238  MAX_JSON - json_size,
239  "{"
240  " \"commandList\": ["
241  " {"
242  " \"command\": {"
243  " \"commandType\": \"throttlingSpecification\","
244  " \"eventDomainThrottleSpecification\": {"
245  " \"eventDomain\": \"fault\""
246  " }"
247  " }"
248  " },"
249  " {"
250  " \"command\": {"
251  " \"commandType\": \"throttlingSpecification\","
252  " \"eventDomainThrottleSpecification\": {"
253  " \"eventDomain\": \"measurementsForVfScaling\""
254  " }"
255  " }"
256  " },"
257  " {"
258  " \"command\": {"
259  " \"commandType\": \"throttlingSpecification\","
260  " \"eventDomainThrottleSpecification\": {"
261  " \"eventDomain\": \"mobileFlow\""
262  " }"
263  " }"
264  " },"
265  " {"
266  " \"command\": {"
267  " \"commandType\": \"throttlingSpecification\","
268  " \"eventDomainThrottleSpecification\": {"
269  " \"eventDomain\": \"serviceEvents\""
270  " }"
271  " }"
272  " },"
273  " {"
274  " \"command\": {"
275  " \"commandType\": \"throttlingSpecification\","
276  " \"eventDomainThrottleSpecification\": {"
277  " \"eventDomain\": \"signaling\""
278  " }"
279  " }"
280  " },"
281  " {"
282  " \"command\": {"
283  " \"commandType\": \"throttlingSpecification\","
284  " \"eventDomainThrottleSpecification\": {"
285  " \"eventDomain\": \"stateChange\""
286  " }"
287  " }"
288  " },"
289  " {"
290  " \"command\": {"
291  " \"commandType\": \"throttlingSpecification\","
292  " \"eventDomainThrottleSpecification\": {"
293  " \"eventDomain\": \"syslog\""
294  " }"
295  " }"
296  " }"
297  " ]"
298  "}");
299  break;
300 
302  json_size += snprintf(
303  json_buffer + json_size,
304  MAX_JSON - json_size,
305  "{"
306  " \"commandList\": ["
307  " {"
308  " \"command\": {"
309  " \"commandType\": \"throttlingSpecification\","
310  " \"eventDomainThrottleSpecification\": {"
311  " \"suppressedFieldNames\": ["
312  " \"alarmInterfaceA\","
313  " \"alarmAdditionalInformation\""
314  " ],"
315  " \"eventDomain\": \"fault\""
316  " }"
317  " }"
318  " }"
319  " ]"
320  "}");
321  break;
322 
324  json_size += snprintf(
325  json_buffer + json_size,
326  MAX_JSON - json_size,
327  "{"
328  " \"commandList\": ["
329  " {"
330  " \"command\": {"
331  " \"commandType\": \"throttlingSpecification\","
332  " \"eventDomainThrottleSpecification\": {"
333  " \"suppressedNvPairsList\": ["
334  " {"
335  " \"nvPairFieldName\": \"alarmAdditionalInformation\","
336  " \"suppressedNvPairNames\": ["
337  " \"name1\","
338  " \"name2\""
339  " ]"
340  " }"
341  " ],"
342  " \"suppressedFieldNames\": ["
343  " \"alarmInterfaceA\""
344  " ],"
345  " \"eventDomain\": \"fault\""
346  " }"
347  " }"
348  " }"
349  " ]"
350  "}");
351  break;
352 
354  json_size += snprintf(
355  json_buffer + json_size,
356  MAX_JSON - json_size,
357  "{"
358  " \"commandList\": ["
359  " {"
360  " \"command\": {"
361  " \"commandType\": \"throttlingSpecification\","
362  " \"eventDomainThrottleSpecification\": {"
363  " \"eventDomain\": \"fault\""
364  " }"
365  " }"
366  " }"
367  " ]"
368  "}");
369  break;
370 
372  json_size += snprintf(
373  json_buffer + json_size,
374  MAX_JSON - json_size,
375  "{"
376  " \"commandList\": ["
377  " {"
378  " \"command\": {"
379  " \"commandType\": \"throttlingSpecification\","
380  " \"eventDomainThrottleSpecification\": {"
381  " \"suppressedNvPairsList\": ["
382  " {"
383  " \"nvPairFieldName\": \"alarmAdditionalInformation\","
384  " \"suppressedNvPairNames\": ["
385  " \"name1\","
386  " \"name2\""
387  " ]"
388  " }"
389  " ],"
390  " \"eventDomain\": \"fault\""
391  " }"
392  " }"
393  " }"
394  " ]"
395  "}");
396  break;
397 
399  json_size += snprintf(
400  json_buffer + json_size,
401  MAX_JSON - json_size,
402  "{"
403  " \"commandList\": ["
404  " {"
405  " \"command\": {"
406  " \"commandType\": \"throttlingSpecification\","
407  " \"eventDomainThrottleSpecification\": {"
408  " \"suppressedNvPairsList\": ["
409  " {"
410  " \"nvPairFieldName\": \"cpuUsageArray\","
411  " \"suppressedNvPairNames\": ["
412  " \"cpu1\","
413  " \"cpu3\""
414  " ]"
415  " }"
416  " ],"
417  " \"suppressedFieldNames\": ["
418  " \"numberOfMediaPortsInUse\","
419  " \"aggregateCpuUsage\""
420  " ],"
421  " \"eventDomain\": \"measurementsForVfScaling\""
422  " }"
423  " }"
424  " }"
425  " ]"
426  "}");
427  break;
428 
430  json_size += snprintf(
431  json_buffer + json_size,
432  MAX_JSON - json_size,
433  "{"
434  " \"commandList\": ["
435  " {"
436  " \"command\": {"
437  " \"commandType\": \"throttlingSpecification\","
438  " \"eventDomainThrottleSpecification\": {"
439  " \"suppressedFieldNames\": ["
440  " \"radioAccessTechnology\","
441  " \"samplingAlgorithm\""
442  " ],"
443  " \"eventDomain\": \"mobileFlow\""
444  " }"
445  " }"
446  " }"
447  " ]"
448  "}");
449  break;
450 
452  json_size += snprintf(
453  json_buffer + json_size,
454  MAX_JSON - json_size,
455  "{"
456  " \"commandList\": ["
457  " {"
458  " \"command\": {"
459  " \"commandType\": \"throttlingSpecification\","
460  " \"eventDomainThrottleSpecification\": {"
461  " \"suppressedNvPairsList\": ["
462  " {"
463  " \"nvPairFieldName\": \"additionalFields\","
464  " \"suppressedNvPairNames\": ["
465  " \"Name1\","
466  " \"Name3\""
467  " ]"
468  " }"
469  " ],"
470  " \"suppressedFieldNames\": ["
471  " \"reportingEntityId\","
472  " \"eventType\","
473  " \"sourceId\","
474  " \"codecSelected\","
475  " \"codecSelectedTranscoding\","
476  " \"midCallRtcp\","
477  " \"endOfCallVqmSummaries\","
478  " \"marker\""
479  " ],"
480  " \"eventDomain\": \"serviceEvents\""
481  " }"
482  " }"
483  " }"
484  " ]"
485  "}");
486  break;
487 
489  json_size += snprintf(
490  json_buffer + json_size,
491  MAX_JSON - json_size,
492  "{"
493  " \"commandList\": ["
494  " {"
495  " \"command\": {"
496  " \"commandType\": \"throttlingSpecification\","
497  " \"eventDomainThrottleSpecification\": {"
498  " \"suppressedFieldNames\": ["
499  " \"reportingEntityId\","
500  " \"eventType\","
501  " \"sourceId\","
502  " \"localIpAddress\","
503  " \"localIpPort\","
504  " \"remoteIpAddress\","
505  " \"remotePort\","
506  " \"compressedSip\""
507  " ],"
508  " \"eventDomain\": \"signaling\""
509  " }"
510  " }"
511  " }"
512  " ]"
513  "}");
514  break;
515 
517  json_size += snprintf(
518  json_buffer + json_size,
519  MAX_JSON - json_size,
520  "{"
521  " \"commandList\": ["
522  " {"
523  " \"command\": {"
524  " \"commandType\": \"throttlingSpecification\","
525  " \"eventDomainThrottleSpecification\": {"
526  " \"suppressedNvPairsList\": ["
527  " {"
528  " \"nvPairFieldName\": \"additionalFields\","
529  " \"suppressedNvPairNames\": ["
530  " \"Name1\""
531  " ]"
532  " }"
533  " ],"
534  " \"suppressedFieldNames\": ["
535  " \"reportingEntityId\","
536  " \"eventType\","
537  " \"sourceId\""
538  " ],"
539  " \"eventDomain\": \"stateChange\""
540  " }"
541  " }"
542  " }"
543  " ]"
544  "}");
545  break;
546 
548  json_size += snprintf(
549  json_buffer + json_size,
550  MAX_JSON - json_size,
551  "{"
552  " \"commandList\": ["
553  " {"
554  " \"command\": {"
555  " \"commandType\": \"throttlingSpecification\","
556  " \"eventDomainThrottleSpecification\": {"
557  " \"suppressedNvPairsList\": ["
558  " {"
559  " \"nvPairFieldName\": \"additionalFields\","
560  " \"suppressedNvPairNames\": ["
561  " \"Name1\","
562  " \"Name4\""
563  " ]"
564  " }"
565  " ],"
566  " \"suppressedFieldNames\": ["
567  " \"syslogFacility\","
568  " \"syslogProc\","
569  " \"syslogProcId\""
570  " ],"
571  " \"eventDomain\": \"syslog\""
572  " }"
573  " }"
574  " }"
575  " ]"
576  "}");
577  break;
578 
580  json_size += snprintf(
581  json_buffer + json_size,
582  MAX_JSON - json_size,
583  "{"
584  " \"commandList\": ["
585  " {"
586  " \"command\": {"
587  " \"commandType\": \"provideThrottlingState\""
588  " }"
589  " }"
590  " ]"
591  "}");
592  break;
593 
594  default:
595  break;
596  }
597 
598  if (json_size != 0)
599  {
600  evel_test_control(json_buffer, json_size, secure, fqdn, port);
601  }
602 
603  EVEL_EXIT();
604 }
605 
606 /**************************************************************************/
614 void evel_test_control_meas_interval(const int interval,
615  const int secure,
616  const char * fqdn,
617  const int port)
618 {
619  const int MAX_JSON = 10000;
620  char json_buffer[MAX_JSON];
621  int json_size = 0;
622 
623  EVEL_ENTER();
624 
625  json_size += snprintf(
626  json_buffer + json_size,
627  MAX_JSON - json_size,
628  "{\"commandList\": [{\"command\": "
629  "{\"commandType\": \"measurementIntervalChange\", "
630  "\"measurementInterval\": %d}}]}",
631  interval);
632  evel_test_control(json_buffer, json_size, secure, fqdn, port);
633 
634  EVEL_EXIT();
635 }
#define EVEL_MAX_URL_LEN
Definition: evel.h:103
A chunk of memory used in the cURL functions.
Definition: evel_internal.h:77
Utility to post test control commands to the test_collector testControl API.
void evel_test_control_scenario(const EVEL_TEST_CONTROL_SCENARIO scenario, const int secure, const char *fqdn, const int port)
POST a pre-set test scenario to the test_collector testControl API.
#define EVEL_EXIT()
Definition: evel.h:3631
#define EVEL_ENTER()
Definition: evel.h:3626
#define min(a, b)
Definition: evel_internal.h:53
void evel_test_control(char *const json_buffer, const int json_size, const int secure, const char *fqdn, const int port)
POST provide JSON to the test_collector testControl API.
#define EVEL_API_MAJOR_VERSION
Definition: evel.h:60
void evel_test_control_meas_interval(const int interval, const int secure, const char *fqdn, const int port)
POST a measurement interval change to the test_collector testControl API.
#define EVEL_API_MINOR_VERSION
Definition: evel.h:61
EVEL internal definitions.
EVEL_TEST_CONTROL_SCENARIO