From 52d2fa383207e665cddfd4688dc88b670e4d70e3 Mon Sep 17 00:00:00 2001 From: Gokul Singaraju Date: Fri, 5 Jan 2018 18:53:31 -0500 Subject: Implement Batch API and https Issue-ID: VNFSDK-198 Change-Id: Id3a124d527630f25baf65c0705655a569121a7cc Signed-Off-by: Gokul Singaraju --- veslibrary/ves_javalibrary/evel_javalib2/pom.xml | 233 +++++++++--------- .../src/evel_javalibrary/att/com/AgentMain.java | 109 +++++++-- .../src/evel_javalibrary/att/com/EvelBatch.java | 83 +++++++ .../src/evel_javalibrary/att/com/EvelFault.java | 6 +- .../src/evel_javalibrary/att/com/EvelHeader.java | 8 +- .../att/com/EvelHeartbeatField.java | 6 +- .../evel_javalibrary/att/com/EvelMobileFlow.java | 8 +- .../src/evel_javalibrary/att/com/EvelObject.java | 41 ++++ .../evel_javalibrary/att/com/EvelOptionString.java | 2 +- .../src/evel_javalibrary/att/com/EvelOther.java | 90 ++++--- .../att/com/EvelScalingMeasurement.java | 8 +- .../evel_javalibrary/att/com/EvelSipSignaling.java | 8 +- .../evel_javalibrary/att/com/EvelStateChange.java | 8 +- .../src/evel_javalibrary/att/com/EvelSyslog.java | 8 +- .../att/com/EvelThresholdCross.java | 4 +- .../evel_javalibrary/att/com/EvelVoiceQuality.java | 6 +- .../src/evel_javalibrary/att/com/RingBuffer.java | 18 +- .../evel_javalibrary/att/com/maindir/Main.java | 272 +++++++++++++++++++++ .../evel_javalib2/src_test/java_agent_test | 2 + .../evel_javalib2/src_test/maindir/Main.java | 253 ------------------- 20 files changed, 704 insertions(+), 469 deletions(-) create mode 100644 veslibrary/ves_javalibrary/evel_javalib2/src/evel_javalibrary/att/com/EvelBatch.java create mode 100644 veslibrary/ves_javalibrary/evel_javalib2/src/evel_javalibrary/att/com/EvelObject.java create mode 100644 veslibrary/ves_javalibrary/evel_javalib2/src_test/evel_javalibrary/att/com/maindir/Main.java create mode 100755 veslibrary/ves_javalibrary/evel_javalib2/src_test/java_agent_test delete mode 100644 veslibrary/ves_javalibrary/evel_javalib2/src_test/maindir/Main.java diff --git a/veslibrary/ves_javalibrary/evel_javalib2/pom.xml b/veslibrary/ves_javalibrary/evel_javalib2/pom.xml index fc4f209..40e499e 100644 --- a/veslibrary/ves_javalibrary/evel_javalib2/pom.xml +++ b/veslibrary/ves_javalibrary/evel_javalib2/pom.xml @@ -1,115 +1,118 @@ - - 4.0.0 - org.onap.vnfsdk.compliance - evel_javalib2 - 0.0.1-SNAPSHOT - VESJavaLibrary - - - - ${env.GIT_NO_PROJECT} - ${project.version} - - - - - onap-snapshots - ONAP Snapshot Repository - http://nexus.onap.org/content/repositories/snapshots - - - - - - src - - - org.codehaus.groovy.maven - gmaven-plugin - - - validate - - execute - - - - println project.properties['ves.project.version']; - def versionArray; - if ( project.properties['ves.project.version'] != null ) { - versionArray = project.properties['ves.project.version'].split('\\.'); - } - - if ( project.properties['ves.project.version'].endsWith("-SNAPSHOT") ) { - project.properties['project.docker.latesttag.version']=versionArray[0] + '.' + versionArray[1] + "-SNAPSHOT-latest"; - } else { - project.properties['project.docker.latesttag.version']=versionArray[0] + '.' + versionArray[1] + "-STAGING-latest"; - } - - println 'New Tag for docker:' + project.properties['project.docker.latesttag.version']; - - - - - - - - org.eclipse.m2e - lifecycle-mapping - 1.0.0 - - - - - - org.codehaus.mojo - aspectj-maven-plugin - [1.0,) - - test-compile - compile - - - - - - - - - - - - - - - - - - junit - junit - 4.11 - test - - - log4j - log4j - 1.2.17 - - - javax.json - javax.json-api - 1.1 - - - org.slf4j - slf4j-log4j12 - 1.7.5 - - - org.glassfish - javax.json - 1.0.4 - - - + + 4.0.0 + org.onap.vnfsdk.compliance + evel_javalib2 + 0.0.1-SNAPSHOT + VESJavaLibrary + + + UTF-8 + 1.7 + 1.7 + + ${env.GIT_NO_PROJECT} + ${project.version} + + + + + onap-snapshots + ONAP Snapshot Repository + http://nexus.onap.org/content/repositories/snapshots + + + + + + src + + + org.codehaus.groovy.maven + gmaven-plugin + + + validate + + execute + + + + println project.properties['ves.project.version']; + def versionArray; + if ( project.properties['ves.project.version'] != null ) { + versionArray = project.properties['ves.project.version'].split('\\.'); + } + + if ( project.properties['ves.project.version'].endsWith("-SNAPSHOT") ) { + project.properties['project.docker.latesttag.version']=versionArray[0] + '.' + versionArray[1] + "-SNAPSHOT-latest"; + } else { + project.properties['project.docker.latesttag.version']=versionArray[0] + '.' + versionArray[1] + "-STAGING-latest"; + } + + println 'New Tag for docker:' + project.properties['project.docker.latesttag.version']; + + + + + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + org.codehaus.mojo + aspectj-maven-plugin + [1.0,) + + test-compile + compile + + + + + + + + + + + + + + + + + + junit + junit + 4.11 + test + + + log4j + log4j + 1.2.17 + + + javax.json + javax.json-api + 1.1 + + + org.slf4j + slf4j-log4j12 + 1.7.5 + + + org.glassfish + javax.json + 1.0.4 + + + diff --git a/veslibrary/ves_javalibrary/evel_javalib2/src/evel_javalibrary/att/com/AgentMain.java b/veslibrary/ves_javalibrary/evel_javalib2/src/evel_javalibrary/att/com/AgentMain.java index 451c5b6..2ba1db5 100644 --- a/veslibrary/ves_javalibrary/evel_javalib2/src/evel_javalibrary/att/com/AgentMain.java +++ b/veslibrary/ves_javalibrary/evel_javalib2/src/evel_javalibrary/att/com/AgentMain.java @@ -11,7 +11,7 @@ package evel_javalibrary.att.com; * * 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 @@ -28,11 +28,13 @@ import org.apache.log4j.Logger; import org.apache.log4j.Level; import java.io.BufferedReader; +//import java.io.BufferedReader; import java.io.BufferedWriter; +import java.io.FileInputStream; //import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; -//import java.io.InputStreamReader; +import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; @@ -40,9 +42,14 @@ import java.net.MalformedURLException; //import java.net.ProtocolException; import java.net.URL; //import java.nio.charset.StandardCharsets; - +import java.security.KeyStore; import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSession; +import javax.net.ssl.TrustManagerFactory; import org.apache.log4j.BasicConfigurator; @@ -77,8 +84,12 @@ public enum EVEL_ERR_CODES { private static String url = null; private static URL vesurl = null; + private static URL vesbatchurl = null; private static HttpURLConnection con = null; private static String userpass = null; + private static String keystore_pth = null; + private static String jks_passw = null; + private static String key_passw = null; private static String version = "5"; /* RingBuffer to forward messages on sending AgentDispatcher thread */ @@ -89,7 +100,7 @@ public enum EVEL_ERR_CODES { /* AgentDispatcher loops on messages in RingBuffer and POSTs them * to external Collector */ - private static class AgentDispatcher implements Runnable { + private static class AgentDispatcher implements Runnable { private String readStream(InputStream stream) throws Exception { StringBuilder builder = new StringBuilder(); @@ -109,25 +120,61 @@ public enum EVEL_ERR_CODES { String datatosend=null; for(;;){ - if( (datatosend = (String) ringb.take()) != null ) + EvelObject tosend = ringb.take(); + if( tosend != null && ((datatosend = (String) tosend.datastr) != null)) { //process data - logger.trace(url + "Got an event size "+datatosend.length()); logger.trace(datatosend); try { //HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); - con = (HttpURLConnection) vesurl.openConnection(); + if( tosend.type == false) + con = (HttpURLConnection) vesurl.openConnection(); + else + con = (HttpURLConnection) vesbatchurl.openConnection(); + if (con instanceof HttpsURLConnection) { HttpsURLConnection httpsConnection = (HttpsURLConnection) con; - //SSLContext sc = SSLContext.getInstance("TLSv1.2"); - // Init the SSLContext with a TrustManager[] and SecureRandom() - //sc.init(null, null, new java.security.SecureRandom()); - //httpsConnection.setHostnameVerifier(getHostnameVerifier()); - //httpsConnection.setSSLSocketFactory(sc.getSocketFactory()); - con = httpsConnection; + + try { + + SSLContext sc = SSLContext.getInstance("TLSv1.2"); + /* Get the JKS contents */ + if( !keystore_pth.isEmpty() && !jks_passw.isEmpty() && !key_passw.isEmpty() ) + { + final KeyStore keyStore = KeyStore.getInstance("JKS"); + try (final InputStream is = new FileInputStream(keystore_pth)) { + keyStore.load(is, jks_passw.toCharArray()); + } + final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory + .getDefaultAlgorithm()); + kmf.init(keyStore, key_passw.toCharArray()); + final TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory + .getDefaultAlgorithm()); + tmf.init(keyStore); + sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new java.security.SecureRandom()); + } + else + { + // Init the SSLContext with a TrustManager[] and SecureRandom() + sc.init(null, null, new java.security.SecureRandom()); + } + httpsConnection.setSSLSocketFactory(sc.getSocketFactory()); + httpsConnection.setHostnameVerifier(new HostnameVerifier() + { + public boolean verify(String hostname, SSLSession session) + { + return true; + } + }); + con = httpsConnection; + + } + catch (final Exception exc) { + exc.printStackTrace(); + } } //add reuqest header @@ -242,6 +289,9 @@ public enum EVEL_ERR_CODES { String topic, String username, String password, + String keystore_path, + String jks_password, + String key_password, Level level) throws IOException { EVEL_ERR_CODES rc = EVEL_ERR_CODES.EVEL_SUCCESS; @@ -273,10 +323,15 @@ public enum EVEL_ERR_CODES { version += "/example_vnf"; } + keystore_pth = keystore_path; + jks_passw = jks_password; + key_passw = key_password; + url = event_api_url+":"+Integer.toString(port)+path+"/eventListener/v"+version; vesurl = null; try { vesurl = new URL(url); + vesbatchurl = new URL(url+"/eventBatch"); } catch (MalformedURLException e) { // TODO Auto-generated catch block logger.info("Error in url input"); @@ -317,14 +372,36 @@ public enum EVEL_ERR_CODES { * @retval boolean True On successful acceptance False on failure *****************************************************************************/ - public static boolean evel_post_event(EvelHeader obj ) + public static boolean evel_post_event(EvelHeader obj) { - String data = obj.evel_json_encode_event(); - boolean ret = ringb.put(data); + String data = obj.evel_json_encode_event().toString(); + EvelObject myobj = new EvelObject(data,false); + boolean ret = ringb.put(myobj); logger.info("Evel Post event ret:"+ret); return ret; } + /**************************************************************************//** + * Handle user formatted post message + * + * @note This function handles VES 5.x formatted messages from all valid + * Domains and stores them in RingBuffer. + * + * @param obj VES 5.x formatted user messages with common header + * and optional specialized body + * + * @retval boolean True On successful acceptance False on failure + *****************************************************************************/ + + public static boolean evel_post_event(EvelBatch obj) + { + String data = obj.evel_json_encode_event().toString(); + EvelObject myobj = new EvelObject(data,true); + boolean ret = ringb.put(myobj); + logger.info("Evel Post batch event ret:"+ret); + return ret; + } + } diff --git a/veslibrary/ves_javalibrary/evel_javalib2/src/evel_javalibrary/att/com/EvelBatch.java b/veslibrary/ves_javalibrary/evel_javalib2/src/evel_javalibrary/att/com/EvelBatch.java new file mode 100644 index 0000000..6db25b5 --- /dev/null +++ b/veslibrary/ves_javalibrary/evel_javalib2/src/evel_javalibrary/att/com/EvelBatch.java @@ -0,0 +1,83 @@ +package evel_javalibrary.att.com; + +/**************************************************************************//** + * @file + * Evel Batch container class + * + * This file implements the Evel Mobile Flow 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 Mobile flow 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.util.ArrayList; + +import javax.json.Json; +import javax.json.JsonArrayBuilder; +import javax.json.JsonObject; +import javax.json.JsonObjectBuilder; + +import org.apache.log4j.Logger; + + +public class EvelBatch { + + ArrayList batch_events; + + private static final Logger LOGGER = Logger.getLogger( EvelScalingMeasurement.class.getName() ); + + + public EvelBatch(){ + batch_events = new ArrayList(); + if( batch_events == null ) LOGGER.error("Unable to allocate new batch"); + } + + public boolean addEvent(EvelHeader element){ + boolean ret = false; + ret = batch_events.add(element); + if( ret == false){ + LOGGER.error("Unable to add batch event"); + } + return ret; + + } + + /**************************************************************************//** + * Encode the event as a JSON event object according to AT&T's schema. + * retval : String of JSON event header only message + *****************************************************************************/ + JsonObject evel_json_encode_event() + { + JsonObjectBuilder batchobj = Json.createObjectBuilder(); + + if( batch_events != null && batch_events.size() > 0 ) + { + JsonArrayBuilder builder = Json.createArrayBuilder(); + for(int i=0;i additional_info; - HashMap> evelmap; + Map> evelmap; private static final Logger LOGGER = Logger.getLogger( EvelOther.class.getName() ); @@ -127,7 +127,7 @@ public class EvelOther extends EvelHeader { * @param name String Name. * @param value String Value. *****************************************************************************/ - public void evel_other_field_add_namedarray(String hashname, String name, String value) + public void evel_other_field_add_namedarray(String hashname,String name, String value) { EVEL_ENTER(); @@ -139,31 +139,39 @@ public class EvelOther extends EvelHeader { assert(name != null); assert(value != null); - if( evelmap == null) - evelmap = new HashMap>(); - - LOGGER.debug("Adding hash : "+hashname+" name="+name+"value= "+value); - Map mymap = null; - try{ - mymap = evelmap.get(hashname); - } catch( Exception e) - { - e.printStackTrace(); - } - if(mymap == null) - mymap = new HashMap(); - try{ - if( mymap.put(name, value) == null) - LOGGER.debug("Unable to add map hash : "+hashname+" name="+name+"value= "+value);; - - if( evelmap.put(hashname, mymap) == null) - LOGGER.debug("Unable to add hash entry : "+hashname+" name="+name+"value= "+value);; - } catch( Exception e) - { - e.printStackTrace(); - } + try{ + + if( evelmap == null) + evelmap = new HashMap>(); + + if( evelmap.containsKey(hashname) ) + { + mymap = evelmap.get(hashname); + } + else + { + mymap = new HashMap(); + evelmap.put(hashname, mymap); + LOGGER.debug("Inserted map hash : "+hashname+" name="+name); + } + + if( mymap.containsKey(name) ) + { + String val = mymap.get(name); + LOGGER.error("Error already contains key " + name + "val "+val ); + } + else + { + mymap.put(name, value); + LOGGER.debug("Adding hash : "+hashname+" name="+name+"value= "+value); + } + + } catch( Exception e) + { + e.printStackTrace(); + } EVEL_EXIT(); } @@ -222,8 +230,10 @@ public class EvelOther extends EvelHeader { if(additional_info == null) additional_info = new HashMap(); - if( additional_info.put(name, value) == null) - LOGGER.debug("Unable to add map : name="+name+"value= "+value); + if(additional_info.containsKey(name)) + LOGGER.error("Already exists Unable to add map : name="+name+"value= "+value); + else + additional_info.put(name, value); EVEL_EXIT(); } @@ -263,11 +273,11 @@ public class EvelOther extends EvelHeader { if( additional_info != null ) { JsonArrayBuilder builder = Json.createArrayBuilder(); - for(Map.Entry entry : additional_info.entrySet()){ + for(Map.Entry entry : additional_info.entrySet()){ LOGGER.debug(MessageFormat.format("Key : {0} and Value: {1}", entry.getKey(), entry.getValue())); JsonObject obj = Json.createObjectBuilder() .add("name", entry.getKey()) - .add("value", entry.getValue()).build(); + .add("value",entry.getValue()).build(); builder.add(obj); } eveloth.add("nameValuePairs", builder); @@ -288,21 +298,21 @@ public class EvelOther extends EvelHeader { JsonArrayBuilder builder = Json.createArrayBuilder(); for(Map.Entry> entry : evelmap.entrySet()){ LOGGER.debug(MessageFormat.format("Key : {0} and Value: {1}", entry.getKey(), entry.getValue())); - Map item = entry.getValue(); + Map item = entry.getValue(); + + JsonObjectBuilder obj = Json.createObjectBuilder() + .add( "name", entry.getKey()); JsonArrayBuilder builder2 = Json.createArrayBuilder(); for(Map.Entry entry2 : item.entrySet()){ LOGGER.debug(MessageFormat.format("Key : {0} and Value: {1}", entry2.getKey(), entry2.getValue())); - JsonObject obj = Json.createObjectBuilder() + JsonObjectBuilder obj2 = Json.createObjectBuilder() .add("name", entry2.getKey()) - .add("value", entry2.getValue()).build(); - builder2.add(obj); + .add("value",entry2.getValue()); + builder2.add(obj2.build()); } - - JsonObjectBuilder obj = Json.createObjectBuilder() - .add(entry.getKey(),builder2); - - builder.add(obj); + obj.add("arrayOfFields", builder2); + builder.add(obj.build()); } eveloth.add("hashOfNameValuePairArrays", builder); } @@ -325,7 +335,7 @@ public class EvelOther extends EvelHeader { * Encode the event as a JSON event object according to AT&T's schema. * retval : String of JSON event message *****************************************************************************/ - String evel_json_encode_event() + JsonObject evel_json_encode_event() { EVEL_ENTER(); @@ -339,7 +349,7 @@ public class EvelOther extends EvelHeader { EVEL_EXIT(); - return obj.toString(); + return obj; } diff --git a/veslibrary/ves_javalibrary/evel_javalib2/src/evel_javalibrary/att/com/EvelScalingMeasurement.java b/veslibrary/ves_javalibrary/evel_javalib2/src/evel_javalibrary/att/com/EvelScalingMeasurement.java index ad13d18..bcbcd20 100644 --- a/veslibrary/ves_javalibrary/evel_javalib2/src/evel_javalibrary/att/com/EvelScalingMeasurement.java +++ b/veslibrary/ves_javalibrary/evel_javalib2/src/evel_javalibrary/att/com/EvelScalingMeasurement.java @@ -3619,8 +3619,8 @@ public class EvelScalingMeasurement extends EvelHeader { for(int j=0;j