1 package evel_javalibrary.att.com;
\r
2 /**************************************************************************//**
\r
4 * Evel State Change class
\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
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
34 import org.slf4j.helpers.MessageFormatter;
\r
36 import evel_javalibrary.att.com.EvelFault.EVEL_SEVERITIES;
\r
39 public class EvelStateChange extends EvelHeader {
\r
41 int major_version = 1;
\r
42 int minor_version = 2;
\r
44 /**************************************************************************//**
\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
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
62 /***************************************************************************/
\r
63 /* Optional fields */
\r
64 /***************************************************************************/
\r
65 ArrayList<String[]> additional_info;
\r
67 private static final Logger LOGGER = Logger.getLogger( EvelStateChange.class.getName() );
\r
69 /**************************************************************************//**
\r
70 * Create a new State Change event.
\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
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
81 *****************************************************************************/
\r
82 public EvelStateChange(String evname, String evid,
\r
83 EVEL_ENTITY_STATE newstate,
\r
84 EVEL_ENTITY_STATE oldstate,
\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
93 new_state = newstate;
\r
94 old_state = oldstate;
\r
95 state_interface = interfce;
\r
97 additional_info = null;
\r
100 /**************************************************************************//**
\r
101 * Add an additional value name/value pair to the Fault.
\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
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
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
113 *****************************************************************************/
\r
114 public void evel_statechange_addl_info_add(String name, String value)
\r
116 String[] addl_info = null;
\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
126 if( additional_info == null )
\r
128 additional_info = new ArrayList<String[]>();
\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
137 additional_info.add(addl_info);
\r
142 /**************************************************************************//**
\r
143 * Convert a ::EVEL_ENTITY_STATE to it's string form for JSON encoding.
\r
145 * @param state The entity state to encode.
\r
147 * @returns the corresponding string
\r
148 *****************************************************************************/
\r
149 String evel_entity_state(EVEL_ENTITY_STATE state)
\r
151 String result=null;
\r
157 case EVEL_ENTITY_STATE_IN_SERVICE:
\r
158 result = "inService";
\r
161 case EVEL_ENTITY_STATE_MAINTENANCE:
\r
162 result = "maintenance";
\r
165 case EVEL_ENTITY_STATE_OUT_OF_SERVICE:
\r
166 result = "outOfService";
\r
170 LOGGER.error("Unexpected entity state "+state);
\r
180 /**************************************************************************//**
\r
181 * Encode the State Change in JSON according to AT&T's schema.
\r
183 *****************************************************************************/
\r
184 JsonObjectBuilder evelStateChangeObject()
\r
188 double version = major_version+(double)minor_version/10;
\r
193 /***************************************************************************/
\r
194 /* Check preconditions. */
\r
195 /***************************************************************************/
\r
196 assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_STATE_CHANGE);
\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
206 JsonObjectBuilder evelstate = Json.createObjectBuilder()
\r
207 .add("newState", nstate)
\r
208 .add("oldState", ostate)
\r
209 .add("stateInterface", state_interface);
\r
211 evelstate.add( "stateChangeFieldsVersion", version);
\r
213 /***************************************************************************/
\r
214 /* Optional additional information */
\r
215 /***************************************************************************/
\r
216 if( additional_info != null )
\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
226 evelstate.add("additionalFields", builder);
\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
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
253 return obj.toString();
\r