From da8db5e5aa1f30cbbd6ae411ea464db7b79d1dc2 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 18 Oct 2022 10:27:19 +0200 Subject: [PATCH] Add AsyncSshHandler.onConnectComplete() Rather than using an anonymous listener, document the lifecycle event, allowing us to refactor locking and extraction in the future. Also use getException() to check for success. JIRA: NETCONF-905 Change-Id: I8569ca007aefad634aff54294b475e565ac4f669 Signed-off-by: Robert Varga (cherry picked from commit 9b3050377a785f809f71dd6722a596e2fdbc4c65) (cherry picked from commit 71cb1c46c65a764e2c0b7d4d50e3b54709f22fe5) --- .../handler/ssh/client/AsyncSshHandler.java | 22 +++++++++++-------- .../ssh/client/AsyncSshHandlerTest.java | 3 +-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java index 325c7062b6..dd02c75a90 100644 --- a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java +++ b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java @@ -189,17 +189,21 @@ public class AsyncSshHandler extends ChannelOutboundHandlerAdapter { } LOG.debug("Starting SSH to {} on channel: {}", remoteAddress, ctx.channel()); - final ConnectFuture sshConnectionFuture = sshClient.connect(authenticationHandler.getUsername(), remoteAddress) + sshClient.connect(authenticationHandler.getUsername(), remoteAddress) // FIXME: this is a blocking call, we should handle this with a concurrently-scheduled timeout. We do not // have a Timer ready, so perhaps we should be using the event loop? - .verify(ctx.channel().config().getConnectTimeoutMillis(), TimeUnit.MILLISECONDS); - sshConnectionFuture.addListener(future -> { - if (future.isConnected()) { - handleSshSessionCreated(future, ctx); - } else { - handleSshSetupFailure(ctx, future.getException()); - } - }); + .verify(ctx.channel().config().getConnectTimeoutMillis(), TimeUnit.MILLISECONDS) + .addListener(future -> onConnectComplete(future, ctx)); + } + + private void onConnectComplete(final ConnectFuture future, final ChannelHandlerContext ctx) { + final var cause = future.getException(); + if (cause != null) { + handleSshSetupFailure(ctx, cause); + return; + } + + handleSshSessionCreated(future, ctx); } @Override diff --git a/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerTest.java b/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerTest.java index 46bce11599..0bb84e5a05 100644 --- a/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerTest.java +++ b/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerTest.java @@ -347,7 +347,7 @@ public class AsyncSshHandlerTest { private static ConnectFuture getSuccessConnectFuture(final ClientSession sshSession) { final ConnectFuture connectFuture = mock(ConnectFuture.class); - doReturn(true).when(connectFuture).isConnected(); + doReturn(null).when(connectFuture).getException(); doReturn(sshSession).when(connectFuture).getSession(); return connectFuture; @@ -490,7 +490,6 @@ public class AsyncSshHandlerTest { private static ConnectFuture getFailedConnectFuture() { final ConnectFuture connectFuture = mock(ConnectFuture.class); - doReturn(false).when(connectFuture).isConnected(); doReturn(new IllegalStateException()).when(connectFuture).getException(); return connectFuture; } -- 2.36.6