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;
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 org.apache.sshd.common.util.security.SecurityUtils;
19 import org.apache.sshd.common.util.threads.ThreadUtils;
20 import org.apache.sshd.server.keyprovider.AbstractGeneratorHostKeyProvider;
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.NetconfConfiguration;
25 import org.osgi.framework.BundleActivator;
26 import org.osgi.framework.BundleContext;
27 import org.osgi.framework.InvalidSyntaxException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class NetconfSSHActivator implements BundleActivator {
32 private static final Logger LOG = LoggerFactory.getLogger(NetconfSSHActivator.class);
34 private static final java.lang.String ALGORITHM = "RSA";
35 private static final int KEY_SIZE = 4096;
36 public static final int POOL_SIZE = 8;
37 private static final int DEFAULT_IDLE_TIMEOUT = Integer.MAX_VALUE;
39 private ScheduledExecutorService minaTimerExecutor;
40 private NioEventLoopGroup clientGroup;
41 private ExecutorService nioExecutor;
42 private AuthProviderTracker authProviderTracker;
44 private SshProxyServer server;
47 public void start(final BundleContext bundleContext) throws IOException, InvalidSyntaxException {
48 minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE,
49 runnable -> new Thread(runnable, "netconf-ssh-server-mina-timers"));
50 clientGroup = new NioEventLoopGroup();
51 nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
52 server = startSSHServer(bundleContext);
56 public void stop(final BundleContext context) throws IOException {
61 if (authProviderTracker != null) {
62 authProviderTracker.stop();
65 if (nioExecutor != null) {
66 nioExecutor.shutdownNow();
69 if (clientGroup != null) {
70 clientGroup.shutdownGracefully();
73 if (minaTimerExecutor != null) {
74 minaTimerExecutor.shutdownNow();
78 private SshProxyServer startSSHServer(final BundleContext bundleContext)
79 throws IOException, InvalidSyntaxException {
80 final NetconfConfiguration netconfConfiguration =
81 NetconfConfigUtil.getNetconfConfigurationService(bundleContext);
83 final InetSocketAddress sshSocketAddress = netconfConfiguration.getSshServerAddress();
84 LOG.info("Starting netconf SSH server at {}", sshSocketAddress);
86 final LocalAddress localAddress = NetconfConfiguration.NETCONF_LOCAL_ADDRESS;
87 authProviderTracker = new AuthProviderTracker(bundleContext);
89 final String path = netconfConfiguration.getPrivateKeyPath();
90 LOG.trace("Starting netconf SSH server with path to ssh private key {}", path);
92 final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerExecutor, clientGroup, nioExecutor);
93 final AbstractGeneratorHostKeyProvider keyPairProvider = SecurityUtils.createGeneratorHostKeyProvider(null);
94 keyPairProvider.setAlgorithm(ALGORITHM);
95 keyPairProvider.setKeySize(KEY_SIZE);
96 keyPairProvider.setFile(new File(path));
98 new SshProxyServerConfigurationBuilder()
99 .setBindingAddress(sshSocketAddress)
100 .setLocalAddress(localAddress)
101 .setAuthenticator(authProviderTracker)
102 .setKeyPairProvider(keyPairProvider)
103 .setIdleTimeout(DEFAULT_IDLE_TIMEOUT)
104 .createSshProxyServerConfiguration());
105 return sshProxyServer;