Introduce TransportSsh{Client,Server}
[netconf.git] / transport / transport-ssh / src / main / java / org / opendaylight / netconf / transport / ssh / SSHServer.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.transport.ssh;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.util.concurrent.ListenableFuture;
14 import io.netty.bootstrap.Bootstrap;
15 import io.netty.bootstrap.ServerBootstrap;
16 import io.netty.channel.group.DefaultChannelGroup;
17 import io.netty.util.concurrent.GlobalEventExecutor;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.netconf.shaded.sshd.common.io.IoHandler;
20 import org.opendaylight.netconf.shaded.sshd.server.ServerFactoryManager;
21 import org.opendaylight.netconf.shaded.sshd.server.session.SessionFactory;
22 import org.opendaylight.netconf.transport.api.TransportChannelListener;
23 import org.opendaylight.netconf.transport.api.TransportStack;
24 import org.opendaylight.netconf.transport.api.UnsupportedConfigurationException;
25 import org.opendaylight.netconf.transport.tcp.TCPClient;
26 import org.opendaylight.netconf.transport.tcp.TCPServer;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ssh.server.rev230417.SshServerGrouping;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.client.rev230417.TcpClientGrouping;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.server.rev230417.TcpServerGrouping;
30
31 /**
32  * A {@link TransportStack} acting as an SSH server.
33  */
34 public final class SSHServer extends SSHTransportStack {
35     private final ServerFactoryManager serverFactoryManager;
36     private final SessionFactory serverSessionFactory;
37
38     private SSHServer(final TransportChannelListener listener, final ServerFactoryManager serverFactoryManager) {
39         super(listener);
40         this.serverFactoryManager = requireNonNull(serverFactoryManager);
41         this.serverFactoryManager.addSessionListener(new UserAuthSessionListener(sessionAuthHandlers, sessions));
42         serverSessionFactory = new SessionFactory(serverFactoryManager);
43         ioService = new SshIoService(this.serverFactoryManager,
44                 new DefaultChannelGroup("sshd-server-channels", GlobalEventExecutor.INSTANCE),
45                 serverSessionFactory);
46     }
47
48     @Override
49     IoHandler getSessionFactory() {
50         return serverSessionFactory;
51     }
52
53     public static @NonNull ListenableFuture<SSHServer> connect(final TransportChannelListener listener,
54             final Bootstrap bootstrap, final TcpClientGrouping connectParams, final SshServerGrouping serverParams)
55                 throws UnsupportedConfigurationException {
56         final var server = newServer(listener, requireNonNull(serverParams), null);
57         return transformUnderlay(server, TCPClient.connect(server.asListener(), bootstrap, connectParams));
58     }
59
60     public static @NonNull ListenableFuture<SSHServer> listen(final TransportChannelListener listener,
61             final ServerBootstrap bootstrap, final TcpServerGrouping connectParams,
62             final SshServerGrouping serverParams) throws UnsupportedConfigurationException {
63         return listen(listener, bootstrap, connectParams, requireNonNull(serverParams), null);
64     }
65
66     /**
67      * Builds and starts SSH Server.
68      *
69      * @param listener server channel listener, required
70      * @param bootstrap server bootstrap instance, required
71      * @param connectParams tcp transport configuration, required
72      * @param serverParams ssh overlay configuration, optional if configurator is defined, required otherwise
73      * @param configurator server factory manager configurator, optional if serverParams is defined, required otherwise
74      * @return server instance as listenable future
75      * @throws UnsupportedConfigurationException if any of configurations is invalid or incomplete
76      * @throws NullPointerException if any of required parameters is null
77      * @throws IllegalArgumentException if both configurator and serverParams are null
78      */
79     public static @NonNull ListenableFuture<SSHServer> listen(final TransportChannelListener listener,
80             final ServerBootstrap bootstrap, final TcpServerGrouping connectParams,
81             final SshServerGrouping serverParams, final ServerFactoryManagerConfigurator configurator)
82                 throws UnsupportedConfigurationException {
83         checkArgument(serverParams != null || configurator != null,
84             "Neither server parameters nor factory configurator is defined");
85         final var server = newServer(listener, serverParams, configurator);
86         return transformUnderlay(server, TCPServer.listen(server.asListener(), bootstrap, connectParams));
87     }
88
89     private static SSHServer newServer(final TransportChannelListener listener, final SshServerGrouping serverParams,
90             final ServerFactoryManagerConfigurator configurator) throws UnsupportedConfigurationException {
91         return new SSHServer(listener, new TransportSshServer.Builder()
92             .serverParams(serverParams)
93             .configurator(configurator)
94             .buildChecked());
95     }
96 }