Update License text
[vnfsdk/compliance.git] / veslibrary / ves_javalibrary / evel_javalib2 / src / evel_javalibrary / att / com / EvelOther.java
1 package evel_javalibrary.att.com;\r
2 /**************************************************************************//**\r
3  * @file\r
4  * Evel Other class\r
5  *\r
6  * This file implements the Evel Other class to handle Other domain events.\r
7  *\r
8  * License\r
9  * -------\r
10  * Unless otherwise specified, all software contained herein is\r
11  * Licensed under the Apache License, Version 2.0 (the "License");\r
12  * you may not use this file except in compliance with the License.\r
13  * You may obtain a copy of the License at\r
14  *        http://www.apache.org/licenses/LICENSE-2.0\r
15  *\r
16  * Unless required by applicable law or agreed to in writing, software\r
17  * distributed under the License is distributed on an "AS IS" BASIS,\r
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
19  * See the License for the specific language governing permissions and\r
20  * limitations under the License.\r
21  *****************************************************************************/\r
22 \r
23 import java.text.MessageFormat;\r
24 import java.util.ArrayList;\r
25 import java.util.HashMap;\r
26 import java.util.Map;\r
27 import java.util.Set;\r
28 \r
29 import javax.json.Json;\r
30 import javax.json.JsonArrayBuilder;\r
31 import javax.json.JsonObject;\r
32 import javax.json.JsonObjectBuilder;\r
33 \r
34 import org.apache.log4j.Logger;\r
35 import org.slf4j.helpers.MessageFormatter;\r
36 \r
37 /*\r
38  * Handles Optional Other fields\r
39  */\r
40 public class EvelOther extends EvelHeader {\r
41 \r
42           int major_version = 1;\r
43           int minor_version = 1;\r
44 \r
45           /***************************************************************************/\r
46           /* Mandatory fields                                                        */\r
47           /***************************************************************************/\r
48           ArrayList<javax.json.JsonObject> additional_objects;\r
49           \r
50           /***************************************************************************/\r
51           /* Optional fields                                                         */\r
52           /***************************************************************************/\r
53           Map<String,String> additional_info;\r
54           HashMap<String,Map<String,String>> evelmap;\r
55           \r
56 \r
57           private static final Logger LOGGER = Logger.getLogger( EvelOther.class.getName() );\r
58 \r
59           /**************************************************************************//**\r
60            * Create a new Other event.\r
61            *\r
62            *****************************************************************************/\r
63           public EvelOther(String evname,String evid)\r
64           {\r
65                   //Init header and domain\r
66         super(evname,evid);\r
67 \r
68             EVEL_ENTER();\r
69 \r
70             /***************************************************************************/\r
71             /* Check preconditions.                                                    */\r
72             /***************************************************************************/\r
73 \r
74             /***************************************************************************/\r
75             /* Allocate the measurement.                                               */\r
76             /***************************************************************************/\r
77             LOGGER.debug("New Evel Other Object");\r
78             \r
79             /***************************************************************************/\r
80             /* Initialize the header & the measurement fields.                         */\r
81             /***************************************************************************/\r
82             event_domain = EvelHeader.DOMAINS.EVEL_DOMAIN_OTHER;\r
83 \r
84             /***************************************************************************/\r
85             /* Optional fields.                                                    */\r
86             /***************************************************************************/\r
87             additional_info = null;\r
88             additional_objects = null;\r
89             evelmap = null;\r
90 \r
91             EVEL_EXIT();\r
92           }\r
93 \r
94           /**************************************************************************//**\r
95            * Set the Event Type property of the Measurement.\r
96            *\r
97            * @note  The property is treated as immutable: it is only valid to call\r
98            *        the setter once.  However, we don't assert if the caller tries to\r
99            *        overwrite, just ignoring the update instead.\r
100            *\r
101            * @param type        The Event Type to be set. ASCIIZ string. The caller\r
102            *                    does not need to preserve the value once the function\r
103            *                    returns.\r
104            *****************************************************************************/\r
105           public void evel_other_type_set(String typ)\r
106           {\r
107             EVEL_ENTER();\r
108             assert(typ != null);\r
109 \r
110             /***************************************************************************/\r
111             /* Check preconditions and call evel_header_type_set.                      */\r
112             /***************************************************************************/\r
113             assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_OTHER);\r
114             evel_header_type_set(typ);\r
115 \r
116             EVEL_EXIT();\r
117           }\r
118 \r
119           \r
120           \r
121           \r
122           /**************************************************************************//**\r
123            * Adds name value pair under hash key\r
124            *\r
125            *\r
126            * @param hashname String         Hash name.\r
127            * @param name String             Name.\r
128            * @param value String            Value.\r
129            *****************************************************************************/\r
130           public void evel_other_field_add_namedarray(String hashname,  String name, String value)\r
131           {\r
132             EVEL_ENTER();\r
133 \r
134             /***************************************************************************/\r
135             /* Check preconditions.                                                    */\r
136             /***************************************************************************/\r
137             assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_OTHER);\r
138             assert(hashname != null);\r
139             assert(name != null);\r
140             assert(value != null);\r
141             \r
142             if( evelmap == null)\r
143                evelmap = new HashMap<String,Map<String,String>>();\r
144                 \r
145             LOGGER.debug("Adding hash : "+hashname+" name="+name+"value= "+value);\r
146             \r
147             Map<String,String> mymap = null;\r
148             try{\r
149              mymap = evelmap.get(hashname);\r
150             } catch( Exception e)\r
151             {\r
152                 e.printStackTrace();\r
153             }\r
154             \r
155             if(mymap == null)\r
156                 mymap = new HashMap<String,String>();\r
157             try{\r
158             if( mymap.put(name, value) == null)\r
159                 LOGGER.debug("Unable to add map hash : "+hashname+" name="+name+"value= "+value);;\r
160             \r
161             if( evelmap.put(hashname, mymap) == null)\r
162                 LOGGER.debug("Unable to add hash entry : "+hashname+" name="+name+"value= "+value);;\r
163             } catch( Exception e)\r
164             {\r
165                 e.printStackTrace();\r
166             }\r
167 \r
168             EVEL_EXIT();\r
169           }\r
170 \r
171 \r
172           /**************************************************************************//**\r
173            * Add a json object to optional jsonObject list.\r
174            *\r
175            * @param jsonobj   Pointer to json object\r
176            *****************************************************************************/\r
177           public void evel_other_field_add_jsonobj(javax.json.JsonObject  jsonobj)\r
178           {\r
179 \r
180             EVEL_ENTER();\r
181 \r
182             /***************************************************************************/\r
183             /* Check preconditions.                                                    */\r
184             /***************************************************************************/\r
185             assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_OTHER);\r
186             assert(jsonobj != null);\r
187 \r
188             LOGGER.debug("Adding jsonObject");\r
189             \r
190             if( additional_objects == null )\r
191                 additional_objects = new ArrayList<javax.json.JsonObject>();\r
192             \r
193             additional_objects.add(jsonobj);\r
194 \r
195             EVEL_EXIT();\r
196           }\r
197 \r
198           /**************************************************************************//**\r
199            * Add a field name/value pair to the Other.\r
200            *\r
201            * The name and value are null delimited ASCII strings.  The library takes\r
202            * a copy so the caller does not have to preserve values after the function\r
203            * returns.\r
204            * @param name      ASCIIZ string with the field's name.  The caller does not\r
205            *                  need to preserve the value once the function returns.\r
206            * @param value     ASCIIZ string with the field's value.  The caller does not\r
207            *                  need to preserve the value once the function returns.\r
208            *****************************************************************************/\r
209           public void evel_other_field_add(String name, String value)\r
210           {\r
211             EVEL_ENTER();\r
212 \r
213             /***************************************************************************/\r
214             /* Check preconditions.                                                    */\r
215             /***************************************************************************/\r
216             assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_OTHER);\r
217             assert(name != null);\r
218             assert(value != null);\r
219 \r
220             LOGGER.debug("Adding name="+name+" value="+value);\r
221             \r
222             if(additional_info == null)\r
223                 additional_info = new HashMap<String,String>();\r
224             \r
225             if( additional_info.put(name, value) == null)\r
226                 LOGGER.debug("Unable to add map : name="+name+"value= "+value);\r
227 \r
228             EVEL_EXIT();\r
229           }\r
230 \r
231 \r
232           /**************************************************************************//**\r
233            * Encode Other Object according to VES schema\r
234            *\r
235            * @retval JSON Object of Other event\r
236            *****************************************************************************/\r
237           JsonObjectBuilder evelOtherObject()\r
238           {\r
239 \r
240             double version = major_version+(double)minor_version/10;\r
241 \r
242             EVEL_ENTER();\r
243 \r
244             /***************************************************************************/\r
245             /* Check preconditions.                                                    */\r
246             /***************************************************************************/\r
247             assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_OTHER);\r
248 \r
249             /***************************************************************************/\r
250             /* Mandatory fields.                                                       */\r
251             /***************************************************************************/\r
252             \r
253             /***************************************************************************/\r
254             /* Additional Objects.                                                 */\r
255             /***************************************************************************/\r
256             \r
257             JsonObjectBuilder eveloth = Json.createObjectBuilder();\r
258             \r
259             /***************************************************************************/\r
260             /* Optional fields.                                                        */\r
261             /***************************************************************************/\r
262             // additional fields\r
263                   if( additional_info != null )\r
264                   {\r
265                     JsonArrayBuilder builder = Json.createArrayBuilder();\r
266            for(Map.Entry<String, String> entry : additional_info.entrySet()){\r
267               LOGGER.debug(MessageFormat.format("Key : {0} and Value: {1}", entry.getKey(), entry.getValue()));\r
268                           JsonObject obj = Json.createObjectBuilder()\r
269                                      .add("name", entry.getKey())\r
270                                      .add("value", entry.getValue()).build();\r
271                           builder.add(obj);\r
272                     }\r
273                         eveloth.add("nameValuePairs", builder);\r
274                   }\r
275                         \r
276         if( additional_objects != null && additional_objects.size() > 0 )\r
277             {\r
278                     JsonArrayBuilder builder = Json.createArrayBuilder();\r
279                     for(int i=0;i<additional_objects.size();i++) {\r
280                           JsonObject jobj = additional_objects.get(i);\r
281                           builder.add(jobj);\r
282                     }\r
283                         eveloth.add("jsonObjects",builder);\r
284             } \r
285         \r
286                    if( evelmap != null && evelmap.size() > 0)\r
287                    {\r
288                             JsonArrayBuilder builder = Json.createArrayBuilder();\r
289                         for(Map.Entry<String, Map<String,String>> entry : evelmap.entrySet()){\r
290                               LOGGER.debug(MessageFormat.format("Key : {0} and Value: {1}", entry.getKey(), entry.getValue()));\r
291                               Map<String,String> item = entry.getValue();   \r
292                               JsonArrayBuilder builder2 = Json.createArrayBuilder();              \r
293                               for(Map.Entry<String, String> entry2 : item.entrySet()){\r
294                                   LOGGER.debug(MessageFormat.format("Key : {0} and Value: {1}", entry2.getKey(), entry2.getValue()));\r
295                                           JsonObject obj = Json.createObjectBuilder()\r
296                                                      .add("name", entry2.getKey())\r
297                                                      .add("value", entry2.getValue()).build();\r
298                                           builder2.add(obj);\r
299                                    }\r
300                               \r
301                               \r
302                                           JsonObjectBuilder obj = Json.createObjectBuilder()\r
303                                                      .add(entry.getKey(),builder2);\r
304                                           \r
305                                           builder.add(obj);\r
306                                 }\r
307                                 eveloth.add("hashOfNameValuePairArrays", builder);\r
308                    }\r
309 \r
310                   \r
311             \r
312 \r
313             /***************************************************************************/\r
314             /* Although optional, we always generate the version.  Note that this      */\r
315             /* closes the object, too.                                                 */\r
316             /***************************************************************************/\r
317             eveloth.add("otherFieldsVersion", version);\r
318 \r
319             EVEL_EXIT();\r
320             \r
321             return eveloth;\r
322           }\r
323           \r
324           /**************************************************************************//**\r
325            * Encode the event as a JSON event object according to AT&T's schema.\r
326            * retval : String of JSON event message\r
327            *****************************************************************************/\r
328           String evel_json_encode_event()\r
329           {\r
330                 EVEL_ENTER();\r
331                 \r
332                 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_OTHER);\r
333                 //encode common event header and body for other     \r
334             JsonObject obj = Json.createObjectBuilder()\r
335                      .add("event", Json.createObjectBuilder()\r
336                                  .add( "commonEventHeader",eventHeaderObject() )\r
337                                  .add( "otherFields",evelOtherObject() )\r
338                                  ).build();\r
339 \r
340             EVEL_EXIT();\r
341             \r
342             return obj.toString();\r
343 \r
344           }\r
345 \r
346 \r
347 }\r