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.controller.netconf.ssh.osgi;
10 import static com.google.common.base.Preconditions.checkState;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Strings;
14 import io.netty.channel.local.LocalAddress;
15 import io.netty.channel.nio.NioEventLoopGroup;
16 import java.io.IOException;
17 import java.net.InetSocketAddress;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.ThreadFactory;
22 import java.util.concurrent.TimeUnit;
23 import org.apache.commons.io.FilenameUtils;
24 import org.apache.sshd.common.util.ThreadUtils;
25 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
26 import org.opendaylight.controller.netconf.ssh.SshProxyServer;
27 import org.opendaylight.controller.netconf.ssh.SshProxyServerConfigurationBuilder;
28 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
29 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil.InfixProp;
30 import org.osgi.framework.BundleActivator;
31 import org.osgi.framework.BundleContext;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class NetconfSSHActivator implements BundleActivator {
36 private static final Logger LOG = LoggerFactory.getLogger(NetconfSSHActivator.class);
38 private static final java.lang.String ALGORITHM = "RSA";
39 private static final int KEY_SIZE = 4096;
40 public static final int POOL_SIZE = 8;
41 private static final int DEFAULT_IDLE_TIMEOUT = (int) TimeUnit.MINUTES.toMillis(20);
43 private ScheduledExecutorService minaTimerExecutor;
44 private NioEventLoopGroup clientGroup;
45 private ExecutorService nioExecutor;
46 private AuthProviderTracker authProviderTracker;
48 private SshProxyServer server;
51 public void start(final BundleContext bundleContext) throws IOException {
52 minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE, new ThreadFactory() {
54 public Thread newThread(final Runnable r) {
55 return new Thread(r, "netconf-ssh-server-mina-timers");
58 clientGroup = new NioEventLoopGroup();
59 nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
60 server = startSSHServer(bundleContext);
64 public void stop(final BundleContext context) throws IOException {
69 if(authProviderTracker != null) {
70 authProviderTracker.stop();
73 if(nioExecutor!=null) {
74 nioExecutor.shutdownNow();
77 if(clientGroup != null) {
78 clientGroup.shutdownGracefully();
81 if(minaTimerExecutor != null) {
82 minaTimerExecutor.shutdownNow();
86 private SshProxyServer startSSHServer(final BundleContext bundleContext) throws IOException {
87 final Optional<InetSocketAddress> maybeSshSocketAddress = NetconfConfigUtil.extractNetconfServerAddress(bundleContext, InfixProp.ssh);
89 if (maybeSshSocketAddress.isPresent() == false) {
90 LOG.trace("SSH bridge not configured");
94 final InetSocketAddress sshSocketAddress = maybeSshSocketAddress.get();
95 LOG.trace("Starting netconf SSH bridge at {}", sshSocketAddress);
97 final LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
99 authProviderTracker = new AuthProviderTracker(bundleContext);
101 final String path = FilenameUtils.separatorsToSystem(NetconfConfigUtil.getPrivateKeyPath(bundleContext));
102 checkState(!Strings.isNullOrEmpty(path), "Path to ssh private key is blank. Reconfigure %s",
103 NetconfConfigUtil.getPrivateKeyKey());
105 final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerExecutor, clientGroup, nioExecutor);
107 new SshProxyServerConfigurationBuilder()
108 .setBindingAddress(sshSocketAddress)
109 .setLocalAddress(localAddress)
110 .setAuthenticator(authProviderTracker)
111 .setKeyPairProvider(new PEMGeneratorHostKeyProvider(path, ALGORITHM, KEY_SIZE))
112 .setIdleTimeout(DEFAULT_IDLE_TIMEOUT)
113 .createSshProxyServerConfiguration());
114 return sshProxyServer;