From: Robert Varga Date: Mon, 16 Oct 2023 18:34:33 +0000 (+0200) Subject: Use SettableFuture instead of Promise X-Git-Tag: v7.0.0~426 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=e67d5e33037c4b9e49de145bdd482ca4002b0767;p=netconf.git Use SettableFuture instead of Promise Perform correct bridging when we have a channel handy -- that allows us to reuse the channel's thread. JIRA: NETCONF-1108 Change-Id: Icdc1437f9ed9e0f560773185b2ba1f6124b0845b Signed-off-by: Robert Varga --- diff --git a/protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactory.java b/protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactory.java index 5d19d58010..5a99f5a542 100644 --- a/protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactory.java +++ b/protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactory.java @@ -7,7 +7,7 @@ */ package org.opendaylight.netconf.client; -import io.netty.util.concurrent.Future; +import com.google.common.util.concurrent.ListenableFuture; import org.opendaylight.netconf.client.conf.NetconfClientConfiguration; import org.opendaylight.netconf.transport.api.UnsupportedConfigurationException; @@ -15,16 +15,14 @@ import org.opendaylight.netconf.transport.api.UnsupportedConfigurationException; * Basic interface for Netconf client factory. */ public interface NetconfClientFactory extends AutoCloseable { - /** - * Create netconf client. Network communication has to be set up based on network protocol specified in + * Create a NETCONF client. Network communication has to be set up based on network protocol specified in * clientConfiguration * * @param clientConfiguration configuration - * @return netconf client as {@link NetconfClientSession} future + * @return A future producing the {@link NetconfClientSession} * @throws UnsupportedConfigurationException if any transport configuration parameters is invalid */ - Future createClient(NetconfClientConfiguration clientConfiguration) + ListenableFuture createClient(NetconfClientConfiguration clientConfiguration) throws UnsupportedConfigurationException; - } diff --git a/protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactoryImpl.java b/protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactoryImpl.java index bf563493fb..06b95737b7 100644 --- a/protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactoryImpl.java +++ b/protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactoryImpl.java @@ -13,12 +13,10 @@ import static org.opendaylight.netconf.client.conf.NetconfClientConfiguration.Ne import static org.opendaylight.netconf.client.conf.NetconfClientConfiguration.NetconfClientProtocol.TLS; import com.google.common.collect.ImmutableSet; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.SettableFuture; import io.netty.util.HashedWheelTimer; import io.netty.util.Timer; -import io.netty.util.concurrent.DefaultPromise; -import io.netty.util.concurrent.Future; -import io.netty.util.concurrent.GlobalEventExecutor; -import io.netty.util.concurrent.Promise; import javax.inject.Singleton; import org.opendaylight.netconf.client.conf.NetconfClientConfiguration; import org.opendaylight.netconf.transport.api.TransportChannel; @@ -56,30 +54,30 @@ public class NetconfClientFactoryImpl implements NetconfClientFactory { } @Override - public Future createClient(final NetconfClientConfiguration configuration) + public ListenableFuture createClient(final NetconfClientConfiguration configuration) throws UnsupportedConfigurationException { final var protocol = configuration.getProtocol(); - final var promise = new DefaultPromise(GlobalEventExecutor.INSTANCE); + final var future = SettableFuture.create(); final var channelInitializer = new ClientChannelInitializer(createNegotiatorFactory(configuration), () -> configuration.getSessionListener()); final var bootstrap = factory.newBootstrap(); if (TCP.equals(protocol)) { - TCPClient.connect(new ClientTransportChannelListener(promise, channelInitializer), bootstrap, + TCPClient.connect(new ClientTransportChannelListener(future, channelInitializer), bootstrap, configuration.getTcpParameters()); } else if (TLS.equals(protocol)) { if (configuration.getTlsParameters() != null) { - TLSClient.connect(new ClientTransportChannelListener(promise, channelInitializer), bootstrap, + TLSClient.connect(new ClientTransportChannelListener(future, channelInitializer), bootstrap, configuration.getTcpParameters(), configuration.getTlsParameters()); } else { - TLSClient.connect(new ClientTransportChannelListener(promise, channelInitializer), bootstrap, + TLSClient.connect(new ClientTransportChannelListener(future, channelInitializer), bootstrap, configuration.getTcpParameters(), configuration.getTransportSslHandlerFactory()); } } else if (SSH.equals(protocol)) { - factory.connectClient("netconf", new ClientTransportChannelListener(promise, channelInitializer), + factory.connectClient("netconf", new ClientTransportChannelListener(future, channelInitializer), configuration.getTcpParameters(), configuration.getSshParameters()); } - return promise; + return future; } private NetconfClientSessionNegotiatorFactory createNegotiatorFactory( @@ -96,21 +94,31 @@ public class NetconfClientFactoryImpl implements NetconfClientFactory { } private record ClientTransportChannelListener( - Promise promise, + SettableFuture future, ClientChannelInitializer initializer) implements TransportChannelListener { ClientTransportChannelListener { - requireNonNull(promise); + requireNonNull(future); requireNonNull(initializer); } @Override public void onTransportChannelEstablished(final TransportChannel channel) { - initializer.initialize(channel.channel(), promise); + final var nettyChannel = channel.channel(); + final var promise = nettyChannel.eventLoop().newPromise(); + initializer.initialize(nettyChannel, promise); + promise.addListener(ignored -> { + final var cause = promise.cause(); + if (cause != null) { + future.setException(cause); + } else { + future.set(promise.getNow()); + } + }); } @Override public void onTransportChannelFailed(final Throwable cause) { - promise.setFailure(cause); + future.setException(cause); } } }