Bug 6023 - Add default address setting
[netconf.git] / netconf / netconf-ssh / src / main / java / org / opendaylight / 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.netconf.ssh.osgi;
9
10 import com.google.common.base.Optional;
11 import io.netty.channel.local.LocalAddress;
12 import io.netty.channel.nio.NioEventLoopGroup;
13 import java.io.IOException;
14 import java.net.InetSocketAddress;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.ScheduledExecutorService;
18 import java.util.concurrent.ThreadFactory;
19 import org.apache.sshd.common.util.ThreadUtils;
20 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
21 import org.opendaylight.netconf.ssh.SshProxyServer;
22 import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
23 import org.opendaylight.netconf.util.osgi.NetconfConfigUtil;
24 import org.opendaylight.netconf.util.osgi.NetconfConfigUtil.InfixProp;
25 import org.osgi.framework.BundleActivator;
26 import org.osgi.framework.BundleContext;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class NetconfSSHActivator implements BundleActivator {
31     private static final Logger LOG = LoggerFactory.getLogger(NetconfSSHActivator.class);
32
33     private static final java.lang.String ALGORITHM = "RSA";
34     private static final int KEY_SIZE = 4096;
35     public static final int POOL_SIZE = 8;
36     private static final int DEFAULT_IDLE_TIMEOUT = Integer.MAX_VALUE;
37
38     private ScheduledExecutorService minaTimerExecutor;
39     private NioEventLoopGroup clientGroup;
40     private ExecutorService nioExecutor;
41     private AuthProviderTracker authProviderTracker;
42
43     private SshProxyServer server;
44
45     @Override
46     public void start(final BundleContext bundleContext) throws IOException {
47         minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE, new ThreadFactory() {
48             @Override
49             public Thread newThread(final Runnable r) {
50                 return new Thread(r, "netconf-ssh-server-mina-timers");
51             }
52         });
53         clientGroup = new NioEventLoopGroup();
54         nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
55         server = startSSHServer(bundleContext);
56     }
57
58     @Override
59     public void stop(final BundleContext context) throws IOException {
60         if (server != null) {
61             server.close();
62         }
63
64         if(authProviderTracker != null) {
65             authProviderTracker.stop();
66         }
67
68         if(nioExecutor!=null) {
69             nioExecutor.shutdownNow();
70         }
71
72         if(clientGroup != null) {
73             clientGroup.shutdownGracefully();
74         }
75
76         if(minaTimerExecutor != null) {
77             minaTimerExecutor.shutdownNow();
78         }
79     }
80
81     private SshProxyServer startSSHServer(final BundleContext bundleContext) throws IOException {
82         final Optional<InetSocketAddress> maybeSshSocketAddress = NetconfConfigUtil.extractNetconfServerAddress(bundleContext, InfixProp.ssh);
83         if (!maybeSshSocketAddress.isPresent()) {
84             LOG.warn("SSH bridge not configured. Using default value {}", NetconfConfigUtil.DEFAULT_SSH_SERVER_ADRESS);
85         }
86         final InetSocketAddress sshSocketAddress = maybeSshSocketAddress
87                 .or(NetconfConfigUtil.DEFAULT_SSH_SERVER_ADRESS);
88         LOG.info("Starting netconf SSH bridge at {}", sshSocketAddress);
89
90         final LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
91
92         authProviderTracker = new AuthProviderTracker(bundleContext);
93
94         final Optional<String> maybePath = NetconfConfigUtil.getPrivateKeyPath(bundleContext);
95         if(!maybePath.isPresent()) {
96             LOG.warn("Private key path not configured. Using default value {}",
97                     NetconfConfigUtil.DEFAULT_PRIVATE_KEY_PATH);
98         }
99         final String path = maybePath.or(NetconfConfigUtil.DEFAULT_PRIVATE_KEY_PATH);
100         LOG.trace("Starting netconf SSH bridge with path to ssh private key {}", path);
101
102         final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerExecutor, clientGroup, nioExecutor);
103         sshProxyServer.bind(
104                 new SshProxyServerConfigurationBuilder()
105                         .setBindingAddress(sshSocketAddress)
106                         .setLocalAddress(localAddress)
107                         .setAuthenticator(authProviderTracker)
108                         .setKeyPairProvider(new PEMGeneratorHostKeyProvider(path, ALGORITHM, KEY_SIZE))
109                         .setIdleTimeout(DEFAULT_IDLE_TIMEOUT)
110                         .createSshProxyServerConfiguration());
111         return sshProxyServer;
112     }
113
114 }