Merge "Netconf-cli compilable and included in project"
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / handler / ssh / client / AsyncSshHandler.java
1 /*
2  * Copyright (c) 2014 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 com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
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.apache.sshd.ClientChannel;
19 import org.apache.sshd.ClientSession;
20 import org.apache.sshd.SshClient;
21 import org.apache.sshd.client.future.AuthFuture;
22 import org.apache.sshd.client.future.ConnectFuture;
23 import org.apache.sshd.client.future.OpenFuture;
24 import org.apache.sshd.common.future.CloseFuture;
25 import org.apache.sshd.common.future.SshFutureListener;
26 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Netty SSH handler class. Acts as interface between Netty and SSH library.
32  */
33 public class AsyncSshHandler extends ChannelOutboundHandlerAdapter {
34
35     private static final Logger LOG = LoggerFactory.getLogger(AsyncSshHandler.class);
36     public static final String SUBSYSTEM = "netconf";
37
38     public static final SshClient DEFAULT_CLIENT = SshClient.setUpDefaultClient();
39
40     public static final int SSH_DEFAULT_NIO_WORKERS = 8;
41
42     static {
43         // TODO make configurable, or somehow reuse netty threadpool
44         DEFAULT_CLIENT.setNioWorkers(SSH_DEFAULT_NIO_WORKERS);
45         DEFAULT_CLIENT.start();
46     }
47
48     private final AuthenticationHandler authenticationHandler;
49     private final SshClient sshClient;
50
51     private AsyncSshHandlerReader sshReadAsyncListener;
52     private AsyncSshHandlerWriter sshWriteAsyncHandler;
53
54     private ClientChannel channel;
55     private ClientSession session;
56     private ChannelPromise connectPromise;
57
58
59     public static AsyncSshHandler createForNetconfSubsystem(final AuthenticationHandler authenticationHandler) throws IOException {
60         return new AsyncSshHandler(authenticationHandler, DEFAULT_CLIENT);
61     }
62
63     /**
64      *
65      * @param authenticationHandler
66      * @param sshClient started SshClient
67      * @throws IOException
68      */
69     public AsyncSshHandler(final AuthenticationHandler authenticationHandler, final SshClient sshClient) throws IOException {
70         this.authenticationHandler = Preconditions.checkNotNull(authenticationHandler);
71         this.sshClient = Preconditions.checkNotNull(sshClient);
72         // Start just in case
73         sshClient.start();
74     }
75
76     private void startSsh(final ChannelHandlerContext ctx, final SocketAddress address) {
77         LOG.debug("Starting SSH to {} on channel: {}", address, ctx.channel());
78
79         final ConnectFuture sshConnectionFuture = sshClient.connect(authenticationHandler.getUsername(), address);
80         sshConnectionFuture.addListener(new SshFutureListener<ConnectFuture>() {
81             @Override
82             public void operationComplete(final ConnectFuture future) {
83                 if (future.isConnected()) {
84                     handleSshSessionCreated(future, ctx);
85                 } else {
86                     handleSshSetupFailure(ctx, future.getException());
87                 }
88             }
89         });
90     }
91
92     private synchronized void handleSshSessionCreated(final ConnectFuture future, final ChannelHandlerContext ctx) {
93         try {
94             LOG.trace("SSH session created on channel: {}", ctx.channel());
95
96             session = future.getSession();
97             final AuthFuture authenticateFuture = authenticationHandler.authenticate(session);
98             authenticateFuture.addListener(new SshFutureListener<AuthFuture>() {
99                 @Override
100                 public void operationComplete(final AuthFuture future) {
101                     if (future.isSuccess()) {
102                         handleSshAuthenticated(session, ctx);
103                     } else {
104                         handleSshSetupFailure(ctx, future.getException());
105                     }
106                 }
107             });
108         } catch (final IOException e) {
109             handleSshSetupFailure(ctx, e);
110         }
111     }
112
113     private synchronized void handleSshAuthenticated(final ClientSession session, final ChannelHandlerContext ctx) {
114         try {
115             LOG.debug("SSH session authenticated on channel: {}, server version: {}", ctx.channel(), session.getServerVersion());
116
117             channel = session.createSubsystemChannel(SUBSYSTEM);
118             channel.setStreaming(ClientChannel.Streaming.Async);
119             channel.open().addListener(new SshFutureListener<OpenFuture>() {
120                 @Override
121                 public void operationComplete(final OpenFuture future) {
122                     if(future.isOpened()) {
123                         handleSshChanelOpened(ctx);
124                     } else {
125                         handleSshSetupFailure(ctx, future.getException());
126                     }
127                 }
128             });
129
130
131         } catch (final IOException e) {
132             handleSshSetupFailure(ctx, e);
133         }
134     }
135
136     private synchronized void handleSshChanelOpened(final ChannelHandlerContext ctx) {
137         LOG.trace("SSH subsystem channel opened successfully on channel: {}", ctx.channel());
138
139         connectPromise.setSuccess();
140         connectPromise = null;
141
142         // TODO we should also read from error stream and at least log from that
143
144         sshReadAsyncListener = new AsyncSshHandlerReader(new AutoCloseable() {
145             @Override
146             public void close() throws Exception {
147                 AsyncSshHandler.this.disconnect(ctx, ctx.newPromise());
148             }
149         }, new AsyncSshHandlerReader.ReadMsgHandler() {
150             @Override
151             public void onMessageRead(final ByteBuf msg) {
152                 ctx.fireChannelRead(msg);
153             }
154         }, channel.toString(), channel.getAsyncOut());
155
156         // if readAsyncListener receives immediate close, it will close this handler and closing this handler sets channel variable to null
157         if(channel != null) {
158             sshWriteAsyncHandler = new AsyncSshHandlerWriter(channel.getAsyncIn());
159             ctx.fireChannelActive();
160         }
161     }
162
163     private synchronized void handleSshSetupFailure(final ChannelHandlerContext ctx, final Throwable e) {
164         LOG.warn("Unable to setup SSH connection on channel: {}", ctx.channel(), e);
165         connectPromise.setFailure(e);
166         connectPromise = null;
167         throw new IllegalStateException("Unable to setup SSH connection on channel: " + ctx.channel(), e);
168     }
169
170     @Override
171     public synchronized void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
172         sshWriteAsyncHandler.write(ctx, msg, promise);
173     }
174
175     @Override
176     public synchronized void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) throws Exception {
177         this.connectPromise = promise;
178         startSsh(ctx, remoteAddress);
179     }
180
181     @Override
182     public void close(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception {
183         disconnect(ctx, promise);
184     }
185
186     @Override
187     public synchronized void disconnect(final ChannelHandlerContext ctx, final ChannelPromise promise) {
188         if(sshReadAsyncListener != null) {
189             sshReadAsyncListener.close();
190         }
191
192         if(sshWriteAsyncHandler != null) {
193             sshWriteAsyncHandler.close();
194         }
195
196         if(session!= null && !session.isClosed() && !session.isClosing()) {
197             session.close(false).addListener(new SshFutureListener<CloseFuture>() {
198                 @Override
199                 public void operationComplete(final CloseFuture future) {
200                     if (future.isClosed() == false) {
201                         session.close(true);
202                     }
203                     session = null;
204                 }
205             });
206         }
207
208         channel = null;
209         promise.setSuccess();
210
211         LOG.debug("SSH session closed on channel: {}", ctx.channel());
212         ctx.fireChannelInactive();
213     }
214
215 }