Remove AsyncSshHandler.handleSshAuthenticated() 18/102718/3
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 18 Oct 2022 08:55:29 +0000 (10:55 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 18 Oct 2022 10:06:26 +0000 (12:06 +0200)
This method is the locked bottom of onAuthComplete(), rename it and
split out onOpenComplete(), making the flow more linear. Also switch
to use getException() to discern success/failure, fixing up badly-named
mocks.

JIRA: NETCONF-905
Change-Id: I7a0c20b872b19b66e99144d0ce83636d6b4cd400
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 ffc52d1f15f689f59aee3cf85e7b17d1fefe7c81..36cc81d6cf170a8cfe0f706f2de98184f7b99877 100644 (file)
@@ -24,6 +24,7 @@ import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.Authenticat
 import org.opendaylight.netconf.shaded.sshd.client.channel.ClientChannel;
 import org.opendaylight.netconf.shaded.sshd.client.future.AuthFuture;
 import org.opendaylight.netconf.shaded.sshd.client.future.ConnectFuture;
+import org.opendaylight.netconf.shaded.sshd.client.future.OpenFuture;
 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
 import org.opendaylight.netconf.shaded.sshd.core.CoreModuleProperties;
 import org.slf4j.Logger;
@@ -102,26 +103,6 @@ public class AsyncSshHandler extends ChannelOutboundHandlerAdapter {
                 negotiationFuture);
     }
 
-    private synchronized void handleSshAuthenticated(final NettyAwareClientSession newSession,
-            final ChannelHandlerContext ctx) {
-        LOG.debug("SSH session authenticated on channel: {}, server version: {}", ctx.channel(),
-            newSession.getServerVersion());
-
-        try {
-            channel = newSession.createSubsystemChannel(SUBSYSTEM, ctx);
-            channel.setStreaming(ClientChannel.Streaming.Async);
-            channel.open().addListener(future -> {
-                if (future.isOpened()) {
-                    handleSshChanelOpened(ctx);
-                } else {
-                    handleSshSetupFailure(ctx, future.getException());
-                }
-            });
-        } catch (final IOException e) {
-            handleSshSetupFailure(ctx, e);
-        }
-    }
-
     private synchronized void handleSshChanelOpened(final ChannelHandlerContext ctx) {
         LOG.trace("SSH subsystem channel opened successfully on channel: {}", ctx.channel());
 
@@ -210,7 +191,35 @@ public class AsyncSshHandler extends ChannelOutboundHandlerAdapter {
             return;
         }
 
-        handleSshAuthenticated(clientSession, ctx);
+        onAuthComplete(clientSession, ctx);
+    }
+
+    private synchronized void onAuthComplete(final NettyAwareClientSession clientSession,
+            final ChannelHandlerContext ctx) {
+        LOG.debug("SSH session authenticated on channel: {}, server version: {}", ctx.channel(),
+            clientSession.getServerVersion());
+
+        final OpenFuture openFuture;
+        try {
+            channel = clientSession.createSubsystemChannel(SUBSYSTEM, ctx);
+            channel.setStreaming(ClientChannel.Streaming.Async);
+            openFuture = channel.open();
+        } catch (final IOException e) {
+            handleSshSetupFailure(ctx, e);
+            return;
+        }
+
+        openFuture.addListener(future -> onOpenComplete(future, ctx));
+    }
+
+    private void onOpenComplete(final OpenFuture future, final ChannelHandlerContext ctx) {
+        final var cause = future.getException();
+        if (cause != null) {
+            handleSshSetupFailure(ctx, cause);
+            return;
+        }
+
+        handleSshChanelOpened(ctx);
     }
 
     @Override
index 90e45a953dace9835453e16766774b510dc69f78..fb2fd690f9990be6dd050fb0d6f1bd76b9f9d078 100644 (file)
@@ -334,9 +334,9 @@ public class AsyncSshHandlerTest {
     }
 
     private static OpenFuture getSuccessOpenFuture() {
-        final OpenFuture failedOpenFuture = mock(OpenFuture.class);
-        doReturn(true).when(failedOpenFuture).isOpened();
-        return failedOpenFuture;
+        final OpenFuture openFuture = mock(OpenFuture.class);
+        doReturn(null).when(openFuture).getException();
+        return openFuture;
     }
 
     private static AuthFuture getSuccessAuthFuture() {
@@ -472,10 +472,9 @@ public class AsyncSshHandlerTest {
     }
 
     private static OpenFuture getFailedOpenFuture() {
-        final OpenFuture authFuture = mock(OpenFuture.class);
-        doReturn(false).when(authFuture).isOpened();
-        doReturn(new IllegalStateException()).when(authFuture).getException();
-        return authFuture;
+        final OpenFuture openFuture = mock(OpenFuture.class);
+        doReturn(new IllegalStateException()).when(openFuture).getException();
+        return openFuture;
     }
 
     @Test