6f164f93d9c9482613a286352a6868e41cf649c3
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / 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.osgi;
9
10 import com.google.common.base.Optional;
11 import java.net.InetSocketAddress;
12 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
13 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
14 import org.osgi.framework.BundleActivator;
15 import org.osgi.framework.BundleContext;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Activator for netconf SSH bundle which creates SSH bridge between netconf client and netconf server. Activator
21  * starts SSH Server in its own thread. This thread is closed when activator calls stop() method. Server opens socket
22  * and listen for client connections. Each client connection creation is handled in separate
23  * {@link org.opendaylight.controller.netconf.ssh.threads.SocketThread} thread.
24  * This thread creates two additional threads {@link org.opendaylight.controller.netconf.ssh.threads.IOThread}
25  * forwarding data from/to client.IOThread closes servers session and server connection when it gets -1 on input stream.
26  * {@link org.opendaylight.controller.netconf.ssh.threads.IOThread}'s run method waits for -1 on input stream to finish.
27  * All threads are daemons.
28  **/
29 public class NetconfSSHActivator implements BundleActivator{
30
31     private NetconfSSHServer server;
32     private static final Logger logger =  LoggerFactory.getLogger(NetconfSSHActivator.class);
33     private static final String EXCEPTION_MESSAGE = "Netconf ssh bridge is not available.";
34
35     @Override
36     public void start(BundleContext context) throws Exception {
37
38         logger.trace("Starting netconf SSH  bridge.");
39
40         Optional<InetSocketAddress> sshSocketAddressOptional = NetconfConfigUtil.extractSSHNetconfAddress(context,EXCEPTION_MESSAGE);
41         InetSocketAddress tcpSocketAddress = NetconfConfigUtil.extractTCPNetconfAddress(context,EXCEPTION_MESSAGE);
42
43         if (sshSocketAddressOptional.isPresent()){
44             server = NetconfSSHServer.start(sshSocketAddressOptional.get().getPort(),tcpSocketAddress);
45             Thread serverThread = new  Thread(server,"netconf SSH server thread");
46             serverThread.setDaemon(true);
47             serverThread.start();
48             logger.trace("Netconf SSH  bridge up and running.");
49         } else {
50             logger.trace("No valid connection configuration for SSH bridge found.");
51             throw new Exception("No valid connection configuration for SSH bridge found.");
52         }
53     }
54
55     @Override
56     public void stop(BundleContext context) throws Exception {
57         if (server != null){
58             logger.trace("Netconf SSH bridge going down ...");
59             server.stop();
60             logger.trace("Netconf SSH bridge is down ...");
61         }
62     }
63 }