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.channel.ChannelFuture;
12 import io.netty.channel.ChannelFutureListener;
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.opendaylight.controller.netconf.util.handler.ssh.authentication.AuthenticationHandler;
19 import org.opendaylight.controller.netconf.util.handler.ssh.client.Invoker;
20 import org.opendaylight.controller.netconf.util.handler.ssh.client.SshClient;
21 import org.opendaylight.controller.netconf.util.handler.ssh.client.SshClientAdapter;
22 import org.opendaylight.controller.netconf.util.handler.ssh.virtualsocket.VirtualSocket;
25 * Netty SSH handler class. Acts as interface between Netty and SSH library. All standard Netty message handling
26 * stops at instance of this class. All downstream events are handed of to wrapped {@link org.opendaylight.controller.netconf.util.handler.ssh.client.SshClientAdapter};
28 public class SshHandler extends ChannelOutboundHandlerAdapter {
29 private final VirtualSocket virtualSocket = new VirtualSocket();
30 private final SshClientAdapter sshClientAdapter;
32 public SshHandler(AuthenticationHandler authenticationHandler, Invoker invoker) throws IOException {
33 SshClient sshClient = new SshClient(virtualSocket, authenticationHandler);
34 this.sshClientAdapter = new SshClientAdapter(sshClient, invoker);
38 public void handlerAdded(ChannelHandlerContext ctx){
39 if (ctx.channel().pipeline().get("socket") == null) {
40 ctx.channel().pipeline().addFirst("socket", virtualSocket);
45 public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
46 if (ctx.channel().pipeline().get("socket") != null) {
47 ctx.channel().pipeline().remove("socket");
52 public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
53 this.sshClientAdapter.write((String) msg);
57 public void connect(final ChannelHandlerContext ctx,
58 SocketAddress remoteAddress,
59 SocketAddress localAddress,
60 ChannelPromise promise) throws Exception {
61 ctx.connect(remoteAddress, localAddress, promise);
63 promise.addListener(new ChannelFutureListener() {
64 public void operationComplete(ChannelFuture channelFuture) throws Exception {
65 sshClientAdapter.start(ctx);
71 public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
72 sshClientAdapter.stop(promise);