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.util.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.util.handler.ssh.authentication.AuthenticationHandler;
22 import org.opendaylight.controller.netconf.util.handler.ssh.client.Invoker;
23 import org.opendaylight.controller.netconf.util.handler.ssh.client.SshClient;
24 import org.opendaylight.controller.netconf.util.handler.ssh.client.SshClientAdapter;
25 import org.opendaylight.controller.netconf.util.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.util.handler.ssh.client.SshClientAdapter};
31 public class SshHandler extends ChannelOutboundHandlerAdapter {
32 private final VirtualSocket virtualSocket = new VirtualSocket();
33 private final SshClientAdapter sshClientAdapter;
35 public SshHandler(AuthenticationHandler authenticationHandler, Invoker invoker) throws IOException {
36 SshClient sshClient = new SshClient(virtualSocket, authenticationHandler);
37 this.sshClientAdapter = new SshClientAdapter(sshClient, invoker);
41 public void handlerAdded(ChannelHandlerContext ctx){
42 if (ctx.channel().pipeline().get("socket") == null) {
43 ctx.channel().pipeline().addFirst("socket", virtualSocket);
48 public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
49 if (ctx.channel().pipeline().get("socket") != null) {
50 ctx.channel().pipeline().remove("socket");
55 public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
56 this.sshClientAdapter.write((ByteBuf) msg);
60 public void connect(final ChannelHandlerContext ctx,
61 SocketAddress remoteAddress,
62 SocketAddress localAddress,
63 ChannelPromise promise) throws Exception {
64 ctx.connect(remoteAddress, localAddress, promise);
66 promise.addListener(new ChannelFutureListener() {
67 public void operationComplete(ChannelFuture channelFuture) throws Exception {
68 sshClientAdapter.start(ctx);
74 public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
75 sshClientAdapter.stop(promise);