Centralize NETCONF over SSH subsystem name
[netconf.git] / protocol / netconf-server / src / main / java / org / opendaylight / netconf / server / NetconfServerFactoryImpl.java
index a27c260100ac06fb99411b7bcf66fa8213285228..9a4d577331753a6ad4280596ab5f51315f72ea23 100644 (file)
@@ -10,96 +10,38 @@ package org.opendaylight.netconf.server;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.util.concurrent.ListenableFuture;
-import io.netty.bootstrap.ServerBootstrap;
-import io.netty.channel.EventLoopGroup;
-import io.netty.util.concurrent.DefaultPromise;
-import io.netty.util.concurrent.EventExecutor;
-import io.netty.util.concurrent.GlobalEventExecutor;
-import java.util.List;
-import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.netconf.api.TransportConstants;
 import org.opendaylight.netconf.server.api.NetconfServerFactory;
-import org.opendaylight.netconf.shaded.sshd.server.SshServer;
-import org.opendaylight.netconf.transport.api.TransportChannel;
-import org.opendaylight.netconf.transport.api.TransportChannelListener;
 import org.opendaylight.netconf.transport.api.UnsupportedConfigurationException;
 import org.opendaylight.netconf.transport.ssh.SSHServer;
+import org.opendaylight.netconf.transport.ssh.SSHTransportStackFactory;
 import org.opendaylight.netconf.transport.ssh.ServerFactoryManagerConfigurator;
-import org.opendaylight.netconf.transport.tcp.NettyTransportSupport;
 import org.opendaylight.netconf.transport.tcp.TCPServer;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ssh.server.rev230417.SshServerGrouping;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.server.rev230417.TcpServerGrouping;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 public final class NetconfServerFactoryImpl implements NetconfServerFactory {
-    private static final Logger LOG = LoggerFactory.getLogger(NetconfServerFactoryImpl.class);
-    private static final TransportChannelListener EMPTY_LISTENER =  new ChannelInitializerListener(null, null);
-
-    private final EventLoopGroup parentGroup;
-    private final EventLoopGroup workerGroup;
+    private final SSHTransportStackFactory factory;
     private final ServerChannelInitializer channelInitializer;
-    private final TransportChannelListener transportChannelListener;
-
-    public NetconfServerFactoryImpl(final ServerChannelInitializer channelInitializer, final EventLoopGroup bossGroup,
-            final EventLoopGroup workerGroup) {
-        this(channelInitializer, bossGroup, workerGroup, GlobalEventExecutor.INSTANCE);
-    }
 
     public NetconfServerFactoryImpl(final ServerChannelInitializer channelInitializer,
-            final EventLoopGroup parentGroup, final EventLoopGroup workerGroup, final EventExecutor executor) {
-        this.parentGroup = requireNonNull(parentGroup);
-        this.workerGroup = requireNonNull(workerGroup);
-        this.channelInitializer = channelInitializer;
-        transportChannelListener = new ChannelInitializerListener(channelInitializer, executor);
-    }
-
-    @NonNull protected ServerBootstrap createBootstrap() {
-        return NettyTransportSupport.newServerBootstrap().group(parentGroup, workerGroup);
+            final SSHTransportStackFactory factory) {
+        this.factory = requireNonNull(factory);
+        this.channelInitializer = requireNonNull(channelInitializer);
     }
 
     @Override
     public ListenableFuture<TCPServer> createTcpServer(final TcpServerGrouping params)
             throws UnsupportedConfigurationException {
-        return TCPServer.listen(transportChannelListener, createBootstrap(), params);
-    }
-
-    @Override
-    public ListenableFuture<SSHServer> createSshServer(final TcpServerGrouping tcpParams,
-            final SshServerGrouping sshParams) throws UnsupportedConfigurationException {
-        return SSHServer.listen(EMPTY_LISTENER, createBootstrap(), tcpParams, sshParams);
+        return TCPServer.listen(new ServerTransportInitializer(channelInitializer), factory.newServerBootstrap(),
+            params);
     }
 
     @Override
     public ListenableFuture<SSHServer> createSshServer(final TcpServerGrouping tcpParams,
             final SshServerGrouping sshParams, final ServerFactoryManagerConfigurator configurator)
                 throws UnsupportedConfigurationException {
-        final var initializer = requireNonNull(channelInitializer);
-
-        return SSHServer.listen(EMPTY_LISTENER, createBootstrap(), tcpParams, sshParams, factoryManager -> {
-            if (configurator != null) {
-                configurator.configureServerFactoryManager(factoryManager);
-            }
-            if (factoryManager instanceof SshServer server) {
-                server.setSubsystemFactories(List.of(new NetconfSubsystemFactory(initializer)));
-            }
-        });
-    }
-
-    private record ChannelInitializerListener(
-            @Nullable ServerChannelInitializer channelInitializer,
-            @Nullable EventExecutor executor) implements TransportChannelListener {
-        @Override
-        public void onTransportChannelEstablished(final TransportChannel channel) {
-            LOG.debug("Transport channel {} established", channel);
-            if (channelInitializer != null && executor != null) {
-                channelInitializer.initialize(channel.channel(), new DefaultPromise<>(executor));
-            }
-        }
-
-        @Override
-        public void onTransportChannelFailed(final Throwable cause) {
-            LOG.error("Transport channel failed", cause);
-        }
+        return factory.listenServer(TransportConstants.SSH_SUBSYSTEM,
+            new ServerTransportInitializer(channelInitializer), tcpParams, sshParams, configurator);
     }
 }