Update License text
[vnfsdk/compliance.git] / veslibrary / ves_javalibrary / evel_javalib2 / src / evel_javalibrary / att / com / EvelHeartbeatField.java
1 package evel_javalibrary.att.com;\r
2 /**************************************************************************//**\r
3  * @file\r
4  * Evel Heartbeat field class\r
5  *\r
6  * This file implements the Evel Heartbeat 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 status.\r
9  *\r
10  * License\r
11  * -------\r
12  * Unless otherwise specified, all software contained herein is\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 \r
35 \r
36 public class EvelHeartbeatField extends EvelHeader {\r
37         \r
38         //version of Heartbeat field format revisions\r
39         int major_version = 1;\r
40         int minor_version = 2;\r
41         \r
42         /**************************************************************************//**\r
43          * Alert types.\r
44          * JSON equivalent fields: newState, oldState\r
45          *****************************************************************************/\r
46         \r
47         /***************************************************************************/\r
48         /* Mandatory fields                                                        */\r
49         /***************************************************************************/\r
50         int    heartbeat_interval;\r
51 \r
52 \r
53         /***************************************************************************/\r
54         /* Optional fields                                                         */\r
55         /***************************************************************************/\r
56           ArrayList<String[]> additional_info;\r
57         \r
58           private static final Logger LOGGER = Logger.getLogger( EvelHeartbeatField.class.getName() );\r
59 \r
60           /**************************************************************************//**\r
61            * Construct Heartbeat field event.\r
62            *\r
63            * @param interval     The Heartbeat interval at which messages are sent.\r
64            * \r
65            *****************************************************************************/\r
66         public EvelHeartbeatField(int interval,String evname,String evid)\r
67         {\r
68                 super(evname,evid);\r
69                 event_domain = EvelHeader.DOMAINS.EVEL_DOMAIN_HEARTBEAT_FIELD;\r
70                 assert( interval > 0 );\r
71                 \r
72                 heartbeat_interval = interval;\r
73 \r
74                 additional_info = null;         \r
75         }\r
76         \r
77         /**************************************************************************//**\r
78          * Add an additional value name/value pair to the Fault.\r
79          *\r
80          * The name and value are null delimited ASCII strings.  The library takes\r
81          * a copy so the caller does not have to preserve values after the function\r
82          * returns.\r
83          *\r
84          * @param name      ASCIIZ string with the attribute's name.  The caller\r
85          *                  does not need to preserve the value once the function\r
86          *                  returns.\r
87          * @param value     ASCIIZ string with the attribute's value.  The caller\r
88          *                  does not need to preserve the value once the function\r
89          *                  returns.\r
90          *****************************************************************************/\r
91         public void evel_hrtbt_field_addl_info_add(String name, String value)\r
92         {\r
93           String[] addl_info = null;\r
94           EVEL_ENTER();\r
95 \r
96           /***************************************************************************/\r
97           /* Check preconditions.                                                    */\r
98           /***************************************************************************/\r
99           assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_STATE_CHANGE);\r
100           assert(name != null);\r
101           assert(value != null);\r
102           \r
103           if( additional_info == null )\r
104           {\r
105                   additional_info = new ArrayList<String[]>();\r
106           }\r
107 \r
108           LOGGER.debug(MessageFormat.format("Adding name={0} value={1}", name, value));\r
109           addl_info = new String[2];\r
110           assert(addl_info != null);\r
111           addl_info[0] = name;\r
112           addl_info[1] = value;\r
113 \r
114           additional_info.add(addl_info);\r
115 \r
116           EVEL_EXIT();\r
117         }\r
118         \r
119         /**************************************************************************//**\r
120          * Set the Interval property of the Heartbeat fields event.\r
121          *\r
122          * @note  The property is treated as immutable: it is only valid to call\r
123          *        the setter once.  However, we don't assert if the caller tries to\r
124          *        overwrite, just ignoring the update instead.\r
125          *\r
126          * @param interval      Heartbeat interval.\r
127          *****************************************************************************/\r
128         public void evel_hrtbt_interval_set( int interval)\r
129         {\r
130           EVEL_ENTER();\r
131 \r
132           /***************************************************************************/\r
133           /* Check preconditions and call evel_set_option_string.                    */\r
134           /***************************************************************************/\r
135           assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_HEARTBEAT_FIELD);\r
136           assert(interval > 0);\r
137 \r
138           heartbeat_interval = interval;\r
139           EVEL_EXIT();\r
140         }\r
141 \r
142 \r
143         /**************************************************************************//**\r
144          * Encode the Heartbeat field in JSON according to AT&T's schema.\r
145          *\r
146          * @retval JsonObjectBuilder of Heartbeat field body portion of message   \r
147          *****************************************************************************/\r
148          JsonObjectBuilder evelHeartbeatFieldObject()\r
149          {\r
150           double version = major_version+(double)minor_version/10;\r
151 \r
152           EVEL_ENTER();\r
153           \r
154 \r
155           /***************************************************************************/\r
156           /* Check preconditions.                                                    */\r
157           /***************************************************************************/\r
158           assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_HEARTBEAT_FIELD);\r
159 \r
160           /***************************************************************************/\r
161           /* Mandatory fields.                                                       */\r
162           /***************************************************************************/\r
163           \r
164           JsonObjectBuilder evelstate = Json.createObjectBuilder()\r
165                          .add("heartbeatInterval", heartbeat_interval);\r
166           \r
167           evelstate.add( "heartbeatFieldsVersion", version);\r
168 \r
169           /***************************************************************************/\r
170           /* Checkpoint, so that we can wind back if all fields are suppressed.      */\r
171           /***************************************************************************/\r
172           if( additional_info != null )\r
173           {\r
174             JsonArrayBuilder builder = Json.createArrayBuilder();\r
175             for(int i=0;i<additional_info.size();i++) {\r
176                   String[] addl_info = additional_info.get(i);\r
177                   JsonObject obj = Json.createObjectBuilder()\r
178                              .add("name", addl_info[0])\r
179                              .add("value", addl_info[1]).build();\r
180                   builder.add(obj);\r
181             }\r
182                 evelstate.add("additionalFields", builder);\r
183           }\r
184 \r
185           EVEL_EXIT();\r
186           \r
187           return evelstate;\r
188         }\r
189         \r
190         \r
191           /**************************************************************************//**\r
192            * Encode the event as a JSON event object according to AT&T's schema.\r
193            * retval : String of JSON event message\r
194            *****************************************************************************/\r
195           String evel_json_encode_event()\r
196           {\r
197                 EVEL_ENTER();\r
198                 \r
199                 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_HEARTBEAT_FIELD);\r
200             //encode common event header and body    \r
201             JsonObject obj = Json.createObjectBuilder()\r
202                      .add("event", Json.createObjectBuilder()\r
203                                  .add( "commonEventHeader",eventHeaderObject() )\r
204                                  .add( "heartbeatFields",evelHeartbeatFieldObject() )\r
205                                  ).build();\r
206 \r
207             EVEL_EXIT();\r
208             \r
209             return obj.toString();\r
210 \r
211           }\r
212         \r
213 \r
214 }\r