summaryrefslogtreecommitdiffstats
path: root/veslibrary/ves_javalibrary/evel_javalib2/src/main/java/evel_javalibrary/att/com/EvelHeartbeatField.java
blob: a1ca829cb03196aa724eac14ea5e4a3c548f4b1e (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
package evel_javalibrary.att.com;
/**************************************************************************//**
 * @file
 * Evel Heartbeat field class
 *
 * This file implements the Evel Heartbeat 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 status.
 *
 * 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.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;


public class EvelHeartbeatField extends EvelHeader {
	
	
	/**************************************************************************//**
	 * Alert types.
	 * JSON equivalent fields: newState, oldState
	 *****************************************************************************/
	
	/***************************************************************************/
	/* Mandatory fields                                                        */
	/***************************************************************************/
	int    heartbeat_interval;


	/***************************************************************************/
	/* Optional fields                                                         */
	/***************************************************************************/
	  
	  HashMap<String, String > additional_inf;
	
	  private static final Logger LOGGER = Logger.getLogger( EvelHeartbeatField.class.getName() );

	  /**************************************************************************//**
	   * Construct Heartbeat field event.
	   *
	   * @param interval     The Heartbeat interval at which messages are sent.
	   * 
	   *****************************************************************************/
	public EvelHeartbeatField(int interval,String evname,String evid)
	{
		super(evname,evid);
		event_domain = EvelHeader.DOMAINS.EVEL_DOMAIN_HEARTBEAT_FIELD;
		assert( interval > 0 );
		
		heartbeat_interval = interval;
		

		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_hrtbt_field_addl_info_add(String name, String value)
	{
	 // String[] addl_info = null;
	  EVEL_ENTER();

	  /***************************************************************************/
	  /* Check preconditions.                                                    */
	  /***************************************************************************/
	  assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_HEARTBEAT_FIELD);
	  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));
	  additional_inf.put(name,  value);
	  EVEL_EXIT();
	}
	
	
	/**************************************************************************//**
	 * Set the Interval property of the Heartbeat fields event.
	 *
	 * @note  The property is treated as immutable: it is only valid to call
	 *        the setter once.  However, we don't assert if the caller tries to
	 *        overwrite, just ignoring the update instead.
	 *
	 * @param interval      Heartbeat interval.
	 *****************************************************************************/
	public void evel_hrtbt_interval_set( int interval)
	{
	  EVEL_ENTER();

	  /***************************************************************************/
	  /* Check preconditions and call evel_set_option_string.                    */
	  /***************************************************************************/
	  assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_HEARTBEAT_FIELD);
	  assert(interval > 0);

	  heartbeat_interval = interval;
	  EVEL_EXIT();
	}


	/**************************************************************************//**
	 * Encode the Heartbeat field in JSON according to AT&T's schema.
	 *
	 * @retval JsonObjectBuilder of Heartbeat field body portion of message   
	 *****************************************************************************/
	 JsonObjectBuilder evelHeartbeatFieldObject()
	 {
	//  double version = major_version+(double)minor_version/10;
        String version = "3.0";
	  EVEL_ENTER();
	  

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

	  /***************************************************************************/
	  /* Mandatory fields.                                                       */
	  /***************************************************************************/
	  
	  JsonObjectBuilder evelHeatbeat = Json.createObjectBuilder()
	   	         .add("heartbeatInterval", heartbeat_interval);
	  
	  evelHeatbeat.add( "heartbeatFieldsVersion", version);

	  /***************************************************************************/
	  /* Checkpoint, so that we can wind back if all fields are suppressed.      */
	  /***************************************************************************/
	/*  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);
	    }
	    evelHeatbeat.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();
			  builder.add("name", addl_info_key);
			  builder.add("value", addl_info_value);
			  
			  
		  }
		  evelHeatbeat.add("additionalFields", builder);
	  }
	  
	  
	  EVEL_EXIT();
	  
	  return evelHeatbeat;
	}
	
	
	  /**************************************************************************//**
	   * Encode the event as a JSON event object according to AT&T's schema.
	   * retval : String of JSON event message
	   *****************************************************************************/
	  public JsonObject evel_json_encode_event()
	  {
		EVEL_ENTER();
		
		assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_HEARTBEAT_FIELD);
	    //encode common event header and body    
	    JsonObject obj = Json.createObjectBuilder()	    		
	    	     .add("event", Json.createObjectBuilder()	    		
		    	         .add( "commonEventHeader",eventHeaderObject())
		    	         .add( "heartbeatFields",evelHeartbeatFieldObject())
		    	         ).build();

	    EVEL_EXIT();
	    
	    return obj;

	  }
	  
	  public JsonObject evel_json_encode_event_batch()
	  {
		EVEL_ENTER();
		
		assert(event_domain == EvelHeader.DOMAINS.EVEL_DOMAIN_HEARTBEAT_FIELD);
	    //encode common event header and body    
	    JsonObject obj = Json.createObjectBuilder()	    		
	    	    // .add("event", Json.createObjectBuilder()
		    	             .add( "commonEventHeader",eventHeaderObject())
		    	             .add( "heartbeatFields",evelHeartbeatFieldObject())
		    	             .build();

	    EVEL_EXIT();
	    
	    return obj;

	  }
	

}