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 org.apache.sshd.common.util.ThreadUtils;
23 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
24 import org.opendaylight.controller.netconf.ssh.SshProxyServer;
25 import org.opendaylight.controller.netconf.ssh.SshProxyServerConfigurationBuilder;
26 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
27 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil.InfixProp;
28 import org.osgi.framework.BundleActivator;
29 import org.osgi.framework.BundleContext;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class NetconfSSHActivator implements BundleActivator {
34 private static final Logger LOG = LoggerFactory.getLogger(NetconfSSHActivator.class);
36 private static final java.lang.String ALGORITHM = "RSA";
37 private static final int KEY_SIZE = 4096;
38 public static final int POOL_SIZE = 8;
39 private static final int DEFAULT_IDLE_TIMEOUT = Integer.MAX_VALUE;
41 private ScheduledExecutorService minaTimerExecutor;
42 private NioEventLoopGroup clientGroup;
43 private ExecutorService nioExecutor;
44 private AuthProviderTracker authProviderTracker;
46 private SshProxyServer server;
49 public void start(final BundleContext bundleContext) throws IOException {
50 minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE, new ThreadFactory() {
52 public Thread newThread(final Runnable r) {
53 return new Thread(r, "netconf-ssh-server-mina-timers");
56 clientGroup = new NioEventLoopGroup();
57 nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
58 server = startSSHServer(bundleContext);
62 public void stop(final BundleContext context) throws IOException {
67 if(authProviderTracker != null) {
68 authProviderTracker.stop();
71 if(nioExecutor!=null) {
72 nioExecutor.shutdownNow();
75 if(clientGroup != null) {
76 clientGroup.shutdownGracefully();
79 if(minaTimerExecutor != null) {
80 minaTimerExecutor.shutdownNow();
84 private SshProxyServer startSSHServer(final BundleContext bundleContext) throws IOException {
85 final Optional<InetSocketAddress> maybeSshSocketAddress = NetconfConfigUtil.extractNetconfServerAddress(bundleContext, InfixProp.ssh);
87 if (!maybeSshSocketAddress.isPresent()) {
88 LOG.trace("SSH bridge not configured");
92 final InetSocketAddress sshSocketAddress = maybeSshSocketAddress.get();
93 LOG.trace("Starting netconf SSH bridge at {}", sshSocketAddress);
95 final LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
97 authProviderTracker = new AuthProviderTracker(bundleContext);
99 final String path = NetconfConfigUtil.getPrivateKeyPath(bundleContext);
101 checkState(!Strings.isNullOrEmpty(path), "Path to ssh private key is blank. Reconfigure %s",
102 NetconfConfigUtil.getPrivateKeyKey());
104 final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerExecutor, clientGroup, nioExecutor);
106 new SshProxyServerConfigurationBuilder()
107 .setBindingAddress(sshSocketAddress)
108 .setLocalAddress(localAddress)
109 .setAuthenticator(authProviderTracker)
110 .setKeyPairProvider(new PEMGeneratorHostKeyProvider(path, ALGORITHM, KEY_SIZE))
111 .setIdleTimeout(DEFAULT_IDLE_TIMEOUT)
112 .createSshProxyServerConfiguration());
113 return sshProxyServer;