Make TCP netconf endpoint configurable
[controller.git] / opendaylight / netconf / netconf-tcp / src / main / java / org / opendaylight / controller / config / yang / netconf / northbound / tcp / NetconfNorthboundTcpModule.java
1 package org.opendaylight.controller.config.yang.netconf.northbound.tcp;
2
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;
11
12 public class NetconfNorthboundTcpModule extends org.opendaylight.controller.config.yang.netconf.northbound.tcp.AbstractNetconfNorthboundTcpModule {
13
14     private static final Logger LOG = LoggerFactory.getLogger(NetconfNorthboundTcpModule.class);
15
16
17     public NetconfNorthboundTcpModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
18         super(identifier, dependencyResolver);
19     }
20
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);
23     }
24
25     @Override
26     public void customValidation() {
27         // add custom validation form module attributes here.
28     }
29
30     @Override
31     public java.lang.AutoCloseable createInstance() {
32         final NetconfServerDispatcher dispatch = getDispatcherDependency();
33         final ChannelFuture tcpServer = dispatch.createServer(getInetAddress());
34
35         tcpServer.addListener(new GenericFutureListener<ChannelFuture>() {
36             @Override
37             public void operationComplete(ChannelFuture future) throws Exception {
38                 if (future.isDone() && future.isSuccess()) {
39                     LOG.info("Netconf TCP endpoint started successfully at {}", getInetAddress());
40                 } else {
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());
43                 }
44             }
45         });
46
47         return new NetconfServerCloseable(tcpServer);
48     }
49
50     private InetSocketAddress getInetAddress() {
51         try {
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);
56         }
57     }
58
59     private static final class NetconfServerCloseable implements AutoCloseable {
60         private final ChannelFuture localServer;
61
62         public NetconfServerCloseable(final ChannelFuture localServer) {
63             this.localServer = localServer;
64         }
65
66         @Override
67         public void close() throws Exception {
68             if(localServer.isDone()) {
69                 localServer.channel().close();
70             } else {
71                 localServer.cancel(true);
72             }
73         }
74     }
75
76 }