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 com.google.common.base.Optional;
11 import io.netty.channel.local.LocalAddress;
12 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 java.util.concurrent.ThreadFactory;
19 import org.apache.sshd.common.util.ThreadUtils;
20 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
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.NetconfConfigUtil.InfixProp;
25 import org.osgi.framework.BundleActivator;
26 import org.osgi.framework.BundleContext;
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 {
47 minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE, new ThreadFactory() {
49 public Thread newThread(final Runnable r) {
50 return new Thread(r, "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) throws IOException {
82 final Optional<InetSocketAddress> maybeSshSocketAddress = NetconfConfigUtil.extractNetconfServerAddress(bundleContext, InfixProp.ssh);
83 if (!maybeSshSocketAddress.isPresent()) {
84 LOG.warn("SSH bridge not configured. Using default value {}", NetconfConfigUtil.DEFAULT_SSH_SERVER_ADRESS);
86 final InetSocketAddress sshSocketAddress = maybeSshSocketAddress
87 .or(NetconfConfigUtil.DEFAULT_SSH_SERVER_ADRESS);
88 LOG.info("Starting netconf SSH bridge at {}", sshSocketAddress);
90 final LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
92 authProviderTracker = new AuthProviderTracker(bundleContext);
94 final Optional<String> maybePath = NetconfConfigUtil.getPrivateKeyPath(bundleContext);
95 if(!maybePath.isPresent()) {
96 LOG.warn("Private key path not configured. Using default value {}",
97 NetconfConfigUtil.DEFAULT_PRIVATE_KEY_PATH);
99 final String path = maybePath.or(NetconfConfigUtil.DEFAULT_PRIVATE_KEY_PATH);
100 LOG.trace("Starting netconf SSH bridge with path to ssh private key {}", path);
102 final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerExecutor, clientGroup, nioExecutor);
104 new SshProxyServerConfigurationBuilder()
105 .setBindingAddress(sshSocketAddress)
106 .setLocalAddress(localAddress)
107 .setAuthenticator(authProviderTracker)
108 .setKeyPairProvider(new PEMGeneratorHostKeyProvider(path, ALGORITHM, KEY_SIZE))
109 .setIdleTimeout(DEFAULT_IDLE_TIMEOUT)
110 .createSshProxyServerConfiguration());
111 return sshProxyServer;