Changed to unmaintained
[appc.git] / appc-config / appc-config-adaptor / provider / src / main / java / org / onap / appc / ccadaptor / SshJcraftWrapper.java
index d4deb1f..8213387 100644 (file)
@@ -7,7 +7,9 @@
  * 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
@@ -65,13 +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 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;
@@ -103,7 +105,7 @@ public class SshJcraftWrapper {
             "Attempting to connect to " + hostname + " username=" + username + " prompt='"
                 + prompt + "' timeOut=" + timeOut);
         DebugLog.printRTAriDebug(fn, "Trace A");
-        RouterName = hostname;
+        routerName = hostname;
         hostName = hostname;
         userName = username;
         passWord = password;
@@ -119,7 +121,7 @@ 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");
@@ -141,11 +143,11 @@ public class SshJcraftWrapper {
         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;
+        routerName = hostname;
         jsch = getJSch();
         try {
             session = jsch.getSession(username, hostname, portNum);
@@ -162,7 +164,7 @@ 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");
@@ -206,8 +208,8 @@ public class SshJcraftWrapper {
             while (!match) {
                 if (new Date().getTime() > deadline) {
                     DebugLog.printRTAriDebug(fn,
-                        "Throwing a TimedOutException: time in routine has exceed our deadline: RouterName:"
-                            + RouterName + " CmdThatWasSent=" + CmdThatWasSent);
+                        "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 {
@@ -215,12 +217,12 @@ public class SshJcraftWrapper {
                 } catch (java.lang.InterruptedException ee) {
                     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);
+                        "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) {
@@ -234,15 +236,15 @@ public class SshJcraftWrapper {
                             routerFileName = st.nextToken();
                             fileWriter = new FileWriter(routerFileName);
                             out = new BufferedWriter(fileWriter);
-                            routerLogFileName = "/tmp/" + RouterName;
-                            _tmpFile = new File(routerLogFileName);
+                            routerLogFileName = "/tmp/" + routerName;
+                            tmpFile = new File(routerLogFileName);
                             DebugLog.printRTAriDebug(fn,
                                 "Will write the swConfigFile to disk, routerFileName=" + routerFileName);
                         }
                         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>");
@@ -265,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());
                         }
@@ -372,7 +374,6 @@ public class SshJcraftWrapper {
             } catch (IOException e) {
                 DebugLog.printAriDebug(fnName, "Caught an Exception, e=" + e);
                 dbLog.outputStackTrace(e);
-                e.printStackTrace();
             }
         } else {
             // When looking at the end of the charBuffer, don't include any linefeeds or spaces. We only want to make the smallest string possible.
@@ -402,7 +403,6 @@ public class SshJcraftWrapper {
                 } catch (IOException e) {
                     DebugLog.printAriDebug(fnName, "Caught an Exception, e=" + e);
                     dbLog.outputStackTrace(e);
-                    e.printStackTrace();
                 }
             }
         }
@@ -609,8 +609,8 @@ public class SshJcraftWrapper {
                 }
             }
         } catch (IOException e) {
-            System.err.println("writeToFile() exception: " + e);
-            e.printStackTrace();
+            DebugLog.printAriDebug(fnName, "writeToFile() exception:, e=" + e);
+            dbLog.outputStackTrace(e);
         }
     }
 
@@ -625,8 +625,7 @@ public class SshJcraftWrapper {
                 }
             }
         } catch (IOException e) {
-            System.err.println("writeToFile() exception: " + e);
-            e.printStackTrace();
+            dbLog.outputStackTrace(e);
         }
     }
 
@@ -655,7 +654,6 @@ public class SshJcraftWrapper {
 
         int numTokens = rr.countTokens();
         if (numTokens > 1) {
-            rr.nextToken(); //Skip the first line.
             while (rr.hasMoreTokens()) {
                 sb.append(rr.nextToken() + '\n');
             }
@@ -718,7 +716,7 @@ public class SshJcraftWrapper {
     }
 
     public String getRouterName() {
-        return (RouterName);
+        return (routerName);
     }
 
     // Routine does reads until it has read 'nchars' or times out.
@@ -741,11 +739,11 @@ public class SshJcraftWrapper {
                             + ncharsSent + " ncharsTotalReceived=" + ncharsTotalReceived);
                     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;
                 if (ncharsTotalReceived >= ncharsSent) {
                     DebugLog.printRTAriDebug(fn,
@@ -870,7 +868,7 @@ public class SshJcraftWrapper {
         DebugLog.printRTAriDebug(fn,
             ":::Attempting to connect to " + hostname + " username=" + username + " prompt='"
                 + prompt + "' timeOut=" + timeOut + " portNum=" + portNum + " subsystem=" + subsystem);
-        RouterName = hostname;
+        routerName = hostname;
         jsch = getJSch();
         try {
             session = jsch.getSession(username, hostname, portNum);
@@ -887,7 +885,7 @@ public class SshJcraftWrapper {
 
             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....");
@@ -908,7 +906,7 @@ public class SshJcraftWrapper {
         DebugLog.printRTAriDebug(fn,
             "::Attempting to connect to " + hostName + " username=" + username + " portNumber=" + portNumber);
         DebugLog.printRTAriDebug(fn, "Trace C");
-        RouterName = hostName;
+        routerName = hostName;
         this.hostName = hostName;
         userName = username;
         passWord = password;
@@ -927,7 +925,7 @@ 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");