Eliminate odl-netconf-tcp
[netconf.git] / apps / netconf-nb / src / main / java / org / opendaylight / netconf / northbound / NetconfNorthboundTcpServer.java
1 /*
2  * Copyright (c) 2016 Inocybe Technologies and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.northbound;
9
10 import io.netty.channel.ChannelFuture;
11 import java.net.InetSocketAddress;
12 import org.opendaylight.netconf.server.api.NetconfServerDispatcher;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
14 import org.osgi.service.component.annotations.Activate;
15 import org.osgi.service.component.annotations.Component;
16 import org.osgi.service.component.annotations.Reference;
17 import org.osgi.service.metatype.annotations.AttributeDefinition;
18 import org.osgi.service.metatype.annotations.Designate;
19 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Create an MD-SAL NETCONF server using TCP.
25  */
26 @Component(service = { }, configurationPid = "org.opendaylight.netconf.tcp", enabled = false)
27 @Designate(ocd = NetconfNorthboundTcpServer.Configuration.class)
28 public final class NetconfNorthboundTcpServer implements AutoCloseable {
29     @ObjectClassDefinition
30     public @interface Configuration {
31         @AttributeDefinition
32         String bindingAddress() default "0.0.0.0";
33         @AttributeDefinition(min = "1", max = "65535")
34         int portNumber() default 2831;
35     }
36
37     private static final Logger LOG = LoggerFactory.getLogger(NetconfNorthboundTcpServer.class);
38
39     private final ChannelFuture tcpServer;
40
41     @Activate
42     public NetconfNorthboundTcpServer(
43             @Reference(target = "(type=netconf-server-dispatcher)") final NetconfServerDispatcher serverDispatcher,
44             final Configuration configuration) {
45         this(serverDispatcher, configuration.bindingAddress(), configuration.portNumber());
46     }
47
48     public NetconfNorthboundTcpServer(final NetconfServerDispatcher serverDispatcher, final String address,
49             final int port) {
50         final InetSocketAddress inetAddress = getInetAddress(address, port);
51         tcpServer = serverDispatcher.createServer(inetAddress);
52         tcpServer.addListener(future -> {
53             if (future.isDone() && future.isSuccess()) {
54                 LOG.info("Netconf TCP endpoint started successfully at {}", inetAddress);
55             } else {
56                 LOG.warn("Unable to start TCP netconf server at {}", inetAddress, future.cause());
57                 throw new IllegalStateException("Unable to start TCP netconf server", future.cause());
58             }
59         });
60     }
61
62     @Override
63     public void close() {
64         if (tcpServer.isDone()) {
65             tcpServer.channel().close();
66         } else {
67             tcpServer.cancel(true);
68         }
69     }
70
71     private static InetSocketAddress getInetAddress(final String bindingAddress, final int portNumber) {
72         final var inetAd = IetfInetUtil.INSTANCE.inetAddressFor(IetfInetUtil.ipAddressFor(bindingAddress));
73         return new InetSocketAddress(inetAd, portNumber);
74     }
75 }