b911989c646b64b139dd4a678cbd0c8adc03596d
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / handler / ssh / 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.util.handler.ssh;
10
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;
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.util.handler.ssh.client.SshClientAdapter};
27  */
28 public class SshHandler extends ChannelOutboundHandlerAdapter {
29     private final VirtualSocket virtualSocket = new VirtualSocket();
30     private final SshClientAdapter sshClientAdapter;
31
32     public SshHandler(AuthenticationHandler authenticationHandler, Invoker invoker) throws IOException {
33         SshClient sshClient = new SshClient(virtualSocket, authenticationHandler);
34         this.sshClientAdapter = new SshClientAdapter(sshClient, invoker);
35     }
36
37     @Override
38     public void handlerAdded(ChannelHandlerContext ctx){
39         if (ctx.channel().pipeline().get("socket") == null) {
40             ctx.channel().pipeline().addFirst("socket", virtualSocket);
41         }
42     }
43
44     @Override
45     public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
46         if (ctx.channel().pipeline().get("socket") != null) {
47             ctx.channel().pipeline().remove("socket");
48         }
49     }
50
51     @Override
52     public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
53         this.sshClientAdapter.write((String) msg);
54     }
55
56     @Override
57     public void connect(final ChannelHandlerContext ctx,
58                         SocketAddress remoteAddress,
59                         SocketAddress localAddress,
60                         ChannelPromise promise) throws Exception {
61         ctx.connect(remoteAddress, localAddress, promise);
62
63         promise.addListener(new ChannelFutureListener() {
64             public void operationComplete(ChannelFuture channelFuture) throws Exception {
65                 sshClientAdapter.start(ctx);
66             }}
67         );
68     }
69
70     @Override
71     public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
72         sshClientAdapter.stop(promise);
73     }
74 }