Merge "BUG-1568 Ssh Handler for netconf client Mina implementation"
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / handler / ssh / client / SshHandler.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.nettyutil.handler.ssh.client;
10
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;
23
24 /**
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};
27  */
28 public class SshHandler extends ChannelOutboundHandlerAdapter {
29     private static final Logger logger = LoggerFactory.getLogger(SshHandler.class);
30     private static final String SOCKET = "socket";
31
32     private final VirtualSocket virtualSocket = new VirtualSocket();
33     private final SshClientAdapter sshClientAdapter;
34
35
36     public static SshHandler createForNetconfSubsystem(AuthenticationHandler authenticationHandler) throws IOException {
37         return new SshHandler(authenticationHandler, Invoker.netconfSubsystem());
38     }
39
40
41     public SshHandler(AuthenticationHandler authenticationHandler, Invoker invoker) throws IOException {
42         SshClient sshClient = new SshClient(virtualSocket, authenticationHandler);
43         this.sshClientAdapter = new SshClientAdapter(sshClient, invoker);
44     }
45
46     @Override
47     public void handlerAdded(ChannelHandlerContext ctx){
48         if (ctx.channel().pipeline().get(SOCKET) == null) {
49             ctx.channel().pipeline().addFirst(SOCKET, virtualSocket);
50         }
51     }
52
53     @Override
54     public void handlerRemoved(ChannelHandlerContext ctx) {
55         if (ctx.channel().pipeline().get(SOCKET) != null) {
56             ctx.channel().pipeline().remove(SOCKET);
57         }
58     }
59
60     @Override
61     public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws IOException {
62         this.sshClientAdapter.write((ByteBuf) msg);
63     }
64
65     @Override
66     public void connect(final ChannelHandlerContext ctx,
67                         SocketAddress remoteAddress,
68                         SocketAddress localAddress,
69                         ChannelPromise promise) {
70         ctx.connect(remoteAddress, localAddress, promise);
71
72         promise.addListener(new ChannelFutureListener() {
73             public void operationComplete(ChannelFuture channelFuture) {
74                 if (channelFuture.isSuccess()) {
75                     sshClientAdapter.start(ctx, channelFuture);
76                 } else {
77                     logger.debug("Failed to connect to remote host");
78                 }
79             }}
80         );
81     }
82
83     @Override
84     public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) {
85         sshClientAdapter.stop(promise);
86     }
87 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.