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.infra.distro.InitializationException;
23 import io.fd.honeycomb.northbound.NetconfConfiguration;
24 import io.netty.channel.ChannelFuture;
25 import io.netty.util.concurrent.GenericFutureListener;
26 import java.net.InetAddress;
27 import java.net.InetSocketAddress;
28 import org.opendaylight.netconf.api.NetconfServerDispatcher;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public final class NetconfTcpServerProvider extends ProviderTrait<NetconfTcpServerProvider.NetconfTcpServer> {
34 private static final Logger LOG = LoggerFactory.getLogger(NetconfTcpServerProvider.class);
37 private NetconfServerDispatcher dispatcher;
39 private NetconfConfiguration cfgAttributes;
42 protected NetconfTcpServer create() {
43 if (!cfgAttributes.isNetconfTcpEnabled()) {
44 LOG.debug("NETCONF TCP disabled, skipping initalization");
47 LOG.info("Starting NETCONF TCP");
48 final InetAddress name = InetAddresses.forString(cfgAttributes.netconfTcpBindingAddress.get());
49 final InetSocketAddress unresolved = new InetSocketAddress(name, cfgAttributes.netconfTcpBindingPort.get());
51 ChannelFuture tcpServer = dispatcher.createServer(unresolved);
52 tcpServer.addListener(new TcpLoggingListener(unresolved));
53 return new NetconfTcpServer(tcpServer);
56 public static final class NetconfTcpServer {
57 private Object tcpServer;
59 NetconfTcpServer(final ChannelFuture tcpServer) {
60 this.tcpServer = tcpServer;
63 public Object getTcpServer() {
68 private static final class TcpLoggingListener implements GenericFutureListener<ChannelFuture> {
69 private final InetSocketAddress unresolved;
71 TcpLoggingListener(final InetSocketAddress unresolved) {
72 this.unresolved = unresolved;
76 public void operationComplete(ChannelFuture future) throws Exception {
77 if (future.isDone() && future.isSuccess()) {
78 LOG.info("Netconf TCP endpoint started successfully at {}", unresolved);
80 LOG.warn("Unable to start TCP netconf server at {}", unresolved, future.cause());
81 throw new InitializationException("Unable to start TCP netconf server", future.cause());