Remove AsyncSshHandler.handleSshSessionCreated() 17/102717/2
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 18 Oct 2022 08:44:52 +0000 (10:44 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 18 Oct 2022 10:04:07 +0000 (12:04 +0200)
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 <robert.varga@pantheon.tech>
netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java
netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerTest.java

index dd02c75a90f3b611ef312ea332bec117a3da5152..ffc52d1f15f689f59aee3cf85e7b17d1fefe7c81 100644 (file)
@@ -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
index 0bb84e5a05cf989f1168964efde03934bab09dea..90e45a953dace9835453e16766774b510dc69f78 100644 (file)
@@ -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;
     }