2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.ssh.osgi;
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;
29 public class NetconfSSHActivator implements BundleActivator {
30 private static final Logger LOG = LoggerFactory.getLogger(NetconfSSHActivator.class);
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;
37 private ScheduledExecutorService minaTimerExecutor;
38 private NioEventLoopGroup clientGroup;
39 private ExecutorService nioExecutor;
40 private AuthProviderTracker authProviderTracker;
42 private SshProxyServer server;
45 public void start(final BundleContext bundleContext) throws IOException {
46 minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE, new ThreadFactory() {
48 public Thread newThread(final Runnable r) {
49 return new Thread(r, "netconf-ssh-server-mina-timers");
52 clientGroup = new NioEventLoopGroup();
53 nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
54 server = startSSHServer(bundleContext);
58 public void stop(final BundleContext context) throws IOException {
63 if(authProviderTracker != null) {
64 authProviderTracker.stop();
67 if(nioExecutor!=null) {
68 nioExecutor.shutdownNow();
71 if(clientGroup != null) {
72 clientGroup.shutdownGracefully();
75 if(minaTimerExecutor != null) {
76 minaTimerExecutor.shutdownNow();
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."));
84 final InetSocketAddress sshSocketAddress = netconfConfiguration.getSshServerAddress();
85 LOG.info("Starting netconf SSH server at {}", sshSocketAddress);
87 final LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
88 authProviderTracker = new AuthProviderTracker(bundleContext);
90 final String path = netconfConfiguration.getPrivateKeyPath();
91 LOG.trace("Starting netconf SSH server with path to ssh private key {}", path);
93 final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerExecutor, clientGroup, nioExecutor);
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;