Initial OpenECOMP Demo commit
[demo.git] / vnfs / VES / code / evel_library / evel_other.c
1 /**************************************************************************//**
2  * @file
3  * Implementation of EVEL functions relating to Other.
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.h"
40 #include "evel_internal.h"
41
42 /**************************************************************************//**
43  * Create a new Other event.
44  *
45  * @note    The mandatory fields on the Other must be supplied to this factory
46  *          function and are immutable once set.  Optional fields have explicit
47  *          setter functions, but again values may only be set once so that the
48  *          Other has immutable properties.
49  * @returns pointer to the newly manufactured ::EVENT_OTHER.  If the event is
50  *          not used (i.e. posted) it must be released using ::evel_free_other.
51  * @retval  NULL  Failed to create the event.
52  *****************************************************************************/
53 EVENT_OTHER * evel_new_other()
54 {
55   EVENT_OTHER * other = NULL;
56   EVEL_ENTER();
57
58   /***************************************************************************/
59   /* Check preconditions.                                                    */
60   /***************************************************************************/
61
62   /***************************************************************************/
63   /* Allocate the Other.                                                     */
64   /***************************************************************************/
65   other = malloc(sizeof(EVENT_OTHER));
66   if (other == NULL)
67   {
68     log_error_state("Out of memory");
69     goto exit_label;
70   }
71   memset(other, 0, sizeof(EVENT_OTHER));
72   EVEL_DEBUG("New Other is at %lp", other);
73
74   /***************************************************************************/
75   /* Initialize the header & the Other fields.  Optional string values are   */
76   /* uninitialized (NULL).                                                   */
77   /***************************************************************************/
78   evel_init_header(&other->header);
79   other->header.event_domain = EVEL_DOMAIN_OTHER;
80   dlist_initialize(&other->other_fields);
81
82 exit_label:
83   EVEL_EXIT();
84   return other;
85 }
86
87 /**************************************************************************//**
88  * Set the Event Type property of the Other.
89  *
90  * @note  The property is treated as immutable: it is only valid to call
91  *        the setter once.  However, we don't assert if the caller tries to
92  *        overwrite, just ignoring the update instead.
93  *
94  * @param other       Pointer to the Other.
95  * @param type        The Event Type to be set. ASCIIZ string. The caller
96  *                    does not need to preserve the value once the function
97  *                    returns.
98  *****************************************************************************/
99 void evel_other_type_set(EVENT_OTHER * other,
100                          const char * const type)
101 {
102   EVEL_ENTER();
103
104   /***************************************************************************/
105   /* Check preconditions and call evel_header_type_set.                      */
106   /***************************************************************************/
107   assert(other != NULL);
108   assert(other->header.event_domain == EVEL_DOMAIN_OTHER);
109   evel_header_type_set(&other->header, type);
110
111   EVEL_EXIT();
112 }
113
114 /**************************************************************************//**
115  * Add a field name/value pair to the Other.
116  *
117  * The name and value are null delimited ASCII strings.  The library takes
118  * a copy so the caller does not have to preserve values after the function
119  * returns.
120  *
121  * @param other     Pointer to the Other.
122  * @param name      ASCIIZ string with the field's name.  The caller does not
123  *                  need to preserve the value once the function returns.
124  * @param value     ASCIIZ string with the field's value.  The caller does not
125  *                  need to preserve the value once the function returns.
126  *****************************************************************************/
127 void evel_other_field_add(EVENT_OTHER * other, char * name, char * value)
128 {
129   OTHER_FIELD * other_field = NULL;
130   EVEL_ENTER();
131
132   /***************************************************************************/
133   /* Check preconditions.                                                    */
134   /***************************************************************************/
135   assert(other != NULL);
136   assert(other->header.event_domain == EVEL_DOMAIN_OTHER);
137   assert(name != NULL);
138   assert(value != NULL);
139
140   EVEL_DEBUG("Adding name=%s value=%s", name, value);
141   other_field = malloc(sizeof(OTHER_FIELD));
142   assert(other_field != NULL);
143   memset(other_field, 0, sizeof(OTHER_FIELD));
144   other_field->name = strdup(name);
145   other_field->value = strdup(value);
146   assert(other_field->name != NULL);
147   assert(other_field->value != NULL);
148
149   dlist_push_last(&other->other_fields, other_field);
150
151   EVEL_EXIT();
152 }
153
154 /**************************************************************************//**
155  * Encode the Other in JSON according to AT&T's schema for the event type.
156  *
157  * @param jbuf          Pointer to the ::EVEL_JSON_BUFFER to encode into.
158  * @param event         Pointer to the ::EVENT_HEADER to encode.
159  *****************************************************************************/
160 void evel_json_encode_other(EVEL_JSON_BUFFER * jbuf,
161                             EVENT_OTHER * event)
162 {
163   OTHER_FIELD * other_field = NULL;
164   DLIST_ITEM * other_field_item = NULL;
165
166   EVEL_ENTER();
167
168   /***************************************************************************/
169   /* Check preconditions.                                                    */
170   /***************************************************************************/
171   assert(event != NULL);
172   assert(event->header.event_domain == EVEL_DOMAIN_OTHER);
173
174   evel_json_encode_header(jbuf, &event->header);
175   evel_json_open_named_list(jbuf, "otherFields");
176   other_field_item = dlist_get_first(&event->other_fields);
177   while (other_field_item != NULL)
178   {
179     other_field = (OTHER_FIELD *) other_field_item->item;
180     assert(other_field != NULL);
181
182     evel_json_open_object(jbuf);
183     evel_enc_kv_string(jbuf, "name", other_field->name);
184     evel_enc_kv_string(jbuf, "value", other_field->value);
185     evel_json_close_object(jbuf);
186     other_field_item = dlist_get_next(other_field_item);
187   }
188   evel_json_close_list(jbuf);
189
190   EVEL_EXIT();
191 }
192
193 /**************************************************************************//**
194  * Free an Other.
195  *
196  * Free off the Other supplied.  Will free all the contained allocated memory.
197  *
198  * @note It does not free the Other itself, since that may be part of a
199  * larger structure.
200  *****************************************************************************/
201 void evel_free_other(EVENT_OTHER * event)
202 {
203   OTHER_FIELD * other_field = NULL;
204
205   EVEL_ENTER();
206
207   /***************************************************************************/
208   /* Check preconditions.  As an internal API we don't allow freeing NULL    */
209   /* events as we do on the public API.                                      */
210   /***************************************************************************/
211   assert(event != NULL);
212   assert(event->header.event_domain == EVEL_DOMAIN_OTHER);
213
214   /***************************************************************************/
215   /* Free all internal strings then the header itself.                       */
216   /***************************************************************************/
217   other_field = dlist_pop_last(&event->other_fields);
218   while (other_field != NULL)
219   {
220     EVEL_DEBUG("Freeing Other Field (%s, %s)",
221                other_field->name,
222                other_field->value);
223     free(other_field->name);
224     free(other_field->value);
225     free(other_field);
226     other_field = dlist_pop_last(&event->other_fields);
227   }
228   evel_free_header(&event->header);
229
230   EVEL_EXIT();
231 }