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