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