Honeynode test tool
[transportpce.git] / tests / honeynode / 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.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;
31
32 public final class NetconfTcpServerProvider extends ProviderTrait<NetconfTcpServerProvider.NetconfTcpServer> {
33
34     private static final Logger LOG = LoggerFactory.getLogger(NetconfTcpServerProvider.class);
35
36     @Inject
37     private NetconfServerDispatcher dispatcher;
38     @Inject
39     private NetconfConfiguration cfgAttributes;
40
41     @Override
42     protected NetconfTcpServer create() {
43         if (!cfgAttributes.isNetconfTcpEnabled()) {
44             LOG.debug("NETCONF TCP disabled, skipping initalization");
45             return null;
46         }
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());
50
51         ChannelFuture tcpServer = dispatcher.createServer(unresolved);
52         tcpServer.addListener(new TcpLoggingListener(unresolved));
53         return new NetconfTcpServer(tcpServer);
54     }
55
56     public static final class NetconfTcpServer {
57         private Object tcpServer;
58
59         NetconfTcpServer(final ChannelFuture tcpServer) {
60             this.tcpServer = tcpServer;
61         }
62
63         public Object getTcpServer() {
64             return tcpServer;
65         }
66     }
67
68     private static final class TcpLoggingListener implements GenericFutureListener<ChannelFuture> {
69         private final InetSocketAddress unresolved;
70
71         TcpLoggingListener(final InetSocketAddress unresolved) {
72             this.unresolved = unresolved;
73         }
74
75         @Override
76         public void operationComplete(ChannelFuture future) throws Exception {
77             if (future.isDone() && future.isSuccess()) {
78                 LOG.info("Netconf TCP endpoint started successfully at {}", unresolved);
79             } else {
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());
82             }
83         }
84     }
85 }