Merge "BUG 932 - Swagger HTTP POST contains incorrect object"
[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.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;
31
32 /**
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.
41  */
42 public class NetconfSSHActivator implements BundleActivator {
43     private static final Logger logger = LoggerFactory.getLogger(NetconfSSHActivator.class);
44
45     private NetconfSSHServer server;
46
47     @Override
48     public void start(final BundleContext bundleContext) throws IOException {
49         server = startSSHServer(bundleContext);
50     }
51
52     @Override
53     public void stop(BundleContext context) throws IOException {
54         if (server != null) {
55             server.close();
56         }
57     }
58
59     private static NetconfSSHServer startSSHServer(BundleContext bundleContext) throws IOException {
60         Optional<InetSocketAddress> maybeSshSocketAddress = NetconfConfigUtil.extractNetconfServerAddress(bundleContext,
61                 InfixProp.ssh);
62
63         if (maybeSshSocketAddress.isPresent() == false) {
64             logger.trace("SSH bridge not configured");
65             return null;
66         }
67         InetSocketAddress sshSocketAddress = maybeSshSocketAddress.get();
68         logger.trace("Starting netconf SSH  bridge at {}", sshSocketAddress);
69
70         LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
71
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));
75
76         final AuthProvider authProvider = new AuthProviderImpl(privateKeyPEMString, bundleContext);
77         EventLoopGroup bossGroup  = new NioEventLoopGroup();
78         NetconfSSHServer server = NetconfSSHServer.start(sshSocketAddress.getPort(), localAddress, authProvider, bossGroup);
79
80         final Thread serverThread = new Thread(server, "netconf SSH server thread");
81         serverThread.setDaemon(true);
82         serverThread.start();
83         logger.trace("Netconf SSH  bridge up and running.");
84         return server;
85     }
86
87
88 }