Merge "Bug 8153: Enforce check-style rules for netconf - netconf-ssh"
[netconf.git] / netconf / netconf-ssh / src / main / java / org / opendaylight / netconf / ssh / osgi / NetconfSSHActivator.java
index 9cbfac1580a3a22a0c3285129f6cee526f300e8b..f31f7b95b1ed93cb0d81e202ee5b5ab99f5f54da 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.netconf.ssh.osgi;
 
-import com.google.common.base.Optional;
 import io.netty.channel.local.LocalAddress;
 import io.netty.channel.nio.NioEventLoopGroup;
 import java.io.IOException;
@@ -21,9 +20,10 @@ import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
 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.NetconfConfigUtil.InfixProp;
+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;
 
@@ -43,11 +43,11 @@ public class NetconfSSHActivator implements BundleActivator {
     private SshProxyServer server;
 
     @Override
-    public void start(final BundleContext bundleContext) throws IOException {
+    public void start(final BundleContext bundleContext) throws IOException, InvalidSyntaxException {
         minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE, new ThreadFactory() {
             @Override
-            public Thread newThread(final Runnable r) {
-                return new Thread(r, "netconf-ssh-server-mina-timers");
+            public Thread newThread(final Runnable runnable) {
+                return new Thread(runnable, "netconf-ssh-server-mina-timers");
             }
         });
         clientGroup = new NioEventLoopGroup();
@@ -61,43 +61,36 @@ 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 Optional<InetSocketAddress> maybeSshSocketAddress = NetconfConfigUtil.extractNetconfServerAddress(bundleContext, InfixProp.ssh);
-        if (!maybeSshSocketAddress.isPresent()) {
-            LOG.warn("SSH bridge not configured. Using default value {}", NetconfConfigUtil.DEFAULT_SSH_SERVER_ADRESS);
-        }
-        final InetSocketAddress sshSocketAddress = maybeSshSocketAddress
-                .or(NetconfConfigUtil.DEFAULT_SSH_SERVER_ADRESS);
-        LOG.info("Starting netconf SSH bridge at {}", sshSocketAddress);
+    private SshProxyServer startSSHServer(final BundleContext bundleContext)
+            throws IOException, InvalidSyntaxException {
+        final NetconfConfiguration netconfConfiguration =
+                NetconfConfigUtil.getNetconfConfigurationService(bundleContext);
 
-        final LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
+        final InetSocketAddress sshSocketAddress = netconfConfiguration.getSshServerAddress();
+        LOG.info("Starting netconf SSH server at {}", sshSocketAddress);
 
+        final LocalAddress localAddress = NetconfConfiguration.NETCONF_LOCAL_ADDRESS;
         authProviderTracker = new AuthProviderTracker(bundleContext);
 
-        final Optional<String> maybePath = NetconfConfigUtil.getPrivateKeyPath(bundleContext);
-        if(!maybePath.isPresent()) {
-            LOG.warn("Private key path not configured. Using default value {}",
-                    NetconfConfigUtil.DEFAULT_PRIVATE_KEY_PATH);
-        }
-        final String path = maybePath.or(NetconfConfigUtil.DEFAULT_PRIVATE_KEY_PATH);
-        LOG.trace("Starting netconf SSH bridge with path to ssh private key {}", path);
+        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);
         sshProxyServer.bind(
@@ -110,5 +103,4 @@ public class NetconfSSHActivator implements BundleActivator {
                         .createSshProxyServerConfiguration());
         return sshProxyServer;
     }
-
 }