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 org.apache.sshd.common.util.ThreadUtils;
18 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
19 import org.opendaylight.netconf.ssh.SshProxyServer;
20 import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
21 import org.opendaylight.netconf.util.osgi.NetconfConfigUtil;
22 import org.opendaylight.netconf.util.osgi.NetconfConfiguration;
23 import org.osgi.framework.BundleActivator;
24 import org.osgi.framework.BundleContext;
25 import org.osgi.framework.InvalidSyntaxException;
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, InvalidSyntaxException {
46 minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE,
47 runnable -> new Thread(runnable, "netconf-ssh-server-mina-timers"));
48 clientGroup = new NioEventLoopGroup();
49 nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
50 server = startSSHServer(bundleContext);
54 public void stop(final BundleContext context) throws IOException {
59 if (authProviderTracker != null) {
60 authProviderTracker.stop();
63 if (nioExecutor != null) {
64 nioExecutor.shutdownNow();
67 if (clientGroup != null) {
68 clientGroup.shutdownGracefully();
71 if (minaTimerExecutor != null) {
72 minaTimerExecutor.shutdownNow();
76 private SshProxyServer startSSHServer(final BundleContext bundleContext)
77 throws IOException, InvalidSyntaxException {
78 final NetconfConfiguration netconfConfiguration =
79 NetconfConfigUtil.getNetconfConfigurationService(bundleContext);
81 final InetSocketAddress sshSocketAddress = netconfConfiguration.getSshServerAddress();
82 LOG.info("Starting netconf SSH server at {}", sshSocketAddress);
84 final LocalAddress localAddress = NetconfConfiguration.NETCONF_LOCAL_ADDRESS;
85 authProviderTracker = new AuthProviderTracker(bundleContext);
87 final String path = netconfConfiguration.getPrivateKeyPath();
88 LOG.trace("Starting netconf SSH server with path to ssh private key {}", path);
90 final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerExecutor, clientGroup, nioExecutor);
92 new SshProxyServerConfigurationBuilder()
93 .setBindingAddress(sshSocketAddress)
94 .setLocalAddress(localAddress)
95 .setAuthenticator(authProviderTracker)
96 .setKeyPairProvider(new PEMGeneratorHostKeyProvider(path, ALGORITHM, KEY_SIZE))
97 .setIdleTimeout(DEFAULT_IDLE_TIMEOUT)
98 .createSshProxyServerConfiguration());
99 return sshProxyServer;