Fix checkstyle warnings in netconf-ssh.
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / osgi / NetconfSSHActivator.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.netconf.ssh.osgi;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import com.google.common.base.Optional;
13 import com.google.common.base.Strings;
14 import io.netty.channel.local.LocalAddress;
15 import io.netty.channel.nio.NioEventLoopGroup;
16 import java.io.IOException;
17 import java.net.InetSocketAddress;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.ThreadFactory;
22 import java.util.concurrent.TimeUnit;
23 import org.apache.commons.io.FilenameUtils;
24 import org.apache.sshd.common.util.ThreadUtils;
25 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
26 import org.opendaylight.controller.netconf.ssh.SshProxyServer;
27 import org.opendaylight.controller.netconf.ssh.SshProxyServerConfigurationBuilder;
28 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
29 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil.InfixProp;
30 import org.osgi.framework.BundleActivator;
31 import org.osgi.framework.BundleContext;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class NetconfSSHActivator implements BundleActivator {
36     private static final Logger LOG = LoggerFactory.getLogger(NetconfSSHActivator.class);
37
38     private static final java.lang.String ALGORITHM = "RSA";
39     private static final int KEY_SIZE = 4096;
40     public static final int POOL_SIZE = 8;
41     private static final int DEFAULT_IDLE_TIMEOUT = (int) TimeUnit.MINUTES.toMillis(20);
42
43     private ScheduledExecutorService minaTimerExecutor;
44     private NioEventLoopGroup clientGroup;
45     private ExecutorService nioExecutor;
46     private AuthProviderTracker authProviderTracker;
47
48     private SshProxyServer server;
49
50     @Override
51     public void start(final BundleContext bundleContext) throws IOException {
52         minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE, new ThreadFactory() {
53             @Override
54             public Thread newThread(final Runnable r) {
55                 return new Thread(r, "netconf-ssh-server-mina-timers");
56             }
57         });
58         clientGroup = new NioEventLoopGroup();
59         nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
60         server = startSSHServer(bundleContext);
61     }
62
63     @Override
64     public void stop(final BundleContext context) throws IOException {
65         if (server != null) {
66             server.close();
67         }
68
69         if(authProviderTracker != null) {
70             authProviderTracker.stop();
71         }
72
73         if(nioExecutor!=null) {
74             nioExecutor.shutdownNow();
75         }
76
77         if(clientGroup != null) {
78             clientGroup.shutdownGracefully();
79         }
80
81         if(minaTimerExecutor != null) {
82             minaTimerExecutor.shutdownNow();
83         }
84     }
85
86     private SshProxyServer startSSHServer(final BundleContext bundleContext) throws IOException {
87         final Optional<InetSocketAddress> maybeSshSocketAddress = NetconfConfigUtil.extractNetconfServerAddress(bundleContext, InfixProp.ssh);
88
89         if (maybeSshSocketAddress.isPresent() == false) {
90             LOG.trace("SSH bridge not configured");
91             return null;
92         }
93
94         final InetSocketAddress sshSocketAddress = maybeSshSocketAddress.get();
95         LOG.trace("Starting netconf SSH bridge at {}", sshSocketAddress);
96
97         final LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
98
99         authProviderTracker = new AuthProviderTracker(bundleContext);
100
101         final String path = FilenameUtils.separatorsToSystem(NetconfConfigUtil.getPrivateKeyPath(bundleContext));
102         checkState(!Strings.isNullOrEmpty(path), "Path to ssh private key is blank. Reconfigure %s",
103                 NetconfConfigUtil.getPrivateKeyKey());
104
105         final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerExecutor, clientGroup, nioExecutor);
106         sshProxyServer.bind(
107                 new SshProxyServerConfigurationBuilder()
108                         .setBindingAddress(sshSocketAddress)
109                         .setLocalAddress(localAddress)
110                         .setAuthenticator(authProviderTracker)
111                         .setKeyPairProvider(new PEMGeneratorHostKeyProvider(path, ALGORITHM, KEY_SIZE))
112                         .setIdleTimeout(DEFAULT_IDLE_TIMEOUT)
113                         .createSshProxyServerConfiguration());
114         return sshProxyServer;
115     }
116
117 }