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.osgi.framework.InvalidSyntaxException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class NetconfSSHActivator implements BundleActivator {
31 private static final Logger LOG = LoggerFactory.getLogger(NetconfSSHActivator.class);
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;
38 private ScheduledExecutorService minaTimerExecutor;
39 private NioEventLoopGroup clientGroup;
40 private ExecutorService nioExecutor;
41 private AuthProviderTracker authProviderTracker;
43 private SshProxyServer server;
46 public void start(final BundleContext bundleContext) throws IOException, InvalidSyntaxException {
47 minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE, new ThreadFactory() {
49 public Thread newThread(final Runnable runnable) {
50 return new Thread(runnable, "netconf-ssh-server-mina-timers");
53 clientGroup = new NioEventLoopGroup();
54 nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
55 server = startSSHServer(bundleContext);
59 public void stop(final BundleContext context) throws IOException {
64 if (authProviderTracker != null) {
65 authProviderTracker.stop();
68 if (nioExecutor != null) {
69 nioExecutor.shutdownNow();
72 if (clientGroup != null) {
73 clientGroup.shutdownGracefully();
76 if (minaTimerExecutor != null) {
77 minaTimerExecutor.shutdownNow();
81 private SshProxyServer startSSHServer(final BundleContext bundleContext)
82 throws IOException, InvalidSyntaxException {
83 final NetconfConfiguration netconfConfiguration =
84 NetconfConfigUtil.getNetconfConfigurationService(bundleContext);
86 final InetSocketAddress sshSocketAddress = netconfConfiguration.getSshServerAddress();
87 LOG.info("Starting netconf SSH server at {}", sshSocketAddress);
89 final LocalAddress localAddress = NetconfConfiguration.NETCONF_LOCAL_ADDRESS;
90 authProviderTracker = new AuthProviderTracker(bundleContext);
92 final String path = netconfConfiguration.getPrivateKeyPath();
93 LOG.trace("Starting netconf SSH server with path to ssh private key {}", path);
95 final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerExecutor, clientGroup, nioExecutor);
97 new SshProxyServerConfigurationBuilder()
98 .setBindingAddress(sshSocketAddress)
99 .setLocalAddress(localAddress)
100 .setAuthenticator(authProviderTracker)
101 .setKeyPairProvider(new PEMGeneratorHostKeyProvider(path, ALGORITHM, KEY_SIZE))
102 .setIdleTimeout(DEFAULT_IDLE_TIMEOUT)
103 .createSshProxyServerConfiguration());
104 return sshProxyServer;