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.AuthProviderImpl;
24 import org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator;
25 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
26 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil.InfixProp;
27 import org.osgi.framework.BundleActivator;
28 import org.osgi.framework.BundleContext;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Activator for netconf SSH bundle which creates SSH bridge between netconf client and netconf server. Activator
34 * starts SSH Server in its own thread. This thread is closed when activator calls stop() method. Server opens socket
35 * and listens for client connections. Each client connection creation is handled in separate
36 * {@link org.opendaylight.controller.netconf.ssh.threads.Handshaker} thread.
37 * This thread creates two additional threads {@link org.opendaylight.controller.netconf.ssh.threads.IOThread}
38 * forwarding data from/to client.IOThread closes servers session and server connection when it gets -1 on input stream.
39 * {@link org.opendaylight.controller.netconf.ssh.threads.IOThread}'s run method waits for -1 on input stream to finish.
40 * All threads are daemons.
42 public class NetconfSSHActivator implements BundleActivator {
43 private static final Logger logger = LoggerFactory.getLogger(NetconfSSHActivator.class);
45 private NetconfSSHServer server;
48 public void start(final BundleContext bundleContext) throws IOException {
49 server = startSSHServer(bundleContext);
53 public void stop(BundleContext context) throws IOException {
59 private static NetconfSSHServer startSSHServer(BundleContext bundleContext) throws IOException {
60 Optional<InetSocketAddress> maybeSshSocketAddress = NetconfConfigUtil.extractNetconfServerAddress(bundleContext,
63 if (maybeSshSocketAddress.isPresent() == false) {
64 logger.trace("SSH bridge not configured");
67 InetSocketAddress sshSocketAddress = maybeSshSocketAddress.get();
68 logger.trace("Starting netconf SSH bridge at {}", sshSocketAddress);
70 LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
72 String path = FilenameUtils.separatorsToSystem(NetconfConfigUtil.getPrivateKeyPath(bundleContext));
73 checkState(StringUtils.isNotBlank(path), "Path to ssh private key is blank. Reconfigure %s", NetconfConfigUtil.getPrivateKeyKey());
74 String privateKeyPEMString = PEMGenerator.readOrGeneratePK(new File(path));
76 final AuthProvider authProvider = new AuthProviderImpl(privateKeyPEMString, bundleContext);
77 EventLoopGroup bossGroup = new NioEventLoopGroup();
78 NetconfSSHServer server = NetconfSSHServer.start(sshSocketAddress.getPort(), localAddress, authProvider, bossGroup);
80 final Thread serverThread = new Thread(server, "netconf SSH server thread");
81 serverThread.setDaemon(true);
83 logger.trace("Netconf SSH bridge up and running.");