BUG-2340 Fix improper cleanup of resources in netconf ssh handler
[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
141         // TODO we should also read from error stream and at least log from that
142
143         sshReadAsyncListener = new AsyncSshHandlerReader(new AutoCloseable() {
144             @Override
145             public void close() throws Exception {
146                 AsyncSshHandler.this.disconnect(ctx, ctx.newPromise());
147             }
148         }, new AsyncSshHandlerReader.ReadMsgHandler() {
149             @Override
150             public void onMessageRead(final ByteBuf msg) {
151                 ctx.fireChannelRead(msg);
152             }
153         }, channel.toString(), channel.getAsyncOut());
154
155         // if readAsyncListener receives immediate close, it will close this handler and closing this handler sets channel variable to null
156         if(channel != null) {
157             sshWriteAsyncHandler = new AsyncSshHandlerWriter(channel.getAsyncIn());
158             ctx.fireChannelActive();
159         }
160     }
161
162     private synchronized void handleSshSetupFailure(final ChannelHandlerContext ctx, final Throwable e) {
163         LOG.warn("Unable to setup SSH connection on channel: {}", ctx.channel(), e);
164         disconnect(ctx, ctx.newPromise());
165
166         // If the promise is not yet done, we have failed with initial connect and set connectPromise to failure
167         if(!connectPromise.isDone()) {
168             connectPromise.setFailure(e);
169         }
170     }
171
172     @Override
173     public synchronized void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
174         sshWriteAsyncHandler.write(ctx, msg, promise);
175     }
176
177     @Override
178     public synchronized void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) throws Exception {
179         this.connectPromise = promise;
180         startSsh(ctx, remoteAddress);
181     }
182
183     @Override
184     public void close(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception {
185         disconnect(ctx, promise);
186     }
187
188     @Override
189     public synchronized void disconnect(final ChannelHandlerContext ctx, final ChannelPromise promise) {
190         // Super disconnect is necessary in this case since we are using NioSocketChannel and it needs to cleanup its resources
191         // e.g. Socket that it tries to open in its constructor (https://bugs.opendaylight.org/show_bug.cgi?id=2430)
192         // TODO better solution would be to implement custom ChannelFactory + Channel that will use mina SSH lib internally: port this to custom channel implementation
193         try {
194             super.disconnect(ctx, ctx.newPromise());
195         } catch (final Exception e) {
196             LOG.warn("Unable to cleanup all resources for channel: {}. Ignoring.", ctx.channel(), e);
197         }
198
199         if(sshReadAsyncListener != null) {
200             sshReadAsyncListener.close();
201         }
202
203         if(sshWriteAsyncHandler != null) {
204             sshWriteAsyncHandler.close();
205         }
206
207         if(session!= null && !session.isClosed() && !session.isClosing()) {
208             session.close(false).addListener(new SshFutureListener<CloseFuture>() {
209                 @Override
210                 public void operationComplete(final CloseFuture future) {
211                     if (future.isClosed() == false) {
212                         session.close(true);
213                     }
214                     session = null;
215                 }
216             });
217         }
218
219         // If we have already succeeded and the session was dropped after, we need to fire inactive to notify reconnect logic
220         if(connectPromise.isSuccess()) {
221             ctx.fireChannelInactive();
222         }
223
224         channel = null;
225
226         promise.setSuccess();
227         LOG.debug("SSH session closed on channel: {}", ctx.channel());
228     }
229
230 }