From: Robert Varga Date: Tue, 18 Oct 2022 08:44:52 +0000 (+0200) Subject: Remove AsyncSshHandler.handleSshSessionCreated() X-Git-Tag: v4.0.3~43 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=netconf.git;a=commitdiff_plain;h=811508380706eea604bab58f3725bc9bf64790aa Remove AsyncSshHandler.handleSshSessionCreated() This method is the synchronized part of onConnectComplete(), move it closer to its source and split out onAuthComplete(). Also switch to using getException() to check for success/failure. JIRA: NETCONF-905 Change-Id: Ic8a49491afa92d68cb0585538a3fc9ffb044e9cb Signed-off-by: Robert Varga --- 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 dd02c75a90..ffc52d1f15 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 @@ -102,28 +102,6 @@ public class AsyncSshHandler extends ChannelOutboundHandlerAdapter { negotiationFuture); } - private synchronized void handleSshSessionCreated(final ConnectFuture future, final ChannelHandlerContext ctx) { - try { - LOG.trace("SSH session created on channel: {}", ctx.channel()); - - session = future.getSession(); - verify(session instanceof NettyAwareClientSession, "Unexpected session %s", session); - - final AuthFuture authenticateFuture = authenticationHandler.authenticate(session); - final NettyAwareClientSession localSession = (NettyAwareClientSession) session; - authenticateFuture.addListener(future1 -> { - if (future1.isSuccess()) { - handleSshAuthenticated(localSession, ctx); - } else { - handleSshSetupFailure(ctx, new AuthenticationFailedException("Authentication failed", - future1.getException())); - } - }); - } catch (final IOException e) { - handleSshSetupFailure(ctx, e); - } - } - private synchronized void handleSshAuthenticated(final NettyAwareClientSession newSession, final ChannelHandlerContext ctx) { LOG.debug("SSH session authenticated on channel: {}, server version: {}", ctx.channel(), @@ -203,7 +181,36 @@ public class AsyncSshHandler extends ChannelOutboundHandlerAdapter { return; } - handleSshSessionCreated(future, ctx); + final var clientSession = future.getSession(); + LOG.trace("SSH session {} created on channel: {}", clientSession, ctx.channel()); + verify(clientSession instanceof NettyAwareClientSession, "Unexpected session %s", clientSession); + onConnectComplete((NettyAwareClientSession) clientSession, ctx); + } + + private synchronized void onConnectComplete(final NettyAwareClientSession clientSession, + final ChannelHandlerContext ctx) { + session = clientSession; + + final AuthFuture authFuture; + try { + authFuture = authenticationHandler.authenticate(clientSession); + } catch (final IOException e) { + handleSshSetupFailure(ctx, e); + return; + } + + authFuture.addListener(future -> onAuthComplete(future, clientSession, ctx)); + } + + private void onAuthComplete(final AuthFuture future, final NettyAwareClientSession clientSession, + final ChannelHandlerContext ctx) { + final var cause = future.getException(); + if (cause != null) { + handleSshSetupFailure(ctx, new AuthenticationFailedException("Authentication failed", cause)); + return; + } + + handleSshAuthenticated(clientSession, 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 0bb84e5a05..90e45a953d 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 @@ -341,7 +341,7 @@ public class AsyncSshHandlerTest { private static AuthFuture getSuccessAuthFuture() { final AuthFuture authFuture = mock(AuthFuture.class); - doReturn(true).when(authFuture).isSuccess(); + doReturn(null).when(authFuture).getException(); return authFuture; } @@ -467,7 +467,6 @@ public class AsyncSshHandlerTest { private static AuthFuture getFailedAuthFuture() { final AuthFuture authFuture = mock(AuthFuture.class); - doReturn(false).when(authFuture).isSuccess(); doReturn(new IllegalStateException()).when(authFuture).getException(); return authFuture; }