Merge "Choice and case resolving in JSON output"
[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.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
18 import java.io.IOException;
19 import java.net.SocketAddress;
20
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;
26
27 /**
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};
30  */
31 public class SshHandler extends ChannelOutboundHandlerAdapter {
32     private final VirtualSocket virtualSocket = new VirtualSocket();
33     private final SshClientAdapter sshClientAdapter;
34
35     public SshHandler(AuthenticationHandler authenticationHandler, Invoker invoker) throws IOException {
36         SshClient sshClient = new SshClient(virtualSocket, authenticationHandler);
37         this.sshClientAdapter = new SshClientAdapter(sshClient, invoker);
38     }
39
40     @Override
41     public void handlerAdded(ChannelHandlerContext ctx){
42         if (ctx.channel().pipeline().get("socket") == null) {
43             ctx.channel().pipeline().addFirst("socket", virtualSocket);
44         }
45     }
46
47     @Override
48     public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
49         if (ctx.channel().pipeline().get("socket") != null) {
50             ctx.channel().pipeline().remove("socket");
51         }
52     }
53
54     @Override
55     public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
56         this.sshClientAdapter.write((ByteBuf) msg);
57     }
58
59     @Override
60     public void connect(final ChannelHandlerContext ctx,
61                         SocketAddress remoteAddress,
62                         SocketAddress localAddress,
63                         ChannelPromise promise) throws Exception {
64         ctx.connect(remoteAddress, localAddress, promise);
65
66         promise.addListener(new ChannelFutureListener() {
67             public void operationComplete(ChannelFuture channelFuture) throws Exception {
68                 sshClientAdapter.start(ctx);
69             }}
70         );
71     }
72
73     @Override
74     public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
75         sshClientAdapter.stop(promise);
76     }
77 }