- netconf SSH bridge bundle
[controller.git] / opendaylight / netconf / netconf-it / src / test / java / org / opendaylight / controller / netconf / it / NetconfITTest.java
index 4526cafe26a281024aec21a8a2a818861d2581ad..e5b9fa3ffcd63404d6ec25264da45fcef7da581a 100644 (file)
@@ -77,12 +77,12 @@ import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.xml.sax.SAXException;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.xml.sax.SAXException;
+import static java.util.Collections.emptyList;
 import static junit.framework.Assert.assertEquals;
 import static junit.framework.Assert.assertNotNull;
 import static junit.framework.Assert.assertTrue;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static junit.framework.Assert.assertEquals;
 import static junit.framework.Assert.assertNotNull;
 import static junit.framework.Assert.assertTrue;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
-import static org.mockito.internal.util.Checks.checkNotNull;
 
 public class NetconfITTest extends AbstractConfigTest {
 
 
 public class NetconfITTest extends AbstractConfigTest {
 
@@ -90,7 +90,7 @@ public class NetconfITTest extends AbstractConfigTest {
     //
 
     private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
     //
 
     private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
-    private static final InetSocketAddress sshAddress = new InetSocketAddress("127.0.0.1", 830);
+    private static final InetSocketAddress sshAddress = new InetSocketAddress("127.0.0.1", 10830);
     private static final String USERNAME = "netconf";
     private static final String PASSWORD = "netconf";
 
     private static final String USERNAME = "netconf";
     private static final String PASSWORD = "netconf";
 
@@ -166,10 +166,16 @@ public class NetconfITTest extends AbstractConfigTest {
                 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang",
                 "/META-INF/yang/ietf-inet-types.yang");
         final Collection<InputStream> yangDependencies = new ArrayList<>();
                 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang",
                 "/META-INF/yang/ietf-inet-types.yang");
         final Collection<InputStream> yangDependencies = new ArrayList<>();
+        List<String> failedToFind = new ArrayList<>();
         for (String path : paths) {
         for (String path : paths) {
-            final InputStream is = checkNotNull(NetconfITTest.class.getResourceAsStream(path), path + " not found");
-            yangDependencies.add(is);
+            InputStream resourceAsStream = NetconfITTest.class.getResourceAsStream(path);
+            if (resourceAsStream == null) {
+                failedToFind.add(path);
+            } else {
+                yangDependencies.add(resourceAsStream);
+            }
         }
         }
+        assertEquals("Some yang files were not found",emptyList(), failedToFind);
         return yangDependencies;
     }
 
         return yangDependencies;
     }
 
@@ -453,18 +459,9 @@ public class NetconfITTest extends AbstractConfigTest {
         return netconfClient;
     }
 
         return netconfClient;
     }
 
-    private class TestSSHServer implements Runnable {
-        public void run()  {
-            try {
-                NetconfSSHServer.start();
-            } catch (Exception e) {
-                logger.info(e.getMessage());
-            }
-        }
-    }
     private void startSSHServer() throws Exception{
         logger.info("Creating SSH server");
     private void startSSHServer() throws Exception{
         logger.info("Creating SSH server");
-        Thread sshServerThread = new Thread(new TestSSHServer());
+        Thread sshServerThread = new Thread(NetconfSSHServer.start(10830,tcpAddress));
         sshServerThread.setDaemon(true);
         sshServerThread.start();
         logger.info("SSH server on");
         sshServerThread.setDaemon(true);
         sshServerThread.start();
         logger.info("SSH server on");
@@ -473,18 +470,34 @@ public class NetconfITTest extends AbstractConfigTest {
     @Test
     public void sshTest() throws Exception {
         startSSHServer();
     @Test
     public void sshTest() throws Exception {
         startSSHServer();
+        logger.info("creating connection");
         Connection conn = new Connection(sshAddress.getHostName(),sshAddress.getPort());
         Assert.assertNotNull(conn);
         Connection conn = new Connection(sshAddress.getHostName(),sshAddress.getPort());
         Assert.assertNotNull(conn);
-        try {
-            conn.connect();
-            boolean isAuthenticated = conn.authenticateWithPassword(USERNAME,PASSWORD);
-            assertTrue(isAuthenticated);
-            Session sess = conn.openSession();
-            sess.startSubSystem("netconf");
-//            sess.requestPTY("");
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
+        logger.info("connection created");
+        conn.connect();
+        boolean isAuthenticated = conn.authenticateWithPassword(USERNAME,PASSWORD);
+        assertTrue(isAuthenticated);
+        logger.info("user authenticated");
+        final Session sess = conn.openSession();
+        sess.startSubSystem("netconf");
+        logger.info("user authenticated");
+        sess.getStdin().write(XmlUtil.toString(this.getConfig.getDocument()).getBytes());
+
+        new Thread(){
+           public void run(){
+               while (true){
+                 byte[] bytes = new byte[1024];
+                   int c = 0;
+                   try {
+                       c = sess.getStdout().read(bytes);
+                   } catch (IOException e) {
+                       e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+                   }
+                   logger.info("got data:"+bytes);
+                 if (c == 0) break;
+               }
+           }
+        }.join();
     }
 
 
     }