f45f7ba80f5a073328ad612b53364926e03d244d
[netconf.git] / netconf / netconf-tcp / src / main / java / org / opendaylight / netconf / tcp / NetconfNorthboundTcpServer.java
1 /*
2  * Copyright (c) 2016 Inocybe Technologies 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.netconf.tcp;
9
10 import io.netty.channel.ChannelFuture;
11 import java.net.InetAddress;
12 import java.net.InetSocketAddress;
13 import java.net.UnknownHostException;
14 import org.opendaylight.netconf.api.NetconfServerDispatcher;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Create an MD-SAL NETCONF server using TCP.
22  */
23 public class NetconfNorthboundTcpServer implements AutoCloseable {
24
25     private static final Logger LOG = LoggerFactory.getLogger(NetconfNorthboundTcpServer.class);
26
27     private final ChannelFuture tcpServer;
28
29     public NetconfNorthboundTcpServer(final NetconfServerDispatcher netconfServerDispatcher,
30                                       final String address,
31                                       final String port) {
32         InetSocketAddress inetAddress = getInetAddress(address, port);
33         tcpServer = netconfServerDispatcher.createServer(inetAddress);
34         tcpServer.addListener(future -> {
35             if (future.isDone() && future.isSuccess()) {
36                 LOG.info("Netconf TCP endpoint started successfully at {}", inetAddress);
37             } else {
38                 LOG.warn("Unable to start TCP netconf server at {}", inetAddress, future.cause());
39                 throw new RuntimeException("Unable to start TCP netconf server", future.cause());
40             }
41         });
42     }
43
44     private InetSocketAddress getInetAddress(final String bindingAddress, final String portNumber) {
45         try {
46             IpAddress ipAddress = IpAddressBuilder.getDefaultInstance(bindingAddress);
47             final InetAddress inetAd = InetAddress.getByName(ipAddress.getIpv4Address() == null
48                     ? ipAddress.getIpv6Address().getValue() : ipAddress.getIpv4Address().getValue());
49             return new InetSocketAddress(inetAd, Integer.valueOf(portNumber));
50         } catch (final UnknownHostException e) {
51             throw new IllegalArgumentException("Unable to bind netconf tcp endpoint to address " + bindingAddress, e);
52         }
53     }
54
55     @Override
56     public void close() throws Exception {
57         if (tcpServer.isDone()) {
58             tcpServer.channel().close();
59         } else {
60             tcpServer.cancel(true);
61         }
62     }
63 }