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