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