e87b5c40ef26923e88736a7db1dbbccb7acff8b3
[netconf.git] / transport / transport-ssh / src / main / java / org / opendaylight / netconf / transport / ssh / SSHClient.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 com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import io.netty.bootstrap.Bootstrap;
15 import io.netty.bootstrap.ServerBootstrap;
16 import io.netty.channel.ChannelHandlerContext;
17 import java.io.IOException;
18 import java.util.concurrent.ScheduledExecutorService;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.netconf.shaded.sshd.client.future.AuthFuture;
21 import org.opendaylight.netconf.shaded.sshd.common.session.Session;
22 import org.opendaylight.netconf.shaded.sshd.netty.NettyIoServiceFactoryFactory;
23 import org.opendaylight.netconf.transport.api.TransportChannelListener;
24 import org.opendaylight.netconf.transport.api.TransportStack;
25 import org.opendaylight.netconf.transport.api.UnsupportedConfigurationException;
26 import org.opendaylight.netconf.transport.tcp.TCPClient;
27 import org.opendaylight.netconf.transport.tcp.TCPServer;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ssh.client.rev231228.SshClientGrouping;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.client.rev231228.TcpClientGrouping;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.server.rev231228.TcpServerGrouping;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * A {@link TransportStack} acting as an SSH client.
36  */
37 public final class SSHClient extends SSHTransportStack {
38     private static final Logger LOG = LoggerFactory.getLogger(SSHClient.class);
39
40     private final String subsystem;
41
42     private SSHClient(final String subsystem, final TransportChannelListener listener,
43             final TransportSshClient sshClient) {
44         super(listener, sshClient, sshClient.getSessionFactory());
45         // Mirrors check in ChannelSubsystem's constructor
46         if (subsystem.isBlank()) {
47             throw new IllegalArgumentException("Blank subsystem");
48         }
49         this.subsystem = subsystem;
50     }
51
52     static SSHClient of(final NettyIoServiceFactoryFactory ioServiceFactory,
53             final ScheduledExecutorService executorService, final String subsystem,
54             final TransportChannelListener listener, final SshClientGrouping clientParams,
55             final ClientFactoryManagerConfigurator configurator) throws UnsupportedConfigurationException {
56         return new SSHClient(subsystem, listener, new TransportSshClient.Builder(ioServiceFactory, executorService)
57             .transportParams(clientParams.getTransportParams())
58             .keepAlives(clientParams.getKeepalives())
59             .clientIdentity(clientParams.getClientIdentity())
60             .serverAuthentication(clientParams.getServerAuthentication())
61             .configurator(configurator)
62             .buildChecked());
63     }
64
65     @NonNull ListenableFuture<SSHClient> connect(final Bootstrap bootstrap, final TcpClientGrouping connectParams)
66             throws UnsupportedConfigurationException {
67         return transformUnderlay(this, TCPClient.connect(asListener(), bootstrap, connectParams));
68     }
69
70     @NonNull ListenableFuture<SSHClient> listen(final ServerBootstrap bootstrap, final TcpServerGrouping listenParams)
71             throws UnsupportedConfigurationException {
72         return transformUnderlay(this, TCPServer.listen(asListener(), bootstrap, listenParams));
73     }
74
75     @Override
76     void onKeyEstablished(final Session session) throws IOException {
77         // server key is accepted, trigger authentication flow
78         final var sessionId = sessionId(session);
79         LOG.debug("Authenticating session {}", sessionId);
80         cast(session).auth().addListener(future -> onAuthComplete(future, sessionId));
81     }
82
83     private void onAuthComplete(final AuthFuture future, final Long sessionId) {
84         if (!future.isSuccess()) {
85             LOG.info("Session {} authentication failed", sessionId);
86             deleteSession(sessionId);
87         } else {
88             LOG.debug("Session {} authenticated", sessionId);
89         }
90     }
91
92     @Override
93     void onAuthenticated(final Session session) throws IOException {
94         final var sessionId = sessionId(session);
95         LOG.debug("Opening \"{}\" subsystem on session {}", subsystem, sessionId);
96
97         final var underlay = getUnderlayOf(sessionId);
98         final var clientSession = cast(session);
99         final var channel = clientSession.createSubsystemChannel(subsystem);
100         channel.onClose(() -> clientSession.close(true));
101         Futures.addCallback(channel.open(underlay), new FutureCallback<>() {
102             @Override
103             public void onSuccess(final ChannelHandlerContext result) {
104                 LOG.debug("Opened \"{}\" subsystem on session {}", subsystem, sessionId);
105                 transportEstablished(sessionId, result);
106             }
107
108             @Override
109             public void onFailure(final Throwable cause) {
110                 LOG.error("Failed to open \"{}\" subsystem on session {}", subsystem, sessionId, cause);
111                 deleteSession(sessionId);
112             }
113         }, MoreExecutors.directExecutor());
114     }
115
116     private static TransportClientSession cast(final Session session) throws IOException {
117         return TransportUtils.checkCast(TransportClientSession.class, session);
118     }
119 }