2 * Copyright (c) 2014 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.config.yang.netconf.northbound.tcp;
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;
20 public class NetconfNorthboundTcpModule extends org.opendaylight.controller.config.yang.netconf.northbound.tcp.AbstractNetconfNorthboundTcpModule {
22 private static final Logger LOG = LoggerFactory.getLogger(NetconfNorthboundTcpModule.class);
25 public NetconfNorthboundTcpModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
26 super(identifier, dependencyResolver);
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);
34 public void customValidation() {
35 // add custom validation form module attributes here.
39 public java.lang.AutoCloseable createInstance() {
40 final NetconfServerDispatcher dispatch = getDispatcherDependency();
41 final ChannelFuture tcpServer = dispatch.createServer(getInetAddress());
43 tcpServer.addListener(new GenericFutureListener<ChannelFuture>() {
45 public void operationComplete(ChannelFuture future) throws Exception {
46 if (future.isDone() && future.isSuccess()) {
47 LOG.info("Netconf TCP endpoint started successfully at {}", getInetAddress());
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());
55 return new NetconfServerCloseable(tcpServer);
58 private InetSocketAddress getInetAddress() {
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);
67 private static final class NetconfServerCloseable implements AutoCloseable {
68 private final ChannelFuture localServer;
70 public NetconfServerCloseable(final ChannelFuture localServer) {
71 this.localServer = localServer;
75 public void close() throws Exception {
76 if(localServer.isDone()) {
77 localServer.channel().close();
79 localServer.cancel(true);