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