Bug 1029: Remove dead code: sal-schema-repository-api
[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 static final String SOCKET = "socket";
33
34     private final VirtualSocket virtualSocket = new VirtualSocket();
35     private final SshClientAdapter sshClientAdapter;
36
37     public SshHandler(AuthenticationHandler authenticationHandler, Invoker invoker) throws IOException {
38         SshClient sshClient = new SshClient(virtualSocket, authenticationHandler);
39         this.sshClientAdapter = new SshClientAdapter(sshClient, invoker);
40     }
41
42     @Override
43     public void handlerAdded(ChannelHandlerContext ctx){
44         if (ctx.channel().pipeline().get(SOCKET) == null) {
45             ctx.channel().pipeline().addFirst(SOCKET, virtualSocket);
46         }
47     }
48
49     @Override
50     public void handlerRemoved(ChannelHandlerContext ctx) {
51         if (ctx.channel().pipeline().get(SOCKET) != null) {
52             ctx.channel().pipeline().remove(SOCKET);
53         }
54     }
55
56     @Override
57     public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws IOException {
58         this.sshClientAdapter.write((ByteBuf) msg);
59     }
60
61     @Override
62     public void connect(final ChannelHandlerContext ctx,
63                         SocketAddress remoteAddress,
64                         SocketAddress localAddress,
65                         ChannelPromise promise) {
66         ctx.connect(remoteAddress, localAddress, promise);
67
68         promise.addListener(new ChannelFutureListener() {
69             public void operationComplete(ChannelFuture channelFuture) {
70                 sshClientAdapter.start(ctx);
71             }}
72         );
73     }
74
75     @Override
76     public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) {
77         sshClientAdapter.stop(promise);
78     }
79 }