X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-tcp%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyang%2Fnetconf%2Fnorthbound%2Ftcp%2FNetconfNorthboundTcpModule.java;fp=opendaylight%2Fnetconf%2Fnetconf-tcp%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyang%2Fnetconf%2Fnorthbound%2Ftcp%2FNetconfNorthboundTcpModule.java;h=b5492a8139326cb29accdadf7979f742f4a740f2;hb=c0b54d22478f7b3350ed43375747a4da1b995c64;hp=0000000000000000000000000000000000000000;hpb=6feb4bef60d7710ed43fb69949034dce30879e80;p=controller.git diff --git a/opendaylight/netconf/netconf-tcp/src/main/java/org/opendaylight/controller/config/yang/netconf/northbound/tcp/NetconfNorthboundTcpModule.java b/opendaylight/netconf/netconf-tcp/src/main/java/org/opendaylight/controller/config/yang/netconf/northbound/tcp/NetconfNorthboundTcpModule.java new file mode 100644 index 0000000000..b5492a8139 --- /dev/null +++ b/opendaylight/netconf/netconf-tcp/src/main/java/org/opendaylight/controller/config/yang/netconf/northbound/tcp/NetconfNorthboundTcpModule.java @@ -0,0 +1,76 @@ +package org.opendaylight.controller.config.yang.netconf.northbound.tcp; + +import io.netty.channel.ChannelFuture; +import io.netty.util.concurrent.GenericFutureListener; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.UnknownHostException; +import org.opendaylight.controller.netconf.api.NetconfServerDispatcher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class NetconfNorthboundTcpModule extends org.opendaylight.controller.config.yang.netconf.northbound.tcp.AbstractNetconfNorthboundTcpModule { + + private static final Logger LOG = LoggerFactory.getLogger(NetconfNorthboundTcpModule.class); + + + public NetconfNorthboundTcpModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) { + super(identifier, dependencyResolver); + } + + 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) { + super(identifier, dependencyResolver, oldModule, oldInstance); + } + + @Override + public void customValidation() { + // add custom validation form module attributes here. + } + + @Override + public java.lang.AutoCloseable createInstance() { + final NetconfServerDispatcher dispatch = getDispatcherDependency(); + final ChannelFuture tcpServer = dispatch.createServer(getInetAddress()); + + tcpServer.addListener(new GenericFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isDone() && future.isSuccess()) { + LOG.info("Netconf TCP endpoint started successfully at {}", getInetAddress()); + } else { + LOG.warn("Unable to start TCP netconf server at {}", getInetAddress(), future.cause()); + throw new RuntimeException("Unable to start TCP netconf server", future.cause()); + } + } + }); + + return new NetconfServerCloseable(tcpServer); + } + + private InetSocketAddress getInetAddress() { + try { + final InetAddress inetAd = InetAddress.getByName(getBindingAddress().getIpv4Address() == null ? getBindingAddress().getIpv6Address().getValue() : getBindingAddress().getIpv4Address().getValue()); + return new InetSocketAddress(inetAd, getPort().getValue()); + } catch (final UnknownHostException e) { + throw new IllegalArgumentException("Unable to bind netconf endpoint to address " + getBindingAddress(), e); + } + } + + private static final class NetconfServerCloseable implements AutoCloseable { + private final ChannelFuture localServer; + + public NetconfServerCloseable(final ChannelFuture localServer) { + this.localServer = localServer; + } + + @Override + public void close() throws Exception { + if(localServer.isDone()) { + localServer.channel().close(); + } else { + localServer.cancel(true); + } + } + } + +}