Merge "Specify type to NetconfClientDispatcher reference"
[netconf.git] / netconf / netconf-ssh / src / main / java / org / opendaylight / netconf / ssh / SshProxyServer.java
index 988ba35854cf678a6233dd4aaebcddb3fe20d85a..9fa4bb33ab5592691bb2c08c028aaca116fdea53 100644 (file)
@@ -12,16 +12,20 @@ import com.google.common.collect.ImmutableList;
 import io.netty.channel.EventLoopGroup;
 import java.io.IOException;
 import java.nio.channels.AsynchronousChannelGroup;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
+import org.apache.sshd.SshServer;
+import org.apache.sshd.common.Cipher;
 import org.apache.sshd.common.FactoryManager;
 import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.RuntimeSshException;
-import org.apache.sshd.common.cipher.BuiltinCiphers;
-import org.apache.sshd.common.cipher.Cipher;
+import org.apache.sshd.common.cipher.ARCFOUR128;
+import org.apache.sshd.common.cipher.ARCFOUR256;
 import org.apache.sshd.common.io.IoAcceptor;
 import org.apache.sshd.common.io.IoConnector;
 import org.apache.sshd.common.io.IoHandler;
@@ -30,10 +34,9 @@ import org.apache.sshd.common.io.IoServiceFactoryFactory;
 import org.apache.sshd.common.io.nio2.Nio2Acceptor;
 import org.apache.sshd.common.io.nio2.Nio2Connector;
 import org.apache.sshd.common.io.nio2.Nio2ServiceFactoryFactory;
-import org.apache.sshd.common.util.closeable.AbstractCloseable;
+import org.apache.sshd.common.util.CloseableUtils;
+import org.apache.sshd.server.PasswordAuthenticator;
 import org.apache.sshd.server.ServerFactoryManager;
-import org.apache.sshd.server.SshServer;
-import org.apache.sshd.server.auth.password.PasswordAuthenticator;
 import org.apache.sshd.server.session.ServerSession;
 
 /**
@@ -42,6 +45,8 @@ import org.apache.sshd.server.session.ServerSession;
  */
 public class SshProxyServer implements AutoCloseable {
 
+    private static final ARCFOUR128.Factory DEFAULT_ARCFOUR128_FACTORY = new ARCFOUR128.Factory();
+    private static final ARCFOUR256.Factory DEFAULT_ARCFOUR256_FACTORY = new ARCFOUR256.Factory();
     private final SshServer sshServer;
     private final ScheduledExecutorService minaTimerExecutor;
     private final EventLoopGroup clientGroup;
@@ -62,8 +67,8 @@ public class SshProxyServer implements AutoCloseable {
         final List<NamedFactory<Cipher>> cipherFactories = sshServer.getCipherFactories();
         for (Iterator<NamedFactory<Cipher>> i = cipherFactories.iterator(); i.hasNext(); ) {
             final NamedFactory<Cipher> factory = i.next();
-            if (factory.getName().contains(BuiltinCiphers.arcfour128.getName())
-                    || factory.getName().contains(BuiltinCiphers.arcfour256.getName())) {
+            if (factory.getName().contains(DEFAULT_ARCFOUR128_FACTORY.getName())
+                    || factory.getName().contains(DEFAULT_ARCFOUR256_FACTORY.getName())) {
                 i.remove();
             }
         }
@@ -78,10 +83,7 @@ public class SshProxyServer implements AutoCloseable {
 
         sshServer.setIoServiceFactoryFactory(nioServiceWithPoolFactoryFactory);
         sshServer.setScheduledExecutorService(minaTimerExecutor);
-        sshServer.getProperties().put(ServerFactoryManager.IDLE_TIMEOUT,
-            String.valueOf(sshProxyServerConfiguration.getIdleTimeout()));
-        sshServer.getProperties().put(ServerFactoryManager.AUTH_TIMEOUT,
-            String.valueOf(sshProxyServerConfiguration.getIdleTimeout()));
+        sshServer.setProperties(getProperties(sshProxyServerConfiguration));
 
         final RemoteNetconfCommand.NetconfCommandFactory netconfCommandFactory =
                 new RemoteNetconfCommand.NetconfCommandFactory(clientGroup, sshProxyServerConfiguration.getLocalAddress());
@@ -89,10 +91,21 @@ public class SshProxyServer implements AutoCloseable {
         sshServer.start();
     }
 
+    private static Map<String, String> getProperties(final SshProxyServerConfiguration sshProxyServerConfiguration) {
+        final Map<String, String> ret = new HashMap<>();
+        ret.put(ServerFactoryManager.IDLE_TIMEOUT, String.valueOf(sshProxyServerConfiguration.getIdleTimeout()));
+        // TODO make auth timeout configurable on its own
+        ret.put(ServerFactoryManager.AUTH_TIMEOUT, String.valueOf(sshProxyServerConfiguration.getIdleTimeout()));
+
+        return ret;
+    }
+
     @Override
-    public void close() throws IOException {
+    public void close() {
         try {
             sshServer.stop(true);
+        } catch (final InterruptedException e) {
+            throw new RuntimeException("Interrupted while stopping sshServer", e);
         } finally {
             sshServer.close(true);
         }
@@ -101,7 +114,7 @@ public class SshProxyServer implements AutoCloseable {
     /**
      * Based on Nio2ServiceFactory with one addition: injectable executor
      */
-    private static final class NioServiceWithPoolFactory extends AbstractCloseable implements IoServiceFactory {
+    private static final class NioServiceWithPoolFactory extends CloseableUtils.AbstractCloseable implements IoServiceFactory {
 
         private final FactoryManager manager;
         private final AsynchronousChannelGroup group;