Merge "BUG-625: migrate InventoryAndReadAdapter"
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / osgi / NetconfSSHActivator.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.netconf.ssh.osgi;
9
10 import static com.google.common.base.Preconditions.checkState;
11
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;
16 import java.io.File;
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;
30
31 /**
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.
40  */
41 public class NetconfSSHActivator implements BundleActivator {
42     private static final Logger logger = LoggerFactory.getLogger(NetconfSSHActivator.class);
43
44     private NetconfSSHServer server;
45
46     @Override
47     public void start(final BundleContext bundleContext) throws IOException {
48         server = startSSHServer(bundleContext);
49     }
50
51     @Override
52     public void stop(BundleContext context) throws IOException {
53         if (server != null) {
54             server.close();
55         }
56     }
57
58     private static NetconfSSHServer startSSHServer(BundleContext bundleContext) throws IOException {
59         Optional<InetSocketAddress> maybeSshSocketAddress = NetconfConfigUtil.extractNetconfServerAddress(bundleContext,
60                 InfixProp.ssh);
61
62         if (maybeSshSocketAddress.isPresent() == false) {
63             logger.trace("SSH bridge not configured");
64             return null;
65         }
66         InetSocketAddress sshSocketAddress = maybeSshSocketAddress.get();
67         logger.trace("Starting netconf SSH  bridge at {}", sshSocketAddress);
68
69         LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
70
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));
74
75         final AuthProvider authProvider = new AuthProvider(privateKeyPEMString, bundleContext);
76         EventLoopGroup bossGroup  = new NioEventLoopGroup();
77         NetconfSSHServer server = NetconfSSHServer.start(sshSocketAddress.getPort(), localAddress, authProvider, bossGroup);
78
79         final Thread serverThread = new Thread(server, "netconf SSH server thread");
80         serverThread.setDaemon(true);
81         serverThread.start();
82         logger.trace("Netconf SSH  bridge up and running.");
83         return server;
84     }
85
86
87 }