2 * Copyright (c) 2014 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.netconf.nettyutil.handler.ssh.client;
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.ChannelOutboundHandlerAdapter;
15 import io.netty.channel.ChannelPromise;
16 import java.io.IOException;
17 import java.net.SocketAddress;
18 import org.apache.sshd.ClientChannel;
19 import org.apache.sshd.ClientSession;
20 import org.apache.sshd.SshClient;
21 import org.apache.sshd.client.future.AuthFuture;
22 import org.apache.sshd.client.future.ConnectFuture;
23 import org.apache.sshd.client.future.OpenFuture;
24 import org.apache.sshd.common.future.CloseFuture;
25 import org.apache.sshd.common.future.SshFutureListener;
26 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Netty SSH handler class. Acts as interface between Netty and SSH library.
33 public class AsyncSshHandler extends ChannelOutboundHandlerAdapter {
35 private static final Logger LOG = LoggerFactory.getLogger(AsyncSshHandler.class);
36 public static final String SUBSYSTEM = "netconf";
38 public static final SshClient DEFAULT_CLIENT = SshClient.setUpDefaultClient();
40 public static final int SSH_DEFAULT_NIO_WORKERS = 8;
43 // TODO make configurable, or somehow reuse netty threadpool
44 DEFAULT_CLIENT.setNioWorkers(SSH_DEFAULT_NIO_WORKERS);
45 DEFAULT_CLIENT.start();
48 private final AuthenticationHandler authenticationHandler;
49 private final SshClient sshClient;
51 private AsyncSshHandlerReader sshReadAsyncListener;
52 private AsyncSshHandlerWriter sshWriteAsyncHandler;
54 private ClientChannel channel;
55 private ClientSession session;
56 private ChannelPromise connectPromise;
59 public static AsyncSshHandler createForNetconfSubsystem(final AuthenticationHandler authenticationHandler) throws IOException {
60 return new AsyncSshHandler(authenticationHandler, DEFAULT_CLIENT);
65 * @param authenticationHandler
66 * @param sshClient started SshClient
69 public AsyncSshHandler(final AuthenticationHandler authenticationHandler, final SshClient sshClient) throws IOException {
70 this.authenticationHandler = Preconditions.checkNotNull(authenticationHandler);
71 this.sshClient = Preconditions.checkNotNull(sshClient);
76 private void startSsh(final ChannelHandlerContext ctx, final SocketAddress address) {
77 LOG.debug("Starting SSH to {} on channel: {}", address, ctx.channel());
79 final ConnectFuture sshConnectionFuture = sshClient.connect(authenticationHandler.getUsername(), address);
80 sshConnectionFuture.addListener(new SshFutureListener<ConnectFuture>() {
82 public void operationComplete(final ConnectFuture future) {
83 if (future.isConnected()) {
84 handleSshSessionCreated(future, ctx);
86 handleSshSetupFailure(ctx, future.getException());
92 private synchronized void handleSshSessionCreated(final ConnectFuture future, final ChannelHandlerContext ctx) {
94 LOG.trace("SSH session created on channel: {}", ctx.channel());
96 session = future.getSession();
97 final AuthFuture authenticateFuture = authenticationHandler.authenticate(session);
98 authenticateFuture.addListener(new SshFutureListener<AuthFuture>() {
100 public void operationComplete(final AuthFuture future) {
101 if (future.isSuccess()) {
102 handleSshAuthenticated(session, ctx);
104 handleSshSetupFailure(ctx, future.getException());
108 } catch (final IOException e) {
109 handleSshSetupFailure(ctx, e);
113 private synchronized void handleSshAuthenticated(final ClientSession session, final ChannelHandlerContext ctx) {
115 LOG.debug("SSH session authenticated on channel: {}, server version: {}", ctx.channel(), session.getServerVersion());
117 channel = session.createSubsystemChannel(SUBSYSTEM);
118 channel.setStreaming(ClientChannel.Streaming.Async);
119 channel.open().addListener(new SshFutureListener<OpenFuture>() {
121 public void operationComplete(final OpenFuture future) {
122 if(future.isOpened()) {
123 handleSshChanelOpened(ctx);
125 handleSshSetupFailure(ctx, future.getException());
131 } catch (final IOException e) {
132 handleSshSetupFailure(ctx, e);
136 private synchronized void handleSshChanelOpened(final ChannelHandlerContext ctx) {
137 LOG.trace("SSH subsystem channel opened successfully on channel: {}", ctx.channel());
139 connectPromise.setSuccess();
141 // TODO we should also read from error stream and at least log from that
143 sshReadAsyncListener = new AsyncSshHandlerReader(new AutoCloseable() {
145 public void close() throws Exception {
146 AsyncSshHandler.this.disconnect(ctx, ctx.newPromise());
148 }, new AsyncSshHandlerReader.ReadMsgHandler() {
150 public void onMessageRead(final ByteBuf msg) {
151 ctx.fireChannelRead(msg);
153 }, channel.toString(), channel.getAsyncOut());
155 // if readAsyncListener receives immediate close, it will close this handler and closing this handler sets channel variable to null
156 if(channel != null) {
157 sshWriteAsyncHandler = new AsyncSshHandlerWriter(channel.getAsyncIn());
158 ctx.fireChannelActive();
162 private synchronized void handleSshSetupFailure(final ChannelHandlerContext ctx, final Throwable e) {
163 LOG.warn("Unable to setup SSH connection on channel: {}", ctx.channel(), e);
164 disconnect(ctx, ctx.newPromise());
166 // If the promise is not yet done, we have failed with initial connect and set connectPromise to failure
167 if(!connectPromise.isDone()) {
168 connectPromise.setFailure(e);
173 public synchronized void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
174 sshWriteAsyncHandler.write(ctx, msg, promise);
178 public synchronized void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) throws Exception {
179 this.connectPromise = promise;
180 startSsh(ctx, remoteAddress);
184 public void close(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception {
185 disconnect(ctx, promise);
189 public synchronized void disconnect(final ChannelHandlerContext ctx, final ChannelPromise promise) {
190 // Super disconnect is necessary in this case since we are using NioSocketChannel and it needs to cleanup its resources
191 // e.g. Socket that it tries to open in its constructor (https://bugs.opendaylight.org/show_bug.cgi?id=2430)
192 // TODO better solution would be to implement custom ChannelFactory + Channel that will use mina SSH lib internally: port this to custom channel implementation
194 super.disconnect(ctx, ctx.newPromise());
195 } catch (final Exception e) {
196 LOG.warn("Unable to cleanup all resources for channel: {}. Ignoring.", ctx.channel(), e);
199 if(sshReadAsyncListener != null) {
200 sshReadAsyncListener.close();
203 if(sshWriteAsyncHandler != null) {
204 sshWriteAsyncHandler.close();
207 if(session!= null && !session.isClosed() && !session.isClosing()) {
208 session.close(false).addListener(new SshFutureListener<CloseFuture>() {
210 public void operationComplete(final CloseFuture future) {
211 if (future.isClosed() == false) {
219 // If we have already succeeded and the session was dropped after, we need to fire inactive to notify reconnect logic
220 if(connectPromise.isSuccess()) {
221 ctx.fireChannelInactive();
226 promise.setSuccess();
227 LOG.debug("SSH session closed on channel: {}", ctx.channel());