1 package evel_javalibrary.att.com;
\r
3 /**************************************************************************//**
\r
5 * Header for EVEL Header library
\r
7 * This file implements the EVEL library which is intended to provide a
\r
8 * simple wrapper around the complexity of AT&T's Vendor Event Listener API so
\r
9 * that VNFs can use it without worrying about details of the API transport.
\r
14 * Licensed under the Apache License, Version 2.0 (the "License");
\r
15 * you may not use this file except in compliance with the License.
\r
16 * You may obtain a copy of the License at
\r
17 * http://www.apache.org/licenses/LICENSE-2.0
\r
19 * Unless required by applicable law or agreed to in writing, software
\r
20 * distributed under the License is distributed on an "AS IS" BASIS,
\r
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
22 * See the License for the specific language governing permissions and
\r
23 * limitations under the License.
\r
24 *****************************************************************************/
\r
27 import java.text.MessageFormat;
\r
28 import java.util.Enumeration;
\r
29 import java.util.logging.Logger;
\r
30 import java.io.StringWriter;
\r
31 import java.net.InetAddress;
\r
32 import java.net.NetworkInterface;
\r
33 import java.net.SocketException;
\r
34 import java.net.UnknownHostException;
\r
36 import javax.json.Json;
\r
37 import javax.json.JsonObject;
\r
38 import javax.json.JsonObjectBuilder;
\r
39 import javax.json.JsonString;
\r
40 import javax.json.JsonWriter;
\r
42 import org.slf4j.helpers.MessageFormatter;
\r
45 public class EvelHeader {
\r
47 /**************************************************************************//**
\r
48 * Event domains for the various events we support.
\r
49 * JSON equivalent field: domain
\r
50 *****************************************************************************/
\r
51 public enum DOMAINS {
\r
52 EVEL_DOMAIN_INTERNAL, /** Internal event, not for external routing. */
\r
53 EVEL_DOMAIN_FAULT, /** A Fault event. */
\r
54 EVEL_DOMAIN_HEARTBEAT, /** A Heartbeat event (event header only). */
\r
55 EVEL_DOMAIN_MEASUREMENT, /** A Measurement for VF Scaling event. */
\r
56 EVEL_DOMAIN_MOBILE_FLOW, /** A Mobile Flow event. */
\r
57 EVEL_DOMAIN_OTHER, /** Another event. */
\r
58 EVEL_DOMAIN_REPORT, /** A Measurement for VF Reporting event. */
\r
59 EVEL_DOMAIN_SIPSIGNALING, /** A Signaling event. */
\r
60 EVEL_DOMAIN_STATE_CHANGE, /** A State Change event. */
\r
61 EVEL_DOMAIN_SYSLOG, /** A Syslog event. */
\r
62 EVEL_DOMAIN_THRESHOLD_CROSSING, /** A Threshold crossing alert Event */
\r
63 EVEL_DOMAIN_VOICE_QUALITY, /** A Voice Quality Event */
\r
64 EVEL_DOMAIN_HEARTBEAT_FIELD,/** A Heartbeat field event. */
\r
65 EVEL_MAX_DOMAINS /** Maximum number of recognized Event types. */
\r
68 /**************************************************************************//**
\r
70 * JSON equivalent field: priority
\r
71 *****************************************************************************/
\r
72 public enum PRIORITIES {
\r
74 EVEL_PRIORITY_MEDIUM,
\r
75 EVEL_PRIORITY_NORMAL,
\r
80 final int EVEL_HEADER_MAJOR_VERSION = 1;
\r
81 final int EVEL_HEADER_MINOR_VERSION = 1;
\r
82 /***************************************************************************/
\r
84 /***************************************************************************/
\r
88 /***************************************************************************/
\r
89 /* Mandatory fields */
\r
90 /***************************************************************************/
\r
91 DOMAINS event_domain;
\r
92 String event_id=null;
\r
93 String event_name=null;
\r
94 String source_name=null;
\r
95 String reporting_entity_name=null;
\r
96 PRIORITIES priority;
\r
97 Long start_epoch_microsec = 0L;
\r
98 Long last_epoch_microsec = 0L;
\r
101 /***************************************************************************/
\r
102 /* Optional fields */
\r
103 /***************************************************************************/
\r
104 EvelOptionString event_type;
\r
105 EvelOptionString source_id;
\r
106 EvelOptionString reporting_entity_id;
\r
107 EvelOptionIntHeader internal_field;
\r
108 EvelOptionString nfcnaming_code;
\r
109 EvelOptionString nfnaming_code;
\r
111 /**************************************************************************//**
\r
112 * Unique sequence number for events from this VNF.
\r
113 *****************************************************************************/
\r
114 static int event_sequence = 1;
\r
115 private static final Logger LOGGER = Logger.getLogger( EvelHeader.class.getName() );
\r
117 protected static void EVEL_EXIT() {
\r
118 // TODO Auto-generated method stub
\r
122 protected static void EVEL_ENTER() {
\r
123 // TODO Auto-generated method stub
\r
126 /**************************************************************************//**
\r
127 * Set the next event_sequence to use.
\r
129 * @param sequence The next sequence number to use.
\r
130 *****************************************************************************/
\r
131 void evel_set_next_event_sequence( int sequence)
\r
135 LOGGER.info(MessageFormat.format("Setting event sequence to {0}, was {1} ", sequence, event_sequence));
\r
136 event_sequence = sequence;
\r
141 private final static char[] hexArray = "0123456789ABCDEF".toCharArray();
\r
142 private static String bytesToHex(byte[] bytes) {
\r
143 char[] hexChars = new char[bytes.length * 2];
\r
144 for ( int j = 0; j < bytes.length; j++ ) {
\r
145 int v = bytes[j] & 0xFF;
\r
146 hexChars[j * 2] = hexArray[v >>> 4];
\r
147 hexChars[j * 2 + 1] = hexArray[v & 0x0F];
\r
149 return new String(hexChars);
\r
152 /**************************************************************************//**
\r
153 * Initialize a newly created event header.
\r
155 * @param header Pointer to the header being initialized.
\r
156 *****************************************************************************/
\r
157 public EvelHeader(String eventname,String ev_id)
\r
161 assert(eventname != null);
\r
163 /***************************************************************************/
\r
164 /* Initialize the header. Get a new event sequence number. Note that if */
\r
165 /* any memory allocation fails in here we will fail gracefully because */
\r
166 /* everything downstream can cope with nulls. */
\r
167 /***************************************************************************/
\r
168 this.event_domain = DOMAINS.EVEL_DOMAIN_HEARTBEAT;
\r
170 event_id = MessageFormat.format("{0}", event_sequence);
\r
171 LOGGER.warning("WARNING:not confirming to Common Event Format 28.3 standard");
\r
174 event_name = eventname;
\r
175 start_epoch_microsec = last_epoch_microsec;
\r
176 last_epoch_microsec = System.nanoTime()/1000;
\r
177 priority = PRIORITIES.EVEL_PRIORITY_NORMAL;
\r
179 String hostname = "Unknown";
\r
180 String uuid = "Unknown";
\r
185 addr = InetAddress.getLocalHost();
\r
186 hostname = addr.getHostName();
\r
188 catch (UnknownHostException ex)
\r
190 System.out.println("Hostname can not be resolved");
\r
195 Enumeration<NetworkInterface> networks =
\r
196 NetworkInterface.getNetworkInterfaces();
\r
197 while(networks.hasMoreElements()) {
\r
198 NetworkInterface network = networks.nextElement();
\r
199 byte[] mac = network.getHardwareAddress();
\r
201 if(hostname.equalsIgnoreCase("unknown"))
\r
203 Enumeration inetAddrs = network.getInetAddresses();
\r
204 while(inetAddrs.hasMoreElements()){
\r
205 InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
\r
206 if (!inetAddr.isLoopbackAddress()) {
\r
207 hostname = inetAddr.getHostAddress();
\r
214 /* System.out.print("Current MAC address : ");
\r
216 StringBuilder sb = new StringBuilder();
\r
217 for (int i = 0; i < mac.length; i++) {
\r
218 sb.append(String.format("%02X%s", mac[i],
\r
219 (i < mac.length - 1) ? "-" : ""));
\r
222 uuid = bytesToHex(mac);
\r
226 } catch (SocketException e) {
\r
227 // TODO Auto-generated catch block
\r
228 e.printStackTrace();
\r
232 reporting_entity_name = hostname;
\r
233 source_name = hostname;
\r
234 sequence = event_sequence;
\r
236 major_version = EVEL_HEADER_MAJOR_VERSION;
\r
237 minor_version = EVEL_HEADER_MINOR_VERSION;
\r
240 /***************************************************************************/
\r
241 /* Optional parameters. */
\r
242 /***************************************************************************/
\r
243 event_type = new EvelOptionString(false, null);
\r
244 nfcnaming_code = new EvelOptionString(false, null);
\r
245 nfnaming_code = new EvelOptionString(false, null);
\r
246 reporting_entity_id = new EvelOptionString(true, uuid);
\r
247 source_id = new EvelOptionString(true, uuid);
\r
248 internal_field = new EvelOptionIntHeader(false, null);
\r
254 /**************************************************************************//**
\r
255 * Create a new heartbeat event.
\r
257 * @note that the heartbeat is just a "naked" commonEventHeader!
\r
259 * @returns pointer to the newly manufactured ::EVENT_HEADER.
\r
260 * @retval null Failed to create the event.
\r
261 ****************************************************************************/
\r
264 public static EvelHeader evel_new_heartbeat()
\r
266 EvelHeader header = null;
\r
268 /***************************************************************************/
\r
269 /* Initialize the header. Get a new event sequence number. Note that if */
\r
270 /* any memory allocation fails in here we will fail gracefully because */
\r
271 /* everything downstream can cope with nulls. */
\r
272 /***************************************************************************/
\r
273 header = new EvelHeader("Heartbeat",null);
\r
274 header.event_type.set_option(true);
\r
275 header.event_type.SetValue("HEARTBEAT");
\r
276 LOGGER.info(header.event_type.value);
\r
282 /**************************************************************************//**
\r
283 * Create a new heartbeat event.
\r
285 * @note that the heartbeat is just a "naked" commonEventHeader!
\r
287 * @returns pointer to the newly manufactured ::EVENT_HEADER.
\r
288 * @retval null Failed to create the event.
\r
289 ****************************************************************************/
\r
292 public static EvelHeader evel_new_heartbeat(String evname,String evid)
\r
294 EvelHeader header = null;
\r
296 /***************************************************************************/
\r
297 /* Initialize the header. Get a new event sequence number. Note that if */
\r
298 /* any memory allocation fails in here we will fail gracefully because */
\r
299 /* everything downstream can cope with nulls. */
\r
300 /***************************************************************************/
\r
301 header = new EvelHeader(evname,evid);
\r
302 header.event_type.set_option(true);
\r
303 header.event_type.SetValue("HEARTBEAT");;
\r
304 LOGGER.info(header.event_type.value);
\r
311 /**************************************************************************//**
\r
312 * Set the Event Type property of the event header.
\r
314 * @note The property is treated as immutable: it is only valid to call
\r
315 * the setter once. However, we don't assert if the caller tries to
\r
316 * overwrite, just ignoring the update instead.
\r
318 * @param header Pointer to the ::EVENT_HEADER.
\r
319 * @param type The Event Type to be set. ASCIIZ string. The caller
\r
320 * does not need to preserve the value once the function
\r
322 *****************************************************************************/
\r
323 public void evel_header_type_set(String type)
\r
327 /***************************************************************************/
\r
328 /* Check preconditions. */
\r
329 /***************************************************************************/
\r
330 assert(type != null);
\r
332 event_type.set_option(true);
\r
333 event_type.SetValue(type);
\r
338 /**************************************************************************//**
\r
339 * Set the Start Epoch property of the event header.
\r
341 * @note The Start Epoch defaults to the time of event creation.
\r
343 * @param header Pointer to the ::EVENT_HEADER.
\r
344 * @param start_epoch_microsec
\r
345 * The start epoch to set, in microseconds.
\r
346 *****************************************************************************/
\r
347 public void evel_start_epoch_set(Long epoch_microsec)
\r
351 /***************************************************************************/
\r
352 /* Check preconditions and assign the new value. */
\r
353 /***************************************************************************/
\r
354 start_epoch_microsec = epoch_microsec;
\r
359 /**************************************************************************//**
\r
360 * Set the Last Epoch property of the event header.
\r
362 * @note The Last Epoch defaults to the time of event creation.
\r
364 * @param header Pointer to the ::EVENT_HEADER.
\r
365 * @param last_epoch_microsec
\r
366 * The last epoch to set, in microseconds.
\r
367 *****************************************************************************/
\r
368 public void evel_last_epoch_set(Long epoch_microsec)
\r
372 /***************************************************************************/
\r
373 /* Check preconditions and assign the new value. */
\r
374 /***************************************************************************/
\r
375 last_epoch_microsec = epoch_microsec;
\r
380 /**************************************************************************//**
\r
381 * Set the NFC Naming code property of the event header.
\r
383 * @param header Pointer to the ::EVENT_HEADER.
\r
384 * @param nfcnamingcode String
\r
385 *****************************************************************************/
\r
386 public void evel_nfcnamingcode_set(String nfcnam)
\r
390 /***************************************************************************/
\r
391 /* Check preconditions and assign the new value. */
\r
392 /***************************************************************************/
\r
393 assert(nfcnam != null);
\r
394 nfcnaming_code.set_option(true);
\r
395 nfcnaming_code.SetValue(nfcnam);
\r
400 /**************************************************************************//**
\r
401 * Set the NF Naming code property of the event header.
\r
403 * @param header Pointer to the ::EVENT_HEADER.
\r
404 * @param nfnamingcode String
\r
405 *****************************************************************************/
\r
406 public void evel_nfnamingcode_set(String nfnam)
\r
410 /***************************************************************************/
\r
411 /* Check preconditions and assign the new value. */
\r
412 /***************************************************************************/
\r
413 assert(nfnam != null);
\r
414 nfnaming_code.set_option(true);
\r
415 nfnaming_code.SetValue(nfnam);
\r
421 /**************************************************************************//**
\r
422 * Set the Reporting Entity Name property of the event header.
\r
424 * @note The Reporting Entity Name defaults to the OpenStack VM Name.
\r
426 * @param header Pointer to the ::EVENT_HEADER.
\r
427 * @param entity_name The entity name to set.
\r
428 *****************************************************************************/
\r
429 public void evel_reporting_entity_name_set(String entity_name)
\r
433 /***************************************************************************/
\r
434 /* Check preconditions and assign the new value. */
\r
435 /***************************************************************************/
\r
436 assert(entity_name != null);
\r
438 /***************************************************************************/
\r
439 /* Free the previously allocated memory and replace it with a copy of the */
\r
440 /* provided one. */
\r
441 /***************************************************************************/
\r
442 reporting_entity_name = entity_name;
\r
447 /**************************************************************************//**
\r
448 * Set the Reporting Entity Id property of the event header.
\r
450 * @note The Reporting Entity Id defaults to the OpenStack VM UUID.
\r
452 * @param header Pointer to the ::EVENT_HEADER.
\r
453 * @param entity_id The entity id to set.
\r
454 *****************************************************************************/
\r
455 public void evel_reporting_entity_id_set(String entity_id)
\r
459 /***************************************************************************/
\r
460 /* Check preconditions and assign the new value. */
\r
461 /***************************************************************************/
\r
462 assert(entity_id != null);
\r
464 /***************************************************************************/
\r
465 /* Free the previously allocated memory and replace it with a copy of the */
\r
466 /* provided one. Note that evel_force_option_string strdups entity_id. */
\r
467 /***************************************************************************/
\r
468 reporting_entity_id.set_option(true);
\r
469 reporting_entity_id.SetValue(entity_id);
\r
474 /**************************************************************************//**
\r
475 * Set the Priority property of the event header.
\r
477 * @note The Priority of event being reported.
\r
479 * @param priorityval Value of priority.
\r
481 *****************************************************************************/
\r
482 public void evel_header_set_priority(PRIORITIES priority_val)
\r
486 assert(EvelHeader.PRIORITIES.EVEL_MAX_PRIORITIES.compareTo(priority_val) < 0 );
\r
488 priority = priority_val;
\r
493 /**************************************************************************//**
\r
494 * Set the Priority property of the event header.
\r
496 * @note The Reporting Entity Id defaults to the OpenStack VM UUID.
\r
498 * @param val Optional true or false.
\r
499 * @param srcid The source id to set.
\r
500 *****************************************************************************/
\r
501 public void evel_header_set_sourceid(boolean val, String srcid)
\r
505 assert(srcid != null);
\r
507 source_id.set_option(val);
\r
509 source_id.SetValue(srcid);
\r
514 /**************************************************************************//**
\r
515 * Set the Source name property of the event header.
\r
517 * @note Source name should identify the IP address or Source Event originator
\r
520 * @param srcname String for sourcename.
\r
521 *****************************************************************************/
\r
522 public void evel_header_set_source_name(String srcname)
\r
526 assert(srcname != null);
\r
528 source_name = srcname;
\r
533 /**************************************************************************//**
\r
534 * Map an ::EVEL_EVENT_DOMAINS enum value to the equivalent string.
\r
536 * @param domain The domain to convert.
\r
537 * @returns The equivalent string.
\r
538 *****************************************************************************/
\r
539 String evel_event_domain(DOMAINS domain)
\r
547 case EVEL_DOMAIN_HEARTBEAT:
\r
548 result = "heartbeat";
\r
551 case EVEL_DOMAIN_FAULT:
\r
555 case EVEL_DOMAIN_MEASUREMENT:
\r
556 result = "measurementsForVfScaling";
\r
559 case EVEL_DOMAIN_REPORT:
\r
560 result = "measurementsForVfReporting";
\r
563 case EVEL_DOMAIN_MOBILE_FLOW:
\r
564 result = "mobileFlow";
\r
567 case EVEL_DOMAIN_HEARTBEAT_FIELD:
\r
568 result = "heartbeat";
\r
571 case EVEL_DOMAIN_SIPSIGNALING:
\r
572 result = "sipSignaling";
\r
575 case EVEL_DOMAIN_STATE_CHANGE:
\r
576 result = "stateChange";
\r
579 case EVEL_DOMAIN_SYSLOG:
\r
583 case EVEL_DOMAIN_OTHER:
\r
587 case EVEL_DOMAIN_VOICE_QUALITY:
\r
588 result = "voiceQuality";
\r
591 case EVEL_DOMAIN_THRESHOLD_CROSSING:
\r
592 result = "thresholdCrossingAlert";
\r
597 LOGGER.severe(MessageFormat.format("Unexpected domain {0}", domain));
\r
605 /**************************************************************************//**
\r
606 * Map an ::EVEL_EVENT_PRIORITIES enum value to the equivalent string.
\r
608 * @param priority The priority to convert.
\r
609 * @returns The equivalent string.
\r
610 *****************************************************************************/
\r
611 String evel_event_priority(PRIORITIES priority)
\r
619 case EVEL_PRIORITY_HIGH:
\r
623 case EVEL_PRIORITY_MEDIUM:
\r
627 case EVEL_PRIORITY_NORMAL:
\r
631 case EVEL_PRIORITY_LOW:
\r
637 LOGGER.severe(MessageFormat.format("Unexpected priority {0}", priority));
\r
645 /**************************************************************************//**
\r
646 * Encode the CommonEventHeaeder as a JSON event object builder
\r
647 * according to AT&T's schema.
\r
649 * @retval JsonObjectBuilder of fault body portion of message
\r
650 *****************************************************************************/
\r
651 JsonObjectBuilder eventHeaderObject()
\r
653 String domain = evel_event_domain(event_domain);
\r
654 String prity = evel_event_priority(priority);
\r
655 double version = major_version+(double)minor_version/10;
\r
659 /***************************************************************************/
\r
660 /* Required fields. */
\r
661 /***************************************************************************/
\r
663 JsonObjectBuilder commheader = Json.createObjectBuilder()
\r
664 .add("domain", domain)
\r
665 .add("eventId", event_id)
\r
666 .add("eventName", event_name)
\r
667 .add("lastEpochMicrosec", last_epoch_microsec)
\r
668 .add("priority", prity)
\r
669 .add("reportingEntityName", reporting_entity_name)
\r
670 .add("sequence", sequence)
\r
671 .add("sourceName", source_name)
\r
672 .add("startEpochMicrosec", start_epoch_microsec)
\r
673 .add("version", version)
\r
674 .add("reportingEntityId", reporting_entity_id.GetValue())
\r
675 .add("sourceId", source_id.GetValue());
\r
677 /***************************************************************************/
\r
678 /* Optional fields. */
\r
679 /***************************************************************************/
\r
681 if( event_type.is_set )
\r
682 commheader.add("eventType", event_type.GetValue());
\r
683 if( source_id.is_set )
\r
684 commheader.add("sourceId", source_id.GetValue());
\r
685 if( reporting_entity_id.is_set )
\r
686 commheader.add("reportingEntityId", reporting_entity_id.GetValue());
\r
688 if( internal_field.is_set )
\r
689 commheader.add("internalField",internal_field.toString());
\r
691 if( nfcnaming_code.is_set )
\r
692 commheader.add("nfcNamingCode", nfcnaming_code.GetValue());
\r
693 if( nfnaming_code.is_set )
\r
694 commheader.add("nfNamingCode", nfnaming_code.GetValue());
\r
703 /**************************************************************************//**
\r
704 * Encode the event as a JSON event object according to AT&T's schema.
\r
705 * retval : String of JSON event header only message
\r
706 *****************************************************************************/
\r
707 String evel_json_encode_event()
\r
710 JsonObject obj = Json.createObjectBuilder()
\r
711 .add("event", Json.createObjectBuilder()
\r
712 .add( "commonEventHeader",eventHeaderObject() )
\r
717 return obj.toString();
\r