Clean up SSHTransportStack.sessionEvent() 95/108395/3
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 13 Oct 2023 12:51:43 +0000 (14:51 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 13 Oct 2023 13:11:02 +0000 (15:11 +0200)
Event type dispatch should forward to individual methods, so we can
specialize them.

JIRA: NETCONF-1108
Change-Id: I84cf131ccd91b84f587f9ff0ca824e064032c3d7
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
transport/transport-ssh/src/main/java/org/opendaylight/netconf/transport/ssh/SSHClient.java
transport/transport-ssh/src/main/java/org/opendaylight/netconf/transport/ssh/SSHServer.java
transport/transport-ssh/src/main/java/org/opendaylight/netconf/transport/ssh/SSHTransportStack.java

index da7475c914cd31c25bda38abe7be6df6bb9e2081..60152c57d4fb0d5066751a9087ef090cacade898 100644 (file)
@@ -11,7 +11,10 @@ import com.google.common.util.concurrent.ListenableFuture;
 import io.netty.bootstrap.Bootstrap;
 import io.netty.bootstrap.ServerBootstrap;
 import io.netty.channel.EventLoopGroup;
+import java.io.IOException;
 import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
+import org.opendaylight.netconf.shaded.sshd.common.session.Session;
 import org.opendaylight.netconf.shaded.sshd.netty.NettyIoServiceFactoryFactory;
 import org.opendaylight.netconf.transport.api.TransportChannelListener;
 import org.opendaylight.netconf.transport.api.TransportStack;
@@ -50,4 +53,18 @@ public final class SSHClient extends SSHTransportStack {
             throws UnsupportedConfigurationException {
         return transformUnderlay(this, TCPServer.listen(asListener(), bootstrap, listenParams));
     }
+
+    @Override
+    void onKeyEstablished(final Session session) throws IOException {
+        if (!(session instanceof ClientSession clientSession)) {
+            throw new IOException("Unexpected session " + session);
+        }
+
+        // server key is accepted, trigger authentication flow
+        clientSession.auth().addListener(future -> {
+            if (!future.isSuccess()) {
+                deleteSession(session);
+            }
+        });
+    }
 }
\ No newline at end of file
index ffdb8aed84b9a82514a638cf7fd1d70b7e6a9e74..bbbf4a933bc7e2b8fbccc3101aa6020ec7cff9c2 100644 (file)
@@ -12,6 +12,7 @@ import io.netty.bootstrap.Bootstrap;
 import io.netty.bootstrap.ServerBootstrap;
 import io.netty.channel.EventLoopGroup;
 import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.netconf.shaded.sshd.common.session.Session;
 import org.opendaylight.netconf.shaded.sshd.netty.NettyIoServiceFactoryFactory;
 import org.opendaylight.netconf.shaded.sshd.server.subsystem.SubsystemFactory;
 import org.opendaylight.netconf.transport.api.TransportChannelListener;
@@ -50,4 +51,9 @@ public final class SSHServer extends SSHTransportStack {
             throws UnsupportedConfigurationException {
         return transformUnderlay(this, TCPServer.listen(asListener(), bootstrap, connectParams));
     }
+
+    @Override
+    void onKeyEstablished(final Session session) {
+        // No-op
+    }
 }
\ No newline at end of file
index 587425fbe82495497d72787c031ceaa943b336df..e7090ee82c4ef9259fd1b6d25835839bb213045c 100644 (file)
@@ -14,7 +14,6 @@ import java.util.Collection;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.function.Consumer;
-import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
 import org.opendaylight.netconf.shaded.sshd.common.FactoryManager;
 import org.opendaylight.netconf.shaded.sshd.common.io.IoHandler;
 import org.opendaylight.netconf.shaded.sshd.common.session.Session;
@@ -82,25 +81,30 @@ public abstract sealed class SSHTransportStack extends AbstractOverlayTransportS
 
     @Override
     public final void sessionEvent(final Session session, final Event event) {
-        if (Event.KeyEstablished == event && session instanceof ClientSession clientSession) {
-            // server key is accepted, trigger authentication flow
-            try {
-                clientSession.auth().addListener(future -> {
-                    if (!future.isSuccess()) {
-                        deleteSession(session);
-                    }
-                });
-            } catch (IOException e) {
-                sessionException(session, e);
-            }
-        }
-        if (Event.Authenticated == event) {
-            // auth success
-            completeAuth(idOf(session), underlay -> addTransportChannel(new SSHTransportChannel(underlay)));
+        switch (event) {
+            case Authenticated:
+                onAuthenticated(session);
+                break;
+            case KeyEstablished:
+                try {
+                    onKeyEstablished(session);
+                } catch (IOException e) {
+                    sessionException(session, e);
+                }
+                break;
+            default:
+                // No-op
         }
     }
 
-    private void deleteSession(final Session session) {
+    abstract void onKeyEstablished(Session session) throws IOException;
+
+    private void onAuthenticated(final Session session) {
+        // auth success
+        completeAuth(idOf(session), underlay -> addTransportChannel(new SSHTransportChannel(underlay)));
+    }
+
+    final void deleteSession(final Session session) {
         final var id = idOf(session);
         sessions.remove(id);
         // auth failure, close underlay if any