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