2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.connect.netconf.listener;
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;
47 public class NetconfDeviceCommunicator implements NetconfClientSessionListener, RemoteDeviceCommunicator<NetconfMessage> {
49 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceCommunicator.class);
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();
56 private final Semaphore semaphore;
57 private final int concurentRpcMsgs;
59 private final Queue<Request> requests = new ArrayDeque<>();
60 private NetconfClientSession session;
62 private Future<?> initFuture;
63 private final SettableFuture<NetconfDeviceCapabilities> firstConnectionFuture;
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);
72 public Boolean isSessionClosing() {
73 return isSessionClosing.get();
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);
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);
87 private NetconfDeviceCommunicator(final RemoteDeviceId id, final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
88 final Optional<UserPreferences> overrideNetconfCapabilities, final int rpcMessageLimit) {
89 this.concurentRpcMsgs = rpcMessageLimit;
91 this.remoteDevice = remoteDevice;
92 this.overrideNetconfCapabilities = overrideNetconfCapabilities;
93 this.firstConnectionFuture = SettableFuture.create();
94 this.semaphore = rpcMessageLimit > 0 ? new Semaphore(rpcMessageLimit) : null;
98 public void onSessionUp(final NetconfClientSession session) {
101 LOG.debug("{}: Session established", id);
102 this.session = session;
104 NetconfSessionPreferences netconfSessionPreferences =
105 NetconfSessionPreferences.fromNetconfSession(session);
106 LOG.trace("{}: Session advertised capabilities: {}", id,
107 netconfSessionPreferences);
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);
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);
123 remoteDevice.onRemoteSessionUp(netconfSessionPreferences, this);
124 if (!firstConnectionFuture.isDone()) {
125 firstConnectionFuture.set(netconfSessionPreferences.getNetconfDeviceCapabilities());
129 sessionLock.unlock();
137 * @return future that returns succes on first succesfull connection and failure when the underlying
138 * reconnecting strategy runs out of reconnection attempts
140 public ListenableFuture<NetconfDeviceCapabilities> initializeRemoteConnection(final NetconfClientDispatcher dispatcher, final NetconfClientConfiguration config) {
141 if(config instanceof NetconfReconnectingClientConfiguration) {
142 initFuture = dispatcher.createReconnectingClient((NetconfReconnectingClientConfiguration) config);
144 initFuture = dispatcher.createClient(config);
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());
157 return firstConnectionFuture;
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)) {
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!");
171 LOG.debug("Tearing down {}", reason);
172 final List<UncancellableFuture<RpcResult<NetconfMessage>>> futuresToCancel = Lists.newArrayList();
175 if( session != null ) {
179 * Walk all requests, check if they have been executing
180 * or cancelled and remove them from the queue.
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 );
188 } else if (r.future.isCancelled()) {
189 // This just does some house-cleaning
194 remoteDevice.onRemoteSessionDown();
198 sessionLock.unlock();
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() );
207 future.set( createErrorRpcResult( RpcError.ErrorType.TRANSPORT, reason ) );
211 isSessionClosing.set(false);
214 private RpcResult<NetconfMessage> createSessionDownRpcResult() {
215 return createErrorRpcResult( RpcError.ErrorType.TRANSPORT,
216 String.format( "The netconf session to %1$s is disconnected", id.getName() ) );
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();
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);
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() );
242 public void close() {
243 // Cancel reconnect if in progress
244 if(initFuture != null) {
245 initFuture.cancel(false);
247 // Disconnect from device
248 // tear down not necessary, called indirectly by the close in disconnect()
253 public void onMessage(final NetconfClientSession session, final NetconfMessage message) {
255 * Dispatch between notifications and messages. Messages need to be processed
256 * with lock held, notifications do not.
258 if (isNotification(message)) {
259 processNotification(message);
261 processMessage(message);
265 private void processMessage(final NetconfMessage message) {
266 Request request = null;
270 request = requests.peek();
271 if (request != null && request.future.isUncancellable()) {
273 // we have just removed one request from the queue
274 // we can also release one permit
275 if(semaphore != null) {
280 LOG.warn("{}: Ignoring unsolicited message {}", id,
285 sessionLock.unlock();
288 if( request != null ) {
290 LOG.debug("{}: Message received {}", id, message);
292 if(LOG.isTraceEnabled()) {
293 LOG.trace( "{}: Matched request: {} to response: {}", id, msgToS( request.request ), msgToS( message ) );
297 NetconfMessageTransformUtil.checkValidReply( request.request, message );
298 } catch (final NetconfDocumentedException e) {
300 "{}: Invalid request-reply match, reply message contains different message-id, request: {}, response: {}",
301 id, msgToS(request.request), msgToS(message), e);
303 request.future.set( RpcResultBuilder.<NetconfMessage>failed()
304 .withRpcError( NetconfMessageTransformUtil.toRpcError( e ) ).build() );
306 //recursively processing message to eventually find matching request
307 processMessage(message);
313 NetconfMessageTransformUtil.checkSuccessReply(message);
314 } catch(final NetconfDocumentedException e) {
316 "{}: Error reply from remote device, request: {}, response: {}",
317 id, msgToS(request.request), msgToS(message), e);
319 request.future.set( RpcResultBuilder.<NetconfMessage>failed()
320 .withRpcError( NetconfMessageTransformUtil.toRpcError( e ) ).build() );
324 request.future.set( RpcResultBuilder.success( message ).build() );
328 private static String msgToS(final NetconfMessage msg) {
329 return XmlUtil.toString(msg.getDocument());
333 public ListenableFuture<RpcResult<NetconfMessage>> sendRequest(final NetconfMessage message, final QName rpc) {
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()));
345 return sendRequestWithLock(message, rpc);
347 sessionLock.unlock();
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));
357 if (session == null) {
358 LOG.warn("{}: Session is disconnected, failing RPC request {}",
360 return Futures.immediateFuture( createSessionDownRpcResult() );
363 final Request req = new Request(new UncancellableFuture<>(true), message);
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()),
373 if( future.cause() != null ) {
374 req.future.set( createErrorRpcResult( RpcError.ErrorType.TRANSPORT,
375 future.cause().getLocalizedMessage() ) );
377 req.future.set( createSessionDownRpcResult() ); // assume session is down
379 req.future.setException( future.cause() );
382 LOG.trace("Finished sending request {}", req.request);
389 private void processNotification(final NetconfMessage notification) {
390 if(LOG.isTraceEnabled()) {
391 LOG.trace("{}: Notification received: {}", id, notification);
394 remoteDevice.onNotification(notification);
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()) ;
402 private static final class Request {
403 final UncancellableFuture<RpcResult<NetconfMessage>> future;
404 final NetconfMessage request;
406 private Request(final UncancellableFuture<RpcResult<NetconfMessage>> future,
407 final NetconfMessage request) {
408 this.future = future;
409 this.request = request;