Merge "MD-SAL StatisticsManager- Stopping statistics thread until i fix all the relev...
[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
34     @Override
35     public void start(BundleContext context) throws Exception {
36
37         logger.trace("Starting netconf SSH  bridge.");
38
39         Optional<InetSocketAddress> sshSocketAddressOptional = NetconfConfigUtil.extractSSHNetconfAddress(context);
40         Optional<InetSocketAddress> tcpSocketAddressOptional = NetconfConfigUtil.extractTCPNetconfAddress(context);
41
42         if (sshSocketAddressOptional.isPresent() && tcpSocketAddressOptional.isPresent()){
43             server = NetconfSSHServer.start(sshSocketAddressOptional.get().getPort(),tcpSocketAddressOptional.get());
44             Thread serverThread = new  Thread(server,"netconf SSH server thread");
45             serverThread.setDaemon(true);
46             serverThread.start();
47             logger.trace("Netconf SSH  bridge up and running.");
48         } else {
49             logger.trace("No valid connection configuration for SSH bridge found.");
50             throw new Exception("No valid connection configuration for SSH bridge found.");
51         }
52     }
53
54     @Override
55     public void stop(BundleContext context) throws Exception {
56         if (server != null){
57             logger.trace("Netconf SSH bridge going down ...");
58             server.stop();
59             logger.trace("Netconf SSH bridge is down ...");
60         }
61     }
62 }