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