Initial OpenECOMP Demo commit
[demo.git] / vnfs / VES / code / evel_library / evel_throttle.h
1 #ifndef EVEL_THROTTLE_INCLUDED
2 #define EVEL_THROTTLE_INCLUDED
3
4 /**************************************************************************//**
5  * @file
6  * EVEL throttle definitions.
7  *
8  * These are internal definitions related to throttling specicications, which
9  * are required within the library but are not intended for external
10  * consumption.
11  *
12  * License
13  * -------
14  *
15  * Copyright(c) <2016>, AT&T Intellectual Property.  All other rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are met:
19  *
20  * 1. Redistributions of source code must retain the above copyright notice,
21  *    this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright notice,
23  *    this list of conditions and the following disclaimer in the documentation
24  *    and/or other materials provided with the distribution.
25  * 3. All advertising materials mentioning features or use of this software
26  *    must display the following acknowledgement:  This product includes
27  *    software developed by the AT&T.
28  * 4. Neither the name of AT&T nor the names of its contributors may be used to
29  *    endorse or promote products derived from this software without specific
30  *    prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY AT&T INTELLECTUAL PROPERTY ''AS IS'' AND ANY
33  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35  * DISCLAIMED. IN NO EVENT SHALL AT&T INTELLECTUAL PROPERTY BE LIABLE FOR ANY
36  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
41  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *****************************************************************************/
43
44 #include "evel_internal.h"
45 #include "jsmn.h"
46
47 /*****************************************************************************/
48 /* Maximum depth of JSON response that we can handle.                        */
49 /*****************************************************************************/
50 #define EVEL_JSON_STACK_DEPTH           10
51
52 /**************************************************************************//**
53  * Maximum number of tokens that we allow for in a JSON response.
54  *****************************************************************************/
55 #define EVEL_MAX_RESPONSE_TOKENS        1024
56
57 /**************************************************************************//**
58  * The nature of the next token that we are iterating through.  Within an
59  * object, we alternate between collecting keys and values.  Within an array,
60  * we only collect items.
61  *****************************************************************************/
62 typedef enum {
63   EVEL_JSON_KEY,
64   EVEL_JSON_VALUE,
65   EVEL_JSON_ITEM
66 } EVEL_JSON_STATE;
67
68 /**************************************************************************//**
69  * States which we move through during JSON processing, tracking our way
70  * through the supported JSON structure.
71  *****************************************************************************/
72 typedef enum
73 {
74   /***************************************************************************/
75   /* Initial state.                                                          */
76   /***************************************************************************/
77   EVEL_JCS_START,
78
79   /***************************************************************************/
80   /* {"commandList": [                                                       */
81   /***************************************************************************/
82   EVEL_JCS_COMMAND_LIST,
83
84   /***************************************************************************/
85   /* {"commandList": [{                                                      */
86   /***************************************************************************/
87   EVEL_JCS_COMMAND_LIST_ENTRY,
88
89   /***************************************************************************/
90   /* {"commandList": [{"command": {                                          */
91   /***************************************************************************/
92   EVEL_JCS_COMMAND,
93
94   /***************************************************************************/
95   /* ... "eventDomainThrottleSpecification": {                               */
96   /***************************************************************************/
97   EVEL_JCS_SPEC,
98
99   /***************************************************************************/
100   /* ... "suppressedFieldNames": [                                           */
101   /***************************************************************************/
102   EVEL_JCS_FIELD_NAMES,
103
104   /***************************************************************************/
105   /* ... "suppressedNvPairsList": [                                          */
106   /***************************************************************************/
107   EVEL_JCS_PAIRS_LIST,
108
109   /***************************************************************************/
110   /* ... "suppressedNvPairsList": [{                                         */
111   /***************************************************************************/
112   EVEL_JCS_PAIRS_LIST_ENTRY,
113
114   /***************************************************************************/
115   /* ... "suppressedNvPairNames": [                                          */
116   /***************************************************************************/
117   EVEL_JCS_NV_PAIR_NAMES,
118
119   EVEL_JCS_MAX
120 } EVEL_JSON_COMMAND_STATE;
121
122 /**************************************************************************//**
123  * An entry in the JSON stack.
124  *****************************************************************************/
125 typedef struct evel_json_stack_entry {
126
127   /***************************************************************************/
128   /* The number of elements required at this level.                          */
129   /***************************************************************************/
130   int num_required;
131
132   /***************************************************************************/
133   /* The number of elements collected at this level.                         */
134   /***************************************************************************/
135   int json_count;
136
137   /***************************************************************************/
138   /* The collection state at this level in the JSON stack.                   */
139   /***************************************************************************/
140   EVEL_JSON_STATE json_state;
141
142   /***************************************************************************/
143   /* The key being collected (if json_state is EVEL_JSON_VALUE), or NULL.    */
144   /***************************************************************************/
145   char * json_key;
146
147 } EVEL_JSON_STACK_ENTRY;
148
149 /**************************************************************************//**
150  * The JSON stack.
151  *****************************************************************************/
152 typedef struct evel_json_stack {
153
154   /***************************************************************************/
155   /* The current position of the stack - starting at zero.                   */
156   /***************************************************************************/
157   int level;
158
159   /***************************************************************************/
160   /* The stack itself.                                                       */
161   /***************************************************************************/
162   EVEL_JSON_STACK_ENTRY entry[EVEL_JSON_STACK_DEPTH];
163
164   /***************************************************************************/
165   /* The underlying memory chunk.                                            */
166   /***************************************************************************/
167   const MEMORY_CHUNK * chunk;
168
169 } EVEL_JSON_STACK;
170
171 /**************************************************************************//**
172  * Initialize event throttling to the default state.
173  *
174  * Called from ::evel_initialize.
175  *****************************************************************************/
176 void evel_throttle_initialize();
177
178 /**************************************************************************//**
179  * Clean up event throttling.
180  *
181  * Called from ::evel_terminate.
182  *****************************************************************************/
183 void evel_throttle_terminate();
184
185 /**************************************************************************//**
186  * Handle a JSON response from the listener, as a list of tokens from JSMN.
187  *
188  * @param chunk         Memory chunk containing the JSON buffer.
189  * @param json_tokens   Array of tokens to handle.
190  * @param num_tokens    The number of tokens to handle.
191  * @param post          The memory chunk in which to place any resulting POST.
192  * @return true if the command was handled, false otherwise.
193  *****************************************************************************/
194 bool evel_handle_command_list(const MEMORY_CHUNK * const chunk,
195                               const jsmntok_t * const json_tokens,
196                               const int num_tokens,
197                               MEMORY_CHUNK * const post);
198
199 /**************************************************************************//**
200  * Return the ::EVEL_THROTTLE_SPEC for a given domain.
201  *
202  * @param domain        The domain for which to return state.
203  *****************************************************************************/
204 EVEL_THROTTLE_SPEC * evel_get_throttle_spec(EVEL_EVENT_DOMAINS domain);
205
206 /**************************************************************************//**
207  * Determine whether a field_name should be suppressed.
208  *
209  * @param throttle_spec Throttle specification for the domain being encoded.
210  * @param field_name    The field name to encoded or suppress.
211  * @return true if the field_name should be suppressed, false otherwise.
212  *****************************************************************************/
213 bool evel_throttle_suppress_field(EVEL_THROTTLE_SPEC * throttle_spec,
214                                   const char * const field_name);
215
216 /**************************************************************************//**
217  * Determine whether a name-value pair should be allowed (not suppressed).
218  *
219  * @param throttle_spec Throttle specification for the domain being encoded.
220  * @param field_name    The field name holding the name-value pairs.
221  * @param name          The name of the name-value pair to encoded or suppress.
222  * @return true if the name-value pair should be suppressed, false otherwise.
223  *****************************************************************************/
224 bool evel_throttle_suppress_nv_pair(EVEL_THROTTLE_SPEC * throttle_spec,
225                                     const char * const field_name,
226                                     const char * const name);
227
228 #endif