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