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