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.buffer.Unpooled;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.channel.ChannelOutboundHandler;
16 import io.netty.channel.ChannelOutboundHandlerAdapter;
17 import io.netty.channel.ChannelPromise;
18 import java.io.IOException;
19 import java.net.SocketAddress;
20 import org.apache.sshd.ClientChannel;
21 import org.apache.sshd.ClientSession;
22 import org.apache.sshd.SshClient;
23 import org.apache.sshd.client.future.AuthFuture;
24 import org.apache.sshd.client.future.ConnectFuture;
25 import org.apache.sshd.client.future.OpenFuture;
26 import org.apache.sshd.common.future.CloseFuture;
27 import org.apache.sshd.common.future.SshFutureListener;
28 import org.apache.sshd.common.io.IoInputStream;
29 import org.apache.sshd.common.io.IoOutputStream;
30 import org.apache.sshd.common.io.IoReadFuture;
31 import org.apache.sshd.common.io.IoWriteFuture;
32 import org.apache.sshd.common.io.WritePendingException;
33 import org.apache.sshd.common.util.Buffer;
34 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Netty SSH handler class. Acts as interface between Netty and SSH library.
41 public class AsyncSshHandler extends ChannelOutboundHandlerAdapter {
43 private static final Logger logger = LoggerFactory.getLogger(AsyncSshHandler.class);
44 public static final String SUBSYSTEM = "netconf";
46 public static final SshClient DEFAULT_CLIENT = SshClient.setUpDefaultClient();
48 public static final int SSH_DEFAULT_NIO_WORKERS = 8;
51 // TODO make configurable, or somehow reuse netty threadpool
52 DEFAULT_CLIENT.setNioWorkers(SSH_DEFAULT_NIO_WORKERS);
53 DEFAULT_CLIENT.start();
56 private final AuthenticationHandler authenticationHandler;
57 private final SshClient sshClient;
59 private SshReadAsyncListener sshReadAsyncListener;
60 private SshWriteAsyncHandler sshWriteAsyncHandler;
62 private ClientChannel channel;
63 private ClientSession session;
64 private ChannelPromise connectPromise;
67 public static AsyncSshHandler createForNetconfSubsystem(final AuthenticationHandler authenticationHandler) throws IOException {
68 return new AsyncSshHandler(authenticationHandler, DEFAULT_CLIENT);
73 * @param authenticationHandler
74 * @param sshClient started SshClient
77 public AsyncSshHandler(final AuthenticationHandler authenticationHandler, final SshClient sshClient) throws IOException {
78 this.authenticationHandler = Preconditions.checkNotNull(authenticationHandler);
79 this.sshClient = Preconditions.checkNotNull(sshClient);
84 private void startSsh(final ChannelHandlerContext ctx, final SocketAddress address) {
85 logger.debug("Starting SSH to {} on channel: {}", address, ctx.channel());
87 final ConnectFuture sshConnectionFuture = sshClient.connect(authenticationHandler.getUsername(), address);
88 sshConnectionFuture.addListener(new SshFutureListener<ConnectFuture>() {
90 public void operationComplete(final ConnectFuture future) {
91 if (future.isConnected()) {
92 handleSshSessionCreated(future, ctx);
94 handleSshSetupFailure(ctx, future.getException());
100 private synchronized void handleSshSessionCreated(final ConnectFuture future, final ChannelHandlerContext ctx) {
102 logger.trace("SSH session created on channel: {}", ctx.channel());
104 session = future.getSession();
105 final AuthFuture authenticateFuture = authenticationHandler.authenticate(session);
106 authenticateFuture.addListener(new SshFutureListener<AuthFuture>() {
108 public void operationComplete(final AuthFuture future) {
109 if (future.isSuccess()) {
110 handleSshAuthenticated(session, ctx);
112 handleSshSetupFailure(ctx, future.getException());
116 } catch (final IOException e) {
117 handleSshSetupFailure(ctx, e);
121 private synchronized void handleSshAuthenticated(final ClientSession session, final ChannelHandlerContext ctx) {
123 logger.debug("SSH session authenticated on channel: {}, server version: {}", ctx.channel(), session.getServerVersion());
125 channel = session.createSubsystemChannel(SUBSYSTEM);
126 channel.setStreaming(ClientChannel.Streaming.Async);
127 channel.open().addListener(new SshFutureListener<OpenFuture>() {
129 public void operationComplete(final OpenFuture future) {
130 if(future.isOpened()) {
131 handleSshChanelOpened(ctx);
133 handleSshSetupFailure(ctx, future.getException());
139 } catch (final IOException e) {
140 handleSshSetupFailure(ctx, e);
144 private synchronized void handleSshChanelOpened(final ChannelHandlerContext ctx) {
145 logger.trace("SSH subsystem channel opened successfully on channel: {}", ctx.channel());
147 connectPromise.setSuccess();
148 connectPromise = null;
150 sshReadAsyncListener = new SshReadAsyncListener(ctx, channel.getAsyncOut());
151 sshWriteAsyncHandler = new SshWriteAsyncHandler(this, channel.getAsyncIn());
153 ctx.fireChannelActive();
156 private synchronized void handleSshSetupFailure(final ChannelHandlerContext ctx, final Throwable e) {
157 logger.warn("Unable to setup SSH connection on channel: {}", ctx.channel(), e);
158 connectPromise.setFailure(e);
159 connectPromise = null;
160 throw new IllegalStateException("Unable to setup SSH connection on channel: " + ctx.channel(), e);
164 public synchronized void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
165 sshWriteAsyncHandler.write(ctx, msg, promise);
168 private static void handleSshSessionClosed(final ChannelHandlerContext ctx) {
169 logger.debug("SSH session closed on channel: {}", ctx.channel());
170 ctx.fireChannelInactive();
174 public synchronized void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) throws Exception {
175 this.connectPromise = promise;
176 startSsh(ctx, remoteAddress);
180 public void close(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception {
181 disconnect(ctx, promise);
185 public synchronized void disconnect(final ChannelHandlerContext ctx, final ChannelPromise promise) {
186 if(sshReadAsyncListener != null) {
187 sshReadAsyncListener.close();
190 if(sshWriteAsyncHandler != null) {
191 sshWriteAsyncHandler.close();
194 if(session!= null && !session.isClosed() && !session.isClosing()) {
195 session.close(false).addListener(new SshFutureListener<CloseFuture>() {
197 public void operationComplete(final CloseFuture future) {
198 if (future.isClosed() == false) {
207 promise.setSuccess();
209 handleSshSessionClosed(ctx);
213 * Listener over async input stream from SSH session.
214 * This listeners schedules reads in a loop until the session is closed or read fails.
216 private static class SshReadAsyncListener implements SshFutureListener<IoReadFuture>, AutoCloseable {
217 private static final int BUFFER_SIZE = 8192;
219 private final ChannelHandlerContext ctx;
221 private IoInputStream asyncOut;
223 private IoReadFuture currentReadFuture;
225 public SshReadAsyncListener(final ChannelHandlerContext ctx, final IoInputStream asyncOut) {
227 this.asyncOut = asyncOut;
228 buf = new Buffer(BUFFER_SIZE);
229 asyncOut.read(buf).addListener(this);
233 public synchronized void operationComplete(final IoReadFuture future) {
234 if(future.getException() != null) {
236 if(asyncOut.isClosed() || asyncOut.isClosing()) {
238 handleSshSessionClosed(ctx);
240 logger.warn("Exception while reading from SSH remote on channel {}", ctx.channel(), future.getException());
241 throw new IllegalStateException("Exception while reading from SSH remote on channel " + ctx.channel(), future.getException());
245 if (future.getRead() > 0) {
246 ctx.fireChannelRead(Unpooled.wrappedBuffer(buf.array(), 0, future.getRead()));
248 // Schedule next read
249 buf = new Buffer(BUFFER_SIZE);
250 currentReadFuture = asyncOut.read(buf);
251 currentReadFuture.addListener(this);
256 public synchronized void close() {
257 // Remove self as listener on close to prevent reading from closed input
258 if(currentReadFuture != null) {
259 currentReadFuture.removeListener(this);
266 private static final class SshWriteAsyncHandler implements AutoCloseable {
267 public static final int MAX_PENDING_WRITES = 100;
269 private final ChannelOutboundHandler channelHandler;
270 private IoOutputStream asyncIn;
272 // Counter that holds the amount of pending write messages
273 // Pending write can occur in case remote window is full
274 // In such case, we need to wait for the pending write to finish
275 private int pendingWriteCounter;
276 // Last write future, that can be pending
277 private IoWriteFuture lastWriteFuture;
279 public SshWriteAsyncHandler(final ChannelOutboundHandler channelHandler, final IoOutputStream asyncIn) {
280 this.channelHandler = channelHandler;
281 this.asyncIn = asyncIn;
284 public synchronized void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
286 if(asyncIn.isClosed() || asyncIn.isClosing()) {
287 handleSshSessionClosed(ctx);
289 lastWriteFuture = asyncIn.write(toBuffer(msg));
290 lastWriteFuture.addListener(new SshFutureListener<IoWriteFuture>() {
293 public void operationComplete(final IoWriteFuture future) {
294 ((ByteBuf) msg).release();
296 // Notify success or failure
297 if (future.isWritten()) {
298 promise.setSuccess();
300 promise.setFailure(future.getException());
302 // Reset last pending future
303 synchronized (SshWriteAsyncHandler.this) {
304 lastWriteFuture = null;
309 } catch (final WritePendingException e) {
310 // Check limit for pending writes
311 pendingWriteCounter++;
312 if(pendingWriteCounter > MAX_PENDING_WRITES) {
313 handlePendingFailed(ctx, new IllegalStateException("Too much pending writes(" + MAX_PENDING_WRITES + ") on channel: " + ctx.channel() +
314 ", remote window is not getting read or is too small"));
317 logger.debug("Write pending to SSH remote on channel: {}, current pending count: {}", ctx.channel(), pendingWriteCounter);
319 // In case of pending, re-invoke write after pending is finished
320 lastWriteFuture.addListener(new SshFutureListener<IoWriteFuture>() {
322 public void operationComplete(final IoWriteFuture future) {
323 if(future.isWritten()) {
324 synchronized (SshWriteAsyncHandler.this) {
325 // Pending done, decrease counter
326 pendingWriteCounter--;
328 write(ctx, msg, promise);
330 // Cannot reschedule pending, fail
331 handlePendingFailed(ctx, e);
339 private void handlePendingFailed(final ChannelHandlerContext ctx, final Exception e) {
340 logger.warn("Exception while writing to SSH remote on channel {}", ctx.channel(), e);
342 channelHandler.disconnect(ctx, ctx.newPromise());
343 } catch (final Exception ex) {
344 // This should not happen
345 throw new IllegalStateException(ex);
350 public void close() {
354 private Buffer toBuffer(final Object msg) {
355 // TODO Buffer vs ByteBuf translate, Can we handle that better ?
356 Preconditions.checkState(msg instanceof ByteBuf);
357 final ByteBuf byteBuf = (ByteBuf) msg;
358 final byte[] temp = new byte[byteBuf.readableBytes()];
359 byteBuf.readBytes(temp, 0, byteBuf.readableBytes());
360 return new Buffer(temp);