diff options
Diffstat (limited to 'veslibrary/ves_javalibrary/evel_javalib2/src/main/java/evel_javalibrary/att/com/AgentMain.java')
-rw-r--r-- | veslibrary/ves_javalibrary/evel_javalib2/src/main/java/evel_javalibrary/att/com/AgentMain.java | 282 |
1 files changed, 243 insertions, 39 deletions
diff --git a/veslibrary/ves_javalibrary/evel_javalib2/src/main/java/evel_javalibrary/att/com/AgentMain.java b/veslibrary/ves_javalibrary/evel_javalib2/src/main/java/evel_javalibrary/att/com/AgentMain.java index a6886e2..aaaf006 100644 --- a/veslibrary/ves_javalibrary/evel_javalib2/src/main/java/evel_javalibrary/att/com/AgentMain.java +++ b/veslibrary/ves_javalibrary/evel_javalib2/src/main/java/evel_javalibrary/att/com/AgentMain.java @@ -31,15 +31,18 @@ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.MalformedURLException; +import java.io.UnsupportedEncodingException; import java.net.URL; +import java.nio.channels.InterruptedByTimeoutException; import java.security.KeyStore; +import java.time.Duration; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.KeyManagerFactory; @@ -86,14 +89,25 @@ public class AgentMain { 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"; + private static String keystorePath = null; + private static String jksPassw = null; + private static String keyPassw = null; + private static String version = "7"; + + + + private static String userpass2 = null; + private static String keystorePath2 = null; + private static String jksPassw2 = null; + private static String keyPassw2 = null; + private static String url2 = null; + private static URL vesurl2 = null; + private static URL vesbatchurl2 = null; + private static HttpURLConnection con2 = null; /* RingBuffer to forward messages on sending AgentDispatcher thread */ private static RingBuffer ringb = new RingBuffer(100); - private static volatile boolean should_stop = false; + private static volatile boolean shouldStop = false; private static Thread thr; @@ -101,19 +115,36 @@ public class AgentMain { * to external Collector */ private static class AgentDispatcher implements Runnable { - - private void send_object(EvelObject tosend){ - AgentMain.sendObjectWithReturn(tosend); + public static int httpResponse = 0; + public static int httpResponse2 = 0; + private void sendObject(EvelObject tosend){ + while(true) { + httpResponse = AgentMain.sendObjectWithReturn(tosend); + if((httpResponse >= 200 && httpResponse < 299) || httpResponse == 400) { + return; + } + httpResponse2 = AgentMain.sendObjectWithReturn2(tosend); + if((httpResponse2 >= 200 && httpResponse2 < 299) || httpResponse2 == 400) { + return; + } + else { + + try { + Thread.sleep(120000); + }catch(Exception ee) { + } + } + } } public void run() { String datatosend=null; - while (!should_stop){ + while (!shouldStop){ EvelObject tosend = ringb.take(); if( tosend != null && ((datatosend = (String) tosend.datastr) != null)) { - send_object(tosend); + sendObject(tosend); } else { @@ -123,8 +154,8 @@ public class AgentMain { } catch (InterruptedException e) { Thread.currentThread().interrupt(); logger.trace("Interrupted on "+url); - System.out.println("Interrupted on "+url); - //e.printStackTrace(); + + } } }//end while @@ -135,14 +166,14 @@ public class AgentMain { EvelObject tosend = ringb.take(); if( tosend != null && ((datatosend = (String) tosend.datastr) != null)) { - send_object(tosend); + sendObject(tosend); } } logger.trace("Send buffer is empty, shutting down"); }//end run }//end AgentDispatcher - public static int doPost(HttpURLConnection con, String dataToSend) throws IOException, UnsupportedEncodingException { + public static int doPost(HttpURLConnection con, String dataToSend) throws IOException { OutputStream os = con.getOutputStream(); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8")); @@ -165,7 +196,7 @@ public class AgentMain { in.close(); } logger.error("Resp: " + builder.toString()); - //System.out.println("Resp: " + builder.toString()); + return builder.toString(); } @@ -183,6 +214,152 @@ public class AgentMain { } } }; + + //Second Collector return object + public static final int sendObjectWithReturn2(EvelObject tosend){ + String datatosend = (String) tosend.datastr; + //process data + logger.trace(url2 + "Got an event size second collector "+datatosend.length()); + logger.trace(datatosend); + int respCode = 0; + Duration timeout = Duration.ofSeconds(2000); + try { + + if( tosend.type == false) + + con2 = (HttpURLConnection) vesurl2.openConnection(); + + else + con2 = (HttpURLConnection) vesbatchurl2.openConnection(); + + if (con2 instanceof HttpsURLConnection) { + + HttpsURLConnection httpsConnection = (HttpsURLConnection) con2; + + try { + + SSLContext sc = SSLContext.getInstance("TLSv1.2"); + /* Get the JKS contents */ + if( !keystorePath2.isEmpty() && !jksPassw2.isEmpty() && !keyPassw2.isEmpty() ) + { + final KeyStore keyStore = KeyStore.getInstance("JKS"); + try (final InputStream is = new FileInputStream(keystorePath2)) { + keyStore.load(is, jksPassw2.toCharArray()); + } + final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory + .getDefaultAlgorithm()); + kmf.init(keyStore, keyPassw2.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, trustAllCerts, new java.security.SecureRandom()); + } + httpsConnection.setDefaultHostnameVerifier(new HostnameVerifier() + { + public boolean verify(String hostname, SSLSession session) + { + return true; + } + }); + httpsConnection.setSSLSocketFactory(sc.getSocketFactory()); + con2 = httpsConnection; + + + } + + catch (final Exception exc) { + logger.error("SSL/TLS connection error"); + } + } + + //add reuqest header + con2.setRequestMethod("POST"); + // No caching, we want the real thing. + + con2.setUseCaches (false); + // Specify the content type. + + con2.setRequestProperty("Content-Type", "application/json"); + con2.setInstanceFollowRedirects( false ); + + //Basic username password authentication + String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass2.getBytes("UTF-8")); + con2.setRequestProperty ("Authorization", basicAuth); + + con2.setReadTimeout(15000 /* milliseconds */); + + con2.setConnectTimeout(15000 /* milliseconds */); + + // Send post request + con2.setDoOutput(true); + con2.setDoInput(true); + + con2.setFixedLengthStreamingMode(datatosend.length()); + + + + try { + respCode = AgentMain.doPost(con2, datatosend); + }catch (ConnectException ie) { + /* Handle the interruption. Or ignore it. */ + + + } + catch (InterruptedByTimeoutException ee) { + /* Handle the error. Or ignore it. */ + + + } + catch (IOException te) { + /* Handle the timeout. Or ignore it. */ + + + } + + + + + logger.trace(url2 + "Connection HTTP Response code :"+respCode); + if(respCode < HttpURLConnection.HTTP_OK ) { + logger.trace(url2 + " **INFO**"); + } + else if(respCode >= HttpURLConnection.HTTP_OK && respCode < HttpURLConnection.HTTP_MULT_CHOICE ) + { + logger.trace(url2 + " **OK**"); + + } + else if(respCode >= HttpURLConnection.HTTP_MULT_CHOICE && respCode < HttpURLConnection.HTTP_BAD_REQUEST ) + { + logger.warn(url2 + " **REDIRECTION**"); + + } + else if(respCode >= HttpURLConnection.HTTP_BAD_REQUEST ) + { + logger.warn(url2 + " **SERVER ERROR**"); + InputStream es = con2.getErrorStream(); + AgentMain.readStream(es); + } + + + return respCode; + } catch (ConnectException e) { + + } catch (IOException e) { + logger.error("Exception during POST", e); + + + return 0; + } + + return respCode; + + } + public static int sendObjectWithReturn(EvelObject tosend){ String datatosend = (String) tosend.datastr; @@ -204,15 +381,15 @@ public class AgentMain { SSLContext sc = SSLContext.getInstance("TLSv1.2"); /* Get the JKS contents */ - if( !keystore_pth.isEmpty() && !jks_passw.isEmpty() && !key_passw.isEmpty() ) + if( !keystorePath.isEmpty() && !jksPassw.isEmpty() && !keyPassw.isEmpty() ) { final KeyStore keyStore = KeyStore.getInstance("JKS"); - try (final InputStream is = new FileInputStream(keystore_pth)) { - keyStore.load(is, jks_passw.toCharArray()); + try (final InputStream is = new FileInputStream(keystorePath)) { + keyStore.load(is, jksPassw.toCharArray()); } final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory .getDefaultAlgorithm()); - kmf.init(keyStore, key_passw.toCharArray()); + kmf.init(keyStore, keyPassw.toCharArray()); final TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory .getDefaultAlgorithm()); tmf.init(keyStore); @@ -235,7 +412,6 @@ public class AgentMain { } catch (final Exception exc) { - exc.printStackTrace(); logger.error("SSL/TLS connection error"); } } @@ -306,7 +482,7 @@ public class AgentMain { * @note This function initializes the Java EVEL library interfaces. * Validates input parameters and starts the AgentDispatcher thread * - * @param event_api_url The API's URL. + * @param eventApiUrl The API's URL. * @param port The API's port. * @param path The optional path (may be NULL). * @param topic The optional topic part of the URL (may be NULL). @@ -319,15 +495,19 @@ public class AgentMain { * @retval ::EVEL_ERR_CODES On failure. *****************************************************************************/ public static EVEL_ERR_CODES evel_initialize( - String event_api_url, + String eventApiUrl, int port, String path, String topic, String username, String password, - String keystore_path, - String jks_password, - String key_password, + String evelKeystorePath, + String evelJksPassword, + String evelKeyPassword, + String eventApiUrl2, + int port2, + String username2, + String password2, Level level) throws IOException { EVEL_ERR_CODES rc = EVEL_ERR_CODES.EVEL_SUCCESS; @@ -339,14 +519,25 @@ public class AgentMain { /***************************************************************************/ /* Check assumptions. */ /***************************************************************************/ - assert(event_api_url != null); + assert(eventApiUrl != null); assert(port > 1024); assert(username != null); + + assert(eventApiUrl2 != null); + assert(port2 > 1024); + assert(username2 != null); logger.setLevel(level); - if( !isValidURL(event_api_url) ){ - System.out.println("Invalid Event API URL"); + if( !isValidURL(eventApiUrl) ){ + + logger.error("Invalid Event API URL"); + rc = EVEL_ERR_CODES.EVEL_ERR_GEN_FAIL; + System.exit(1); + } + + if( !isValidURL(eventApiUrl2) ){ + logger.error("Invalid Event API URL"); rc = EVEL_ERR_CODES.EVEL_ERR_GEN_FAIL; System.exit(1); @@ -358,21 +549,34 @@ public class AgentMain { version += "/example_vnf"; } - keystore_pth = keystore_path; - jks_passw = jks_password; - key_passw = key_password; + keystorePath = evelKeystorePath; + jksPassw = evelJksPassword; + keyPassw = evelKeyPassword; + + keystorePath2 = evelKeystorePath; + jksPassw2 = evelJksPassword; + keyPassw2 = evelKeyPassword; - url = event_api_url+":"+Integer.toString(port)+path+"/eventListener/v"+version; + url = eventApiUrl+":"+Integer.toString(port)+path+"/eventListener/v"+version; vesurl = null; + + url2 = eventApiUrl2+":"+Integer.toString(port2)+path+"/eventListener/v"+version; + vesurl2 = null; try { vesurl = new URL(url); vesbatchurl = new URL(url+"/eventBatch"); + + + vesurl2 = new URL(url2); + vesbatchurl2 = new URL(url2+"/eventBatch"); } catch (MalformedURLException e) { logger.info("Error in url input"); - e.printStackTrace(); + System.exit(1); } userpass = username + ":" + password; + + userpass2 = username2 + ":" + password2; logger.info("Starting Agent Dispatcher thread"); thr = new Thread(new AgentDispatcher()); @@ -384,10 +588,10 @@ public class AgentMain { } public static void evel_shutdown() { - if(should_stop){ + if(shouldStop){ logger.warn("evel_shutdown was called whilst the agent was already stopping - this has no effect"); } - should_stop = true; + shouldStop = true; logger.debug("Called evel_shutdown, will shutdown when the buffer is empty"); } @@ -414,7 +618,7 @@ public class AgentMain { public static boolean evel_post_event(EvelHeader obj) { - if (should_stop){ + if (shouldStop){ logger.error("evel_post_event called whilst agent is shutting down - event will not be posted"); return false; } @@ -439,7 +643,7 @@ public class AgentMain { public static int evel_post_event_immediate(EvelHeader obj) { - if (should_stop){ + if (shouldStop){ logger.error("evel_post_event_immediate called while agent is shutting down - event will not be posted"); return 0; } @@ -465,7 +669,7 @@ public class AgentMain { public static boolean evel_post_event(EvelBatch obj) { - if (should_stop){ + if (shouldStop){ logger.error("evel_post_event called whilst agent is shutting down - event will not be posted"); return false; } |