817b28300596433400bc9373d28a0205c4252660
[vnfsdk/compliance.git] / veslibrary / ves_javalibrary / evel_javalib2 / src / evel_javalibrary / att / com / EvelStateChange.java
1 package evel_javalibrary.att.com;\r
2 /**************************************************************************//**\r
3  * @file\r
4  * Evel State Change class\r
5  *\r
6   * This file implements the Evel State Change Event class which is intended to provide a\r
7  * simple wrapper around the complexity of AT&T's Vendor Event Listener API so\r
8  * that VNFs can use it to send Agent State change events.\r
9  *\r
10  * License\r
11  * -------\r
12  *\r
13  * Licensed under the Apache License, Version 2.0 (the "License");\r
14  * you may not use this file except in compliance with the License.\r
15  * You may obtain a copy of the License at\r
16  *        http://www.apache.org/licenses/LICENSE-2.0\r
17  *\r
18  * Unless required by applicable law or agreed to in writing, software\r
19  * distributed under the License is distributed on an "AS IS" BASIS,\r
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
21  * See the License for the specific language governing permissions and\r
22  * limitations under the License.\r
23  *****************************************************************************/\r
24 \r
25 import java.text.MessageFormat;\r
26 import java.util.ArrayList;\r
27 \r
28 import javax.json.Json;\r
29 import javax.json.JsonArrayBuilder;\r
30 import javax.json.JsonObject;\r
31 import javax.json.JsonObjectBuilder;\r
32 \r
33 import org.apache.log4j.Logger;\r
34 import org.slf4j.helpers.MessageFormatter;\r
35 \r
36 import evel_javalibrary.att.com.EvelFault.EVEL_SEVERITIES;\r
37 \r
38 \r
39 public class EvelStateChange extends EvelHeader {\r
40         \r
41         int major_version = 1;\r
42         int minor_version = 2;\r
43         \r
44         /**************************************************************************//**\r
45          * Alert types.\r
46          * JSON equivalent fields: newState, oldState\r
47          *****************************************************************************/\r
48         public enum EVEL_ENTITY_STATE{\r
49           EVEL_ENTITY_STATE_IN_SERVICE,\r
50           EVEL_ENTITY_STATE_MAINTENANCE,\r
51           EVEL_ENTITY_STATE_OUT_OF_SERVICE,\r
52           EVEL_MAX_ENTITY_STATES\r
53         }\r
54         \r
55         /***************************************************************************/\r
56         /* Mandatory fields                                                        */\r
57         /***************************************************************************/\r
58           EVEL_ENTITY_STATE new_state;\r
59           EVEL_ENTITY_STATE old_state;\r
60           String state_interface;\r
61 \r
62         /***************************************************************************/\r
63         /* Optional fields                                                         */\r
64         /***************************************************************************/\r
65           ArrayList<String[]> additional_info;\r
66         \r
67           private static final Logger LOGGER = Logger.getLogger( EvelStateChange.class.getName() );\r
68 \r
69           /**************************************************************************//**\r
70            * Create a new State Change event.\r
71            *\r
72            * @note    The mandatory fields on the State Change must be supplied to this\r
73            *          factory function and are immutable once set.  Optional fields have\r
74            *          explicit setter functions, but again values may only be set once\r
75            *          so that the State Change has immutable properties.\r
76            *\r
77            * @param new_state     The new state of the reporting entity.\r
78            * @param old_state     The old state of the reporting entity.\r
79            * @param interface     The card or port name of the reporting entity.\r
80            *\r
81            *****************************************************************************/\r
82         public EvelStateChange(String evname, String evid,\r
83                                        EVEL_ENTITY_STATE newstate,\r
84                            EVEL_ENTITY_STATE oldstate,\r
85                            String interfce)\r
86         {\r
87                 super(evname,evid);\r
88                 event_domain = EvelHeader.DOMAINS.EVEL_DOMAIN_STATE_CHANGE;\r
89                 assert(EVEL_ENTITY_STATE.EVEL_MAX_ENTITY_STATES.compareTo(newstate) < 0 );\r
90                 assert(EVEL_ENTITY_STATE.EVEL_MAX_ENTITY_STATES.compareTo(oldstate) < 0 );\r
91                 assert( interfce != null);\r
92                 \r
93                 new_state = newstate;\r
94                 old_state = oldstate;\r
95                 state_interface = interfce;\r
96 \r
97                 additional_info = null;         \r
98         }\r
99         \r
100         /**************************************************************************//**\r
101          * Add an additional value name/value pair to the Fault.\r
102          *\r
103          * The name and value are null delimited ASCII strings.  The library takes\r
104          * a copy so the caller does not have to preserve values after the function\r
105          * returns.\r
106          *\r
107          * @param name      ASCIIZ string with the attribute's name.  The caller\r
108          *                  does not need to preserve the value once the function\r
109          *                  returns.\r
110          * @param value     ASCIIZ string with the attribute's value.  The caller\r
111          *                  does not need to preserve the value once the function\r
112          *                  returns.\r
113          *****************************************************************************/\r
114         public void evel_statechange_addl_info_add(String name, String value)\r
115         {\r
116           String[] addl_info = null;\r
117           EVEL_ENTER();\r
118 \r
119           /***************************************************************************/\r
120           /* Check preconditions.                                                    */\r
121           /***************************************************************************/\r
122           assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_STATE_CHANGE);\r
123           assert(name != null);\r
124           assert(value != null);\r
125           \r
126           if( additional_info == null )\r
127           {\r
128                   additional_info = new ArrayList<String[]>();\r
129           }\r
130 \r
131           LOGGER.debug(MessageFormat.format("Adding name={0} value={1}", name, value));\r
132           addl_info = new String[2];\r
133           assert(addl_info != null);\r
134           addl_info[0] = name;\r
135           addl_info[1] = value;\r
136 \r
137           additional_info.add(addl_info);\r
138 \r
139           EVEL_EXIT();\r
140         }\r
141         \r
142         /**************************************************************************//**\r
143          * Convert a ::EVEL_ENTITY_STATE to it's string form for JSON encoding.\r
144          *\r
145          * @param state         The entity state to encode.\r
146          *\r
147          * @returns the corresponding string\r
148          *****************************************************************************/\r
149         String evel_entity_state(EVEL_ENTITY_STATE state)\r
150         {\r
151           String result=null;\r
152 \r
153           EVEL_ENTER();\r
154 \r
155           switch (state)\r
156           {\r
157             case EVEL_ENTITY_STATE_IN_SERVICE:\r
158               result = "inService";\r
159               break;\r
160 \r
161             case EVEL_ENTITY_STATE_MAINTENANCE:\r
162               result = "maintenance";\r
163               break;\r
164 \r
165             case EVEL_ENTITY_STATE_OUT_OF_SERVICE:\r
166               result = "outOfService";\r
167               break;\r
168 \r
169             default:\r
170               LOGGER.error("Unexpected entity state "+state);\r
171               System.exit(1);\r
172           }\r
173 \r
174           EVEL_EXIT();\r
175 \r
176           return result;\r
177         }\r
178 \r
179 \r
180         /**************************************************************************//**\r
181          * Encode the State Change in JSON according to AT&T's schema.\r
182          *\r
183          *****************************************************************************/\r
184          JsonObjectBuilder evelStateChangeObject()\r
185          {\r
186           String nstate;\r
187           String ostate;\r
188           double version = major_version+(double)minor_version/10;\r
189 \r
190           EVEL_ENTER();\r
191           \r
192 \r
193           /***************************************************************************/\r
194           /* Check preconditions.                                                    */\r
195           /***************************************************************************/\r
196           assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_STATE_CHANGE);\r
197 \r
198 \r
199 \r
200           /***************************************************************************/\r
201           /* Mandatory fields.                                                       */\r
202           /***************************************************************************/\r
203       nstate = evel_entity_state(new_state);\r
204       ostate = evel_entity_state(old_state);\r
205           \r
206           JsonObjectBuilder evelstate = Json.createObjectBuilder()\r
207                          .add("newState", nstate)\r
208                          .add("oldState", ostate)\r
209                          .add("stateInterface", state_interface);\r
210           \r
211           evelstate.add( "stateChangeFieldsVersion", version);\r
212 \r
213           /***************************************************************************/\r
214           /* Optional additional information      */\r
215           /***************************************************************************/\r
216           if( additional_info != null )\r
217           {\r
218             JsonArrayBuilder builder = Json.createArrayBuilder();\r
219             for(int i=0;i<additional_info.size();i++) {\r
220                   String[] addl_info = additional_info.get(i);\r
221                   JsonObject obj = Json.createObjectBuilder()\r
222                              .add("name", addl_info[0])\r
223                              .add("value", addl_info[1]).build();\r
224                   builder.add(obj);\r
225             }\r
226                 evelstate.add("additionalFields", builder);\r
227           }\r
228 \r
229           EVEL_EXIT();\r
230           \r
231           return evelstate;\r
232         }\r
233         \r
234         \r
235           /**************************************************************************//**\r
236            * Encode the event as a JSON event object according to AT&T's schema.\r
237            * retval : String of JSON state change event message\r
238            *****************************************************************************/\r
239           String evel_json_encode_event()\r
240           {\r
241                 EVEL_ENTER();\r
242                 \r
243                 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_STATE_CHANGE);\r
244             //encode header and state change fields    \r
245             JsonObject obj = Json.createObjectBuilder()\r
246                      .add("event", Json.createObjectBuilder()\r
247                                  .add( "commonEventHeader",eventHeaderObject() )\r
248                                  .add( "stateChangeFields",evelStateChangeObject() )\r
249                                  ).build();\r
250 \r
251             EVEL_EXIT();\r
252             \r
253             return obj.toString();\r
254 \r
255           }\r
256         \r
257 \r
258 }\r