Merge "removed vFW vLB extra executables"
[demo.git] / vnfs / VES5.0 / evel / evel-library / code / evel_demo / evel_test_control.cbak
1 /**************************************************************************//**
2  * @file
3  * Utility to post test control commands to the test_collector testControl API.
4  *
5  * This software is a test capability, allowing test cases to trigger
6  * expected throttling behaviours at the test collector, for automated test
7  * capabilty.
8  *
9  * License
10  * -------
11  *
12  * Copyright(c) <2016>, AT&T Intellectual Property.  All other rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright notice,
18  * this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright notice,
20  * this list of conditions and the following disclaimer in the documentation
21  * and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  * must display the following acknowledgement:  This product includes software
24  * developed by the AT&T.
25  * 4. Neither the name of AT&T nor the names of its contributors may be used to
26  * endorse or promote products derived from this software without specific
27  * prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY AT&T INTELLECTUAL PROPERTY ''AS IS'' AND ANY
30  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32  * DISCLAIMED. IN NO EVENT SHALL AT&T INTELLECTUAL PROPERTY BE LIABLE FOR ANY
33  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  *****************************************************************************/
40
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 /**************************************************************************//**
55  * POST provide JSON to the test_collector testControl API.
56  *
57  * This function does not take ownership of the json_buffer passed in.
58  *
59  * @param json_buffer   Pointer to the JSON to POST
60  * @param json_size     The number of bytes to POST
61  * @param secure  Whether to use HTTPS (0=HTTP, 1=HTTPS)
62  * @param fqdn          The test control API FQDN or IP address.
63  * @param port          The test control API port.
64  *****************************************************************************/
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,
99            EVEL_MAX_URL_LEN,
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 /**************************************************************************//**
181  * Callback function to provide POST data.
182  *
183  * Copy data into the supplied buffer, read_callback::ptr, checking size
184  * limits.
185  *
186  * @returns   Number of bytes placed into read_callback::ptr. 0 for EOF.
187  *****************************************************************************/
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 /**************************************************************************//**
212  * POST a pre-set test scenario to the test_collector testControl API.
213  *
214  * This function provides various pre-configured scenarios, purely to avoid
215  * duplicating them elsewhere.
216  *
217  * @param scenario      The scenario to POST.
218  * @param secure        Whether to use HTTPS (0=HTTP, 1=HTTPS)
219  * @param fqdn          The test control API FQDN or IP address.
220  * @param port          The test control API port.
221  *****************************************************************************/
222 void evel_test_control_scenario(const EVEL_TEST_CONTROL_SCENARIO scenario,
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   {
235     case TC_RESET_ALL_DOMAINS:
236       json_size += snprintf(
237         json_buffer + json_size,
238         MAX_JSON - json_size,
239         "{"
240         "  \"commandList\": ["
241         "      \"command\": {"
242         "        \"commandType\": \"throttlingSpecification\","
243         "        \"eventDomainThrottleSpecification\": {"
244         "          \"eventDomain\": \"fault\""
245         "        }"
246         "    },"
247         "      \"command\": {"
248         "        \"commandType\": \"throttlingSpecification\","
249         "        \"eventDomainThrottleSpecification\": {"
250         "          \"eventDomain\": \"measurementsForVfScaling\""
251         "        }"
252         "    },"
253         "      \"command\": {"
254         "        \"commandType\": \"throttlingSpecification\","
255         "        \"eventDomainThrottleSpecification\": {"
256         "          \"eventDomain\": \"mobileFlow\""
257         "        }"
258         "    },"
259         "      \"command\": {"
260         "        \"commandType\": \"throttlingSpecification\","
261         "        \"eventDomainThrottleSpecification\": {"
262         "          \"eventDomain\": \"serviceEvents\""
263         "        }"
264         "    },"
265         "      \"command\": {"
266         "        \"commandType\": \"throttlingSpecification\","
267         "        \"eventDomainThrottleSpecification\": {"
268         "          \"eventDomain\": \"signaling\""
269         "        }"
270         "    },"
271         "      \"command\": {"
272         "        \"commandType\": \"throttlingSpecification\","
273         "        \"eventDomainThrottleSpecification\": {"
274         "          \"eventDomain\": \"stateChange\""
275         "        }"
276         "    },"
277         "      \"command\": {"
278         "        \"commandType\": \"throttlingSpecification\","
279         "        \"eventDomainThrottleSpecification\": {"
280         "          \"eventDomain\": \"syslog\""
281         "        }"
282         "    }"
283         "  ]"
284         "}");
285       break;
286
287     case TC_FAULT_SUPPRESS_FIELDS:
288       json_size += snprintf(
289         json_buffer + json_size,
290         MAX_JSON - json_size,
291         "{"
292         "  \"commandList\": ["
293         "      \"command\": {"
294         "        \"commandType\": \"throttlingSpecification\","
295         "        \"eventDomainThrottleSpecification\": {"
296         "          \"suppressedFieldNames\": ["
297         "            \"alarmInterfaceA\","
298         "            \"alarmAdditionalInformation\""
299         "          ],"
300         "          \"eventDomain\": \"fault\""
301         "        }"
302         "    }"
303         "  ]"
304         "}");
305       break;
306
307     case TC_FAULT_SUPPRESS_FIELDS_AND_PAIRS:
308       json_size += snprintf(
309         json_buffer + json_size,
310         MAX_JSON - json_size,
311         "{"
312         "  \"commandList\": ["
313         "      \"command\": {"
314         "        \"commandType\": \"throttlingSpecification\","
315         "        \"eventDomainThrottleSpecification\": {"
316         "          \"suppressedNvPairsList\": ["
317         "            {"
318         "              \"nvPairFieldName\": \"alarmAdditionalInformation\","
319         "              \"suppressedNvPairNames\": ["
320         "                \"name1\","
321         "                \"name2\""
322         "              ]"
323         "            }"
324         "          ],"
325         "          \"suppressedFieldNames\": ["
326         "            \"alarmInterfaceA\""
327         "          ],"
328         "          \"eventDomain\": \"fault\""
329         "        }"
330         "      }"
331         "  ]"
332         "}");
333       break;
334
335     case TC_FAULT_SUPPRESS_NOTHING:
336       json_size += snprintf(
337         json_buffer + json_size,
338         MAX_JSON - json_size,
339         "{"
340         "  \"commandList\": ["
341         "      \"command\": {"
342         "        \"commandType\": \"throttlingSpecification\","
343         "        \"eventDomainThrottleSpecification\": {"
344         "          \"eventDomain\": \"fault\""
345         "        }"
346         "      }"
347         "  ]"
348         "}");
349       break;
350
351     case TC_FAULT_SUPPRESS_PAIRS:
352       json_size += snprintf(
353         json_buffer + json_size,
354         MAX_JSON - json_size,
355         "{"
356         "  \"commandList\": ["
357         "      \"command\": {"
358         "        \"commandType\": \"throttlingSpecification\","
359         "        \"eventDomainThrottleSpecification\": {"
360         "          \"suppressedNvPairsList\": ["
361         "            {"
362         "              \"nvPairFieldName\": \"alarmAdditionalInformation\","
363         "              \"suppressedNvPairNames\": ["
364         "                \"name1\","
365         "                \"name2\""
366         "              ]"
367         "            }"
368         "          ],"
369         "          \"eventDomain\": \"fault\""
370         "        }"
371         "      }"
372         "  ]"
373         "}");
374       break;
375
376     case TC_MEAS_SUPPRESS_FIELDS_AND_PAIRS:
377       json_size += snprintf(
378         json_buffer + json_size,
379         MAX_JSON - json_size,
380         "{"
381         "  \"commandList\": ["
382         "      \"command\": {"
383         "        \"commandType\": \"throttlingSpecification\","
384         "        \"eventDomainThrottleSpecification\": {"
385         "          \"suppressedNvPairsList\": ["
386         "            {"
387         "              \"nvPairFieldName\": \"cpuUsageArray\","
388         "              \"suppressedNvPairNames\": ["
389         "                \"cpu1\","
390         "                \"cpu3\""
391         "              ]"
392         "            }"
393         "          ],"
394         "          \"suppressedFieldNames\": ["
395         "            \"numberOfMediaPortsInUse\","
396         "            \"aggregateCpuUsage\""
397         "          ],"
398         "          \"eventDomain\": \"measurementsForVfScaling\""
399         "        }"
400         "      }"
401         "  ]"
402         "}");
403       break;
404
405     case TC_MOBILE_SUPPRESS_FIELDS_AND_PAIRS:
406       json_size += snprintf(
407         json_buffer + json_size,
408         MAX_JSON - json_size,
409         "{"
410         "  \"commandList\": ["
411         "      \"command\": {"
412         "        \"commandType\": \"throttlingSpecification\","
413         "        \"eventDomainThrottleSpecification\": {"
414         "          \"suppressedFieldNames\": ["
415         "            \"radioAccessTechnology\","
416         "            \"samplingAlgorithm\""
417         "          ],"
418         "          \"eventDomain\": \"mobileFlow\""
419         "        }"
420         "      }"
421         "  ]"
422         "}");
423       break;
424
425     case TC_SERVICE_SUPPRESS_FIELDS_AND_PAIRS:
426       json_size += snprintf(
427         json_buffer + json_size,
428         MAX_JSON - json_size,
429         "{"
430         "  \"commandList\": ["
431         "      \"command\": {"
432         "        \"commandType\": \"throttlingSpecification\","
433         "        \"eventDomainThrottleSpecification\": {"
434         "          \"suppressedNvPairsList\": ["
435         "            {"
436         "              \"nvPairFieldName\": \"additionalFields\","
437         "              \"suppressedNvPairNames\": ["
438         "                \"Name1\","
439         "                \"Name3\""
440         "              ]"
441         "            }"
442         "          ],"
443         "          \"suppressedFieldNames\": ["
444         "            \"reportingEntityId\","
445         "            \"eventType\","
446         "            \"sourceId\","
447         "            \"codecSelected\","
448         "            \"codecSelectedTranscoding\","
449         "            \"midCallRtcp\","
450         "            \"endOfCallVqmSummaries\","
451         "            \"marker\""
452         "          ],"
453         "          \"eventDomain\": \"serviceEvents\""
454         "        }"
455         "      }"
456         "  ]"
457         "}");
458       break;
459
460     case TC_SIGNALING_SUPPRESS_FIELDS:
461       json_size += snprintf(
462         json_buffer + json_size,
463         MAX_JSON - json_size,
464         "{"
465         "  \"commandList\": ["
466         "      \"command\": {"
467         "        \"commandType\": \"throttlingSpecification\","
468         "        \"eventDomainThrottleSpecification\": {"
469         "          \"suppressedFieldNames\": ["
470         "            \"reportingEntityId\","
471         "            \"eventType\","
472         "            \"sourceId\","
473         "            \"localIpAddress\","
474         "            \"localIpPort\","
475         "            \"remoteIpAddress\","
476         "            \"remotePort\","
477         "            \"compressedSip\""
478         "          ],"
479         "          \"eventDomain\": \"signaling\""
480         "        }"
481         "    }"
482         "  ]"
483         "}");
484       break;
485
486     case TC_STATE_SUPPRESS_FIELDS_AND_PAIRS:
487       json_size += snprintf(
488         json_buffer + json_size,
489         MAX_JSON - json_size,
490         "{"
491         "  \"commandList\": ["
492         "      \"command\": {"
493         "        \"commandType\": \"throttlingSpecification\","
494         "        \"eventDomainThrottleSpecification\": {"
495         "          \"suppressedNvPairsList\": ["
496         "            {"
497         "              \"nvPairFieldName\": \"additionalFields\","
498         "              \"suppressedNvPairNames\": ["
499         "                \"Name1\""
500         "              ]"
501         "            }"
502         "          ],"
503         "          \"suppressedFieldNames\": ["
504         "            \"reportingEntityId\","
505         "            \"eventType\","
506         "            \"sourceId\""
507         "          ],"
508         "          \"eventDomain\": \"stateChange\""
509         "        }"
510         "      }"
511         "  ]"
512         "}");
513       break;
514
515     case TC_SYSLOG_SUPPRESS_FIELDS_AND_PAIRS:
516       json_size += snprintf(
517         json_buffer + json_size,
518         MAX_JSON - json_size,
519         "{"
520         "  \"commandList\": ["
521         "      \"command\": {"
522         "        \"commandType\": \"throttlingSpecification\","
523         "        \"eventDomainThrottleSpecification\": {"
524         "          \"suppressedNvPairsList\": ["
525         "            {"
526         "              \"nvPairFieldName\": \"additionalFields\","
527         "              \"suppressedNvPairNames\": ["
528         "                \"Name1\","
529         "                \"Name4\""
530         "              ]"
531         "            }"
532         "          ],"
533         "          \"suppressedFieldNames\": ["
534         "            \"syslogFacility\","
535         "            \"syslogProc\","
536         "            \"syslogProcId\""
537         "          ],"
538         "          \"eventDomain\": \"syslog\""
539         "        }"
540         "      }"
541         "  ]"
542         "}");
543       break;
544
545     case TC_PROVIDE_THROTTLING_SPEC:
546       json_size += snprintf(
547         json_buffer + json_size,
548         MAX_JSON - json_size,
549         "{"
550         "  \"commandList\": ["
551         "      \"command\": {"
552         "        \"commandType\": \"provideThrottlingState\""
553         "      }"
554         "  ]"
555         "}");
556       break;
557
558     default:
559       break;
560   }
561
562   if (json_size != 0)
563   {
564     evel_test_control(json_buffer, json_size, secure, fqdn, port);
565   }
566
567   EVEL_EXIT();
568 }
569
570 /**************************************************************************//**
571  * POST a measurement interval change to the test_collector testControl API.
572  *
573  * @param interval      The measurement interval.
574  * @param secure        Whether to use HTTPS (0=HTTP, 1=HTTPS)
575  * @param fqdn          The test control API FQDN or IP address.
576  * @param port          The test control API port.
577  *****************************************************************************/
578 void evel_test_control_meas_interval(const int interval,
579                                      const int secure,
580                                      const char * fqdn,
581                                      const int port)
582 {
583   const int MAX_JSON = 10000;
584   char json_buffer[MAX_JSON];
585   int json_size = 0;
586
587   EVEL_ENTER();
588
589   json_size += snprintf(
590     json_buffer + json_size,
591     MAX_JSON - json_size,
592     "{\"commandList\": [\"command\": "
593     "{\"commandType\": \"measurementIntervalChange\", "
594     "\"measurementInterval\": %d}]}",
595     interval);
596   evel_test_control(json_buffer, json_size, secure, fqdn, port);
597
598   EVEL_EXIT();
599 }