summaryrefslogtreecommitdiffstats
path: root/veslibrary/ves_javalibrary/evel_javalib2/src/main/java/evel_javalibrary/att/com/EvelStateChange.java
blob: 25ffef5e92ebd141d50ed33c76681666bd6797cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package evel_javalibrary.att.com;
/**************************************************************************//**
 * @file
 * Evel State Change class
 *
  * This file implements the Evel State Change Event class which is intended to provide a
 * simple wrapper around the complexity of AT&T's Vendor Event Listener API so
 * that VNFs can use it to send Agent State change events.
 *
 * License
 * -------
 * Unless otherwise specified, all software contained herein is
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;

import org.apache.log4j.Logger;
import org.slf4j.helpers.MessageFormatter;

import evel_javalibrary.att.com.EvelFault.EVEL_SEVERITIES;


public class EvelStateChange extends EvelHeader {
	
	int major_version = 2;
	int minor_version = 0;
	
	/**************************************************************************//**
	 * Alert types.
	 * JSON equivalent fields: newState, oldState
	 *****************************************************************************/
	public enum EVEL_ENTITY_STATE{
	  EVEL_ENTITY_STATE_IN_SERVICE,
	  EVEL_ENTITY_STATE_MAINTENANCE,
	  EVEL_ENTITY_STATE_OUT_OF_SERVICE,
	  EVEL_MAX_ENTITY_STATES
	}
	
	/***************************************************************************/
	/* Mandatory fields                                                        */
	/***************************************************************************/
	  EVEL_ENTITY_STATE new_state;
	  EVEL_ENTITY_STATE old_state;
	  String state_interface;
	  

	/***************************************************************************/
	/* Optional fields                                                         */
	/***************************************************************************/
	//  ArrayList<String[]> additional_info;
	  HashMap<String, String > additional_inf;
	
	  private static final Logger LOGGER = Logger.getLogger( EvelStateChange.class.getName() );

	  /**************************************************************************//**
	   * Create a new State Change event.
	   *
	   * @note    The mandatory fields on the State Change must be supplied to this
	   *          factory function and are immutable once set.  Optional fields have
	   *          explicit setter functions, but again values may only be set once
	   *          so that the State Change has immutable properties.
	   *
	   * @param new_state     The new state of the reporting entity.
	   * @param old_state     The old state of the reporting entity.
	   * @param interface     The card or port name of the reporting entity.
	   *
	   *****************************************************************************/
	public EvelStateChange(String evname, String evid,
			               EVEL_ENTITY_STATE newstate,
                           EVEL_ENTITY_STATE oldstate,
                           String interfce)
	{
		super(evname,evid);
		event_domain = EvelHeader.DOMAINS.EVEL_DOMAIN_STATE_CHANGE;
		assert(EVEL_ENTITY_STATE.EVEL_MAX_ENTITY_STATES.compareTo(newstate) >= 0 );
		assert(EVEL_ENTITY_STATE.EVEL_MAX_ENTITY_STATES.compareTo(oldstate) >= 0 );
		assert( interfce != null);
		
		new_state = newstate;
		old_state = oldstate;
		state_interface = interfce;

		additional_inf = null;		
	}
	
	/**************************************************************************//**
	 * Add an additional value name/value pair to the Fault.
	 *
	 * The name and value are null delimited ASCII strings.  The library takes
	 * a copy so the caller does not have to preserve values after the function
	 * returns.
	 *
	 * @param name      ASCIIZ string with the attribute's name.  The caller
	 *                  does not need to preserve the value once the function
	 *                  returns.
	 * @param value     ASCIIZ string with the attribute's value.  The caller
	 *                  does not need to preserve the value once the function
	 *                  returns.
	 *****************************************************************************/
	public void evel_statechange_addl_info_add(String name, String value)
	{
	  String[] addl_info = null;
	  EVEL_ENTER();

	  /***************************************************************************/
	  /* Check preconditions.                                                    */
	  /***************************************************************************/
	  assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_STATE_CHANGE);
	  assert(name != null);
	  assert(value != null);
	  
	  if( additional_inf == null )
	  {
		  additional_inf = new HashMap<>();
	  }

	  LOGGER.debug(MessageFormat.format("Adding name={0} value={1}", name, value));
	//  addl_info = new String[2];
	 // assert(addl_info != null);
	// addl_info[0] = name;
	//  addl_info[1] = value;
	  additional_inf.put(name,  value);
	//  additional_info.add(addl_info);

	  EVEL_EXIT();
	}
	
	/**************************************************************************//**
	 * Convert a ::EVEL_ENTITY_STATE to it's string form for JSON encoding.
	 *
	 * @param state         The entity state to encode.
	 *
	 * @returns the corresponding string
	 *****************************************************************************/
	String evel_entity_state(EVEL_ENTITY_STATE state)
	{
	  String result=null;

	  EVEL_ENTER();

	  switch (state)
	  {
	    case EVEL_ENTITY_STATE_IN_SERVICE:
	      result = "inService";
	      break;

	    case EVEL_ENTITY_STATE_MAINTENANCE:
	      result = "maintenance";
	      break;

	    case EVEL_ENTITY_STATE_OUT_OF_SERVICE:
	      result = "outOfService";
	      break;

	    default:
	      LOGGER.error("Unexpected entity state "+state);
	      System.exit(1);
	  }

	  EVEL_EXIT();

	  return result;
	}


	/**************************************************************************//**
	 * Encode the State Change in JSON according to AT&T's schema.
	 *
	 *****************************************************************************/
	 JsonObjectBuilder evelStateChangeObject()
	 {
	  String nstate;
	  String ostate;
	  //double version = major_version+(double)minor_version/10;
      String version = "4.0";
	  EVEL_ENTER();
	  

	  /***************************************************************************/
	  /* Check preconditions.                                                    */
	  /***************************************************************************/
	  assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_STATE_CHANGE);



	  /***************************************************************************/
	  /* Mandatory fields.                                                       */
	  /***************************************************************************/
      nstate = evel_entity_state(new_state);
      ostate = evel_entity_state(old_state);
	  
	  JsonObjectBuilder evelstate = Json.createObjectBuilder()
	   	         .add("newState", nstate)
	   	         .add("oldState", ostate)
	   	         .add("stateInterface", state_interface);
	  
	  evelstate.add( "stateChangeFieldsVersion", version);

	  /***************************************************************************/
	  /* Optional additional information      */
	  /***************************************************************************/
	/*  if( additional_info != null )
	  {
	    JsonArrayBuilder builder = Json.createArrayBuilder();
	    for(int i=0;i<additional_info.size();i++) {
		  String[] addl_info = additional_info.get(i);
		  JsonObject obj = Json.createObjectBuilder()
		    	     .add("name", addl_info[0])
		    	     .add("value", addl_info[1]).build();
		  builder.add(obj);
	    }
		evelstate.add("additionalFields", builder);
	  }   */
	  
	  
	  if(additional_inf != null) {
		  //JsonArrayBuilder builder = Json.createArrayBuilder();
		  JsonObjectBuilder builder = Json.createObjectBuilder();
		  Iterator<Entry<String, String>> it = additional_inf.entrySet().iterator();
		  while(it.hasNext()) {
			  Map.Entry<String, String> add_inf = (Map.Entry<String, String>)it.next();
			  String addl_info_key = add_inf.getKey();
			  String addl_info_value = add_inf.getValue();
//			  JsonObject obj1 = Json.createObjectBuilder()
//			    	     .add("name", addl_info_key)
//			    	     .add("value", addl_info_value).build();
			  builder.add(addl_info_key, addl_info_value);
		  }
		  evelstate.add("additionalFields", builder);
	  }

	  EVEL_EXIT();
	  
	  return evelstate;
	}
	
	
	  /**************************************************************************//**
	   * Encode the event as a JSON event object according to AT&T's schema.
	   * retval : String of JSON state change event message
	   *****************************************************************************/
	 JsonObject evel_json_encode_event()
	  {
		EVEL_ENTER();
		
		assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_STATE_CHANGE);
	    //encode header and state change fields    
	    JsonObject obj = Json.createObjectBuilder()
	    	     .add("event", Json.createObjectBuilder()
		    	         .add( "commonEventHeader",eventHeaderObject() )
		    	         .add( "stateChangeFields",evelStateChangeObject() )
		    	         ).build();

	    EVEL_EXIT();
	    
	    return obj;

	  }
	 
	 JsonObject evel_json_encode_event_batch()
	  {
		EVEL_ENTER();
		
		assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_STATE_CHANGE);
	    //encode header and state change fields    
	    JsonObject obj = Json.createObjectBuilder()
	    	    // .add("event", Json.createObjectBuilder()
		    	         .add( "commonEventHeader",eventHeaderObject() )
		    	         .add( "stateChangeFields",evelStateChangeObject() )
		    	         .build();

	    EVEL_EXIT();
	    
	    return obj;

	  }
	

}