bc94e596d77ceabd53667c5a9ab74a2928f74fff
[controller.git] / opendaylight / netconf / netconf-tcp / src / main / java / org / opendaylight / controller / netconf / tcp / osgi / NetconfTCPActivator.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.netconf.tcp.osgi;
10
11 import com.google.common.base.Optional;
12 import java.net.InetSocketAddress;
13 import org.opendaylight.controller.netconf.tcp.netty.ProxyServer;
14 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
15 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil.InfixProp;
16 import org.osgi.framework.BundleActivator;
17 import org.osgi.framework.BundleContext;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Opens TCP port specified in config.ini, creates bridge between this port and local netconf server.
23  */
24 public class NetconfTCPActivator implements BundleActivator {
25     private static final Logger logger = LoggerFactory.getLogger(NetconfTCPActivator.class);
26     private ProxyServer proxyServer;
27
28     @Override
29     public void start(BundleContext context) {
30         final Optional<InetSocketAddress> maybeAddress = NetconfConfigUtil.extractNetconfServerAddress(context, InfixProp.tcp);
31         if (maybeAddress.isPresent() == false) {
32             logger.debug("Netconf tcp server is not configured to start");
33             return;
34         }
35         InetSocketAddress address = maybeAddress.get();
36         if (address.getAddress().isAnyLocalAddress()) {
37             logger.warn("Unprotected netconf TCP address is configured to ANY local address. This is a security risk. " +
38                     "Consider changing {} to 127.0.0.1", NetconfConfigUtil.getNetconfServerAddressKey(InfixProp.tcp));
39         }
40         logger.info("Starting TCP netconf server at {}", address);
41         proxyServer = new ProxyServer(address, NetconfConfigUtil.getNetconfLocalAddress());
42     }
43
44     @Override
45     public void stop(BundleContext context) {
46         if (proxyServer != null) {
47             proxyServer.close();
48         }
49     }
50 }