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 io.netty.channel.EventLoopGroup;
14 import io.netty.channel.local.LocalAddress;
15 import io.netty.channel.nio.NioEventLoopGroup;
17 import java.io.IOException;
18 import java.net.InetSocketAddress;
19 import org.apache.commons.io.FilenameUtils;
20 import org.apache.commons.lang3.StringUtils;
21 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
22 import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider;
23 import org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator;
24 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
25 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil.InfixProp;
26 import org.osgi.framework.BundleActivator;
27 import org.osgi.framework.BundleContext;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Activator for netconf SSH bundle which creates SSH bridge between netconf client and netconf server. Activator
33 * starts SSH Server in its own thread. This thread is closed when activator calls stop() method. Server opens socket
34 * and listens for client connections. Each client connection creation is handled in separate
35 * {@link org.opendaylight.controller.netconf.ssh.threads.Handshaker} thread.
36 * This thread creates two additional threads {@link org.opendaylight.controller.netconf.ssh.threads.IOThread}
37 * forwarding data from/to client.IOThread closes servers session and server connection when it gets -1 on input stream.
38 * {@link org.opendaylight.controller.netconf.ssh.threads.IOThread}'s run method waits for -1 on input stream to finish.
39 * All threads are daemons.
41 public class NetconfSSHActivator implements BundleActivator {
42 private static final Logger logger = LoggerFactory.getLogger(NetconfSSHActivator.class);
44 private NetconfSSHServer server;
47 public void start(final BundleContext bundleContext) throws IOException {
48 server = startSSHServer(bundleContext);
52 public void stop(BundleContext context) throws IOException {
58 private static NetconfSSHServer startSSHServer(BundleContext bundleContext) throws IOException {
59 Optional<InetSocketAddress> maybeSshSocketAddress = NetconfConfigUtil.extractNetconfServerAddress(bundleContext,
62 if (maybeSshSocketAddress.isPresent() == false) {
63 logger.trace("SSH bridge not configured");
66 InetSocketAddress sshSocketAddress = maybeSshSocketAddress.get();
67 logger.trace("Starting netconf SSH bridge at {}", sshSocketAddress);
69 LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
71 String path = FilenameUtils.separatorsToSystem(NetconfConfigUtil.getPrivateKeyPath(bundleContext));
72 checkState(StringUtils.isNotBlank(path), "Path to ssh private key is blank. Reconfigure %s", NetconfConfigUtil.getPrivateKeyKey());
73 String privateKeyPEMString = PEMGenerator.readOrGeneratePK(new File(path));
75 final AuthProvider authProvider = new AuthProvider(privateKeyPEMString, bundleContext);
76 EventLoopGroup bossGroup = new NioEventLoopGroup();
77 NetconfSSHServer server = NetconfSSHServer.start(sshSocketAddress.getPort(), localAddress, authProvider, bossGroup);
79 final Thread serverThread = new Thread(server, "netconf SSH server thread");
80 serverThread.setDaemon(true);
82 logger.trace("Netconf SSH bridge up and running.");