Refresh IETF client/server models
[netconf.git] / transport / transport-ssh / src / main / java / org / opendaylight / netconf / transport / ssh / SSHTransportStackFactory.java
1 /*
2  * Copyright (c) 2023 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 org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.netconf.shaded.sshd.netty.NettyIoServiceFactoryFactory;
16 import org.opendaylight.netconf.transport.api.TransportChannelListener;
17 import org.opendaylight.netconf.transport.api.UnsupportedConfigurationException;
18 import org.opendaylight.netconf.transport.tcp.BootstrapFactory;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ssh.client.rev240208.SshClientGrouping;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ssh.server.rev240208.SshServerGrouping;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.client.rev240208.TcpClientGrouping;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.server.rev240208.TcpServerGrouping;
23
24 /**
25  * A {@link BootstrapFactory} additionally capable of instantiating {@link SSHClient}s and {@link SSHServer}s.
26  */
27 public final class SSHTransportStackFactory extends BootstrapFactory {
28     private final NettyIoServiceFactoryFactory ioServiceFactory = new NettyIoServiceFactoryFactory(group);
29
30     public SSHTransportStackFactory(final @NonNull String groupName, final int groupThreads) {
31         super(groupName, groupThreads);
32     }
33
34     public SSHTransportStackFactory(final @NonNull String groupName, final int groupThreads,
35             final @NonNull String parentGroupName, final int parentGroupThreads) {
36         super(groupName, groupThreads, parentGroupName, parentGroupThreads);
37     }
38
39     public @NonNull ListenableFuture<SSHClient> connectClient(final String subsystem,
40             final TransportChannelListener listener, final TcpClientGrouping connectParams,
41             final SshClientGrouping clientParams) throws UnsupportedConfigurationException {
42         return SSHClient.of(ioServiceFactory, group, subsystem, listener, clientParams, null)
43             .connect(newBootstrap(), connectParams);
44     }
45
46     /** Builds the SSH Client and initiates connection.
47      *
48      * @param subsystem bound subsystem name
49      * @param listener client channel listener, required
50      * @param connectParams TCP transport configuration addressing server to connect, required
51      * @param clientParams SSH overlay configuration, required, should contain username
52      * @param configurator client factory manager configurator, optional
53      * @return a future producing {@link SSHClient}
54      * @throws UnsupportedConfigurationException if any of configurations is invalid or incomplete
55      * @throws NullPointerException if any of required parameters is null
56      */
57     public @NonNull ListenableFuture<SSHClient> connectClient(final String subsystem,
58             final TransportChannelListener listener, final TcpClientGrouping connectParams,
59             final SshClientGrouping clientParams, final ClientFactoryManagerConfigurator configurator)
60             throws UnsupportedConfigurationException {
61         return SSHClient.of(ioServiceFactory, group, subsystem, listener, clientParams, configurator)
62             .connect(newBootstrap(), connectParams);
63     }
64
65     public @NonNull ListenableFuture<SSHClient> listenClient(final String subsystem,
66             final TransportChannelListener listener, final TcpServerGrouping listenParams,
67             final SshClientGrouping clientParams) throws UnsupportedConfigurationException {
68         return SSHClient.of(ioServiceFactory, group, subsystem, listener, clientParams, null)
69             .listen(newServerBootstrap(), listenParams);
70     }
71
72     /**
73      * Builds and starts Call-Home SSH Client.
74      *
75      * @param subsystem bound subsystem name
76      * @param listener client channel listener, required
77      * @param listenParams TCP transport configuration addressing inbound connection, required
78      * @param clientParams SSH overlay configuration, required, should contain username
79      * @param configurator client factory manager configurator, optional
80      * @return a future producing {@link SSHClient}
81      * @throws UnsupportedConfigurationException if any of configurations is invalid or incomplete
82      * @throws NullPointerException if any of required parameters is null
83      */
84     public @NonNull ListenableFuture<SSHClient> listenClient(final String subsystem,
85             final TransportChannelListener listener, final TcpServerGrouping listenParams,
86             final SshClientGrouping clientParams, final ClientFactoryManagerConfigurator configurator)
87             throws UnsupportedConfigurationException {
88         return SSHClient.of(ioServiceFactory, group, subsystem, listener, clientParams, configurator)
89             .listen(newServerBootstrap(), listenParams);
90     }
91
92     public @NonNull ListenableFuture<SSHServer> connectServer(final String subsystem,
93             final TransportChannelListener listener, final TcpClientGrouping connectParams,
94             final SshServerGrouping serverParams) throws UnsupportedConfigurationException {
95         return SSHServer.of(ioServiceFactory, group, subsystem, listener, requireNonNull(serverParams), null)
96             .connect(newBootstrap(), connectParams);
97     }
98
99     /**
100      * Builds and starts a Call-Home SSH Server, initiates connection to client.
101      *
102      * @param subsystem bound subsystem name
103      * @param listener server channel listener, required
104      * @param connectParams TCP transport configuration addressing client to be connected, required
105      * @param serverParams SSH overlay configuration, optional if configurator is defined, required otherwise
106      * @param configurator server factory manager configurator, optional if serverParams is defined, required otherwise
107      * @return a future producing {@link SSHServer}
108      * @throws UnsupportedConfigurationException if any of configurations is invalid or incomplete
109      * @throws NullPointerException if any of required parameters is null
110      * @throws IllegalArgumentException if both configurator and serverParams are null
111      */
112     public @NonNull ListenableFuture<SSHServer> connectServer(final String subsystem,
113             final TransportChannelListener listener, final TcpClientGrouping connectParams,
114             final SshServerGrouping serverParams, final ServerFactoryManagerConfigurator configurator)
115             throws UnsupportedConfigurationException {
116         checkArgument(serverParams != null || configurator != null,
117             "Neither server parameters nor factory configurator is defined");
118         return SSHServer.of(ioServiceFactory, group, subsystem, listener, serverParams, configurator)
119             .connect(newBootstrap(), connectParams);
120     }
121
122     public @NonNull ListenableFuture<SSHServer> listenServer(final String subsystem,
123             final TransportChannelListener listener, final TcpServerGrouping connectParams,
124             final SshServerGrouping serverParams) throws UnsupportedConfigurationException {
125         return listenServer(subsystem, listener, connectParams, requireNonNull(serverParams), null);
126     }
127
128     /**
129      * Builds and starts SSH Server.
130      *
131      * @param listener server channel listener, required
132      * @param subsystem bound subsystem name
133      * @param listenParams TCP transport configuration, required
134      * @param serverParams SSH overlay configuration, optional if configurator is defined, required otherwise
135      * @param configurator server factory manager configurator, optional if serverParams is defined, required otherwise
136      * @return a future producing {@link SSHServer}
137      * @throws UnsupportedConfigurationException if any of configurations is invalid or incomplete
138      * @throws NullPointerException if any of required parameters is null
139      * @throws IllegalArgumentException if both configurator and serverParams are null
140      */
141     public @NonNull ListenableFuture<SSHServer> listenServer(final String subsystem,
142             final TransportChannelListener listener, final TcpServerGrouping listenParams,
143             final SshServerGrouping serverParams, final ServerFactoryManagerConfigurator configurator)
144                 throws UnsupportedConfigurationException {
145         checkArgument(serverParams != null || configurator != null,
146             "Neither server parameters nor factory configurator is defined");
147         return SSHServer.of(ioServiceFactory, group, subsystem, listener, serverParams, configurator)
148             .listen(newServerBootstrap(), listenParams);
149     }
150 }