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