Merge "Bug 5475 - File descriptor leak on netconf connector reconnects" into stable...
[netconf.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / 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.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 io.netty.util.concurrent.Future;
17 import io.netty.util.concurrent.GenericFutureListener;
18 import java.io.IOException;
19 import java.net.SocketAddress;
20 import java.util.HashMap;
21 import org.apache.sshd.ClientChannel;
22 import org.apache.sshd.ClientSession;
23 import org.apache.sshd.SshClient;
24 import org.apache.sshd.client.future.AuthFuture;
25 import org.apache.sshd.client.future.ConnectFuture;
26 import org.apache.sshd.client.future.OpenFuture;
27 import org.apache.sshd.common.future.CloseFuture;
28 import org.apache.sshd.common.future.SshFutureListener;
29 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Netty SSH handler class. Acts as interface between Netty and SSH library.
35  */
36 public class AsyncSshHandler extends ChannelOutboundHandlerAdapter {
37
38     public static final String SUBSYSTEM = "netconf";
39     public static final SshClient DEFAULT_CLIENT = SshClient.setUpDefaultClient();
40     public static final int SSH_DEFAULT_NIO_WORKERS = 8;
41     private static final Logger LOG = LoggerFactory.getLogger(AsyncSshHandler.class);
42     // Disable default timeouts from mina sshd
43     private static final long DEFAULT_TIMEOUT = -1L;
44
45     static {
46         DEFAULT_CLIENT.setProperties(new HashMap<String, String>(){
47             {
48                 put(SshClient.AUTH_TIMEOUT, Long.toString(DEFAULT_TIMEOUT));
49                 put(SshClient.IDLE_TIMEOUT, Long.toString(DEFAULT_TIMEOUT));
50             }
51         });
52         // TODO make configurable, or somehow reuse netty threadpool
53         DEFAULT_CLIENT.setNioWorkers(SSH_DEFAULT_NIO_WORKERS);
54         DEFAULT_CLIENT.start();
55     }
56
57     private final AuthenticationHandler authenticationHandler;
58     private final SshClient sshClient;
59     private Future negotiationFuture;
60
61     private AsyncSshHandlerReader sshReadAsyncListener;
62     private AsyncSshHandlerWriter sshWriteAsyncHandler;
63
64     private ClientChannel channel;
65     private ClientSession session;
66     private ChannelPromise connectPromise;
67     private GenericFutureListener negotiationFutureListener;
68
69
70     public AsyncSshHandler(final AuthenticationHandler authenticationHandler, final SshClient sshClient, final Future negotiationFuture) throws IOException {
71         this(authenticationHandler, sshClient);
72         this.negotiationFuture = negotiationFuture;
73     }
74
75     /**
76      *
77      * @param authenticationHandler
78      * @param sshClient started SshClient
79      * @throws IOException
80      */
81     public AsyncSshHandler(final AuthenticationHandler authenticationHandler, final SshClient sshClient) throws IOException {
82         this.authenticationHandler = Preconditions.checkNotNull(authenticationHandler);
83         this.sshClient = Preconditions.checkNotNull(sshClient);
84     }
85
86     public static AsyncSshHandler createForNetconfSubsystem(final AuthenticationHandler authenticationHandler) throws IOException {
87         return new AsyncSshHandler(authenticationHandler, DEFAULT_CLIENT);
88     }
89
90     /**
91      *
92      * Create AsyncSshHandler for netconf subsystem. Negotiation future has to be set to success after successful netconf
93      * negotiation.
94      *
95      * @param authenticationHandler
96      * @param negotiationFuture
97      * @return
98      * @throws IOException
99      */
100     public static AsyncSshHandler createForNetconfSubsystem(final AuthenticationHandler authenticationHandler, final Future negotiationFuture) throws IOException {
101         return new AsyncSshHandler(authenticationHandler, DEFAULT_CLIENT, negotiationFuture);
102     }
103
104     private void startSsh(final ChannelHandlerContext ctx, final SocketAddress address) {
105         LOG.debug("Starting SSH to {} on channel: {}", address, ctx.channel());
106
107         final ConnectFuture sshConnectionFuture = sshClient.connect(authenticationHandler.getUsername(), address);
108         sshConnectionFuture.addListener(new SshFutureListener<ConnectFuture>() {
109             @Override
110             public void operationComplete(final ConnectFuture future) {
111                 if (future.isConnected()) {
112                     handleSshSessionCreated(future, ctx);
113                 } else {
114                     handleSshSetupFailure(ctx, future.getException());
115                 }
116             }
117         });
118     }
119
120     private synchronized void handleSshSessionCreated(final ConnectFuture future, final ChannelHandlerContext ctx) {
121         try {
122             LOG.trace("SSH session created on channel: {}", ctx.channel());
123
124             session = future.getSession();
125             final AuthFuture authenticateFuture = authenticationHandler.authenticate(session);
126             authenticateFuture.addListener(new SshFutureListener<AuthFuture>() {
127                 @Override
128                 public void operationComplete(final AuthFuture future) {
129                     if (future.isSuccess()) {
130                         handleSshAuthenticated(session, ctx);
131                     } else {
132                         // Exception does not have to be set in the future, add simple exception in such case
133                         final Throwable exception = future.getException() == null ?
134                                 new IllegalStateException("Authentication failed") :
135                                 future.getException();
136                         handleSshSetupFailure(ctx, exception);
137                     }
138                 }
139             });
140         } catch (final IOException e) {
141             handleSshSetupFailure(ctx, e);
142         }
143     }
144
145     private synchronized void handleSshAuthenticated(final ClientSession session, final ChannelHandlerContext ctx) {
146         try {
147             LOG.debug("SSH session authenticated on channel: {}, server version: {}", ctx.channel(), session.getServerVersion());
148
149             channel = session.createSubsystemChannel(SUBSYSTEM);
150             channel.setStreaming(ClientChannel.Streaming.Async);
151             channel.open().addListener(new SshFutureListener<OpenFuture>() {
152                 @Override
153                 public void operationComplete(final OpenFuture future) {
154                     if(future.isOpened()) {
155                         handleSshChanelOpened(ctx);
156                     } else {
157                         handleSshSetupFailure(ctx, future.getException());
158                     }
159                 }
160             });
161
162
163         } catch (final IOException e) {
164             handleSshSetupFailure(ctx, e);
165         }
166     }
167
168     private synchronized void handleSshChanelOpened(final ChannelHandlerContext ctx) {
169         LOG.trace("SSH subsystem channel opened successfully on channel: {}", ctx.channel());
170
171         if(negotiationFuture == null) {
172             connectPromise.setSuccess();
173         }
174
175         // TODO we should also read from error stream and at least log from that
176
177         sshReadAsyncListener = new AsyncSshHandlerReader(new AutoCloseable() {
178             @Override
179             public void close() throws Exception {
180                 AsyncSshHandler.this.disconnect(ctx, ctx.newPromise());
181             }
182         }, new AsyncSshHandlerReader.ReadMsgHandler() {
183             @Override
184             public void onMessageRead(final ByteBuf msg) {
185                 ctx.fireChannelRead(msg);
186             }
187         }, channel.toString(), channel.getAsyncOut());
188
189         // if readAsyncListener receives immediate close, it will close this handler and closing this handler sets channel variable to null
190         if(channel != null) {
191             sshWriteAsyncHandler = new AsyncSshHandlerWriter(channel.getAsyncIn());
192             ctx.fireChannelActive();
193         }
194     }
195
196     private synchronized void handleSshSetupFailure(final ChannelHandlerContext ctx, final Throwable e) {
197         LOG.warn("Unable to setup SSH connection on channel: {}", ctx.channel(), e);
198
199         // If the promise is not yet done, we have failed with initial connect and set connectPromise to failure
200         if(!connectPromise.isDone()) {
201             connectPromise.setFailure(e);
202         }
203
204         disconnect(ctx, ctx.newPromise());
205     }
206
207     @Override
208     public synchronized void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
209         sshWriteAsyncHandler.write(ctx, msg, promise);
210     }
211
212     @Override
213     public synchronized void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) throws Exception {
214         LOG.debug("SSH session connecting on channel {}. promise: {} ", ctx.channel(), connectPromise);
215         this.connectPromise = promise;
216
217         if(negotiationFuture != null) {
218
219             negotiationFutureListener = new GenericFutureListener<Future<Object>>() {
220                 @Override
221                 public void operationComplete(Future future) throws Exception {
222                     if (future.isSuccess())
223                         connectPromise.setSuccess();
224                 }
225             };
226             //complete connection promise with netconf negotiation future
227             negotiationFuture.addListener(negotiationFutureListener);
228         }
229         startSsh(ctx, remoteAddress);
230     }
231
232     @Override
233     public void close(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception {
234         disconnect(ctx, promise);
235     }
236
237     @Override
238     public synchronized void disconnect(final ChannelHandlerContext ctx, final ChannelPromise promise) {
239         LOG.trace("Closing SSH session on channel: {} with connect promise in state: {}", ctx.channel(), connectPromise);
240
241         // If we have already succeeded and the session was dropped after, we need to fire inactive to notify reconnect logic
242         if(connectPromise.isSuccess()) {
243             ctx.fireChannelInactive();
244         }
245
246         if(sshWriteAsyncHandler != null) {
247             sshWriteAsyncHandler.close();
248         }
249
250         if(sshReadAsyncListener != null) {
251             sshReadAsyncListener.close();
252         }
253
254         //If connection promise is not already set, it means negotiation failed
255         //we must set connection promise to failure
256         if(!connectPromise.isDone()) {
257             connectPromise.setFailure(new IllegalStateException("Negotiation failed"));
258         }
259
260         //Remove listener from negotiation future, we don't want notifications
261         //from negotiation anymore
262         if(negotiationFuture != null) {
263             negotiationFuture.removeListener(negotiationFutureListener);
264         }
265
266         if(session!= null && !session.isClosed() && !session.isClosing()) {
267             session.close(false).addListener(new SshFutureListener<CloseFuture>() {
268                 @Override
269                 public void operationComplete(final CloseFuture future) {
270                     if (future.isClosed() == false) {
271                         session.close(true);
272                     }
273                     session = null;
274                 }
275             });
276         }
277
278         // Super disconnect is necessary in this case since we are using NioSocketChannel and it needs to cleanup its resources
279         // e.g. Socket that it tries to open in its constructor (https://bugs.opendaylight.org/show_bug.cgi?id=2430)
280         // TODO better solution would be to implement custom ChannelFactory + Channel that will use mina SSH lib internally: port this to custom channel implementation
281         try {
282             // Disconnect has to be closed after inactive channel event was fired, because it interferes with it
283             super.disconnect(ctx, ctx.newPromise());
284         } catch (final Exception e) {
285             LOG.warn("Unable to cleanup all resources for channel: {}. Ignoring.", ctx.channel(), e);
286         }
287
288         channel = null;
289         promise.setSuccess();
290         LOG.debug("SSH session closed on channel: {}", ctx.channel());
291     }
292
293 }