2 * Copyright (c) 2016 Inocybe Technologies and others. All rights reserved.
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
8 package org.opendaylight.netconf.northbound;
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;
24 * Create an MD-SAL NETCONF server using TCP.
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 {
32 String bindingAddress() default "0.0.0.0";
33 @AttributeDefinition(min = "1", max = "65535")
34 int portNumber() default 2831;
37 private static final Logger LOG = LoggerFactory.getLogger(NetconfNorthboundTcpServer.class);
39 private final ChannelFuture tcpServer;
42 public NetconfNorthboundTcpServer(
43 @Reference(target = "(type=netconf-server-dispatcher)") final NetconfServerDispatcher serverDispatcher,
44 final Configuration configuration) {
45 this(serverDispatcher, configuration.bindingAddress(), configuration.portNumber());
48 public NetconfNorthboundTcpServer(final NetconfServerDispatcher serverDispatcher, final String address,
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);
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());
64 if (tcpServer.isDone()) {
65 tcpServer.channel().close();
67 tcpServer.cancel(true);
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);