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