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