2 * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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.transport.tcp;
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.rev231228.TcpClientGrouping;
22 import org.opendaylight.yangtools.yang.common.Empty;
25 * A {@link TCPTransportStack} acting as a TCP client.
27 public final class TCPClient extends TCPTransportStack {
29 private static final class ConnectChannelInitializer extends ChannelInitializer<Channel> {
30 static final ConnectChannelInitializer INSTANCE = new ConnectChannelInitializer();
33 protected void initChannel(final Channel ch) {
38 private static final @NonNull ListenableFuture<Empty> SHUTDOWN_FUTURE = Futures.immediateFuture(Empty.value());
40 private TCPClient(final TransportChannelListener listener) {
45 * Attempt to establish a {@link TCPClient} by connecting to a remote address.
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
51 * @throws UnsupportedConfigurationException when {@code connectParams} contains an unsupported options
52 * @throws NullPointerException if any argument is {@code null}
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());
59 final var ret = SettableFuture.<TCPClient>create();
61 .handler(ConnectChannelInitializer.INSTANCE)
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);
70 stack.addTransportChannel(new TCPTransportChannel(future.channel()));
72 ret.setException(future.cause());
79 protected ListenableFuture<Empty> startShutdown() {
80 return SHUTDOWN_FUTURE;