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.commons.io.FilenameUtils;
23 import org.apache.sshd.common.util.ThreadUtils;
24 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
25 import org.opendaylight.controller.netconf.ssh.SshProxyServer;
26 import org.opendaylight.controller.netconf.ssh.SshProxyServerConfigurationBuilder;
27 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
28 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil.InfixProp;
29 import org.osgi.framework.BundleActivator;
30 import org.osgi.framework.BundleContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class NetconfSSHActivator implements BundleActivator {
35 private static final Logger LOG = LoggerFactory.getLogger(NetconfSSHActivator.class);
37 private static final java.lang.String ALGORITHM = "RSA";
38 private static final int KEY_SIZE = 4096;
39 public static final int POOL_SIZE = 8;
40 private static final int DEFAULT_IDLE_TIMEOUT = Integer.MAX_VALUE;
42 private ScheduledExecutorService minaTimerExecutor;
43 private NioEventLoopGroup clientGroup;
44 private ExecutorService nioExecutor;
45 private AuthProviderTracker authProviderTracker;
47 private SshProxyServer server;
50 public void start(final BundleContext bundleContext) throws IOException {
51 minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE, new ThreadFactory() {
53 public Thread newThread(final Runnable r) {
54 return new Thread(r, "netconf-ssh-server-mina-timers");
57 clientGroup = new NioEventLoopGroup();
58 nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
59 server = startSSHServer(bundleContext);
63 public void stop(final BundleContext context) throws IOException {
68 if(authProviderTracker != null) {
69 authProviderTracker.stop();
72 if(nioExecutor!=null) {
73 nioExecutor.shutdownNow();
76 if(clientGroup != null) {
77 clientGroup.shutdownGracefully();
80 if(minaTimerExecutor != null) {
81 minaTimerExecutor.shutdownNow();
85 private SshProxyServer startSSHServer(final BundleContext bundleContext) throws IOException {
86 final Optional<InetSocketAddress> maybeSshSocketAddress = NetconfConfigUtil.extractNetconfServerAddress(bundleContext, InfixProp.ssh);
88 if (maybeSshSocketAddress.isPresent() == false) {
89 LOG.trace("SSH bridge not configured");
93 final InetSocketAddress sshSocketAddress = maybeSshSocketAddress.get();
94 LOG.trace("Starting netconf SSH bridge at {}", sshSocketAddress);
96 final LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
98 authProviderTracker = new AuthProviderTracker(bundleContext);
100 final String path = FilenameUtils.separatorsToSystem(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;