X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=appc-config%2Fappc-config-adaptor%2Fprovider%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fappc%2Fccadaptor%2FSshJcraftWrapper.java;h=82133876828a78d63f292f004d79cbeb5aeed168;hb=refs%2Fheads%2Fmaster;hp=1f161c3b0de21ace5bac2d02733085f6f8f3ae26;hpb=40bae6d46b1fc0d91da8b16d07b145c86ecf7c0c;p=appc.git diff --git a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java index 1f161c3b0..821338768 100644 --- a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java +++ b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java @@ -2,10 +2,14 @@ * ============LICENSE_START======================================================= * ONAP : APPC * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs - * ============================================================================= + * ================================================================================ + * Modifications Copyright (C) 2018 Ericsson + * ================================================================================ + * Modifications Copyright (c) 2019 IBM + * ================================================================================ * 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 @@ -18,7 +22,6 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. * ============LICENSE_END========================================================= */ @@ -42,7 +45,6 @@ import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; @@ -65,14 +67,13 @@ public class SshJcraftWrapper { private TelnetListener listener = null; private String routerLogFileName = null; private String host = null; - private String RouterName = null; - private int BUFFER_SIZE = 512000; - char[] charBuffer = new char[BUFFER_SIZE]; - // private int BUFFER_SIZE = 4000000; + private String routerName = null; + private int bufferSize = 512000; + char[] charBuffer = new char[bufferSize]; private DataInputStream dis = null; private BufferedReader reader = null; private BufferedWriter out = null; - private File _tmpFile = null; + private File tmpFile = null; private JSch jsch = null; private Session session = null; private Channel channel = null; @@ -90,22 +91,21 @@ public class SshJcraftWrapper { private Runtime runtime = Runtime.getRuntime(); private DebugLog dbLog = new DebugLog(); - public void SshJcraftWrapper() { + public SshJcraftWrapper() { String fn = "SshJcraftWrapper.SshJcraftWrapper"; - debugLog.printRTAriDebug(fn, "SshJcraftWrapper has been instantated"); + DebugLog.printRTAriDebug(fn, "SshJcraftWrapper has been instantated"); routerLogFileName = "/tmp/" + host; - this.host = host; } public void connect(String hostname, String username, String password, String prompt, int timeOut) throws IOException { String fn = "SshJcraftWrapper.connect"; - jsch = new JSch(); - debugLog.printRTAriDebug(fn, - "Attempting to connect to " + hostname + " username=" + username + " password=" + password + " prompt='" + jsch = getJSch(); + DebugLog.printRTAriDebug(fn, + "Attempting to connect to " + hostname + " username=" + username + " prompt='" + prompt + "' timeOut=" + timeOut); - debugLog.printRTAriDebug(fn, "Trace A"); - RouterName = hostname; + DebugLog.printRTAriDebug(fn, "Trace A"); + routerName = hostname; hostName = hostname; userName = username; passWord = password; @@ -121,18 +121,17 @@ public class SshJcraftWrapper { ((ChannelShell) channel).setPtyType("vt102"); inputStream = channel.getInputStream(); dis = new DataInputStream(inputStream); - reader = new BufferedReader(new InputStreamReader(dis), BUFFER_SIZE); + reader = new BufferedReader(new InputStreamReader(dis), bufferSize); channel.connect(); - debugLog.printRTAriDebug(fn, "Successfully connected."); - debugLog.printRTAriDebug(fn, "Flushing input buffer"); + DebugLog.printRTAriDebug(fn, "Successfully connected."); + DebugLog.printRTAriDebug(fn, "Flushing input buffer"); try { receiveUntil(prompt, 3000, "No cmd was sent, just waiting"); } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception: Nothing to flush out."); + DebugLog.printRTAriDebug(fn, "Caught an Exception: Nothing to flush out."); } } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception. e=" + e); - // dbLog.storeData("ErrorMsg= Exception trying to connect to "+hostname +" "+e); + DebugLog.printRTAriDebug(fn, "Caught an Exception. e=" + e); throw new IOException(e.toString()); } } @@ -141,22 +140,22 @@ public class SshJcraftWrapper { public void connect(String hostname, String username, String password, String prompt, int timeOut, int portNum) throws IOException { String fn = "SshJcraftWrapper.connect"; - debugLog.printRTAriDebug(fn, - ":Attempting to connect to " + hostname + " username=" + username + " password=" + password + " prompt='" + DebugLog.printRTAriDebug(fn, + ":Attempting to connect to " + hostname + " username=" + username + " prompt='" + prompt + "' timeOut=" + timeOut + " portNum=" + portNum); - RouterName = hostname; + routerName = hostname; hostName = hostname; userName = username; passWord = password; - RouterName = hostname; - jsch = new JSch(); + routerName = hostname; + jsch = getJSch(); try { session = jsch.getSession(username, hostname, portNum); UserInfo ui = new MyUserInfo(); session.setPassword(password); session.setUserInfo(ui); session.setConfig("StrictHostKeyChecking", "no"); - debugLog.printRTAriDebug(fn, ":StrictHostKeyChecking set to 'no'"); + DebugLog.printRTAriDebug(fn, ":StrictHostKeyChecking set to 'no'"); session.connect(timeOut); session.setServerAliveCountMax( @@ -165,10 +164,10 @@ public class SshJcraftWrapper { ((ChannelShell) channel).setPtyType("vt102"); inputStream = channel.getInputStream(); dis = new DataInputStream(inputStream); - reader = new BufferedReader(new InputStreamReader(dis), BUFFER_SIZE); + reader = new BufferedReader(new InputStreamReader(dis), bufferSize); channel.connect(); - debugLog.printRTAriDebug(fn, ":Successfully connected."); - debugLog.printRTAriDebug(fn, ":Flushing input buffer"); + DebugLog.printRTAriDebug(fn, ":Successfully connected."); + DebugLog.printRTAriDebug(fn, ":Flushing input buffer"); try { if (prompt.equals("]]>]]>")) { receiveUntil("]]>]]>", 10000, "No cmd was sent, just waiting"); @@ -176,13 +175,12 @@ public class SshJcraftWrapper { receiveUntil(":~#", 5000, "No cmd was sent, just waiting"); } } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception::: Nothing to flush out."); + DebugLog.printRTAriDebug(fn, "Caught an Exception::: Nothing to flush out."); } } catch (Exception e) { - debugLog.printRTAriDebug(fn, ":Caught an Exception. e=" + e); + DebugLog.printRTAriDebug(fn, ":Caught an Exception. e=" + e); dbLog.outputStackTrace(e); - // dbLog.storeData("ErrorMsg= Exception trying to connect to "+hostname +" "+e); throw new IOException(e.toString()); } } @@ -195,35 +193,36 @@ public class SshJcraftWrapper { boolean cliPromptCmd = false; StringBuffer sb2 = new StringBuffer(); StringBuffer sbReceive = new StringBuffer(); - debugLog.printRTAriDebug(fn, + DebugLog.printRTAriDebug(fn, "delimeters='" + delimeters + "' timeout=" + timeout + " cmdThatWasSent='" + cmdThatWasSent + "'"); appendToFile(debugLogFileName, fn + " delimeters='" + delimeters + "' timeout=" + timeout + " cmdThatWasSent='" + cmdThatWasSent + "'\n"); String CmdThatWasSent = removeWhiteSpaceAndNewLineCharactersAroundString(cmdThatWasSent); int readCounts = 0; aggregatedReceivedString = ""; + FileWriter fileWriter = null; long deadline = new Date().getTime() + timeout; try { session.setTimeout(timeout); // This is the socket timeout value. while (!match) { if (new Date().getTime() > deadline) { - debugLog.printRTAriDebug(fn, - "Throwing a TimedOutException: time in routine has exceed our deadline: RouterName:" - + RouterName + " CmdThatWasSent=" + CmdThatWasSent); + DebugLog.printRTAriDebug(fn, + "Throwing a TimedOutException: time in routine has exceed our deadline: routerName:" + + routerName + " CmdThatWasSent=" + CmdThatWasSent); throw new TimedOutException("Timeout: time in routine has exceed our deadline"); } try { - Thread.sleep(500); + delay(500); } catch (java.lang.InterruptedException ee) { - boolean ignore = true; + Thread.currentThread().interrupt(); } - int len = reader.read(charBuffer, 0, BUFFER_SIZE); + int len = reader.read(charBuffer, 0, bufferSize); appendToFile(debugLogFileName, fn + " After reader.read cmd: len=" + len + "\n"); if (len <= 0) { - debugLog.printRTAriDebug(fn, - "Reader read " + len + " bytes. Looks like we timed out, router=" + RouterName); - throw new TimedOutException("Received a SocketTimeoutException router=" + RouterName); + DebugLog.printRTAriDebug(fn, + "Reader read " + len + " bytes. Looks like we timed out, router=" + routerName); + throw new TimedOutException("Received a SocketTimeoutException router=" + routerName); } if (!cliPromptCmd) { if (cmdThatWasSent.indexOf("IOS_XR_uploadedSwConfigCmd") != -1) { @@ -231,21 +230,21 @@ public class SshJcraftWrapper { // This is a IOS XR sw config file. We will write it to the disk. timeout = timeout * 2; deadline = new Date().getTime() + timeout; - debugLog.printRTAriDebug(fn, "IOS XR upload for software config: timeout=" + timeout); + DebugLog.printRTAriDebug(fn, "IOS XR upload for software config: timeout=" + timeout); StringTokenizer st = new StringTokenizer(cmdThatWasSent); st.nextToken(); routerFileName = st.nextToken(); - out = new BufferedWriter(new FileWriter(routerFileName)); - routerLogFileName = "/tmp/" + RouterName; - _tmpFile = new File(routerLogFileName); - debugLog.printRTAriDebug(fn, + fileWriter = new FileWriter(routerFileName); + out = new BufferedWriter(fileWriter); + routerLogFileName = "/tmp/" + routerName; + tmpFile = new File(routerLogFileName); + DebugLog.printRTAriDebug(fn, "Will write the swConfigFile to disk, routerFileName=" + routerFileName); } - int c; out.write(charBuffer, 0, len); out.flush(); appendToFile(debugLogFileName, fn + " Wrote " + len + " bytes to the disk\n"); - if (_tmpFile.exists()) { + if (tmpFile.exists()) { appendToRouterFile(routerLogFileName, len); } match = checkIfReceivedStringMatchesDelimeter(len, "\nXML>"); @@ -268,7 +267,7 @@ public class SshJcraftWrapper { sb2.append((char) charBuffer[i]); } } - appendToRouterFile("/tmp/" + RouterName, len); + appendToRouterFile("/tmp/" + routerName, len); if (listener != null) { listener.receivedString(sb2.toString()); } @@ -281,37 +280,28 @@ public class SshJcraftWrapper { break; } } - } else { - debugLog.printRTAriDebug(fn, "cliPromptCmd, Trace 2"); - sb2.setLength(0); - for (int i = 0; i < len; i++) { - sbReceive.append((char) charBuffer[i]); - sb2.append((char) charBuffer[i]); - } - appendToRouterFile("/tmp/" + RouterName, sb2); - if (listener != null) { - listener.receivedString(sb2.toString()); - } - debugLog.printRTAriDebug(fn, "sb2='" + sb2.toString() + "' delimeters='" + delimeters + "'"); - if (sb2.toString().indexOf("\nariPrompt>") != -1) { - debugLog.printRTAriDebug(fn, "Found our prompt"); - match = true; - break; - } } } } catch (JSchException e) { - debugLog.printRTAriDebug(fn, "Caught an JSchException e=" + e.toString()); + DebugLog.printRTAriDebug(fn, "Caught an JSchException e=" + e.toString()); dbLog.outputStackTrace(e); throw new TimedOutException(e.toString()); } catch (IOException ee) { - debugLog.printRTAriDebug(fn, "Caught an IOException: ee=" + ee.toString()); + DebugLog.printRTAriDebug(fn, "Caught an IOException: ee=" + ee.toString()); dbLog.outputStackTrace(ee); throw new TimedOutException(ee.toString()); + } finally { + try { + if (fileWriter != null) { + fileWriter.close(); + } + } catch(IOException ex) { + DebugLog.printRTAriDebug(fn, "Failed to close fileWriter output stream: ex=" + ex); + } } String result = stripOffCmdFromRouterResponse(sbReceive.toString()); - debugLog.printRTAriDebug(fn, "Leaving method successfully"); - return (result); + DebugLog.printRTAriDebug(fn, "Leaving method successfully"); + return result; } public boolean checkIfReceivedStringMatchesDelimeter(String delimeters, String receivedString, @@ -332,11 +322,7 @@ public class SshJcraftWrapper { appendToFile(debugLogFileName, fn + " :::cmdThatWasSent='" + cmdThatWasSent + "' x=" + x + " y=" + y + "\n"); } - if ((x != -1) && (y == x)) { - return (true); - } else { - return (false); - } + return (x != -1) && (y == x); } if (cmdThatWasSent.indexOf("show config") != -1) { appendToFile(debugLogFileName, fn + "In the block for 'show config'\n"); @@ -363,7 +349,7 @@ public class SshJcraftWrapper { appendToFile(debugLogFileName, fn + " Looking for an delimeter of:'" + delimeter + "'\n"); appendToFile(debugLogFileName, fn + " receivedString='" + receivedString); if (aggregatedReceivedString.indexOf(delimeter) != -1) { - debugLog.printRTAriDebug(fn, "Found our delimeter, which was: '" + delimeter + "'"); + DebugLog.printRTAriDebug(fn, "Found our delimeter, which was: '" + delimeter + "'"); aggregatedReceivedString = ""; return (true); } @@ -388,10 +374,8 @@ public class SshJcraftWrapper { } catch (IOException e) { DebugLog.printAriDebug(fnName, "Caught an Exception, e=" + e); dbLog.outputStackTrace(e); - e.printStackTrace(); } } else { - // DebugLog.printAriDebug(fnName, "TRACE 1: ******************************"); // When looking at the end of the charBuffer, don't include any linefeeds or spaces. We only want to make the smallest string possible. for (x = len - 1; x >= 0; x--) { c = charBuffer[x]; @@ -419,7 +403,6 @@ public class SshJcraftWrapper { } catch (IOException e) { DebugLog.printAriDebug(fnName, "Caught an Exception, e=" + e); dbLog.outputStackTrace(e); - e.printStackTrace(); } } } @@ -438,7 +421,7 @@ public class SshJcraftWrapper { public void closeConnection() { String fn = "SshJcraftWrapper.closeConnection"; - debugLog.printRTAriDebug(fn, "Executing the closeConnection...."); + DebugLog.printRTAriDebug(fn, "Executing the closeConnection...."); inputStream = null; outputStream = null; dis = null; @@ -450,8 +433,7 @@ public class SshJcraftWrapper { public void send(String cmd) throws IOException { String fn = "SshJcraftWrapper.send"; OutputStream out = channel.getOutputStream(); - DataOutputStream dos = new DataOutputStream(out); - + DataOutputStream dos = getDataOutputStream(out); if ((cmd.charAt(cmd.length() - 1) != '\n') && (cmd.charAt(cmd.length() - 1) != '\r')) { cmd += "\n"; } @@ -462,8 +444,7 @@ public class SshJcraftWrapper { int ncharsSent = 0; appendToFile(debugLogFileName, fn + ": Sending: '" + cmd); - // debugLog.printRTAriDebug (fn, "cmd = "+cmd); - debugLog.printRTAriDebug(fn, "Length of cmd is:" + length); // 2,937,706 + DebugLog.printRTAriDebug(fn, "Length of cmd is:" + length); // 2,937,706 try { if (length > 600000) { int timeout = 9000; @@ -471,30 +452,30 @@ public class SshJcraftWrapper { String Cmd = cmd.substring(i, Math.min(length, i + nchars)); ncharsSent = Cmd.length(); ncharsTotalSent = ncharsTotalSent + Cmd.length(); - debugLog.printRTAriDebug(fn, "i=" + i + " Sending Cmd: ncharsSent=" + ncharsSent); + DebugLog.printRTAriDebug(fn, "i=" + i + " Sending Cmd: ncharsSent=" + ncharsSent); dos.writeBytes(Cmd); dos.flush(); try { - debugLog.printRTAriDebug(fn, ":::i=" + i + " length=" + length); + DebugLog.printRTAriDebug(fn, ":::i=" + i + " length=" + length); if (ncharsSent < length) { receiveUntilBufferFlush(ncharsSent, timeout, "buffer flush i=" + i); } else { - debugLog.printRTAriDebug(fn, "i=" + i + " No Waiting this time...."); + DebugLog.printRTAriDebug(fn, "i=" + i + " No Waiting this time...."); dos.flush(); } } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception: Nothing to flush out."); + DebugLog.printRTAriDebug(fn, "Caught an Exception: Nothing to flush out."); } } } else { - debugLog.printRTAriDebug(fn, "Before executing the dos.writeBytes"); + DebugLog.printRTAriDebug(fn, "Before executing the dos.writeBytes"); dos.writeBytes(cmd); } dos.flush(); - debugLog.printRTAriDebug(fn, "Leaving method"); + DebugLog.printRTAriDebug(fn, "Leaving method"); appendToFile(debugLogFileName, fn + ": Leaving method\n"); } catch (IOException e) { - debugLog.printRTAriDebug(fn, "Caught an IOException. e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an IOException. e=" + e); dbLog.outputStackTrace(e); throw new IOException(e.toString()); } @@ -504,13 +485,13 @@ public class SshJcraftWrapper { public void sendChar(int v) throws IOException { String fn = "SshJcraftWrapper.sendChar"; OutputStream out = channel.getOutputStream(); - DataOutputStream dos = new DataOutputStream(out); + DataOutputStream dos = getDataOutputStream(out); try { - debugLog.printRTAriDebug(fn, "Sending: '" + v + "'"); + DebugLog.printRTAriDebug(fn, "Sending: '" + v + "'"); dos.writeChar(v); dos.flush(); } catch (IOException e) { - debugLog.printRTAriDebug(fn, "Caught an IOException. e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an IOException. e=" + e); throw new IOException(e.toString()); } } @@ -518,12 +499,12 @@ public class SshJcraftWrapper { public void send(byte[] b, int off, int len) throws IOException { String fn = "SshJcraftWrapper.send:byte[]"; OutputStream out = channel.getOutputStream(); - DataOutputStream dos = new DataOutputStream(out); + DataOutputStream dos = getDataOutputStream(out); try { dos.write(b, off, len); dos.flush(); } catch (IOException e) { - debugLog.printRTAriDebug(fn, "Caught an IOException. e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an IOException. e=" + e); throw new IOException(e.toString()); } } @@ -574,16 +555,14 @@ public class SshJcraftWrapper { // write to it. If it does not exist, don't write to it. File tmpFile = new File(fileName); if (tmpFile.exists()) { - BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true)); - // out.write(dataToWrite); - // out.write(getTheDate() +": " +Thread.currentThread().getName() +": "+dataToWrite); - out.write(getTheDate() + ": " + tId + ": " + dataToWrite); - out.close(); + try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) { + out.write(getTheDate() + ": " + tId + ": " + dataToWrite); + } } } catch (IOException e) { - debugLog.printRTAriDebug(fn, "Caught an IOException: e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an IOException: e=" + e); } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception: e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an Exception: e=" + e); } } @@ -595,14 +574,14 @@ public class SshJcraftWrapper { // write to it. If it does not exist, don't write to it. File tmpFile = new File(fileName); if (tmpFile.exists()) { - BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true)); - out.write(dataToWrite); - out.close(); + try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) { + out.write(dataToWrite); + } } } catch (IOException e) { - debugLog.printRTAriDebug(fn, "Caught an IOException: e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an IOException: e=" + e); } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception: e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an Exception: e=" + e); } } @@ -610,7 +589,6 @@ public class SshJcraftWrapper { public String getTheDate() { Calendar cal = Calendar.getInstance(); java.util.Date today = cal.getTime(); - DateFormat df1 = DateFormat.getDateInstance(); DateFormat df3 = new SimpleDateFormat("MM/dd/yyyy H:mm:ss "); return (df3.format(today)); } @@ -618,43 +596,36 @@ public class SshJcraftWrapper { public void appendToRouterFile(String fileName, StringBuffer dataToWrite) { String fnName = "SshJcraftWrapper.appendToRouterFile"; - debugLog.printRTAriDebug(fnName, "Entered.... "); + DebugLog.printRTAriDebug(fnName, "Entered.... "); try { // First check to see if a file 'fileName' exist, if it does // write to it. If it does not exist, don't write to it. File tmpFile = new File(fileName); { - // if ((tmpFile.exists()) && (tmpFile.setWritable(true, true))) if (tmpFile.exists()) { - BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true)); - // out.write("\n"); - out.write(dataToWrite.toString()); - out.close(); + try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) { + out.write(dataToWrite.toString()); + } } } } catch (IOException e) { - System.err.println("writeToFile() exception: " + e); - e.printStackTrace(); + DebugLog.printAriDebug(fnName, "writeToFile() exception:, e=" + e); + dbLog.outputStackTrace(e); } } public void appendToRouterFile(String fileName, int len) { - String fnName = "SshJcraftWrapper.appendToFile"; - // debugLog.printRTAriDebug (fnName, "Entered.... len="+len); try { // First check to see if a file 'fileName' exist, if it does // write to it. If it does not exist, don't write to it. File tmpFile = new File(fileName); - // if ((tmpFile.exists()) && (tmpFile.setWritable(true, true))) if (tmpFile.exists()) { - BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true)); - // out.write("\n"); - out.write(charBuffer, 0, len); - out.close(); + try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) { + out.write(charBuffer, 0, len); + } } } catch (IOException e) { - System.err.println("writeToFile() exception: " + e); - e.printStackTrace(); + dbLog.outputStackTrace(e); } } @@ -674,20 +645,15 @@ public class SshJcraftWrapper { } public String stripOffCmdFromRouterResponse(String routerResponse) { - String fn = "SshJcraftWrapper.stripOffCmdFromRouterResponse"; - // appendToFile(debugLogFileName, fn+": routerResponse='"+routerResponse +"'\n"); - // The session of SSH will echo the command sent to the router, in the router's response. // Since all our commands are terminated by a '\n', strip off the first line - // of the response from the router. This first line contains the orginal command. + // of the response from the router. This first line contains the original command. StringTokenizer rr = new StringTokenizer(routerResponse, "\n"); StringBuffer sb = new StringBuffer(); int numTokens = rr.countTokens(); - // debugLog.printRTAriDebug (fn, "Number of lines in the response from the router is:" +numTokens); if (numTokens > 1) { - rr.nextToken(); //Skip the first line. while (rr.hasMoreTokens()) { sb.append(rr.nextToken() + '\n'); } @@ -698,36 +664,35 @@ public class SshJcraftWrapper { public void setRouterCommandType(String type) { String fn = "SshJcraftWrapper.setRouterCommandType"; this.routerCmdType = type; - debugLog.printRTAriDebug(fn, "Setting routerCmdType to a value of '" + type + "'"); + DebugLog.printRTAriDebug(fn, "Setting routerCmdType to a value of '" + type + "'"); } - public String getLastFewLinesOfFile(File file, int linesToRead) throws FileNotFoundException, IOException { + public String getLastFewLinesOfFile(File file, int linesToRead) throws IOException { String fn = "SshJcraftWrapper.getLastFewLinesOfFile"; - RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"); - int lines = 0; - StringBuilder builder = new StringBuilder(); String tail = ""; - long length = file.length(); - length--; - randomAccessFile.seek(length); - for (long seek = length; seek >= 0; --seek) { - randomAccessFile.seek(seek); - char c = (char) randomAccessFile.read(); - builder.append(c); - if (c == '\n') { - builder = builder.reverse(); - // System.out.println(builder.toString()); - tail = builder.toString() + tail; - lines++; - builder.setLength(0); - if (lines == linesToRead) { - break; + try(RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) { + int lines = 0; + StringBuilder builder = new StringBuilder(); + long length = file.length(); + length--; + randomAccessFile.seek(length); + for (long seek = length; seek >= 0; --seek) { + randomAccessFile.seek(seek); + char c = (char) randomAccessFile.read(); + builder.append(c); + if (c == '\n') { + builder = builder.reverse(); + tail = builder.toString() + tail; + lines++; + builder.setLength(0); + if (lines == linesToRead) { + break; + } } } } - randomAccessFile.close(); if (!jcraftReadSwConfigFileFromDisk()) { - debugLog.printRTAriDebug(fn, "tail='" + tail + "'"); + DebugLog.printRTAriDebug(fn, "tail='" + tail + "'"); } appendToFile(debugLogFileName, "tail='" + tail + "'\n"); return tail; @@ -751,19 +716,16 @@ public class SshJcraftWrapper { } public String getRouterName() { - return (RouterName); + return (routerName); } // Routine does reads until it has read 'nchars' or times out. public void receiveUntilBufferFlush(int ncharsSent, int timeout, String message) throws TimedOutException, IOException { String fn = "SshJcraftWrapper.receiveUntilBufferFlush"; - StringBuffer sb2 = new StringBuffer(); - StringBuffer sbReceive = new StringBuffer(); - debugLog.printRTAriDebug(fn, "ncharsSent=" + ncharsSent + " timeout=" + timeout + " " + message); + DebugLog.printRTAriDebug(fn, "ncharsSent=" + ncharsSent + " timeout=" + timeout + " " + message); int ncharsTotalReceived = 0; int ncharsRead = 0; - boolean flag = false; charactersFromBufferFlush.setLength(0); long deadline = new Date().getTime() + timeout; @@ -772,21 +734,19 @@ public class SshJcraftWrapper { session.setTimeout(timeout); // This is the socket timeout value. while (true) { if (new Date().getTime() > deadline) { - debugLog.printRTAriDebug(fn, + DebugLog.printRTAriDebug(fn, "Throwing a TimedOutException: time in routine has exceed our deadline: ncharsSent=" + ncharsSent + " ncharsTotalReceived=" + ncharsTotalReceived); - flag = true; throw new TimedOutException("Timeout: time in routine has exceed our deadline"); } - ncharsRead = reader.read(charBuffer, 0, BUFFER_SIZE); + ncharsRead = reader.read(charBuffer, 0, bufferSize); if (listener != null) { listener.receivedString(String.copyValueOf(charBuffer, 0, ncharsRead)); } - appendToRouterFile("/tmp/" + RouterName, ncharsRead); + appendToRouterFile("/tmp/" + routerName, ncharsRead); ncharsTotalReceived = ncharsTotalReceived + ncharsRead; - // debugLog.printRTAriDebug (fn, "::ncharsSent="+ncharsSent+" ncharsTotalReceived="+ncharsTotalReceived +" ncharsRead="+ncharsRead); if (ncharsTotalReceived >= ncharsSent) { - debugLog.printRTAriDebug(fn, + DebugLog.printRTAriDebug(fn, "Received the correct number of characters, ncharsSent=" + ncharsSent + " ncharsTotalReceived=" + ncharsTotalReceived); logMemoryUsage(); @@ -794,8 +754,8 @@ public class SshJcraftWrapper { } } } catch (JSchException e) { - debugLog.printRTAriDebug(fn, "Caught an JSchException e=" + e.toString()); - debugLog.printRTAriDebug(fn, + DebugLog.printRTAriDebug(fn, "Caught an JSchException e=" + e); + DebugLog.printRTAriDebug(fn, "ncharsSent=" + ncharsSent + " ncharsTotalReceived=" + ncharsTotalReceived + " ncharsRead=" + ncharsRead); throw new TimedOutException(e.toString()); @@ -815,26 +775,8 @@ public class SshJcraftWrapper { } public void sftpPut(String sourcePath, String destDirectory) throws IOException { - String fn = "SshJcraftWrapper.sftp"; - try { - Session sftpSession = jsch.getSession(userName, hostName, 22); - UserInfo ui = new MyUserInfo(); - sftpSession.setPassword(passWord); - sftpSession.setUserInfo(ui); - sftpSession.connect(30 * 1000); - debugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); - ChannelSftp sftp = (ChannelSftp) sftpSession.openChannel("sftp"); - debugLog.printRTAriDebug(fn, "Connecting...."); - sftp.connect(); - debugLog.printRTAriDebug(fn, "Sending " + sourcePath + " --> " + destDirectory); - sftp.put(sourcePath, destDirectory, ChannelSftp.OVERWRITE); - debugLog.printRTAriDebug(fn, "Sent successfully"); - sftpSession.disconnect(); - } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); - // dbLog.storeData("ErrorMsg= sftp threw an Exception. error is:"+e); - throw new IOException(e.toString()); - } + // delegate to duplicate method + put(sourcePath, destDirectory); } @@ -846,18 +788,17 @@ public class SshJcraftWrapper { sftpSession.setPassword(passWord); sftpSession.setUserInfo(ui); sftpSession.connect(30 * 1000); - debugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); + DebugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); ChannelSftp sftp = (ChannelSftp) sftpSession.openChannel("sftp"); - debugLog.printRTAriDebug(fn, "Connecting...."); + DebugLog.printRTAriDebug(fn, "Connecting...."); sftp.connect(); InputStream is = new ByteArrayInputStream(stringOfData.getBytes()); - debugLog.printRTAriDebug(fn, "Sending stringOfData --> " + fullPathDest); + DebugLog.printRTAriDebug(fn, "Sending stringOfData --> " + fullPathDest); sftp.put(is, fullPathDest, ChannelSftp.OVERWRITE); - debugLog.printRTAriDebug(fn, "Sent successfully"); + DebugLog.printRTAriDebug(fn, "Sent successfully"); sftpSession.disconnect(); } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); - // dbLog.storeData("ErrorMsg= sftp threw an Exception. error is:"+e); + DebugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); throw new IOException(e.toString()); } } @@ -870,20 +811,18 @@ public class SshJcraftWrapper { sftpSession.setPassword(passWord); sftpSession.setUserInfo(ui); sftpSession.connect(30 * 1000); - debugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); + DebugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); ChannelSftp sftp = (ChannelSftp) sftpSession.openChannel("sftp"); - debugLog.printRTAriDebug(fn, "Connecting...."); + DebugLog.printRTAriDebug(fn, "Connecting...."); sftp.connect(); InputStream in = null; in = sftp.get(fullFilePathName); String sftpFileString = readInputStreamAsString(in); - debugLog.printRTAriDebug(fn, "Retreived successfully"); - // debugLog.printRTAriDebug (fn, "sftpFileString="+sftpFileString); + DebugLog.printRTAriDebug(fn, "Retreived successfully"); sftpSession.disconnect(); return (sftpFileString); } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); - // dbLog.storeData("ErrorMsg= sftp threw an Exception. error is:"+e); + DebugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); throw new IOException(e.toString()); } } @@ -926,11 +865,11 @@ public class SshJcraftWrapper { String subsystem) throws IOException { String fn = "SshJcraftWrapper.connect"; - debugLog.printRTAriDebug(fn, - ":::Attempting to connect to " + hostname + " username=" + username + " password=" + password + " prompt='" + DebugLog.printRTAriDebug(fn, + ":::Attempting to connect to " + hostname + " username=" + username + " prompt='" + prompt + "' timeOut=" + timeOut + " portNum=" + portNum + " subsystem=" + subsystem); - RouterName = hostname; - jsch = new JSch(); + routerName = hostname; + jsch = getJSch(); try { session = jsch.getSession(username, hostname, portNum); UserInfo ui = new MyUserInfo(); @@ -942,34 +881,32 @@ public class SshJcraftWrapper { 0); // If this is not set to '0', then socket timeout on all reads will not work!!!! channel = session.openChannel("subsystem"); ((ChannelSubsystem) channel).setSubsystem(subsystem); - // ((ChannelSubsystem)channel).setPtyType("vt102"); ((ChannelSubsystem) channel).setPty(true); inputStream = channel.getInputStream(); dis = new DataInputStream(inputStream); - reader = new BufferedReader(new InputStreamReader(dis), BUFFER_SIZE); + reader = new BufferedReader(new InputStreamReader(dis), bufferSize); channel.connect(); - debugLog.printRTAriDebug(fn, "Successfully connected."); - debugLog.printRTAriDebug(fn, "Five second sleep...."); + DebugLog.printRTAriDebug(fn, "Successfully connected."); + DebugLog.printRTAriDebug(fn, "Five second sleep...."); try { - Thread.sleep(5000); + delay(5000); } catch (java.lang.InterruptedException ee) { - boolean ignore = true; + Thread.currentThread().interrupt(); } } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception. e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an Exception. e=" + e); throw new IOException(e.toString()); } } public void connect(String hostName, String username, String password, int portNumber) throws IOException { String fn = "SshJcraftWrapper.connect"; - jsch = new JSch(); - debugLog.printRTAriDebug(fn, - "::Attempting to connect to " + hostName + " username=" + username + " password=" + password - + " portNumber=" + portNumber); - debugLog.printRTAriDebug(fn, "Trace C"); - RouterName = hostName; + jsch = getJSch(); + DebugLog.printRTAriDebug(fn, + "::Attempting to connect to " + hostName + " username=" + username + " portNumber=" + portNumber); + DebugLog.printRTAriDebug(fn, "Trace C"); + routerName = hostName; this.hostName = hostName; userName = username; passWord = password; @@ -977,7 +914,6 @@ public class SshJcraftWrapper { java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session = jsch.getSession(username, hostName, 22); - // session = jsch.getSession(username, hostName, portNumber); UserInfo ui = new MyUserInfo(); session.setConfig(config); session.setPassword(password); @@ -989,19 +925,18 @@ public class SshJcraftWrapper { ((ChannelShell) channel).setPtyType("vt102"); inputStream = channel.getInputStream(); dis = new DataInputStream(inputStream); - reader = new BufferedReader(new InputStreamReader(dis), BUFFER_SIZE); + reader = new BufferedReader(new InputStreamReader(dis), bufferSize); channel.connect(); - debugLog.printRTAriDebug(fn, "::Successfully connected."); - debugLog.printRTAriDebug(fn, "::Flushing input buffer"); + DebugLog.printRTAriDebug(fn, "::Successfully connected."); + DebugLog.printRTAriDebug(fn, "::Flushing input buffer"); try { receiveUntil(":~#", 9000, "No cmd was sent, just waiting, but we can stop on a '~#'"); } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception::: Nothing to flush out."); + DebugLog.printRTAriDebug(fn, "Caught an Exception::: Nothing to flush out."); } } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception. e=" + e); - // dbLog.storeData("ErrorMsg= Exception trying to connect to "+hostName +" "+e); + DebugLog.printRTAriDebug(fn, "Caught an Exception. e=" + e); throw new IOException(e.toString()); } } @@ -1015,17 +950,16 @@ public class SshJcraftWrapper { sftpSession.setPassword(passWord); sftpSession.setUserInfo(ui); sftpSession.connect(30 * 1000); - debugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); + DebugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); ChannelSftp sftp = (ChannelSftp) sftpSession.openChannel("sftp"); - debugLog.printRTAriDebug(fn, "Connecting...."); + DebugLog.printRTAriDebug(fn, "Connecting...."); sftp.connect(); - debugLog.printRTAriDebug(fn, "Sending " + sourcePath + " --> " + destDirectory); + DebugLog.printRTAriDebug(fn, "Sending " + sourcePath + " --> " + destDirectory); sftp.put(sourcePath, destDirectory, ChannelSftp.OVERWRITE); - debugLog.printRTAriDebug(fn, "Sent successfully"); + DebugLog.printRTAriDebug(fn, "Sent successfully"); sftpSession.disconnect(); } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); - // dbLog.storeData("ErrorMsg= sftp threw an Exception. error is:"+e); + DebugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); throw new IOException(e.toString()); } } @@ -1035,8 +969,8 @@ public class SshJcraftWrapper { String fn = "SshJcraftWrapper.put"; Session sftpSession = null; try { - debugLog.printRTAriDebug(fn, "userName=" + userName + " hostName=" + hostName + " passWord=" + passWord); - jsch = new JSch(); + DebugLog.printRTAriDebug(fn, "userName=" + userName + " hostName=" + hostName); + jsch = getJSch(); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); sftpSession = jsch.getSession(userName, hostName, 22); @@ -1045,29 +979,28 @@ public class SshJcraftWrapper { sftpSession.setUserInfo(ui); sftpSession.setConfig(config); sftpSession.connect(30 * 1000); - debugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); + DebugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); ChannelSftp sftp = (ChannelSftp) sftpSession.openChannel("sftp"); - debugLog.printRTAriDebug(fn, "Connecting...."); + DebugLog.printRTAriDebug(fn, "Connecting...."); sftp.connect(); String oldFiles = fullPathDest + "*"; - debugLog.printRTAriDebug(fn, "Deleting old files --> " + oldFiles); + DebugLog.printRTAriDebug(fn, "Deleting old files --> " + oldFiles); try { sftp.rm(oldFiles); - debugLog.printRTAriDebug(fn, "Sending stringOfData --> " + fullPathDest); + DebugLog.printRTAriDebug(fn, "Sending stringOfData --> " + fullPathDest); } catch (SftpException sft) { String exp = "No such file"; if (sft.getMessage() != null && sft.getMessage().contains(exp)) { - debugLog.printRTAriDebug(fn, "No files found -- Continue"); + DebugLog.printRTAriDebug(fn, "No files found -- Continue"); } else { - debugLog.printRTAriDebug(fn, "Exception while sftp.rm " + sft.getMessage()); - sft.printStackTrace(); + DebugLog.printRTAriDebug(fn, "Exception while sftp.rm " + sft.getMessage()); throw sft; } } sftp.put(is, fullPathDest, ChannelSftp.OVERWRITE); - debugLog.printRTAriDebug(fn, "Sent successfully"); + DebugLog.printRTAriDebug(fn, "Sent successfully"); } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); throw new IOException(e.toString()); } finally { if(sftpSession != null) { @@ -1081,8 +1014,8 @@ public class SshJcraftWrapper { String fn = "SshJcraftWrapper.get"; Session sftpSession = null; try { - debugLog.printRTAriDebug(fn, "userName=" + userName + " hostName=" + hostName + " passWord=" + passWord); - jsch = new JSch(); + DebugLog.printRTAriDebug(fn, "userName=" + userName + " hostName=" + hostName); + jsch = getJSch(); sftpSession = jsch.getSession(userName, hostName, 22); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); @@ -1091,16 +1024,16 @@ public class SshJcraftWrapper { sftpSession.setUserInfo(ui); sftpSession.setConfig(config); sftpSession.connect(30 * 1000); - debugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); + DebugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); ChannelSftp sftp = (ChannelSftp) sftpSession.openChannel("sftp"); - debugLog.printRTAriDebug(fn, "Connecting...."); + DebugLog.printRTAriDebug(fn, "Connecting...."); sftp.connect(); InputStream in = sftp.get(fullFilePathName); String sftpFileString = readInputStreamAsString(in); - debugLog.printRTAriDebug(fn, "Retreived successfully"); + DebugLog.printRTAriDebug(fn, "Retreived successfully"); return sftpFileString; } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); throw new IOException(e.toString()); } finally { if(sftpSession != null) { @@ -1112,7 +1045,7 @@ public class SshJcraftWrapper { public String send(String cmd, String delimiter) throws IOException { String fn = "SshJcraftWrapper.send"; OutputStream out = channel.getOutputStream(); - DataOutputStream dos = new DataOutputStream(out); + DataOutputStream dos = getDataOutputStream(out); if ((cmd.charAt(cmd.length() - 1) != '\n') && (cmd.charAt(cmd.length() - 1) != '\r')) { cmd += "\n"; @@ -1123,8 +1056,7 @@ public class SshJcraftWrapper { int ncharsTotalSent = 0; int ncharsSent = 0; - debugLog.printRTAriDebug(fn, "Length of cmd is:" + length); // 2,937,706 - debugLog.printRTAriDebug(fn, "Length of cmd is:" + length); // 2,937,706 + DebugLog.printRTAriDebug(fn, "Length of cmd is:" + length); // 2,937,706 try { if (length > 600000) { int timeout = 9000; @@ -1132,35 +1064,45 @@ public class SshJcraftWrapper { String Cmd = cmd.substring(i, Math.min(length, i + nchars)); ncharsSent = Cmd.length(); ncharsTotalSent = ncharsTotalSent + Cmd.length(); - debugLog.printRTAriDebug(fn, "i=" + i + " Sending Cmd: ncharsSent=" + ncharsSent); + DebugLog.printRTAriDebug(fn, "i=" + i + " Sending Cmd: ncharsSent=" + ncharsSent); dos.writeBytes(Cmd); dos.flush(); try { - debugLog.printRTAriDebug(fn, ":::i=" + i + " length=" + length); + DebugLog.printRTAriDebug(fn, ":::i=" + i + " length=" + length); if (ncharsSent < length) { receiveUntilBufferFlush(ncharsSent, timeout, "buffer flush i=" + i); } else { - debugLog.printRTAriDebug(fn, "i=" + i + " No Waiting this time...."); + DebugLog.printRTAriDebug(fn, "i=" + i + " No Waiting this time...."); dos.flush(); } } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception: Nothing to flush out."); + DebugLog.printRTAriDebug(fn, "Caught an Exception: Nothing to flush out."); } } } else { - debugLog.printRTAriDebug(fn, "Before executing the dos.writeBytes"); + DebugLog.printRTAriDebug(fn, "Before executing the dos.writeBytes"); dos.writeBytes(cmd); } dos.flush(); // Now lets get the response. String response = receiveUntil(delimiter, 300000, cmd); - debugLog.printRTAriDebug(fn, "Leaving method"); + DebugLog.printRTAriDebug(fn, "Leaving method"); return (response); } catch (IOException e) { - debugLog.printRTAriDebug(fn, "Caught an IOException. e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an IOException. e=" + e); throw new IOException(e.toString()); } } + protected JSch getJSch() { + return new JSch(); + } + protected DataOutputStream getDataOutputStream(OutputStream out) { + return new DataOutputStream(out); + } + + protected void delay(int milliseconds) throws InterruptedException { + Thread.sleep(5000); + } }