Add copyright headers to sal-rest-connector
[netconf.git] / opendaylight / netconf / netconf-tcp / src / main / java / org / opendaylight / controller / config / yang / netconf / northbound / tcp / NetconfNorthboundTcpModule.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.config.yang.netconf.northbound.tcp;
10
11 import io.netty.channel.ChannelFuture;
12 import io.netty.util.concurrent.GenericFutureListener;
13 import java.net.InetAddress;
14 import java.net.InetSocketAddress;
15 import java.net.UnknownHostException;
16 import org.opendaylight.netconf.api.NetconfServerDispatcher;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class NetconfNorthboundTcpModule extends org.opendaylight.controller.config.yang.netconf.northbound.tcp.AbstractNetconfNorthboundTcpModule {
21
22     private static final Logger LOG = LoggerFactory.getLogger(NetconfNorthboundTcpModule.class);
23
24
25     public NetconfNorthboundTcpModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
26         super(identifier, dependencyResolver);
27     }
28
29     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) {
30         super(identifier, dependencyResolver, oldModule, oldInstance);
31     }
32
33     @Override
34     public void customValidation() {
35         // add custom validation form module attributes here.
36     }
37
38     @Override
39     public java.lang.AutoCloseable createInstance() {
40         final NetconfServerDispatcher dispatch = getDispatcherDependency();
41         final ChannelFuture tcpServer = dispatch.createServer(getInetAddress());
42
43         tcpServer.addListener(new GenericFutureListener<ChannelFuture>() {
44             @Override
45             public void operationComplete(ChannelFuture future) throws Exception {
46                 if (future.isDone() && future.isSuccess()) {
47                     LOG.info("Netconf TCP endpoint started successfully at {}", getInetAddress());
48                 } else {
49                     LOG.warn("Unable to start TCP netconf server at {}", getInetAddress(), future.cause());
50                     throw new RuntimeException("Unable to start TCP netconf server", future.cause());
51                 }
52             }
53         });
54
55         return new NetconfServerCloseable(tcpServer);
56     }
57
58     private InetSocketAddress getInetAddress() {
59         try {
60             final InetAddress inetAd = InetAddress.getByName(getBindingAddress().getIpv4Address() == null ? getBindingAddress().getIpv6Address().getValue() : getBindingAddress().getIpv4Address().getValue());
61             return new InetSocketAddress(inetAd, getPort().getValue());
62         } catch (final UnknownHostException e) {
63             throw new IllegalArgumentException("Unable to bind netconf endpoint to address " + getBindingAddress(), e);
64         }
65     }
66
67     private static final class NetconfServerCloseable implements AutoCloseable {
68         private final ChannelFuture localServer;
69
70         public NetconfServerCloseable(final ChannelFuture localServer) {
71             this.localServer = localServer;
72         }
73
74         @Override
75         public void close() throws Exception {
76             if(localServer.isDone()) {
77                 localServer.channel().close();
78             } else {
79                 localServer.cancel(true);
80             }
81         }
82     }
83
84 }