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