Adjust to yangtools-2.0.0/odlparent-3.0.0 changes
[netconf.git] / netconf / netconf-ssh / src / main / java / org / opendaylight / netconf / ssh / osgi / NetconfSSHActivator.java
index 14cbae81e1891edb357717c5f0dfc88e88dcd545..0a15fdb608c3bde1774ab265ddb9a9ae2ddc9e06 100644 (file)
@@ -9,20 +9,22 @@ package org.opendaylight.netconf.ssh.osgi;
 
 import io.netty.channel.local.LocalAddress;
 import io.netty.channel.nio.NioEventLoopGroup;
+import java.io.File;
 import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ThreadFactory;
-import org.apache.sshd.common.util.ThreadUtils;
-import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
+import org.apache.sshd.common.util.security.SecurityUtils;
+import org.apache.sshd.common.util.threads.ThreadUtils;
+import org.apache.sshd.server.keyprovider.AbstractGeneratorHostKeyProvider;
 import org.opendaylight.netconf.ssh.SshProxyServer;
 import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
 import org.opendaylight.netconf.util.osgi.NetconfConfigUtil;
 import org.opendaylight.netconf.util.osgi.NetconfConfiguration;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -42,13 +44,9 @@ public class NetconfSSHActivator implements BundleActivator {
     private SshProxyServer server;
 
     @Override
-    public void start(final BundleContext bundleContext) throws IOException {
-        minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE, new ThreadFactory() {
-            @Override
-            public Thread newThread(final Runnable r) {
-                return new Thread(r, "netconf-ssh-server-mina-timers");
-            }
-        });
+    public void start(final BundleContext bundleContext) throws IOException, InvalidSyntaxException {
+        minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE,
+            runnable -> new Thread(runnable, "netconf-ssh-server-mina-timers"));
         clientGroup = new NioEventLoopGroup();
         nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
         server = startSSHServer(bundleContext);
@@ -60,43 +58,48 @@ public class NetconfSSHActivator implements BundleActivator {
             server.close();
         }
 
-        if(authProviderTracker != null) {
+        if (authProviderTracker != null) {
             authProviderTracker.stop();
         }
 
-        if(nioExecutor!=null) {
+        if (nioExecutor != null) {
             nioExecutor.shutdownNow();
         }
 
-        if(clientGroup != null) {
+        if (clientGroup != null) {
             clientGroup.shutdownGracefully();
         }
 
-        if(minaTimerExecutor != null) {
+        if (minaTimerExecutor != null) {
             minaTimerExecutor.shutdownNow();
         }
     }
 
-    private SshProxyServer startSSHServer(final BundleContext bundleContext) throws IOException {
-        final NetconfConfiguration netconfConfiguration = NetconfConfigUtil.getNetconfConfigurationService(bundleContext).
-                        orElseThrow(() -> new IllegalStateException("Configuration for SSH not found."));
+    private SshProxyServer startSSHServer(final BundleContext bundleContext)
+            throws IOException, InvalidSyntaxException {
+        final NetconfConfiguration netconfConfiguration =
+                NetconfConfigUtil.getNetconfConfigurationService(bundleContext);
 
         final InetSocketAddress sshSocketAddress = netconfConfiguration.getSshServerAddress();
         LOG.info("Starting netconf SSH server at {}", sshSocketAddress);
 
-        final LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
+        final LocalAddress localAddress = NetconfConfiguration.NETCONF_LOCAL_ADDRESS;
         authProviderTracker = new AuthProviderTracker(bundleContext);
 
         final String path = netconfConfiguration.getPrivateKeyPath();
         LOG.trace("Starting netconf SSH server with path to ssh private key {}", path);
 
         final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerExecutor, clientGroup, nioExecutor);
+        final AbstractGeneratorHostKeyProvider keyPairProvider = SecurityUtils.createGeneratorHostKeyProvider(null);
+        keyPairProvider.setAlgorithm(ALGORITHM);
+        keyPairProvider.setKeySize(KEY_SIZE);
+        keyPairProvider.setFile(new File(path));
         sshProxyServer.bind(
                 new SshProxyServerConfigurationBuilder()
                         .setBindingAddress(sshSocketAddress)
                         .setLocalAddress(localAddress)
                         .setAuthenticator(authProviderTracker)
-                        .setKeyPairProvider(new PEMGeneratorHostKeyProvider(path, ALGORITHM, KEY_SIZE))
+                        .setKeyPairProvider(keyPairProvider)
                         .setIdleTimeout(DEFAULT_IDLE_TIMEOUT)
                         .createSshProxyServerConfiguration());
         return sshProxyServer;