2 * Copyright (c) 2013 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;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.channel.ChannelFuture;
13 import io.netty.channel.ChannelFutureListener;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.channel.ChannelOutboundHandlerAdapter;
16 import io.netty.channel.ChannelPromise;
18 import java.io.IOException;
19 import java.net.SocketAddress;
21 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.SshClient;
22 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
23 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.Invoker;
24 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.SshClientAdapter;
25 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.virtualsocket.VirtualSocket;
28 * Netty SSH handler class. Acts as interface between Netty and SSH library. All standard Netty message handling
29 * stops at instance of this class. All downstream events are handed of to wrapped {@link org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.SshClientAdapter};
31 public class SshHandler extends ChannelOutboundHandlerAdapter {
32 private static final String SOCKET = "socket";
34 private final VirtualSocket virtualSocket = new VirtualSocket();
35 private final SshClientAdapter sshClientAdapter;
37 public SshHandler(AuthenticationHandler authenticationHandler, Invoker invoker) throws IOException {
38 SshClient sshClient = new SshClient(virtualSocket, authenticationHandler);
39 this.sshClientAdapter = new SshClientAdapter(sshClient, invoker);
43 public void handlerAdded(ChannelHandlerContext ctx){
44 if (ctx.channel().pipeline().get(SOCKET) == null) {
45 ctx.channel().pipeline().addFirst(SOCKET, virtualSocket);
50 public void handlerRemoved(ChannelHandlerContext ctx) {
51 if (ctx.channel().pipeline().get(SOCKET) != null) {
52 ctx.channel().pipeline().remove(SOCKET);
57 public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws IOException {
58 this.sshClientAdapter.write((ByteBuf) msg);
62 public void connect(final ChannelHandlerContext ctx,
63 SocketAddress remoteAddress,
64 SocketAddress localAddress,
65 ChannelPromise promise) {
66 ctx.connect(remoteAddress, localAddress, promise);
68 promise.addListener(new ChannelFutureListener() {
69 public void operationComplete(ChannelFuture channelFuture) {
70 sshClientAdapter.start(ctx);
76 public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) {
77 sshClientAdapter.stop(promise);