Refresh IETF client/server models
[netconf.git] / apps / netconf-nb / src / main / java / org / opendaylight / netconf / northbound / TcpServerTransport.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 java.util.concurrent.ExecutionException;
11 import org.opendaylight.netconf.server.ServerTransportInitializer;
12 import org.opendaylight.netconf.transport.api.UnsupportedConfigurationException;
13 import org.opendaylight.netconf.transport.tcp.TCPServer;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.server.rev231228.netconf.server.listen.stack.grouping.transport.tls.tls.TcpServerParametersBuilder;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.server.rev231228.TcpServerGrouping;
18 import org.opendaylight.yangtools.yang.common.Uint16;
19 import org.osgi.service.component.annotations.Activate;
20 import org.osgi.service.component.annotations.Component;
21 import org.osgi.service.component.annotations.Deactivate;
22 import org.osgi.service.component.annotations.Reference;
23 import org.osgi.service.metatype.annotations.AttributeDefinition;
24 import org.osgi.service.metatype.annotations.Designate;
25 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Create an MD-SAL NETCONF server using TCP.
31  */
32 @Component(service = {}, configurationPid = "org.opendaylight.netconf.tcp", enabled = false)
33 @Designate(ocd = TcpServerTransport.Configuration.class)
34 public final class TcpServerTransport implements AutoCloseable {
35     @ObjectClassDefinition
36     public @interface Configuration {
37         @AttributeDefinition
38         String bindingAddress() default "0.0.0.0";
39
40         @AttributeDefinition(min = "1", max = "65535")
41         int portNumber() default 2831;
42     }
43
44     private static final Logger LOG = LoggerFactory.getLogger(TcpServerTransport.class);
45
46     private final TCPServer tcpServer;
47
48     @Activate
49     public TcpServerTransport(@Reference final TransportFactoryHolder factoryHolder,
50             @Reference final OSGiNetconfServer backend, final Configuration configuration) {
51         // FIXME: create an instantiation and do not use TLS
52         this(factoryHolder, backend.serverTransportInitializer(), new TcpServerParametersBuilder()
53             .setLocalAddress(IetfInetUtil.ipAddressFor(configuration.bindingAddress()))
54             .setLocalPort(new PortNumber(Uint16.valueOf(configuration.portNumber())))
55             .build());
56     }
57
58     public TcpServerTransport(final TransportFactoryHolder factoryHolder, final ServerTransportInitializer initializer,
59             final TcpServerGrouping listenParams) {
60         final var localAddr = listenParams.requireLocalAddress().stringValue();
61         final var localPort = listenParams.requireLocalPort().getValue();
62
63         try {
64             tcpServer = TCPServer.listen(initializer, factoryHolder.factory().newServerBootstrap(), listenParams).get();
65         } catch (UnsupportedConfigurationException | ExecutionException | InterruptedException e) {
66             LOG.warn("Could not start TCP NETCONF server at {}:{}", localAddr, localPort, e);
67             throw new IllegalStateException("Could not start TCP NETCONF server", e);
68         }
69
70         LOG.info("TCP NETCONF server at {}:{} started", localAddr, localPort);
71     }
72
73     @Deactivate
74     @Override
75     public void close() {
76         try {
77             tcpServer.shutdown().get();
78         } catch (ExecutionException | InterruptedException e) {
79             LOG.warn("Could not stop TCP NETCONF server {}", tcpServer, e);
80         }
81     }
82 }