0a8cf7fecf83b8beee605ac25fcafa26273af01b
[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 java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import io.netty.bootstrap.Bootstrap;
17 import io.netty.bootstrap.ServerBootstrap;
18 import io.netty.channel.ChannelHandlerContext;
19 import java.io.IOException;
20 import java.util.concurrent.ScheduledExecutorService;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.netconf.shaded.sshd.common.session.Session;
23 import org.opendaylight.netconf.shaded.sshd.netty.NettyIoServiceFactoryFactory;
24 import org.opendaylight.netconf.transport.api.TransportChannelListener;
25 import org.opendaylight.netconf.transport.api.TransportStack;
26 import org.opendaylight.netconf.transport.api.UnsupportedConfigurationException;
27 import org.opendaylight.netconf.transport.tcp.TCPClient;
28 import org.opendaylight.netconf.transport.tcp.TCPServer;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ssh.server.rev231228.SshServerGrouping;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.client.rev231228.TcpClientGrouping;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.server.rev231228.TcpServerGrouping;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * A {@link TransportStack} acting as an SSH server.
37  */
38 public final class SSHServer extends SSHTransportStack {
39     private static final Logger LOG = LoggerFactory.getLogger(SSHServer.class);
40
41     private final String subsystem;
42
43     private SSHServer(final String subsystem, final TransportChannelListener listener,
44             final TransportSshServer sshServer) {
45         super(listener, sshServer, sshServer.getSessionFactory());
46         this.subsystem = requireNonNull(subsystem);
47     }
48
49     static SSHServer of(final NettyIoServiceFactoryFactory ioServiceFactory,
50             final ScheduledExecutorService executorService, final String subsystem,
51             final TransportChannelListener listener, final SshServerGrouping serverParams,
52             final ServerFactoryManagerConfigurator configurator) throws UnsupportedConfigurationException {
53         return new SSHServer(subsystem, listener,
54             new TransportSshServer.Builder(ioServiceFactory, executorService)
55                 .serverParams(serverParams)
56                 .configurator(configurator)
57                 .buildChecked());
58     }
59
60     @NonNull ListenableFuture<SSHServer> connect(final Bootstrap bootstrap, final TcpClientGrouping connectParams)
61             throws UnsupportedConfigurationException {
62         return transformUnderlay(this, TCPClient.connect(asListener(), bootstrap, connectParams));
63     }
64
65     @NonNull ListenableFuture<SSHServer> listen(final ServerBootstrap bootstrap, final TcpServerGrouping connectParams)
66             throws UnsupportedConfigurationException {
67         return transformUnderlay(this, TCPServer.listen(asListener(), bootstrap, connectParams));
68     }
69
70     @Override
71     void onKeyEstablished(final Session session) {
72         // No-op
73     }
74
75     @Override
76     void onAuthenticated(final Session session) throws IOException {
77         final var sessionId = sessionId(session);
78         LOG.debug("Awaiting \"{}\" subsystem on session {}", subsystem, sessionId);
79
80         Futures.addCallback(cast(session).attachUnderlay(subsystem, getUnderlayOf(sessionId)), new FutureCallback<>() {
81             @Override
82             public void onSuccess(final ChannelHandlerContext result) {
83                 LOG.debug("Established \"{}\" subsystem on session {}", subsystem, sessionId);
84                 // Note: we re-validating the underlay ... we may need to refactor state management to make this
85                 //       non-awkward
86                 transportEstablished(sessionId, result);
87             }
88
89             @Override
90             public void onFailure(final Throwable cause) {
91                 LOG.debug("Binding to \"{}\" subsystem on session {} failed", subsystem, sessionId, cause);
92                 deleteSession(sessionId);
93             }
94         }, MoreExecutors.directExecutor());
95     }
96
97     private static TransportServerSession cast(final Session session) throws IOException {
98         return TransportUtils.checkCast(TransportServerSession.class, session);
99     }
100 }