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.client;
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;
17 import java.io.IOException;
18 import java.net.SocketAddress;
19 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
20 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.virtualsocket.VirtualSocket;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
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.nettyutil.handler.ssh.client.SshClientAdapter};
28 public class SshHandler extends ChannelOutboundHandlerAdapter {
29 private static final Logger logger = LoggerFactory.getLogger(SshHandler.class);
30 private static final String SOCKET = "socket";
32 private final VirtualSocket virtualSocket = new VirtualSocket();
33 private final SshClientAdapter sshClientAdapter;
36 public static SshHandler createForNetconfSubsystem(AuthenticationHandler authenticationHandler) throws IOException {
37 return new SshHandler(authenticationHandler, Invoker.netconfSubsystem());
41 public SshHandler(AuthenticationHandler authenticationHandler, Invoker invoker) throws IOException {
42 SshClient sshClient = new SshClient(virtualSocket, authenticationHandler);
43 this.sshClientAdapter = new SshClientAdapter(sshClient, invoker);
47 public void handlerAdded(ChannelHandlerContext ctx){
48 if (ctx.channel().pipeline().get(SOCKET) == null) {
49 ctx.channel().pipeline().addFirst(SOCKET, virtualSocket);
54 public void handlerRemoved(ChannelHandlerContext ctx) {
55 if (ctx.channel().pipeline().get(SOCKET) != null) {
56 ctx.channel().pipeline().remove(SOCKET);
61 public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws IOException {
62 this.sshClientAdapter.write((ByteBuf) msg);
66 public void connect(final ChannelHandlerContext ctx,
67 SocketAddress remoteAddress,
68 SocketAddress localAddress,
69 ChannelPromise promise) {
70 ctx.connect(remoteAddress, localAddress, promise);
72 promise.addListener(new ChannelFutureListener() {
73 public void operationComplete(ChannelFuture channelFuture) {
74 if (channelFuture.isSuccess()) {
75 sshClientAdapter.start(ctx, channelFuture);
77 logger.debug("Failed to connect to remote host");
84 public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) {
85 sshClientAdapter.stop(promise);