2 * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.netconf.transport.ssh;
10 import java.util.Collection;
12 import java.util.concurrent.ConcurrentHashMap;
13 import org.opendaylight.netconf.shaded.sshd.common.io.IoHandler;
14 import org.opendaylight.netconf.shaded.sshd.common.session.Session;
15 import org.opendaylight.netconf.shaded.sshd.netty.NettyIoService;
16 import org.opendaylight.netconf.transport.api.AbstractOverlayTransportStack;
17 import org.opendaylight.netconf.transport.api.TransportChannel;
18 import org.opendaylight.netconf.transport.api.TransportChannelListener;
19 import org.opendaylight.netconf.transport.api.TransportStack;
22 * An SSH {@link TransportStack}. Instances of this class are built indirectly.
24 public abstract sealed class SSHTransportStack extends AbstractOverlayTransportStack<SSHTransportChannel>
25 permits SSHClient, SSHServer {
26 protected final Map<Long, UserAuthSessionListener.AuthHandler> sessionAuthHandlers = new ConcurrentHashMap<>();
27 protected final Map<Long, Session> sessions = new ConcurrentHashMap<>();
28 protected NettyIoService ioService;
30 SSHTransportStack(final TransportChannelListener listener) {
35 protected void onUnderlayChannelEstablished(final TransportChannel underlayChannel) {
36 final var channel = underlayChannel.channel();
37 final var ioSession = new SshIoSession(ioService, getSessionFactory(), channel.localAddress());
38 channel.pipeline().addLast(ioSession.getHandler());
39 // authentication triggering and handlers processing is performed by UserAuthSessionListener
40 sessionAuthHandlers.put(ioSession.getId(), new UserAuthSessionListener.AuthHandler(
42 () -> addTransportChannel(new SSHTransportChannel(underlayChannel)),
44 () -> channel.close())
48 abstract IoHandler getSessionFactory();
50 public Collection<Session> getSessions() {
51 return sessions.values();