Add License to VES library
[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  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *        http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *****************************************************************************/
21
22 #include <string.h>
23 #include <assert.h>
24 #include <stdlib.h>
25
26 #include "evel.h"
27 #include "evel_internal.h"
28
29 /**************************************************************************//**
30  * Create a new Other event.
31  *
32  * @note    The mandatory fields on the Other must be supplied to this factory
33  *          function and are immutable once set.  Optional fields have explicit
34  *          setter functions, but again values may only be set once so that the
35  *          Other has immutable properties.
36  * @returns pointer to the newly manufactured ::EVENT_OTHER.  If the event is
37  *          not used (i.e. posted) it must be released using ::evel_free_other.
38  * @retval  NULL  Failed to create the event.
39  *****************************************************************************/
40 EVENT_OTHER * evel_new_other()
41 {
42   EVENT_OTHER * other = NULL;
43   EVEL_ENTER();
44
45   /***************************************************************************/
46   /* Check preconditions.                                                    */
47   /***************************************************************************/
48
49   /***************************************************************************/
50   /* Allocate the Other.                                                     */
51   /***************************************************************************/
52   other = malloc(sizeof(EVENT_OTHER));
53   if (other == NULL)
54   {
55     log_error_state("Out of memory");
56     goto exit_label;
57   }
58   memset(other, 0, sizeof(EVENT_OTHER));
59   EVEL_DEBUG("New Other is at %lp", other);
60
61   /***************************************************************************/
62   /* Initialize the header & the Other fields.  Optional string values are   */
63   /* uninitialized (NULL).                                                   */
64   /***************************************************************************/
65   evel_init_header(&other->header);
66   other->header.event_domain = EVEL_DOMAIN_OTHER;
67   dlist_initialize(&other->other_fields);
68
69 exit_label:
70   EVEL_EXIT();
71   return other;
72 }
73
74 /**************************************************************************//**
75  * Set the Event Type property of the Other.
76  *
77  * @note  The property is treated as immutable: it is only valid to call
78  *        the setter once.  However, we don't assert if the caller tries to
79  *        overwrite, just ignoring the update instead.
80  *
81  * @param other       Pointer to the Other.
82  * @param type        The Event Type to be set. ASCIIZ string. The caller
83  *                    does not need to preserve the value once the function
84  *                    returns.
85  *****************************************************************************/
86 void evel_other_type_set(EVENT_OTHER * other,
87                          const char * const type)
88 {
89   EVEL_ENTER();
90
91   /***************************************************************************/
92   /* Check preconditions and call evel_header_type_set.                      */
93   /***************************************************************************/
94   assert(other != NULL);
95   assert(other->header.event_domain == EVEL_DOMAIN_OTHER);
96   evel_header_type_set(&other->header, type);
97
98   EVEL_EXIT();
99 }
100
101 /**************************************************************************//**
102  * Add a field name/value pair to the Other.
103  *
104  * The name and value are null delimited ASCII strings.  The library takes
105  * a copy so the caller does not have to preserve values after the function
106  * returns.
107  *
108  * @param other     Pointer to the Other.
109  * @param name      ASCIIZ string with the field's name.  The caller does not
110  *                  need to preserve the value once the function returns.
111  * @param value     ASCIIZ string with the field's value.  The caller does not
112  *                  need to preserve the value once the function returns.
113  *****************************************************************************/
114 void evel_other_field_add(EVENT_OTHER * other, char * name, char * value)
115 {
116   OTHER_FIELD * other_field = NULL;
117   EVEL_ENTER();
118
119   /***************************************************************************/
120   /* Check preconditions.                                                    */
121   /***************************************************************************/
122   assert(other != NULL);
123   assert(other->header.event_domain == EVEL_DOMAIN_OTHER);
124   assert(name != NULL);
125   assert(value != NULL);
126
127   EVEL_DEBUG("Adding name=%s value=%s", name, value);
128   other_field = malloc(sizeof(OTHER_FIELD));
129   assert(other_field != NULL);
130   memset(other_field, 0, sizeof(OTHER_FIELD));
131   other_field->name = strdup(name);
132   other_field->value = strdup(value);
133   assert(other_field->name != NULL);
134   assert(other_field->value != NULL);
135
136   dlist_push_last(&other->other_fields, other_field);
137
138   EVEL_EXIT();
139 }
140
141 /**************************************************************************//**
142  * Encode the Other in JSON according to AT&T's schema for the event type.
143  *
144  * @param jbuf          Pointer to the ::EVEL_JSON_BUFFER to encode into.
145  * @param event         Pointer to the ::EVENT_HEADER to encode.
146  *****************************************************************************/
147 void evel_json_encode_other(EVEL_JSON_BUFFER * jbuf,
148                             EVENT_OTHER * event)
149 {
150   OTHER_FIELD * other_field = NULL;
151   DLIST_ITEM * other_field_item = NULL;
152
153   EVEL_ENTER();
154
155   /***************************************************************************/
156   /* Check preconditions.                                                    */
157   /***************************************************************************/
158   assert(event != NULL);
159   assert(event->header.event_domain == EVEL_DOMAIN_OTHER);
160
161   evel_json_encode_header(jbuf, &event->header);
162   evel_json_open_named_list(jbuf, "otherFields");
163   other_field_item = dlist_get_first(&event->other_fields);
164   while (other_field_item != NULL)
165   {
166     other_field = (OTHER_FIELD *) other_field_item->item;
167     assert(other_field != NULL);
168
169     evel_json_open_object(jbuf);
170     evel_enc_kv_string(jbuf, "name", other_field->name);
171     evel_enc_kv_string(jbuf, "value", other_field->value);
172     evel_json_close_object(jbuf);
173     other_field_item = dlist_get_next(other_field_item);
174   }
175   evel_json_close_list(jbuf);
176
177   EVEL_EXIT();
178 }
179
180 /**************************************************************************//**
181  * Free an Other.
182  *
183  * Free off the Other supplied.  Will free all the contained allocated memory.
184  *
185  * @note It does not free the Other itself, since that may be part of a
186  * larger structure.
187  *****************************************************************************/
188 void evel_free_other(EVENT_OTHER * event)
189 {
190   OTHER_FIELD * other_field = NULL;
191
192   EVEL_ENTER();
193
194   /***************************************************************************/
195   /* Check preconditions.  As an internal API we don't allow freeing NULL    */
196   /* events as we do on the public API.                                      */
197   /***************************************************************************/
198   assert(event != NULL);
199   assert(event->header.event_domain == EVEL_DOMAIN_OTHER);
200
201   /***************************************************************************/
202   /* Free all internal strings then the header itself.                       */
203   /***************************************************************************/
204   other_field = dlist_pop_last(&event->other_fields);
205   while (other_field != NULL)
206   {
207     EVEL_DEBUG("Freeing Other Field (%s, %s)",
208                other_field->name,
209                other_field->value);
210     free(other_field->name);
211     free(other_field->value);
212     free(other_field);
213     other_field = dlist_pop_last(&event->other_fields);
214   }
215   evel_free_header(&event->header);
216
217   EVEL_EXIT();
218 }