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