2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.northbound.netconf;
19 import com.google.common.net.InetAddresses;
20 import com.google.inject.Inject;
21 import io.fd.honeycomb.binding.init.ProviderTrait;
22 import io.fd.honeycomb.data.init.ShutdownHandler;
23 import io.fd.honeycomb.infra.distro.InitializationException;
24 import io.fd.honeycomb.northbound.NetconfConfiguration;
25 import io.netty.channel.ChannelFuture;
26 import io.netty.util.concurrent.GenericFutureListener;
27 import java.net.InetAddress;
28 import java.net.InetSocketAddress;
29 import org.opendaylight.netconf.api.NetconfServerDispatcher;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public final class NetconfTcpServerProvider extends ProviderTrait<NetconfTcpServerProvider.NetconfTcpServer> {
35 private static final Logger LOG = LoggerFactory.getLogger(NetconfTcpServerProvider.class);
38 private NetconfServerDispatcher dispatcher;
40 private NetconfConfiguration cfgAttributes;
42 private ShutdownHandler shutdownHandler;
45 protected NetconfTcpServer create() {
46 if (!cfgAttributes.isNetconfTcpEnabled()) {
47 LOG.debug("NETCONF TCP disabled, skipping initalization");
50 LOG.info("Starting NETCONF TCP");
51 final InetAddress name = InetAddresses.forString(cfgAttributes.netconfTcpBindingAddress.get());
52 final InetSocketAddress unresolved = new InetSocketAddress(name, cfgAttributes.netconfTcpBindingPort.get());
54 ChannelFuture tcpServerFuture = dispatcher.createServer(unresolved);
55 tcpServerFuture.addListener(new TcpLoggingListener(unresolved));
56 final NetconfTcpServer tcpServer = new NetconfTcpServer(tcpServerFuture);
57 shutdownHandler.register("netconf-northbound-tcp-server", tcpServer);
61 public static final class NetconfTcpServer implements AutoCloseable {
62 private ChannelFuture channelFuture;
64 NetconfTcpServer(final ChannelFuture channelFuture) {
65 this.channelFuture = channelFuture;
69 public void close() throws Exception {
70 LOG.info("Stopping Netconf TCP server");
71 channelFuture.channel().close();
72 LOG.info("Netconf TCP server stopped successfully");
76 private static final class TcpLoggingListener implements GenericFutureListener<ChannelFuture> {
77 private final InetSocketAddress unresolved;
79 TcpLoggingListener(final InetSocketAddress unresolved) {
80 this.unresolved = unresolved;
84 public void operationComplete(ChannelFuture future) throws Exception {
85 if (future.isDone() && future.isSuccess()) {
86 LOG.info("Netconf TCP endpoint started successfully at {}", unresolved);
88 LOG.warn("Unable to start TCP netconf server at {}", unresolved, future.cause());
89 throw new InitializationException("Unable to start TCP netconf server", future.cause());