be270c58d3fb61292b09165887da33d88e2ef3b7
[transportpce.git] / tests / honeynode / 1.2.1 / netconf / src / main / java / io / fd / honeycomb / northbound / netconf / NetconfTcpServerProvider.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.northbound.netconf;
18
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;
32
33 public final class NetconfTcpServerProvider extends ProviderTrait<NetconfTcpServerProvider.NetconfTcpServer> {
34
35     private static final Logger LOG = LoggerFactory.getLogger(NetconfTcpServerProvider.class);
36
37     @Inject
38     private NetconfServerDispatcher dispatcher;
39     @Inject
40     private NetconfConfiguration cfgAttributes;
41     @Inject
42     private ShutdownHandler shutdownHandler;
43
44     @Override
45     protected NetconfTcpServer create() {
46         if (!cfgAttributes.isNetconfTcpEnabled()) {
47             LOG.debug("NETCONF TCP disabled, skipping initalization");
48             return null;
49         }
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());
53
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);
58         return tcpServer;
59     }
60
61     public static final class NetconfTcpServer implements AutoCloseable {
62         private ChannelFuture channelFuture;
63
64         NetconfTcpServer(final ChannelFuture channelFuture) {
65             this.channelFuture = channelFuture;
66         }
67
68         @Override
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");
73         }
74     }
75
76     private static final class TcpLoggingListener implements GenericFutureListener<ChannelFuture> {
77         private final InetSocketAddress unresolved;
78
79         TcpLoggingListener(final InetSocketAddress unresolved) {
80             this.unresolved = unresolved;
81         }
82
83         @Override
84         public void operationComplete(ChannelFuture future) throws Exception {
85             if (future.isDone() && future.isSuccess()) {
86                 LOG.info("Netconf TCP endpoint started successfully at {}", unresolved);
87             } else {
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());
90             }
91         }
92     }
93 }