2 * Copyright (c) 2016 Inocybe Technologies and others. All rights reserved.
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
8 package org.opendaylight.netconf.tcp;
10 import io.netty.channel.ChannelFuture;
11 import java.net.InetAddress;
12 import java.net.InetSocketAddress;
13 import org.opendaylight.netconf.api.NetconfServerDispatcher;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * Create an MD-SAL NETCONF server using TCP.
22 public class NetconfNorthboundTcpServer implements AutoCloseable {
23 private static final Logger LOG = LoggerFactory.getLogger(NetconfNorthboundTcpServer.class);
25 private final ChannelFuture tcpServer;
27 public NetconfNorthboundTcpServer(final NetconfServerDispatcher netconfServerDispatcher, final String address,
29 final InetSocketAddress inetAddress = getInetAddress(address, port);
30 tcpServer = netconfServerDispatcher.createServer(inetAddress);
31 tcpServer.addListener(future -> {
32 if (future.isDone() && future.isSuccess()) {
33 LOG.info("Netconf TCP endpoint started successfully at {}", inetAddress);
35 LOG.warn("Unable to start TCP netconf server at {}", inetAddress, future.cause());
36 throw new IllegalStateException("Unable to start TCP netconf server", future.cause());
41 private static InetSocketAddress getInetAddress(final String bindingAddress, final String portNumber) {
42 final IpAddress ipAddress = IetfInetUtil.ipAddressFor(bindingAddress);
43 final InetAddress inetAd = IetfInetUtil.INSTANCE.inetAddressFor(ipAddress);
44 return new InetSocketAddress(inetAd, Integer.parseInt(portNumber));
49 if (tcpServer.isDone()) {
50 tcpServer.channel().close();
52 tcpServer.cancel(true);