Add transport-{api,tcp}
[netconf.git] / transport / transport-tcp / src / main / java / org / opendaylight / netconf / transport / tcp / TCPClient.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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.transport.tcp;
9
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.SettableFuture;
13 import io.netty.bootstrap.Bootstrap;
14 import io.netty.channel.Channel;
15 import io.netty.channel.ChannelFutureListener;
16 import io.netty.channel.ChannelHandler.Sharable;
17 import io.netty.channel.ChannelInitializer;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.netconf.transport.api.TransportChannelListener;
20 import org.opendaylight.netconf.transport.api.UnsupportedConfigurationException;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.client.rev220524.TcpClientGrouping;
22 import org.opendaylight.yangtools.yang.common.Empty;
23
24 /**
25  * A {@link TCPTransportStack} acting as a TCP client.
26  */
27 public final class TCPClient extends TCPTransportStack {
28     @Sharable
29     private static final class ConnectChannelInitializer extends ChannelInitializer<Channel> {
30         static final ConnectChannelInitializer INSTANCE = new ConnectChannelInitializer();
31
32         @Override
33         protected void initChannel(final Channel ch) {
34             // No-op
35         }
36     }
37
38     private static final @NonNull ListenableFuture<Empty> SHUTDOWN_FUTURE = Futures.immediateFuture(Empty.value());
39
40     private TCPClient(final TransportChannelListener listener) {
41         super(listener);
42     }
43
44     /**
45      * Attempt to establish a {@link TCPClient} by connecting to a remote address.
46      *
47      * @param listener {@link TransportChannelListener} to notify when the session is established
48      * @param bootstrap Client {@link Bootstrap} to use for the underlying Netty channel
49      * @param connectParams Connection parameters
50      * @return A future
51      * @throws UnsupportedConfigurationException when {@code connectParams} contains an unsupported options
52      * @throws NullPointerException if any argument is {@code null}
53      */
54     public static @NonNull ListenableFuture<TCPClient> connect(final TransportChannelListener listener,
55             final Bootstrap bootstrap, final TcpClientGrouping connectParams)
56                 throws UnsupportedConfigurationException {
57         NettyTransportSupport.configureKeepalives(bootstrap, connectParams.getKeepalives());
58
59         final var ret = SettableFuture.<TCPClient>create();
60         bootstrap
61             .handler(ConnectChannelInitializer.INSTANCE)
62             .connect(
63                 socketAddressOf(connectParams.requireRemoteAddress(), connectParams.requireRemotePort()),
64                 socketAddressOf(connectParams.getLocalAddress(), connectParams.getLocalPort()))
65             .addListener((ChannelFutureListener) future -> {
66                 if (future.isSuccess()) {
67                     // Order of operations is important here: the stack should be visible before the underlying channel
68                     final var stack = new TCPClient(listener);
69                     ret.set(stack);
70                     stack.addTransportChannel(new TCPTransportChannel(future.channel()));
71                 } else {
72                     ret.setException(future.cause());
73                 }
74             });
75         return ret;
76     }
77
78     @Override
79     protected ListenableFuture<Empty> startShutdown() {
80         return SHUTDOWN_FUTURE;
81     }
82 }