1 package evel_javalibrary.att.com;
\r
2 /**************************************************************************//**
\r
4 * Evel Heartbeat field class
\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
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
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
25 import java.text.MessageFormat;
\r
26 import java.util.ArrayList;
\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
33 import org.apache.log4j.Logger;
\r
36 public class EvelHeartbeatField extends EvelHeader {
\r
38 //version of Heartbeat field format revisions
\r
39 int major_version = 1;
\r
40 int minor_version = 2;
\r
42 /**************************************************************************//**
\r
44 * JSON equivalent fields: newState, oldState
\r
45 *****************************************************************************/
\r
47 /***************************************************************************/
\r
48 /* Mandatory fields */
\r
49 /***************************************************************************/
\r
50 int heartbeat_interval;
\r
53 /***************************************************************************/
\r
54 /* Optional fields */
\r
55 /***************************************************************************/
\r
56 ArrayList<String[]> additional_info;
\r
58 private static final Logger LOGGER = Logger.getLogger( EvelHeartbeatField.class.getName() );
\r
60 /**************************************************************************//**
\r
61 * Construct Heartbeat field event.
\r
63 * @param interval The Heartbeat interval at which messages are sent.
\r
65 *****************************************************************************/
\r
66 public EvelHeartbeatField(int interval,String evname,String evid)
\r
69 event_domain = EvelHeader.DOMAINS.EVEL_DOMAIN_HEARTBEAT_FIELD;
\r
70 assert( interval > 0 );
\r
72 heartbeat_interval = interval;
\r
74 additional_info = null;
\r
77 /**************************************************************************//**
\r
78 * Add an additional value name/value pair to the Fault.
\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
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
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
90 *****************************************************************************/
\r
91 public void evel_hrtbt_field_addl_info_add(String name, String value)
\r
93 String[] addl_info = null;
\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
103 if( additional_info == null )
\r
105 additional_info = new ArrayList<String[]>();
\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
114 additional_info.add(addl_info);
\r
119 /**************************************************************************//**
\r
120 * Set the Interval property of the Heartbeat fields event.
\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
126 * @param interval Heartbeat interval.
\r
127 *****************************************************************************/
\r
128 public void evel_hrtbt_interval_set( int interval)
\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
138 heartbeat_interval = interval;
\r
143 /**************************************************************************//**
\r
144 * Encode the Heartbeat field in JSON according to AT&T's schema.
\r
146 * @retval JsonObjectBuilder of Heartbeat field body portion of message
\r
147 *****************************************************************************/
\r
148 JsonObjectBuilder evelHeartbeatFieldObject()
\r
150 double version = major_version+(double)minor_version/10;
\r
155 /***************************************************************************/
\r
156 /* Check preconditions. */
\r
157 /***************************************************************************/
\r
158 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_HEARTBEAT_FIELD);
\r
160 /***************************************************************************/
\r
161 /* Mandatory fields. */
\r
162 /***************************************************************************/
\r
164 JsonObjectBuilder evelstate = Json.createObjectBuilder()
\r
165 .add("heartbeatInterval", heartbeat_interval);
\r
167 evelstate.add( "heartbeatFieldsVersion", version);
\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
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
182 evelstate.add("additionalFields", builder);
\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
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
209 return obj.toString();
\r