BUG 5634 : Implement concurrent message limit
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / listener / NetconfDeviceCommunicator.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 package org.opendaylight.netconf.sal.connect.netconf.listener;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Strings;
12 import com.google.common.collect.Lists;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import io.netty.util.concurrent.Future;
17 import io.netty.util.concurrent.FutureListener;
18 import io.netty.util.concurrent.GenericFutureListener;
19 import java.util.ArrayDeque;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Queue;
23 import java.util.concurrent.Semaphore;
24 import java.util.concurrent.locks.Lock;
25 import java.util.concurrent.locks.ReentrantLock;
26 import org.opendaylight.controller.config.util.xml.XmlElement;
27 import org.opendaylight.controller.config.util.xml.XmlUtil;
28 import org.opendaylight.netconf.api.NetconfDocumentedException;
29 import org.opendaylight.netconf.api.NetconfMessage;
30 import org.opendaylight.netconf.api.NetconfTerminationReason;
31 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
32 import org.opendaylight.netconf.client.NetconfClientDispatcher;
33 import org.opendaylight.netconf.client.NetconfClientSession;
34 import org.opendaylight.netconf.client.NetconfClientSessionListener;
35 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
36 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
37 import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
38 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
39 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
40 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.common.RpcError;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class NetconfDeviceCommunicator implements NetconfClientSessionListener, RemoteDeviceCommunicator<NetconfMessage> {
49
50     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceCommunicator.class);
51
52     protected final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice;
53     private final Optional<UserPreferences> overrideNetconfCapabilities;
54     protected final RemoteDeviceId id;
55     private final Lock sessionLock = new ReentrantLock();
56
57     private final Semaphore semaphore;
58     private final int concurentRpcMsgs;
59
60     private final Queue<Request> requests = new ArrayDeque<>();
61     private NetconfClientSession session;
62
63     private Future<?> initFuture;
64     private SettableFuture<NetconfDeviceCapabilities> firstConnectionFuture;
65
66     public NetconfDeviceCommunicator(final RemoteDeviceId id, final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
67             final UserPreferences NetconfSessionPreferences, final int rpcMessageLimit) {
68         this(id, remoteDevice, Optional.of(NetconfSessionPreferences), rpcMessageLimit);
69     }
70
71     public NetconfDeviceCommunicator(final RemoteDeviceId id,
72                                      final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
73                                      final int rpcMessageLimit) {
74         this(id, remoteDevice, Optional.<UserPreferences>absent(), rpcMessageLimit);
75     }
76
77     private NetconfDeviceCommunicator(final RemoteDeviceId id, final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
78                                       final Optional<UserPreferences> overrideNetconfCapabilities, final int rpcMessageLimit) {
79         this.concurentRpcMsgs = rpcMessageLimit;
80         this.id = id;
81         this.remoteDevice = remoteDevice;
82         this.overrideNetconfCapabilities = overrideNetconfCapabilities;
83         this.firstConnectionFuture = SettableFuture.create();
84         this.semaphore = rpcMessageLimit > 0 ? new Semaphore(rpcMessageLimit) : null;
85     }
86
87     @Override
88     public void onSessionUp(final NetconfClientSession session) {
89         sessionLock.lock();
90         try {
91             LOG.debug("{}: Session established", id);
92             this.session = session;
93
94             NetconfSessionPreferences netconfSessionPreferences =
95                                              NetconfSessionPreferences.fromNetconfSession(session);
96             LOG.trace("{}: Session advertised capabilities: {}", id,
97                     netconfSessionPreferences);
98
99             if(overrideNetconfCapabilities.isPresent()) {
100                 netconfSessionPreferences = overrideNetconfCapabilities.get().isOverride() ?
101                         netconfSessionPreferences.replaceModuleCaps(overrideNetconfCapabilities.get().getSessionPreferences()) :
102                         netconfSessionPreferences.addModuleCaps(overrideNetconfCapabilities.get().getSessionPreferences());
103                 LOG.debug(
104                         "{}: Session capabilities overridden, capabilities that will be used: {}",
105                         id, netconfSessionPreferences);
106             }
107
108
109             remoteDevice.onRemoteSessionUp(netconfSessionPreferences, this);
110             if (!firstConnectionFuture.isDone()) {
111                 firstConnectionFuture.set(netconfSessionPreferences.getNetconfDeviceCapabilities());
112             }
113         }
114         finally {
115             sessionLock.unlock();
116         }
117     }
118
119     /**
120      *
121      * @param dispatcher
122      * @param config
123      * @return future that returns succes on first succesfull connection and failure when the underlying
124      * reconnecting strategy runs out of reconnection attempts
125      */
126     public ListenableFuture<NetconfDeviceCapabilities> initializeRemoteConnection(final NetconfClientDispatcher dispatcher, final NetconfClientConfiguration config) {
127         if(config instanceof NetconfReconnectingClientConfiguration) {
128             initFuture = dispatcher.createReconnectingClient((NetconfReconnectingClientConfiguration) config);
129         } else {
130             initFuture = dispatcher.createClient(config);
131         }
132
133
134         initFuture.addListener(new GenericFutureListener<Future<Object>>(){
135
136             @Override
137             public void operationComplete(Future<Object> future) throws Exception {
138                 if (!future.isSuccess() && !future.isCancelled()) {
139                     LOG.debug("{}: Connection failed", id, future.cause());
140                     NetconfDeviceCommunicator.this.remoteDevice.onRemoteSessionFailed(future.cause());
141                     if (firstConnectionFuture.isDone()) {
142                         firstConnectionFuture.setException(future.cause());
143                     }
144                 }
145             }
146         });
147         return firstConnectionFuture;
148     }
149
150     public void disconnect() {
151         if(session != null) {
152             session.close();
153         }
154     }
155
156     private void tearDown( String reason ) {
157         LOG.debug("Tearing down {}", reason);
158         List<UncancellableFuture<RpcResult<NetconfMessage>>> futuresToCancel = Lists.newArrayList();
159         sessionLock.lock();
160         try {
161             if( session != null ) {
162                 session = null;
163
164                 /*
165                  * Walk all requests, check if they have been executing
166                  * or cancelled and remove them from the queue.
167                  */
168                 final Iterator<Request> it = requests.iterator();
169                 while (it.hasNext()) {
170                     final Request r = it.next();
171                     if (r.future.isUncancellable()) {
172                         futuresToCancel.add( r.future );
173                         it.remove();
174                     } else if (r.future.isCancelled()) {
175                         // This just does some house-cleaning
176                         it.remove();
177                     }
178                 }
179
180                 remoteDevice.onRemoteSessionDown();
181             }
182         }
183         finally {
184             sessionLock.unlock();
185         }
186
187         // Notify pending request futures outside of the sessionLock to avoid unnecessarily
188         // blocking the caller.
189         for( UncancellableFuture<RpcResult<NetconfMessage>> future: futuresToCancel ) {
190             if( Strings.isNullOrEmpty( reason ) ) {
191                 future.set( createSessionDownRpcResult() );
192             } else {
193                 future.set( createErrorRpcResult( RpcError.ErrorType.TRANSPORT, reason ) );
194             }
195         }
196     }
197
198     private RpcResult<NetconfMessage> createSessionDownRpcResult() {
199         return createErrorRpcResult( RpcError.ErrorType.TRANSPORT,
200                              String.format( "The netconf session to %1$s is disconnected", id.getName() ) );
201     }
202
203     private RpcResult<NetconfMessage> createErrorRpcResult( RpcError.ErrorType errorType, String message ) {
204         return RpcResultBuilder.<NetconfMessage>failed()
205                 .withError(errorType, NetconfDocumentedException.ErrorTag.operation_failed.getTagValue(), message).build();
206     }
207
208     @Override
209     public void onSessionDown(final NetconfClientSession session, final Exception e) {
210         LOG.warn("{}: Session went down", id, e);
211         tearDown( null );
212     }
213
214     @Override
215     public void onSessionTerminated(final NetconfClientSession session, final NetconfTerminationReason reason) {
216         LOG.warn("{}: Session terminated {}", id, reason);
217         tearDown( reason.getErrorMessage() );
218     }
219
220     @Override
221     public void close() {
222         // Cancel reconnect if in progress
223         if(initFuture != null) {
224             initFuture.cancel(false);
225         }
226         // Disconnect from device
227         if(session != null) {
228             session.close();
229             // tear down not necessary, called indirectly by above close
230         }
231     }
232
233     @Override
234     public void onMessage(final NetconfClientSession session, final NetconfMessage message) {
235         /*
236          * Dispatch between notifications and messages. Messages need to be processed
237          * with lock held, notifications do not.
238          */
239         if (isNotification(message)) {
240             processNotification(message);
241         } else {
242             processMessage(message);
243         }
244     }
245
246     private void processMessage(final NetconfMessage message) {
247         Request request = null;
248         sessionLock.lock();
249
250         try {
251             request = requests.peek();
252             if (request != null && request.future.isUncancellable()) {
253                 requests.poll();
254                 // we have just removed one request from the queue
255                 // we can also release one permit
256                 if(semaphore != null) {
257                     semaphore.release();
258                 }
259             } else {
260                 request = null;
261                 LOG.warn("{}: Ignoring unsolicited message {}", id,
262                         msgToS(message));
263             }
264         }
265         finally {
266             sessionLock.unlock();
267         }
268
269         if( request != null ) {
270
271             LOG.debug("{}: Message received {}", id, message);
272
273             if(LOG.isTraceEnabled()) {
274                 LOG.trace( "{}: Matched request: {} to response: {}", id, msgToS( request.request ), msgToS( message ) );
275             }
276
277             try {
278                 NetconfMessageTransformUtil.checkValidReply( request.request, message );
279             } catch (final NetconfDocumentedException e) {
280                 LOG.warn(
281                         "{}: Invalid request-reply match, reply message contains different message-id, request: {}, response: {}",
282                         id, msgToS(request.request), msgToS(message), e);
283
284                 request.future.set( RpcResultBuilder.<NetconfMessage>failed()
285                         .withRpcError( NetconfMessageTransformUtil.toRpcError( e ) ).build() );
286
287                 //recursively processing message to eventually find matching request
288                 processMessage(message);
289
290                 return;
291             }
292
293             try {
294                 NetconfMessageTransformUtil.checkSuccessReply(message);
295             } catch(final NetconfDocumentedException e) {
296                 LOG.warn(
297                         "{}: Error reply from remote device, request: {}, response: {}",
298                         id, msgToS(request.request), msgToS(message), e);
299
300                 request.future.set( RpcResultBuilder.<NetconfMessage>failed()
301                         .withRpcError( NetconfMessageTransformUtil.toRpcError( e ) ).build() );
302                 return;
303             }
304
305             request.future.set( RpcResultBuilder.success( message ).build() );
306         }
307     }
308
309     private static String msgToS(final NetconfMessage msg) {
310         return XmlUtil.toString(msg.getDocument());
311     }
312
313     @Override
314     public ListenableFuture<RpcResult<NetconfMessage>> sendRequest(final NetconfMessage message, final QName rpc) {
315         sessionLock.lock();
316
317         if (semaphore != null && !semaphore.tryAcquire()) {
318             LOG.warn("Limit of concurrent rpc messages was reached (limit :" +
319                     concurentRpcMsgs + "). Rpc reply message is needed. Discarding request of Netconf device with id" + id.getName());
320             sessionLock.unlock();
321             return Futures.immediateFailedFuture(new NetconfDocumentedException("Limit of rpc messages was reached (Limit :" +
322                     concurentRpcMsgs + ") waiting for emptying the queue of Netconf device with id" + id.getName()));
323         }
324
325         try {
326             return sendRequestWithLock(message, rpc);
327         } finally {
328             sessionLock.unlock();
329         }
330     }
331
332     private ListenableFuture<RpcResult<NetconfMessage>> sendRequestWithLock(
333                                                final NetconfMessage message, final QName rpc) {
334         if(LOG.isTraceEnabled()) {
335             LOG.trace("{}: Sending message {}", id, msgToS(message));
336         }
337
338         if (session == null) {
339             LOG.warn("{}: Session is disconnected, failing RPC request {}",
340                     id, message);
341             return Futures.immediateFuture( createSessionDownRpcResult() );
342         }
343
344         final Request req = new Request( new UncancellableFuture<RpcResult<NetconfMessage>>(true),
345                                          message );
346         requests.add(req);
347
348         session.sendMessage(req.request).addListener(new FutureListener<Void>() {
349             @Override
350             public void operationComplete(final Future<Void> future) throws Exception {
351                 if( !future.isSuccess() ) {
352                     // We expect that a session down will occur at this point
353                     LOG.debug("{}: Failed to send request {}", id,
354                             XmlUtil.toString(req.request.getDocument()),
355                             future.cause());
356
357                     if( future.cause() != null ) {
358                         req.future.set( createErrorRpcResult( RpcError.ErrorType.TRANSPORT,
359                                                               future.cause().getLocalizedMessage() ) );
360                     } else {
361                         req.future.set( createSessionDownRpcResult() ); // assume session is down
362                     }
363                     req.future.setException( future.cause() );
364                 }
365                 else {
366                     LOG.trace("Finished sending request {}", req.request);
367                 }
368             }
369         });
370
371         return req.future;
372     }
373
374     private void processNotification(final NetconfMessage notification) {
375         if(LOG.isTraceEnabled()) {
376             LOG.trace("{}: Notification received: {}", id, notification);
377         }
378
379         remoteDevice.onNotification(notification);
380     }
381
382     private static boolean isNotification(final NetconfMessage message) {
383         final XmlElement xmle = XmlElement.fromDomDocument(message.getDocument());
384         return XmlNetconfConstants.NOTIFICATION_ELEMENT_NAME.equals(xmle.getName()) ;
385     }
386
387     private static final class Request {
388         final UncancellableFuture<RpcResult<NetconfMessage>> future;
389         final NetconfMessage request;
390
391         private Request(final UncancellableFuture<RpcResult<NetconfMessage>> future,
392                         final NetconfMessage request) {
393             this.future = future;
394             this.request = request;
395         }
396     }
397 }