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 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;
49 public class NetconfDeviceCommunicator implements NetconfClientSessionListener, RemoteDeviceCommunicator<NetconfMessage> {
51 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceCommunicator.class);
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();
58 private final Semaphore semaphore;
59 private final int concurentRpcMsgs;
61 private final Queue<Request> requests = new ArrayDeque<>();
62 private NetconfClientSession session;
64 private Future<?> initFuture;
65 private final SettableFuture<NetconfDeviceCapabilities> firstConnectionFuture;
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 final AtomicBoolean isSessionClosing = new AtomicBoolean(false);
74 public Boolean isSessionClosing() {
75 return isSessionClosing.get();
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);
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);
89 private NetconfDeviceCommunicator(final RemoteDeviceId id, final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
90 final Optional<UserPreferences> overrideNetconfCapabilities, final int rpcMessageLimit) {
91 this.concurentRpcMsgs = rpcMessageLimit;
93 this.remoteDevice = remoteDevice;
94 this.overrideNetconfCapabilities = overrideNetconfCapabilities;
95 this.firstConnectionFuture = SettableFuture.create();
96 this.semaphore = rpcMessageLimit > 0 ? new Semaphore(rpcMessageLimit) : null;
100 public void onSessionUp(final NetconfClientSession session) {
103 LOG.debug("{}: Session established", id);
104 this.session = session;
106 NetconfSessionPreferences netconfSessionPreferences =
107 NetconfSessionPreferences.fromNetconfSession(session);
108 LOG.trace("{}: Session advertised capabilities: {}", id,
109 netconfSessionPreferences);
111 if(overrideNetconfCapabilities.isPresent()) {
112 netconfSessionPreferences = overrideNetconfCapabilities.get().isOverride() ?
113 netconfSessionPreferences.replaceModuleCaps(overrideNetconfCapabilities.get().getSessionPreferences()) :
114 netconfSessionPreferences.addModuleCaps(overrideNetconfCapabilities.get().getSessionPreferences());
116 "{}: Session capabilities overridden, capabilities that will be used: {}",
117 id, netconfSessionPreferences);
121 remoteDevice.onRemoteSessionUp(netconfSessionPreferences, this);
122 if (!firstConnectionFuture.isDone()) {
123 firstConnectionFuture.set(netconfSessionPreferences.getNetconfDeviceCapabilities());
127 sessionLock.unlock();
135 * @return future that returns succes on first succesfull connection and failure when the underlying
136 * reconnecting strategy runs out of reconnection attempts
138 public ListenableFuture<NetconfDeviceCapabilities> initializeRemoteConnection(final NetconfClientDispatcher dispatcher, final NetconfClientConfiguration config) {
139 if(config instanceof NetconfReconnectingClientConfiguration) {
140 initFuture = dispatcher.createReconnectingClient((NetconfReconnectingClientConfiguration) config);
142 initFuture = dispatcher.createClient(config);
146 initFuture.addListener(new GenericFutureListener<Future<Object>>(){
149 public void operationComplete(final 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());
159 return firstConnectionFuture;
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)) {
169 private void tearDown(final String reason) {
170 if (!isSessionClosing()) {
171 LOG.warn("It's curious that no one to close the session but tearDown is called!");
173 LOG.debug("Tearing down {}", reason);
174 final List<UncancellableFuture<RpcResult<NetconfMessage>>> futuresToCancel = Lists.newArrayList();
177 if( session != null ) {
181 * Walk all requests, check if they have been executing
182 * or cancelled and remove them from the queue.
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 );
190 } else if (r.future.isCancelled()) {
191 // This just does some house-cleaning
196 remoteDevice.onRemoteSessionDown();
200 sessionLock.unlock();
203 // Notify pending request futures outside of the sessionLock to avoid unnecessarily
204 // blocking the caller.
205 for (final UncancellableFuture<RpcResult<NetconfMessage>> future : futuresToCancel) {
206 if( Strings.isNullOrEmpty( reason ) ) {
207 future.set( createSessionDownRpcResult() );
209 future.set( createErrorRpcResult( RpcError.ErrorType.TRANSPORT, reason ) );
213 isSessionClosing.set(false);
216 private RpcResult<NetconfMessage> createSessionDownRpcResult() {
217 return createErrorRpcResult( RpcError.ErrorType.TRANSPORT,
218 String.format( "The netconf session to %1$s is disconnected", id.getName() ) );
221 private RpcResult<NetconfMessage> createErrorRpcResult(final RpcError.ErrorType errorType, final String message) {
222 return RpcResultBuilder.<NetconfMessage>failed()
223 .withError(errorType, NetconfDocumentedException.ErrorTag.OPERATION_FAILED.getTagValue(), message).build();
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);
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() );
243 public void close() {
244 // Cancel reconnect if in progress
245 if(initFuture != null) {
246 initFuture.cancel(false);
248 // Disconnect from device
249 // tear down not necessary, called indirectly by the close in disconnect()
254 public void onMessage(final NetconfClientSession session, final NetconfMessage message) {
256 * Dispatch between notifications and messages. Messages need to be processed
257 * with lock held, notifications do not.
259 if (isNotification(message)) {
260 processNotification(message);
262 processMessage(message);
266 private void processMessage(final NetconfMessage message) {
267 Request request = null;
271 request = requests.peek();
272 if (request != null && request.future.isUncancellable()) {
274 // we have just removed one request from the queue
275 // we can also release one permit
276 if(semaphore != null) {
281 LOG.warn("{}: Ignoring unsolicited message {}", id,
286 sessionLock.unlock();
289 if( request != null ) {
291 LOG.debug("{}: Message received {}", id, message);
293 if(LOG.isTraceEnabled()) {
294 LOG.trace( "{}: Matched request: {} to response: {}", id, msgToS( request.request ), msgToS( message ) );
298 NetconfMessageTransformUtil.checkValidReply( request.request, message );
299 } catch (final NetconfDocumentedException e) {
301 "{}: Invalid request-reply match, reply message contains different message-id, request: {}, response: {}",
302 id, msgToS(request.request), msgToS(message), e);
304 request.future.set( RpcResultBuilder.<NetconfMessage>failed()
305 .withRpcError( NetconfMessageTransformUtil.toRpcError( e ) ).build() );
307 //recursively processing message to eventually find matching request
308 processMessage(message);
314 NetconfMessageTransformUtil.checkSuccessReply(message);
315 } catch(final NetconfDocumentedException e) {
317 "{}: Error reply from remote device, request: {}, response: {}",
318 id, msgToS(request.request), msgToS(message), e);
320 request.future.set( RpcResultBuilder.<NetconfMessage>failed()
321 .withRpcError( NetconfMessageTransformUtil.toRpcError( e ) ).build() );
325 request.future.set( RpcResultBuilder.success( message ).build() );
329 private static String msgToS(final NetconfMessage msg) {
330 return XmlUtil.toString(msg.getDocument());
334 public ListenableFuture<RpcResult<NetconfMessage>> sendRequest(final NetconfMessage message, final QName rpc) {
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()));
346 return sendRequestWithLock(message, rpc);
348 sessionLock.unlock();
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));
358 if (session == null) {
359 LOG.warn("{}: Session is disconnected, failing RPC request {}",
361 return Futures.immediateFuture( createSessionDownRpcResult() );
364 final Request req = new Request( new UncancellableFuture<RpcResult<NetconfMessage>>(true),
368 session.sendMessage(req.request).addListener(new FutureListener<Void>() {
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()),
377 if( future.cause() != null ) {
378 req.future.set( createErrorRpcResult( RpcError.ErrorType.TRANSPORT,
379 future.cause().getLocalizedMessage() ) );
381 req.future.set( createSessionDownRpcResult() ); // assume session is down
383 req.future.setException( future.cause() );
386 LOG.trace("Finished sending request {}", req.request);
394 private void processNotification(final NetconfMessage notification) {
395 if(LOG.isTraceEnabled()) {
396 LOG.trace("{}: Notification received: {}", id, notification);
399 remoteDevice.onNotification(notification);
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()) ;
407 private static final class Request {
408 final UncancellableFuture<RpcResult<NetconfMessage>> future;
409 final NetconfMessage request;
411 private Request(final UncancellableFuture<RpcResult<NetconfMessage>> future,
412 final NetconfMessage request) {
413 this.future = future;
414 this.request = request;