import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
-import org.opendaylight.controller.config.util.xml.XmlElement;
-import org.opendaylight.controller.config.util.xml.XmlUtil;
+import org.opendaylight.netconf.api.FailedNetconfMessage;
import org.opendaylight.netconf.api.NetconfDocumentedException;
import org.opendaylight.netconf.api.NetconfMessage;
import org.opendaylight.netconf.api.NetconfTerminationReason;
+import org.opendaylight.netconf.api.xml.XmlElement;
import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
+import org.opendaylight.netconf.api.xml.XmlUtil;
import org.opendaylight.netconf.client.NetconfClientDispatcher;
import org.opendaylight.netconf.client.NetconfClientSession;
import org.opendaylight.netconf.client.NetconfClientSessionListener;
private final int concurentRpcMsgs;
private final Queue<Request> requests = new ArrayDeque<>();
- private NetconfClientSession session;
+ private NetconfClientSession currentSession;
private Future<?> initFuture;
private final SettableFuture<NetconfDeviceCapabilities> firstConnectionFuture;
final RemoteDeviceId id,
final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> remoteDevice,
final int rpcMessageLimit) {
- this(id, remoteDevice, Optional.<UserPreferences>absent(), rpcMessageLimit);
+ this(id, remoteDevice, Optional.absent(), rpcMessageLimit);
}
private NetconfDeviceCommunicator(
sessionLock.lock();
try {
LOG.debug("{}: Session established", id);
- this.session = session;
+ currentSession = session;
NetconfSessionPreferences netconfSessionPreferences =
NetconfSessionPreferences.fromNetconfSession(session);
public void disconnect() {
// If session is already in closing, no need to close it again
- if (session != null && isSessionClosing.compareAndSet(false, true)) {
- session.close();
+ if (currentSession != null && isSessionClosing.compareAndSet(false, true)) {
+ currentSession.close();
}
}
final List<UncancellableFuture<RpcResult<NetconfMessage>>> futuresToCancel = Lists.newArrayList();
sessionLock.lock();
try {
- if (session != null) {
- session = null;
+ if (currentSession != null) {
+ currentSession = null;
/*
* Walk all requests, check if they have been executing
* or cancelled and remove them from the queue.
try {
request = requests.peek();
if (request != null && request.future.isUncancellable()) {
- requests.poll();
+ request = requests.poll();
// we have just removed one request from the queue
// we can also release one permit
if (semaphore != null) {
if (request != null) {
+ if (FailedNetconfMessage.class.isInstance(message)) {
+ request.future.set(NetconfMessageTransformUtil.toRpcResult((FailedNetconfMessage) message));
+ return;
+ }
+
LOG.debug("{}: Message received {}", id, message);
if (LOG.isTraceEnabled()) {
@Override
public ListenableFuture<RpcResult<NetconfMessage>> sendRequest(final NetconfMessage message, final QName rpc) {
sessionLock.lock();
-
- if (semaphore != null && !semaphore.tryAcquire()) {
- LOG.warn("Limit of concurrent rpc messages was reached (limit :"
- + concurentRpcMsgs + "). Rpc reply message is needed. Discarding request of Netconf device with id"
- + id.getName());
- sessionLock.unlock();
- return Futures.immediateFailedFuture(new NetconfDocumentedException(
- "Limit of rpc messages was reached (Limit :" + concurentRpcMsgs
- + ") waiting for emptying the queue of Netconf device with id" + id.getName()));
- }
-
try {
+ if (semaphore != null && !semaphore.tryAcquire()) {
+ LOG.warn("Limit of concurrent rpc messages was reached (limit :" + concurentRpcMsgs
+ + "). Rpc reply message is needed. Discarding request of Netconf device with id" + id.getName());
+ return Futures.immediateFailedFuture(new NetconfDocumentedException(
+ "Limit of rpc messages was reached (Limit :" + concurentRpcMsgs
+ + ") waiting for emptying the queue of Netconf device with id" + id.getName()));
+ }
+
return sendRequestWithLock(message, rpc);
} finally {
sessionLock.unlock();
LOG.trace("{}: Sending message {}", id, msgToS(message));
}
- if (session == null) {
+ if (currentSession == null) {
LOG.warn("{}: Session is disconnected, failing RPC request {}",
id, message);
return Futures.immediateFuture(createSessionDownRpcResult());
final Request req = new Request(new UncancellableFuture<>(true), message);
requests.add(req);
- session.sendMessage(req.request).addListener(future -> {
+ currentSession.sendMessage(req.request).addListener(future -> {
if (!future.isSuccess()) {
// We expect that a session down will occur at this point
LOG.debug("{}: Failed to send request {}", id,
}
private static boolean isNotification(final NetconfMessage message) {
+ if (message.getDocument() == null) {
+ // We have no message, which mean we have a FailedNetconfMessage
+ return false;
+ }
final XmlElement xmle = XmlElement.fromDomDocument(message.getDocument());
return XmlNetconfConstants.NOTIFICATION_ELEMENT_NAME.equals(xmle.getName()) ;
}