Adding C and Java VES Vendor libs
[vnfsdk/compliance.git] / veslibrary / ves_clibrary / evel / evel-library / code / evel_library / evel_sipsignaling.c
1 /*************************************************************************//**
2  *
3  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and 
14  * limitations under the License.
15  *
16  ****************************************************************************/
17 /**************************************************************************//**
18  * @file
19  * Implementation of EVEL functions relating to Signaling.
20  *
21  ****************************************************************************/
22
23 #include <string.h>
24 #include <assert.h>
25 #include <stdlib.h>
26
27 #include "evel_throttle.h"
28
29 /**************************************************************************//**
30  * Create a new Signaling event.
31  *
32  * @note    The mandatory fields on the Signaling must be supplied to
33  *          this factory function and are immutable once set.  Optional fields
34  *          have explicit setter functions, but again values may only be set
35  *          once so that the event has immutable properties.
36  * @param event_name  Unique Event Name confirming Domain AsdcModel Description
37  * @param event_id    A universal identifier of the event for: troubleshooting correlation, analysis, etc
38  * @param vendor_name   The vendor id to encode in the event vnf field.
39  * @param module        The module to encode in the event.
40  * @param vnfname       The Virtual network function to encode in the event.
41  * @returns pointer to the newly manufactured ::EVENT_SIGNALING.  If the event
42  *          is not used (i.e. posted) it must be released using
43  *          ::evel_free_signaling.
44  * @retval  NULL  Failed to create the event.
45  *****************************************************************************/
46 EVENT_SIGNALING * evel_new_signaling(const char* ev_name, const char *ev_id,
47                                      const char * const vendor_name,
48                                      const char * const correlator,
49                                      const char * const local_ip_address,
50                                      const char * const local_port,
51                                      const char * const remote_ip_address,
52                                      const char * const remote_port)
53 {
54   EVENT_SIGNALING * event = NULL;
55
56   EVEL_ENTER();
57
58   /***************************************************************************/
59   /* Check preconditions.                                                    */
60   /***************************************************************************/
61   assert(vendor_name != NULL);
62
63   /***************************************************************************/
64   /* Allocate the Signaling event.                                           */
65   /***************************************************************************/
66   event = malloc(sizeof(EVENT_SIGNALING));
67   if (event == NULL)
68   {
69     log_error_state("Out of memory");
70     goto exit_label;
71   }
72   memset(event, 0, sizeof(EVENT_SIGNALING));
73   EVEL_DEBUG("New Signaling event is at %lp", event);
74
75   /***************************************************************************/
76   /* Initialize the header & the Signaling fields.                           */
77   /***************************************************************************/
78   evel_init_header_nameid(&event->header,ev_name,ev_id);
79   event->header.event_domain = EVEL_DOMAIN_SIPSIGNALING;
80   event->major_version = EVEL_SIGNALING_MAJOR_VERSION;
81   event->minor_version = EVEL_SIGNALING_MINOR_VERSION;
82   evel_init_vendor_field(&event->vnfname_field, vendor_name);
83   evel_set_option_string(&event->correlator,correlator,"Init correlator");
84   evel_set_option_string(&event->local_ip_address,local_ip_address,"Init correlator");
85   evel_set_option_string(&event->local_port,local_port,"Init local port");
86   evel_set_option_string(&event->remote_ip_address,remote_ip_address,"Init remote ip");
87   evel_set_option_string(&event->remote_port,remote_port,"Init remote port");
88   evel_init_option_string(&event->compressed_sip);
89   evel_init_option_string(&event->summary_sip);
90   dlist_initialize(&event->additional_info);
91
92 exit_label:
93
94   EVEL_EXIT();
95   return event;
96 }
97
98 /**************************************************************************//**
99  * Add an additional value name/value pair to the SIP signaling.
100  *
101  * The name and value are null delimited ASCII strings.  The library takes
102  * a copy so the caller does not have to preserve values after the function
103  * returns.
104  *
105  * @param event     Pointer to the fault.
106  * @param name      ASCIIZ string with the attribute's name.  The caller
107  *                  does not need to preserve the value once the function
108  *                  returns.
109  * @param value     ASCIIZ string with the attribute's value.  The caller
110  *                  does not need to preserve the value once the function
111  *                  returns.
112  *****************************************************************************/
113 void evel_signaling_addl_info_add(EVENT_SIGNALING * event, char * name, char * value)
114 {
115   FAULT_ADDL_INFO * addl_info = NULL;
116   EVEL_ENTER();
117
118   /***************************************************************************/
119   /* Check preconditions.                                                    */
120   /***************************************************************************/
121   assert(event != NULL);
122   assert(event->header.event_domain == EVEL_DOMAIN_SIPSIGNALING);
123   assert(name != NULL);
124   assert(value != NULL);
125
126   EVEL_DEBUG("Adding name=%s value=%s", name, value);
127   addl_info = malloc(sizeof(SIGNALING_ADDL_FIELD));
128   assert(addl_info != NULL);
129   memset(addl_info, 0, sizeof(SIGNALING_ADDL_FIELD));
130   addl_info->name = strdup(name);
131   addl_info->value = strdup(value);
132   assert(addl_info->name != NULL);
133   assert(addl_info->value != NULL);
134
135   dlist_push_last(&event->additional_info, addl_info);
136
137   EVEL_EXIT();
138 }
139
140
141 /**************************************************************************//**
142  * Set the Event Type property of the Signaling event.
143  *
144  * @note  The property is treated as immutable: it is only valid to call
145  *        the setter once.  However, we don't assert if the caller tries to
146  *        overwrite, just ignoring the update instead.
147  *
148  * @param event         Pointer to the Signaling event.
149  * @param type          The Event Type to be set. ASCIIZ string. The caller
150  *                      does not need to preserve the value once the function
151  *                      returns.
152  *****************************************************************************/
153 void evel_signaling_type_set(EVENT_SIGNALING * const event,
154                              const char * const type)
155 {
156   EVEL_ENTER();
157
158   /***************************************************************************/
159   /* Check preconditions and call evel_header_type_set.                      */
160   /***************************************************************************/
161   assert(event != NULL);
162   assert(event->header.event_domain == EVEL_DOMAIN_SIPSIGNALING);
163   evel_header_type_set(&event->header, type);
164
165   EVEL_EXIT();
166 }
167
168 /**************************************************************************//**
169  * Set the Local Ip Address property of the Signaling event.
170  *
171  * @note  The property is treated as immutable: it is only valid to call
172  *        the setter once.  However, we don't assert if the caller tries to
173  *        overwrite, just ignoring the update instead.
174  *
175  * @param event         Pointer to the Signaling event.
176  * @param local_ip_address
177  *                      The Local Ip Address to be set. ASCIIZ string. The
178  *                      caller does not need to preserve the value once the
179  *                      function returns.
180  *****************************************************************************/
181 void evel_signaling_local_ip_address_set(EVENT_SIGNALING * const event,
182                                          const char * const local_ip_address)
183 {
184   EVEL_ENTER();
185
186   /***************************************************************************/
187   /* Check preconditions.                                                    */
188   /***************************************************************************/
189   assert(event != NULL);
190   assert(event->header.event_domain == EVEL_DOMAIN_SIPSIGNALING);
191   assert(local_ip_address != NULL);
192
193   evel_set_option_string(&event->local_ip_address,
194                          local_ip_address,
195                          "Local Ip Address");
196
197   EVEL_EXIT();
198 }
199
200 /**************************************************************************//**
201  * Set the Local Port property of the Signaling event.
202  *
203  * @note  The property is treated as immutable: it is only valid to call
204  *        the setter once.  However, we don't assert if the caller tries to
205  *        overwrite, just ignoring the update instead.
206  *
207  * @param event         Pointer to the Signaling event.
208  * @param local_port    The Local Port to be set. ASCIIZ string. The caller
209  *                      does not need to preserve the value once the function
210  *                      returns.
211  *****************************************************************************/
212 void evel_signaling_local_port_set(EVENT_SIGNALING * const event,
213                                    const char * const local_port)
214 {
215   EVEL_ENTER();
216
217   /***************************************************************************/
218   /* Check preconditions.                                                    */
219   /***************************************************************************/
220   assert(event != NULL);
221   assert(event->header.event_domain == EVEL_DOMAIN_SIPSIGNALING);
222   assert(local_port != NULL);
223
224   evel_set_option_string(&event->local_port,
225                          local_port,
226                          "Local Port");
227
228   EVEL_EXIT();
229 }
230
231 /**************************************************************************//**
232  * Set the Remote Ip Address property of the Signaling event.
233  *
234  * @note  The property is treated as immutable: it is only valid to call
235  *        the setter once.  However, we don't assert if the caller tries to
236  *        overwrite, just ignoring the update instead.
237  *
238  * @param event         Pointer to the Signaling event.
239  * @param remote_ip_address
240  *                      The Remote Ip Address to be set. ASCIIZ string. The
241  *                      caller does not need to preserve the value once the
242  *                      function returns.
243  *****************************************************************************/
244 void evel_signaling_remote_ip_address_set(EVENT_SIGNALING * const event,
245                                           const char * const remote_ip_address)
246 {
247   EVEL_ENTER();
248
249   /***************************************************************************/
250   /* Check preconditions.                                                    */
251   /***************************************************************************/
252   assert(event != NULL);
253   assert(event->header.event_domain == EVEL_DOMAIN_SIPSIGNALING);
254   assert(remote_ip_address != NULL);
255
256   evel_set_option_string(&event->remote_ip_address,
257                          remote_ip_address,
258                          "Remote Ip Address");
259
260   EVEL_EXIT();
261 }
262
263 /**************************************************************************//**
264  * Set the Remote Port property of the Signaling event.
265  *
266  * @note  The property is treated as immutable: it is only valid to call
267  *        the setter once.  However, we don't assert if the caller tries to
268  *        overwrite, just ignoring the update instead.
269  *
270  * @param event         Pointer to the Signaling event.
271  * @param remote_port   The Remote Port to be set. ASCIIZ string. The caller
272  *                      does not need to preserve the value once the function
273  *                      returns.
274  *****************************************************************************/
275 void evel_signaling_remote_port_set(EVENT_SIGNALING * const event,
276                                     const char * const remote_port)
277 {
278   EVEL_ENTER();
279
280   /***************************************************************************/
281   /* Check preconditions.                                                    */
282   /***************************************************************************/
283   assert(event != NULL);
284   assert(event->header.event_domain == EVEL_DOMAIN_SIPSIGNALING);
285   assert(remote_port != NULL);
286
287   evel_set_option_string(&event->remote_port,
288                          remote_port,
289                          "Remote Port");
290
291   EVEL_EXIT();
292 }
293
294 /**************************************************************************//**
295  * Set the Vendor module property of the Signaling event.
296  *
297  * @note  The property is treated as immutable: it is only valid to call
298  *        the setter once.  However, we don't assert if the caller tries to
299  *        overwrite, just ignoring the update instead.
300  *
301  * @param event         Pointer to the Signaling event.
302  * @param modulename    The module name to be set. ASCIIZ string. The caller
303  *                      does not need to preserve the value once the function
304  *                      returns.
305  *****************************************************************************/
306 void evel_signaling_vnfmodule_name_set(EVENT_SIGNALING * const event,
307                                     const char * const module_name)
308 {
309   EVEL_ENTER();
310
311   /***************************************************************************/
312   /* Check preconditions.                                                    */
313   /***************************************************************************/
314   assert(event != NULL);
315   assert(event->header.event_domain == EVEL_DOMAIN_SIPSIGNALING);
316   assert(module_name != NULL);
317
318   evel_vendor_field_module_set(&event->vnfname_field, module_name);
319
320   EVEL_EXIT();
321 }
322
323 /**************************************************************************//**
324  * Set the Vendor module property of the Signaling event.
325  *
326  * @note  The property is treated as immutable: it is only valid to call
327  *        the setter once.  However, we don't assert if the caller tries to
328  *        overwrite, just ignoring the update instead.
329  *
330  * @param event         Pointer to the Signaling event.
331  * @param vnfname       The Virtual Network function to be set. ASCIIZ string.
332  *                      The caller does not need to preserve the value once
333  *                      the function returns.
334  *****************************************************************************/
335 void evel_signaling_vnfname_set(EVENT_SIGNALING * const event,
336                                     const char * const vnfname)
337 {
338   EVEL_ENTER();
339
340   /***************************************************************************/
341   /* Check preconditions.                                                    */
342   /***************************************************************************/
343   assert(event != NULL);
344   assert(event->header.event_domain == EVEL_DOMAIN_SIPSIGNALING);
345   assert(vnfname != NULL);
346
347   evel_vendor_field_vnfname_set(&event->vnfname_field, vnfname);
348
349   EVEL_EXIT();
350 }
351
352 /**************************************************************************//**
353  * Set the Compressed SIP property of the Signaling event.
354  *
355  * @note  The property is treated as immutable: it is only valid to call
356  *        the setter once.  However, we don't assert if the caller tries to
357  *        overwrite, just ignoring the update instead.
358  *
359  * @param event         Pointer to the Signaling event.
360  * @param compressed_sip
361  *                      The Compressed SIP to be set. ASCIIZ string. The caller
362  *                      does not need to preserve the value once the function
363  *                      returns.
364  *****************************************************************************/
365 void evel_signaling_compressed_sip_set(EVENT_SIGNALING * const event,
366                                        const char * const compressed_sip)
367 {
368   EVEL_ENTER();
369
370   /***************************************************************************/
371   /* Check preconditions.                                                    */
372   /***************************************************************************/
373   assert(event != NULL);
374   assert(event->header.event_domain == EVEL_DOMAIN_SIPSIGNALING);
375   assert(compressed_sip != NULL);
376
377   evel_set_option_string(&event->compressed_sip,
378                          compressed_sip,
379                          "Compressed SIP");
380
381   EVEL_EXIT();
382 }
383
384 /**************************************************************************//**
385  * Set the Summary SIP property of the Signaling event.
386  *
387  * @note  The property is treated as immutable: it is only valid to call
388  *        the setter once.  However, we don't assert if the caller tries to
389  *        overwrite, just ignoring the update instead.
390  *
391  * @param event         Pointer to the Signaling event.
392  * @param summary_sip   The Summary SIP to be set. ASCIIZ string. The caller
393  *                      does not need to preserve the value once the function
394  *                      returns.
395  *****************************************************************************/
396 void evel_signaling_summary_sip_set(EVENT_SIGNALING * const event,
397                                     const char * const summary_sip)
398 {
399   EVEL_ENTER();
400
401   /***************************************************************************/
402   /* Check preconditions.                                                    */
403   /***************************************************************************/
404   assert(event != NULL);
405   assert(event->header.event_domain == EVEL_DOMAIN_SIPSIGNALING);
406   assert(summary_sip != NULL);
407
408   evel_set_option_string(&event->summary_sip,
409                          summary_sip,
410                          "Summary SIP");
411
412   EVEL_EXIT();
413 }
414
415 /**************************************************************************//**
416  * Set the Correlator property of the Signaling event.
417  *
418  * @note  The property is treated as immutable: it is only valid to call
419  *        the setter once.  However, we don't assert if the caller tries to
420  *        overwrite, just ignoring the update instead.
421  *
422  * @param event         Pointer to the Signaling event.
423  * @param correlator    The correlator to be set. ASCIIZ string. The caller
424  *                      does not need to preserve the value once the function
425  *                      returns.
426  *****************************************************************************/
427 void evel_signaling_correlator_set(EVENT_SIGNALING * const event,
428                                    const char * const correlator)
429 {
430   EVEL_ENTER();
431
432   /***************************************************************************/
433   /* Check preconditions and call evel_header_type_set.                      */
434   /***************************************************************************/
435   assert(event != NULL);
436   assert(event->header.event_domain == EVEL_DOMAIN_SIPSIGNALING);
437   evel_set_option_string(&event->correlator,
438                          correlator,
439                          "Correlator");
440
441   EVEL_EXIT();
442 }
443
444 /**************************************************************************//**
445  * Encode the Signaling in JSON according to AT&T's schema for the
446  * event type.
447  *
448  * @param jbuf          Pointer to the ::EVEL_JSON_BUFFER to encode into.
449  * @param event         Pointer to the ::EVENT_HEADER to encode.
450  *****************************************************************************/
451 void evel_json_encode_signaling(EVEL_JSON_BUFFER * const jbuf,
452                                 EVENT_SIGNALING * const event)
453 {
454   SIGNALING_ADDL_FIELD * addl_info = NULL;
455   DLIST_ITEM * addl_info_item = NULL;
456
457   EVEL_ENTER();
458
459   /***************************************************************************/
460   /* Check preconditions.                                                    */
461   /***************************************************************************/
462   assert(event != NULL);
463   assert(event->header.event_domain == EVEL_DOMAIN_SIPSIGNALING);
464
465   evel_json_encode_header(jbuf, &event->header);
466   evel_json_open_named_object(jbuf, "signalingFields");
467
468   /***************************************************************************/
469   /* Mandatory fields                                                        */
470   /***************************************************************************/
471
472   /***************************************************************************/
473   /* Optional fields                                                         */
474   /***************************************************************************/
475   evel_enc_kv_opt_string(jbuf, "compressedSip", &event->compressed_sip);
476   evel_enc_kv_opt_string(jbuf, "correlator", &event->correlator);
477   evel_enc_kv_opt_string(jbuf, "localIpAddress", &event->local_ip_address);
478   evel_enc_kv_opt_string(jbuf, "localPort", &event->local_port);
479   evel_enc_kv_opt_string(jbuf, "remoteIpAddress", &event->remote_ip_address);
480   evel_enc_kv_opt_string(jbuf, "remotePort", &event->remote_port);
481   evel_enc_version(jbuf, "signalingFieldsVersion", event->major_version,event->minor_version);
482   evel_enc_kv_opt_string(jbuf, "summarySip", &event->summary_sip);
483   evel_json_encode_vendor_field(jbuf, &event->vnfname_field);
484
485
486   /***************************************************************************/
487   /* Checkpoint, so that we can wind back if all fields are suppressed.      */
488   /***************************************************************************/
489   evel_json_checkpoint(jbuf);
490   if (evel_json_open_opt_named_list(jbuf, "additionalInformation"))
491   {
492     bool item_added = false;
493
494     addl_info_item = dlist_get_first(&event->additional_info);
495     while (addl_info_item != NULL)
496     { 
497       addl_info = (SIGNALING_ADDL_FIELD*) addl_info_item->item;
498       assert(addl_info != NULL);
499
500       if (!evel_throttle_suppress_nv_pair(jbuf->throttle_spec,
501                                           "additionalInformation",
502                                           addl_info->name))
503       {
504         evel_json_open_object(jbuf);
505         evel_enc_kv_string(jbuf, "name", addl_info->name);
506         evel_enc_kv_string(jbuf, "value", addl_info->value);
507         evel_json_close_object(jbuf);
508         item_added = true;
509       }
510       addl_info_item = dlist_get_next(addl_info_item);
511     }
512     evel_json_close_list(jbuf);
513     
514     /*************************************************************************/
515     /* If we've not written anything, rewind to before we opened the list.   */
516     /*************************************************************************/
517     if (!item_added)
518     {
519       evel_json_rewind(jbuf);
520     }
521   }
522
523   evel_json_close_object(jbuf);
524
525   EVEL_EXIT();
526 }
527
528 /**************************************************************************//**
529  * Free a Signaling event.
530  *
531  * Free off the event supplied.  Will free all the contained allocated memory.
532  *
533  * @note It does not free the event itself, since that may be part of a larger
534  * structure.
535  *****************************************************************************/
536 void evel_free_signaling(EVENT_SIGNALING * const event)
537 {
538   SIGNALING_ADDL_FIELD * addl_info = NULL;
539   EVEL_ENTER();
540
541   /***************************************************************************/
542   /* Check preconditions.  As an internal API we don't allow freeing NULL    */
543   /* events as we do on the public API.                                      */
544   /***************************************************************************/
545   assert(event != NULL);
546   assert(event->header.event_domain == EVEL_DOMAIN_SIPSIGNALING);
547
548  /***************************************************************************/
549   /* Free all internal strings then the header itself.                       */
550   /***************************************************************************/
551   addl_info = dlist_pop_last(&event->additional_info);
552   while (addl_info != NULL)
553   {
554     EVEL_DEBUG("Freeing Additional Info (%s, %s)",
555                addl_info->name,
556                addl_info->value);
557     free(addl_info->name);
558     free(addl_info->value);
559     free(addl_info);
560     addl_info = dlist_pop_last(&event->additional_info);
561   }
562
563   evel_free_event_vendor_field(&event->vnfname_field);
564   evel_free_option_string(&event->correlator);
565   evel_free_option_string(&event->local_ip_address);
566   evel_free_option_string(&event->local_port);
567   evel_free_option_string(&event->remote_ip_address);
568   evel_free_option_string(&event->remote_port);
569   evel_free_option_string(&event->compressed_sip);
570   evel_free_option_string(&event->summary_sip);
571   evel_free_header(&event->header);
572
573   EVEL_EXIT();
574 }