Update License text
[vnfsdk/compliance.git] / veslibrary / ves_javalibrary / evel_javalib2 / src / evel_javalibrary / att / com / EvelFault.java
1 package evel_javalibrary.att.com;\r
2 \r
3 /**************************************************************************//**\r
4  * @file\r
5  * Evel Fault Event class extends EvelHeader class\r
6  *\r
7  * This file implements the Evel Fault Event class which is intended to provide a\r
8  * simple wrapper around the complexity of AT&T's Vendor Event Listener API so\r
9  * that VNFs can use it to send Fault events.\r
10  *\r
11  * License\r
12  * -------\r
13  * Unless otherwise specified, all software contained herein is\r
14  * Licensed under the Apache License, Version 2.0 (the "License");\r
15  * you may not use this file except in compliance with the License.\r
16  * You may obtain a copy of the License at\r
17  *        http://www.apache.org/licenses/LICENSE-2.0\r
18  *\r
19  * Unless required by applicable law or agreed to in writing, software\r
20  * distributed under the License is distributed on an "AS IS" BASIS,\r
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
22  * See the License for the specific language governing permissions and\r
23  * limitations under the License.\r
24  *****************************************************************************/\r
25 \r
26 import java.text.MessageFormat;\r
27 import java.util.ArrayList;\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 public class EvelFault extends EvelHeader {\r
39         //version of EvelFault format revisions\r
40         int major_version = 2;\r
41         int minor_version = 1;\r
42         \r
43         /**************************************************************************//**\r
44          * Fault / Threshold severities.\r
45          * JSON equivalent field: eventSeverity\r
46          *****************************************************************************/\r
47         public enum EVEL_SEVERITIES{\r
48           EVEL_SEVERITY_CRITICAL,\r
49           EVEL_SEVERITY_MAJOR,\r
50           EVEL_SEVERITY_MINOR,\r
51           EVEL_SEVERITY_WARNING,\r
52           EVEL_SEVERITY_NORMAL,\r
53           EVEL_MAX_SEVERITIES\r
54         }\r
55 \r
56         /**************************************************************************//**\r
57          * Fault source types.\r
58          * JSON equivalent field: eventSourceType\r
59          *****************************************************************************/\r
60         public enum EVEL_SOURCE_TYPES{\r
61           EVEL_SOURCE_OTHER,\r
62           EVEL_SOURCE_ROUTER,\r
63           EVEL_SOURCE_SWITCH,\r
64           EVEL_SOURCE_HOST,\r
65           EVEL_SOURCE_CARD,\r
66           EVEL_SOURCE_PORT,\r
67           EVEL_SOURCE_SLOT_THRESHOLD,\r
68           EVEL_SOURCE_PORT_THRESHOLD,\r
69           EVEL_SOURCE_VIRTUAL_MACHINE,\r
70           EVEL_SOURCE_VIRTUAL_NETWORK_FUNCTION,\r
71           /***************************************************************************/\r
72           /* START OF VENDOR-SPECIFIC VALUES                                         */\r
73           /*                                                                         */\r
74           /* Vendor-specific values should be added here, and handled appropriately  */\r
75           /* in evel_event.c.                                                        */\r
76           /***************************************************************************/\r
77 \r
78           /***************************************************************************/\r
79           /* END OF VENDOR-SPECIFIC VALUES                                           */\r
80           /***************************************************************************/\r
81           EVEL_MAX_SOURCE_TYPES\r
82         }\r
83 \r
84         /**************************************************************************//**\r
85          * Fault VNF Status.\r
86          * JSON equivalent field: vfStatus\r
87          *****************************************************************************/\r
88         public enum EVEL_VF_STATUSES{\r
89           EVEL_VF_STATUS_ACTIVE,\r
90           EVEL_VF_STATUS_IDLE,\r
91           EVEL_VF_STATUS_PREP_TERMINATE,\r
92           EVEL_VF_STATUS_READY_TERMINATE,\r
93           EVEL_VF_STATUS_REQ_TERMINATE,\r
94           EVEL_MAX_VF_STATUSES\r
95         }\r
96 \r
97         \r
98         /***************************************************************************/\r
99         /* Mandatory fields                                                        */\r
100         /***************************************************************************/\r
101           EVEL_SEVERITIES event_severity;\r
102           EVEL_SOURCE_TYPES event_source_type;\r
103           String alarm_condition;\r
104           String specific_problem;\r
105           EVEL_VF_STATUSES vf_status;\r
106 \r
107         /***************************************************************************/\r
108         /* Optional fields                                                         */\r
109         /***************************************************************************/\r
110           EvelOptionString category;\r
111           EvelOptionString alarm_interface_a;\r
112           ArrayList<String[]> additional_info;\r
113         \r
114           private static final Logger LOGGER = Logger.getLogger( EvelFault.class.getName() );\r
115 \r
116           /**************************************************************************//**\r
117            * Create a new fault event.\r
118            *\r
119            * @note    The mandatory fields on the Fault must be supplied to this factory\r
120            *          function and are immutable once set.  Optional fields have explicit\r
121            *          setter functions, but again values may only be set once so that the\r
122            *          Fault has immutable properties.\r
123            * @param   condition   The condition indicated by the Fault.\r
124            * @param   specproblem  The specific problem triggering the fault.\r
125            * @param   priority    The priority of the event.\r
126            * @param   severity    The severity of the Fault.\r
127            * @param   ev_source_type    Source of Alarm event\r
128            * @param   status      status of Virtual Function\r
129            *****************************************************************************/\r
130         public EvelFault(String evname, String ev_id,\r
131                                  String condition, String specproblem,\r
132                      EvelHeader.PRIORITIES tpriority,\r
133                      EVEL_SEVERITIES severity,\r
134                      EVEL_SOURCE_TYPES ev_source_type,\r
135                      EVEL_VF_STATUSES status)\r
136         {\r
137                 //Initializes Evel Header and Domain\r
138                 super(evname,ev_id);            \r
139                 event_domain = EvelHeader.DOMAINS.EVEL_DOMAIN_FAULT;\r
140                 //Validate inputs\r
141                 assert( condition != null);\r
142                 assert( specific_problem != null);\r
143                 assert(EvelHeader.PRIORITIES.EVEL_MAX_PRIORITIES.compareTo(tpriority) < 0 );\r
144                 assert(EVEL_SEVERITIES.EVEL_MAX_SEVERITIES.compareTo(severity) < 0 );\r
145                 assert(EVEL_VF_STATUSES.EVEL_MAX_VF_STATUSES.compareTo(status) < 0 );\r
146                 //Init mandatory fields\r
147                 event_severity = severity;\r
148                 event_source_type = ev_source_type;\r
149                 alarm_condition = condition;\r
150                 specific_problem = specproblem;\r
151                 vf_status = status;\r
152                 priority = tpriority;\r
153                 //Init optional fields\r
154                 category = new EvelOptionString(false, null);\r
155                 alarm_interface_a = new EvelOptionString(false, null);\r
156                 additional_info = null;         \r
157         }\r
158         \r
159         /**************************************************************************//**\r
160          * Add an additional value name/value pair to the Fault.\r
161          *\r
162          * The name and value are null delimited ASCII strings.  The library takes\r
163          * a copy so the caller does not have to preserve values after the function\r
164          * returns.\r
165          *\r
166          * @param fault     Pointer to the fault.\r
167          * @param name      ASCIIZ string with the attribute's name.  The caller\r
168          *                  does not need to preserve the value once the function\r
169          *                  returns.\r
170          * @param value     ASCIIZ string with the attribute's value.  The caller\r
171          *                  does not need to preserve the value once the function\r
172          *                  returns.\r
173          *****************************************************************************/\r
174         public void evel_fault_addl_info_add(String name, String value)\r
175         {\r
176           String[] addl_info = null;\r
177           EVEL_ENTER();\r
178 \r
179           /***************************************************************************/\r
180           /* Check preconditions.                                                    */\r
181           /***************************************************************************/\r
182           assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_FAULT);\r
183           assert(name != null);\r
184           assert(value != null);\r
185           \r
186           if( additional_info == null )\r
187           {\r
188                   additional_info = new ArrayList<String[]>();\r
189           }\r
190 \r
191           LOGGER.debug(MessageFormat.format("Adding name={0} value={1}", name, value));\r
192           addl_info = new String[2];\r
193           assert(addl_info != null);\r
194           addl_info[0] = name;\r
195           addl_info[1] = value;\r
196 \r
197           additional_info.add(addl_info);\r
198 \r
199           EVEL_EXIT();\r
200         }\r
201 \r
202         /**************************************************************************//**\r
203          * Set the Fault Category property of the Fault.\r
204          *\r
205          * @note  The property is treated as immutable: it is only valid to call\r
206          *        the setter once.  However, we don't assert if the caller tries to\r
207          *        overwrite, just ignoring the update instead.\r
208          *\r
209          * @param fault      Pointer to the fault.\r
210          * @param category   Category : license, link, routing, security, signaling.\r
211          *                       ASCIIZ string. The caller\r
212          *                   does not need to preserve the value once the function\r
213          *                   returns.\r
214          *****************************************************************************/\r
215         public void evel_fault_category_set( String categ)\r
216         {\r
217           EVEL_ENTER();\r
218 \r
219           /***************************************************************************/\r
220           /* Check preconditions.                                                    */\r
221           /***************************************************************************/\r
222           assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_FAULT);\r
223           assert(categ != null);\r
224           \r
225           category.SetValuePr(categ,"Fault Category set");\r
226 \r
227           EVEL_EXIT();\r
228         }\r
229 \r
230         /**************************************************************************//**\r
231          * Set the Alarm Interface A property of the Fault.\r
232          *\r
233          * @note  The property is treated as immutable: it is only valid to call\r
234          *        the setter once.  However, we don't assert if the caller tries to\r
235          *        overwrite, just ignoring the update instead.\r
236          *\r
237          * @param fault      Pointer to the fault.\r
238          * @param interface  The Alarm Interface A to be set. ASCIIZ string. The caller\r
239          *                   does not need to preserve the value once the function\r
240          *                   returns.\r
241          *****************************************************************************/\r
242         public void evel_fault_interface_set(String intf)\r
243         {\r
244           EVEL_ENTER();\r
245 \r
246           /***************************************************************************/\r
247           /* Check preconditions.                                                    */\r
248           /***************************************************************************/\r
249           assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_FAULT);\r
250           assert(intf != null);\r
251 \r
252           alarm_interface_a.SetValuePr(intf,"Alarm Interface A");\r
253 \r
254           EVEL_EXIT();\r
255         }\r
256 \r
257         /**************************************************************************//**\r
258          * Set the Event Type property of the Fault.\r
259          *\r
260          * @note  The property is treated as immutable: it is only valid to call\r
261          *        the setter once.  However, we don't assert if the caller tries to\r
262          *        overwrite, just ignoring the update instead.\r
263          *\r
264          * @param fault      Pointer to the fault.\r
265          * @param type       The Event Type to be set. ASCIIZ string. The caller\r
266          *                   does not need to preserve the value once the function\r
267          *                   returns.\r
268          *****************************************************************************/\r
269         public void evel_fault_type_set(String type)\r
270         {\r
271           EVEL_ENTER();\r
272 \r
273           /***************************************************************************/\r
274           /* Check preconditions and call evel_header_type_set.                      */\r
275           /***************************************************************************/\r
276           assert(type != null);\r
277           assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_FAULT);\r
278           \r
279           evel_header_type_set(type);\r
280 \r
281           EVEL_EXIT();\r
282         }\r
283 \r
284         \r
285         /**************************************************************************//**\r
286          * Map an ::EVEL_SOURCE_TYPES enum value to the equivalent string.\r
287          *\r
288          * @param source_type   The source type to convert.\r
289          * @returns The equivalent string.\r
290          *****************************************************************************/\r
291         String evel_source_type(EVEL_SOURCE_TYPES source_type)\r
292         {\r
293           String result;\r
294 \r
295           EVEL_ENTER();\r
296 \r
297           switch (source_type)\r
298           {\r
299             case EVEL_SOURCE_OTHER:\r
300               result = "other";\r
301               break;\r
302 \r
303             case EVEL_SOURCE_ROUTER:\r
304               result = "router";\r
305               break;\r
306 \r
307             case EVEL_SOURCE_SWITCH:\r
308               result = "switch";\r
309               break;\r
310 \r
311             case EVEL_SOURCE_HOST:\r
312               result = "host";\r
313               break;\r
314 \r
315             case EVEL_SOURCE_CARD:\r
316               result = "card";\r
317               break;\r
318 \r
319             case EVEL_SOURCE_PORT:\r
320               result = "port";\r
321               break;\r
322 \r
323             case EVEL_SOURCE_SLOT_THRESHOLD:\r
324               result = "slotThreshold";\r
325               break;\r
326 \r
327             case EVEL_SOURCE_PORT_THRESHOLD:\r
328               result = "portThreshold";\r
329               break;\r
330 \r
331             case EVEL_SOURCE_VIRTUAL_MACHINE:\r
332               result = "virtualMachine";\r
333               break;\r
334 \r
335             case EVEL_SOURCE_VIRTUAL_NETWORK_FUNCTION:\r
336               result = "virtualNetworkFunction";\r
337               break;\r
338 \r
339             default:\r
340               result = null;\r
341               LOGGER.error(MessageFormatter.format("Unexpected Event Source Type {0}", source_type));\r
342               System.exit(1);\r
343           }\r
344 \r
345           EVEL_EXIT();\r
346 \r
347           return result;\r
348         }\r
349         \r
350         /**************************************************************************//**\r
351          * Map an ::EVEL_SEVERITIES enum value to the equivalent string.\r
352          *\r
353          * @param severity      The severity to convert.\r
354          * @returns The equivalent string.\r
355          *****************************************************************************/\r
356         String evel_severity(EVEL_SEVERITIES severity)\r
357         {\r
358           String result = null;\r
359 \r
360           EVEL_ENTER();\r
361 \r
362           switch (severity)\r
363           {\r
364             case EVEL_SEVERITY_CRITICAL:\r
365               result = "CRITICAL";\r
366               break;\r
367 \r
368             case EVEL_SEVERITY_MAJOR:\r
369               result = "MAJOR";\r
370               break;\r
371 \r
372             case EVEL_SEVERITY_MINOR:\r
373               result = "MINOR";\r
374               break;\r
375 \r
376             case EVEL_SEVERITY_WARNING:\r
377               result = "WARNING";\r
378               break;\r
379 \r
380             case EVEL_SEVERITY_NORMAL:\r
381               result = "NORMAL";\r
382               break;\r
383 \r
384             default:\r
385               LOGGER.error("Unexpected event severity "+severity);\r
386               System.exit(1);\r
387           }\r
388 \r
389           EVEL_EXIT();\r
390 \r
391           return result;\r
392         }\r
393 \r
394 \r
395         /**************************************************************************//**\r
396          * Map an ::EVEL_VF_STATUSES enum value to the equivalent string.\r
397          *\r
398          * @param vf_status     The vf_status to convert.\r
399          * @returns The equivalent string.\r
400          *****************************************************************************/\r
401         String evel_vf_status(EVEL_VF_STATUSES vf_status)\r
402         {\r
403           String result;\r
404 \r
405           EVEL_ENTER();\r
406 \r
407           switch (vf_status)\r
408           {\r
409             case EVEL_VF_STATUS_ACTIVE:\r
410               result = "Active";\r
411               break;\r
412 \r
413             case EVEL_VF_STATUS_IDLE:\r
414               result = "Idle";\r
415               break;\r
416 \r
417             case EVEL_VF_STATUS_PREP_TERMINATE:\r
418               result = "Preparing to terminate";\r
419               break;\r
420 \r
421             case EVEL_VF_STATUS_READY_TERMINATE:\r
422               result = "Ready to terminate";\r
423               break;\r
424 \r
425             case EVEL_VF_STATUS_REQ_TERMINATE:\r
426               result = "Requesting termination";\r
427               break;\r
428 \r
429             default:\r
430               result = null;\r
431               LOGGER.error("Unexpected VF Status "+vf_status);\r
432               System.exit(1);\r
433           }\r
434 \r
435           EVEL_EXIT();\r
436 \r
437           return result;\r
438         }\r
439 \r
440         /**************************************************************************//**\r
441          * Encode the fault in JSON according to AT&T's schema for the fault type.\r
442          *\r
443          * @retval JsonObjectBuilder of fault body portion of message   \r
444          *****************************************************************************/\r
445          JsonObjectBuilder evelFaultObject()\r
446          {\r
447           String fault_severity;\r
448           String fault_source_type;\r
449           String fault_vf_status;\r
450           double version = major_version+(double)minor_version/10;\r
451 \r
452           EVEL_ENTER();\r
453           \r
454           assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_FAULT);\r
455 \r
456           /***************************************************************************/\r
457           /* Check preconditions.                                                    */\r
458           /***************************************************************************/\r
459 \r
460       fault_severity = evel_severity(event_severity);\r
461           fault_source_type = evel_source_type(event_source_type);\r
462           fault_vf_status = evel_vf_status(vf_status);\r
463           \r
464           JsonObjectBuilder evelfault = Json.createObjectBuilder()\r
465                          .add("alarmCondition", alarm_condition);\r
466 \r
467           /***************************************************************************/\r
468           /* Optional fields.                                                        */\r
469           /***************************************************************************/\r
470           \r
471           if( category.is_set )\r
472                   evelfault.add("eventCategory", category.GetValue());\r
473           if( alarm_interface_a.is_set )\r
474                   evelfault.add("eventCategory", alarm_interface_a.GetValue());\r
475           \r
476 \r
477           /***************************************************************************/\r
478           /* Mandatory fields.                                                       */\r
479           /***************************************************************************/\r
480           evelfault.add( "eventSeverity", fault_severity);\r
481           evelfault.add( "eventSourceType", fault_source_type);\r
482           evelfault.add( "specificProblem", specific_problem);\r
483           evelfault.add( "vfStatus", fault_vf_status);\r
484           evelfault.add( "faultFieldsVersion", version);\r
485 \r
486           /***************************************************************************/\r
487           /* Encode additional Name value pairs if any.      */\r
488           /***************************************************************************/\r
489           if( additional_info != null )\r
490           {\r
491             JsonArrayBuilder builder = Json.createArrayBuilder();\r
492             for(int i=0;i<additional_info.size();i++) {\r
493                   String[] addl_info = additional_info.get(i);\r
494                   JsonObject obj = Json.createObjectBuilder()\r
495                              .add("name", addl_info[0])\r
496                              .add("value", addl_info[1]).build();\r
497                   builder.add(obj);\r
498             }\r
499                 evelfault.add("alarmAdditionalInformation", builder);\r
500           }\r
501 \r
502           EVEL_EXIT();\r
503           \r
504           return evelfault;\r
505         }\r
506         \r
507         \r
508           /**************************************************************************//**\r
509            * Encode the event as a JSON event object according to AT&T's schema.\r
510            * retval : String of JSON event message\r
511            *****************************************************************************/\r
512           String evel_json_encode_event()\r
513           {\r
514                 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_FAULT);\r
515                 //encode common event header and body     \r
516             JsonObject obj = Json.createObjectBuilder()\r
517                      .add("event", Json.createObjectBuilder()\r
518                                  .add( "commonEventHeader",eventHeaderObject() )\r
519                                  .add( "faultFields",evelFaultObject() )\r
520                                  ).build();\r
521 \r
522             EVEL_EXIT();\r
523             \r
524             return obj.toString();\r
525 \r
526           }\r
527         \r
528 \r
529 }\r