Merge "Fix modules Restconf call for mounted devices"
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / 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.controller.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 io.netty.util.concurrent.Future;
16 import io.netty.util.concurrent.FutureListener;
17 import io.netty.util.concurrent.GenericFutureListener;
18 import java.util.ArrayDeque;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Queue;
22 import java.util.concurrent.locks.Lock;
23 import java.util.concurrent.locks.ReentrantLock;
24 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
25 import org.opendaylight.controller.netconf.api.NetconfMessage;
26 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
27 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
28 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
29 import org.opendaylight.controller.netconf.client.NetconfClientSession;
30 import org.opendaylight.controller.netconf.client.NetconfClientSessionListener;
31 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
32 import org.opendaylight.controller.netconf.client.conf.NetconfReconnectingClientConfiguration;
33 import org.opendaylight.controller.netconf.util.xml.XmlElement;
34 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
35 import org.opendaylight.controller.sal.connect.api.RemoteDevice;
36 import org.opendaylight.controller.sal.connect.api.RemoteDeviceCommunicator;
37 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
38 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.common.RpcError;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public class NetconfDeviceCommunicator implements NetconfClientSessionListener, RemoteDeviceCommunicator<NetconfMessage> {
47
48     private static final Logger logger = LoggerFactory.getLogger(NetconfDeviceCommunicator.class);
49
50     private final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice;
51     private final Optional<NetconfSessionPreferences> overrideNetconfCapabilities;
52     private final RemoteDeviceId id;
53     private final Lock sessionLock = new ReentrantLock();
54
55     // TODO implement concurrent message limit
56     private final Queue<Request> requests = new ArrayDeque<>();
57     private NetconfClientSession session;
58     private Future<?> initFuture;
59
60     public NetconfDeviceCommunicator(final RemoteDeviceId id, final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
61             final NetconfSessionPreferences NetconfSessionPreferences) {
62         this(id, remoteDevice, Optional.of(NetconfSessionPreferences));
63     }
64
65     public NetconfDeviceCommunicator(final RemoteDeviceId id,
66                                      final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice) {
67         this(id, remoteDevice, Optional.<NetconfSessionPreferences>absent());
68     }
69
70     private NetconfDeviceCommunicator(final RemoteDeviceId id, final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
71             final Optional<NetconfSessionPreferences> overrideNetconfCapabilities) {
72         this.id = id;
73         this.remoteDevice = remoteDevice;
74         this.overrideNetconfCapabilities = overrideNetconfCapabilities;
75     }
76
77     @Override
78     public void onSessionUp(final NetconfClientSession session) {
79         sessionLock.lock();
80         try {
81             logger.debug("{}: Session established", id);
82             this.session = session;
83
84             NetconfSessionPreferences netconfSessionPreferences =
85                                              NetconfSessionPreferences.fromNetconfSession(session);
86             logger.trace("{}: Session advertised capabilities: {}", id, netconfSessionPreferences);
87
88             if(overrideNetconfCapabilities.isPresent()) {
89                 netconfSessionPreferences = netconfSessionPreferences.addModuleCaps(overrideNetconfCapabilities.get());
90                 logger.debug("{}: Session capabilities overridden, capabilities that will be used: {}", id, netconfSessionPreferences);
91             }
92
93             remoteDevice.onRemoteSessionUp(netconfSessionPreferences, this);
94         }
95         finally {
96             sessionLock.unlock();
97         }
98     }
99
100     public void initializeRemoteConnection(final NetconfClientDispatcher dispatcher, final NetconfClientConfiguration config) {
101         // TODO 2313 extract listener from configuration
102         if(config instanceof NetconfReconnectingClientConfiguration) {
103             initFuture = dispatcher.createReconnectingClient((NetconfReconnectingClientConfiguration) config);
104         } else {
105             initFuture = dispatcher.createClient(config);
106         }
107
108
109         initFuture.addListener(new GenericFutureListener<Future<Object>>(){
110
111             @Override
112             public void operationComplete(Future<Object> future) throws Exception {
113                 if (!future.isSuccess() && !future.isCancelled()) {
114                     logger.debug("{}: Connection failed", id, future.cause());
115                     NetconfDeviceCommunicator.this.remoteDevice.onRemoteSessionFailed(future.cause());
116                 }
117             }
118         });
119
120     }
121
122     public void disconnect() {
123         if(session != null) {
124             session.close();
125         }
126     }
127
128     private void tearDown( String reason ) {
129         List<UncancellableFuture<RpcResult<NetconfMessage>>> futuresToCancel = Lists.newArrayList();
130         sessionLock.lock();
131         try {
132             if( session != null ) {
133                 session = null;
134
135                 /*
136                  * Walk all requests, check if they have been executing
137                  * or cancelled and remove them from the queue.
138                  */
139                 final Iterator<Request> it = requests.iterator();
140                 while (it.hasNext()) {
141                     final Request r = it.next();
142                     if (r.future.isUncancellable()) {
143                         futuresToCancel.add( r.future );
144                         it.remove();
145                     } else if (r.future.isCancelled()) {
146                         // This just does some house-cleaning
147                         it.remove();
148                     }
149                 }
150
151                 remoteDevice.onRemoteSessionDown();
152             }
153         }
154         finally {
155             sessionLock.unlock();
156         }
157
158         // Notify pending request futures outside of the sessionLock to avoid unnecessarily
159         // blocking the caller.
160         for( UncancellableFuture<RpcResult<NetconfMessage>> future: futuresToCancel ) {
161             if( Strings.isNullOrEmpty( reason ) ) {
162                 future.set( createSessionDownRpcResult() );
163             } else {
164                 future.set( createErrorRpcResult( RpcError.ErrorType.TRANSPORT, reason ) );
165             }
166         }
167     }
168
169     private RpcResult<NetconfMessage> createSessionDownRpcResult() {
170         return createErrorRpcResult( RpcError.ErrorType.TRANSPORT,
171                              String.format( "The netconf session to %1$s is disconnected", id.getName() ) );
172     }
173
174     private RpcResult<NetconfMessage> createErrorRpcResult( RpcError.ErrorType errorType, String message ) {
175         return RpcResultBuilder.<NetconfMessage>failed()
176                 .withError(errorType, NetconfDocumentedException.ErrorTag.operation_failed.getTagValue(), message).build();
177     }
178
179     @Override
180     public void onSessionDown(final NetconfClientSession session, final Exception e) {
181         logger.warn("{}: Session went down", id, e);
182         tearDown( null );
183     }
184
185     @Override
186     public void onSessionTerminated(final NetconfClientSession session, final NetconfTerminationReason reason) {
187         logger.warn("{}: Session terminated {}", id, reason);
188         tearDown( reason.getErrorMessage() );
189     }
190
191     @Override
192     public void close() {
193         // Cancel reconnect if in progress
194         if(initFuture != null) {
195             initFuture.cancel(false);
196         }
197         // Disconnect from device
198         if(session != null) {
199             session.close();
200             // tear down not necessary, called indirectly by above close
201         }
202     }
203
204     @Override
205     public void onMessage(final NetconfClientSession session, final NetconfMessage message) {
206         /*
207          * Dispatch between notifications and messages. Messages need to be processed
208          * with lock held, notifications do not.
209          */
210         if (isNotification(message)) {
211             processNotification(message);
212         } else {
213             processMessage(message);
214         }
215     }
216
217     private void processMessage(final NetconfMessage message) {
218         Request request = null;
219         sessionLock.lock();
220
221         try {
222             request = requests.peek();
223             if (request != null && request.future.isUncancellable()) {
224                 requests.poll();
225             } else {
226                 request = null;
227                 logger.warn("{}: Ignoring unsolicited message {}", id, msgToS(message));
228             }
229         }
230         finally {
231             sessionLock.unlock();
232         }
233
234         if( request != null ) {
235
236             logger.debug("{}: Message received {}", id, message);
237
238             if(logger.isTraceEnabled()) {
239                 logger.trace( "{}: Matched request: {} to response: {}", id, msgToS( request.request ), msgToS( message ) );
240             }
241
242             try {
243                 NetconfMessageTransformUtil.checkValidReply( request.request, message );
244             } catch (final NetconfDocumentedException e) {
245                 logger.warn( "{}: Invalid request-reply match, reply message contains different message-id, request: {}, response: {}",
246                              id, msgToS( request.request ), msgToS( message ), e );
247
248                 request.future.set( RpcResultBuilder.<NetconfMessage>failed()
249                         .withRpcError( NetconfMessageTransformUtil.toRpcError( e ) ).build() );
250
251                 //recursively processing message to eventually find matching request
252                 processMessage(message);
253
254                 return;
255             }
256
257             try {
258                 NetconfMessageTransformUtil.checkSuccessReply(message);
259             } catch(final NetconfDocumentedException e) {
260                 logger.warn( "{}: Error reply from remote device, request: {}, response: {}", id,
261                              msgToS( request.request ), msgToS( message ), e );
262
263                 request.future.set( RpcResultBuilder.<NetconfMessage>failed()
264                         .withRpcError( NetconfMessageTransformUtil.toRpcError( e ) ).build() );
265                 return;
266             }
267
268             request.future.set( RpcResultBuilder.success( message ).build() );
269         }
270     }
271
272     private static String msgToS(final NetconfMessage msg) {
273         return XmlUtil.toString(msg.getDocument());
274     }
275
276     @Override
277     public ListenableFuture<RpcResult<NetconfMessage>> sendRequest(final NetconfMessage message, final QName rpc) {
278         sessionLock.lock();
279         try {
280             return sendRequestWithLock( message, rpc );
281         } finally {
282             sessionLock.unlock();
283         }
284     }
285
286     private ListenableFuture<RpcResult<NetconfMessage>> sendRequestWithLock(
287                                                final NetconfMessage message, final QName rpc) {
288         if(logger.isTraceEnabled()) {
289             logger.trace("{}: Sending message {}", id, msgToS(message));
290         }
291
292         if (session == null) {
293             logger.warn("{}: Session is disconnected, failing RPC request {}", id, message);
294             return Futures.immediateFuture( createSessionDownRpcResult() );
295         }
296
297         final Request req = new Request( new UncancellableFuture<RpcResult<NetconfMessage>>(true),
298                                          message );
299         requests.add(req);
300
301         session.sendMessage(req.request).addListener(new FutureListener<Void>() {
302             @Override
303             public void operationComplete(final Future<Void> future) throws Exception {
304                 if( !future.isSuccess() ) {
305                     // We expect that a session down will occur at this point
306                     logger.debug( "{}: Failed to send request {}", id,
307                                   XmlUtil.toString(req.request.getDocument()), future.cause() );
308
309                     if( future.cause() != null ) {
310                         req.future.set( createErrorRpcResult( RpcError.ErrorType.TRANSPORT,
311                                                               future.cause().getLocalizedMessage() ) );
312                     } else {
313                         req.future.set( createSessionDownRpcResult() ); // assume session is down
314                     }
315                     req.future.setException( future.cause() );
316                 }
317                 else {
318                     logger.trace( "Finished sending request {}", req.request );
319                 }
320             }
321         });
322
323         return req.future;
324     }
325
326     private void processNotification(final NetconfMessage notification) {
327         logger.debug("{}: Notification received: {}", id, notification);
328
329         if(logger.isTraceEnabled()) {
330             logger.trace("{}: Notification received: {}", id, msgToS(notification));
331         }
332
333         remoteDevice.onNotification(notification);
334     }
335
336     private static boolean isNotification(final NetconfMessage message) {
337         final XmlElement xmle = XmlElement.fromDomDocument(message.getDocument());
338         return XmlNetconfConstants.NOTIFICATION_ELEMENT_NAME.equals(xmle.getName()) ;
339     }
340
341     private static final class Request {
342         final UncancellableFuture<RpcResult<NetconfMessage>> future;
343         final NetconfMessage request;
344
345         private Request(final UncancellableFuture<RpcResult<NetconfMessage>> future,
346                         final NetconfMessage request) {
347             this.future = future;
348             this.request = request;
349         }
350     }
351 }