1 package org.opendaylight.controller.config.yang.netconf.northbound.tcp;
3 import io.netty.channel.ChannelFuture;
4 import io.netty.util.concurrent.GenericFutureListener;
5 import java.net.InetAddress;
6 import java.net.InetSocketAddress;
7 import java.net.UnknownHostException;
8 import org.opendaylight.controller.netconf.api.NetconfServerDispatcher;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
12 public class NetconfNorthboundTcpModule extends org.opendaylight.controller.config.yang.netconf.northbound.tcp.AbstractNetconfNorthboundTcpModule {
14 private static final Logger LOG = LoggerFactory.getLogger(NetconfNorthboundTcpModule.class);
17 public NetconfNorthboundTcpModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
18 super(identifier, dependencyResolver);
21 public NetconfNorthboundTcpModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, org.opendaylight.controller.config.yang.netconf.northbound.tcp.NetconfNorthboundTcpModule oldModule, java.lang.AutoCloseable oldInstance) {
22 super(identifier, dependencyResolver, oldModule, oldInstance);
26 public void customValidation() {
27 // add custom validation form module attributes here.
31 public java.lang.AutoCloseable createInstance() {
32 final NetconfServerDispatcher dispatch = getDispatcherDependency();
33 final ChannelFuture tcpServer = dispatch.createServer(getInetAddress());
35 tcpServer.addListener(new GenericFutureListener<ChannelFuture>() {
37 public void operationComplete(ChannelFuture future) throws Exception {
38 if (future.isDone() && future.isSuccess()) {
39 LOG.info("Netconf TCP endpoint started successfully at {}", getInetAddress());
41 LOG.warn("Unable to start TCP netconf server at {}", getInetAddress(), future.cause());
42 throw new RuntimeException("Unable to start TCP netconf server", future.cause());
47 return new NetconfServerCloseable(tcpServer);
50 private InetSocketAddress getInetAddress() {
52 final InetAddress inetAd = InetAddress.getByName(getBindingAddress().getIpv4Address() == null ? getBindingAddress().getIpv6Address().getValue() : getBindingAddress().getIpv4Address().getValue());
53 return new InetSocketAddress(inetAd, getPort().getValue());
54 } catch (final UnknownHostException e) {
55 throw new IllegalArgumentException("Unable to bind netconf endpoint to address " + getBindingAddress(), e);
59 private static final class NetconfServerCloseable implements AutoCloseable {
60 private final ChannelFuture localServer;
62 public NetconfServerCloseable(final ChannelFuture localServer) {
63 this.localServer = localServer;
67 public void close() throws Exception {
68 if(localServer.isDone()) {
69 localServer.channel().close();
71 localServer.cancel(true);