Bind SshClient/SshServer to NettyIoServiceFactoryFactory
[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.ListenableFuture;
11 import io.netty.bootstrap.Bootstrap;
12 import io.netty.bootstrap.ServerBootstrap;
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.channel.group.DefaultChannelGroup;
15 import io.netty.util.concurrent.GlobalEventExecutor;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.netconf.shaded.sshd.client.ClientFactoryManager;
18 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSessionImpl;
19 import org.opendaylight.netconf.shaded.sshd.client.session.SessionFactory;
20 import org.opendaylight.netconf.shaded.sshd.common.io.IoHandler;
21 import org.opendaylight.netconf.shaded.sshd.netty.NettyIoServiceFactoryFactory;
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.client.rev230417.SshClientGrouping;
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 client.
33  */
34 public final class SSHClient extends SSHTransportStack {
35     private final ClientFactoryManager clientFactoryManager;
36     private final SessionFactory sessionFactory;
37
38     private SSHClient(final TransportChannelListener listener, final ClientFactoryManager clientFactoryManager,
39             final String username) {
40         super(listener);
41         this.clientFactoryManager = clientFactoryManager;
42         this.clientFactoryManager.addSessionListener(new UserAuthSessionListener(sessionAuthHandlers, sessions));
43         sessionFactory = new SessionFactory(clientFactoryManager) {
44             @Override
45             protected ClientSessionImpl setupSession(final ClientSessionImpl session) {
46                 session.setUsername(username);
47                 return session;
48             }
49         };
50         ioService = new SshIoService(this.clientFactoryManager,
51                 new DefaultChannelGroup("sshd-client-channels", GlobalEventExecutor.INSTANCE),
52                 sessionFactory);
53     }
54
55     static SSHClient of(final NettyIoServiceFactoryFactory ioServiceFactory, final EventLoopGroup group,
56             final TransportChannelListener listener, final SshClientGrouping clientParams)
57                 throws UnsupportedConfigurationException {
58         final var clientIdentity = clientParams.getClientIdentity();
59         final var username = clientIdentity == null ? "" : clientIdentity.getUsername();
60
61         return new SSHClient(listener, new TransportSshClient.Builder(ioServiceFactory, group)
62             .transportParams(clientParams.getTransportParams())
63             .keepAlives(clientParams.getKeepalives())
64             .clientIdentity(clientParams.getClientIdentity())
65             .serverAuthentication(clientParams.getServerAuthentication())
66             .buildChecked(), username);
67     }
68
69     @Override
70     IoHandler getSessionFactory() {
71         return sessionFactory;
72     }
73
74     @NonNull ListenableFuture<SSHClient> connect(final Bootstrap bootstrap, final TcpClientGrouping connectParams)
75             throws UnsupportedConfigurationException {
76         return transformUnderlay(this, TCPClient.connect(asListener(), bootstrap, connectParams));
77     }
78
79     @NonNull ListenableFuture<SSHClient> listen(final ServerBootstrap bootstrap, final TcpServerGrouping listenParams)
80             throws UnsupportedConfigurationException {
81         return transformUnderlay(this, TCPServer.listen(asListener(), bootstrap, listenParams));
82     }
83 }