Merge "Close read-only transactions"
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / RemoteOperationTxProcessorImpl.java
index da95e9f02afada156b16b34754d39757d31fe500..f19d0d535b98bf5afffe37bdaaa01979024afdbc 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.netconf.topology.singleton.impl;
 
 import akka.actor.ActorRef;
+import akka.actor.Status;
 import com.google.common.base.Optional;
 import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.FutureCallback;
@@ -23,8 +24,8 @@ import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.netconf.topology.singleton.api.RemoteOperationTxProcessor;
 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
-import org.opendaylight.netconf.topology.singleton.messages.SubmitFailedReply;
 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
+import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitFailedReply;
 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitReply;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -40,12 +41,28 @@ public class RemoteOperationTxProcessorImpl implements RemoteOperationTxProcesso
     private DOMDataWriteTransaction writeTx;
     private DOMDataReadOnlyTransaction readTx;
 
+    private ActorRef currentUser = null;
+
     public RemoteOperationTxProcessorImpl(final DOMDataBroker dataBroker, final RemoteDeviceId id) {
         this.dataBroker = dataBroker;
         this.id = id;
         this.readTx = dataBroker.newReadOnlyTransaction();
     }
 
+    @Override
+    public void doOpenTransaction(ActorRef recipient, ActorRef sender) {
+        if (currentUser != null) {
+            LOG.error("{}: Opening a new transaction for {} failed.", id, recipient);
+            recipient.tell(new Status.Failure(
+                    new IllegalStateException("Transaction is already opened for another user")), recipient);
+            return;
+        }
+
+        LOG.debug("{}: Opening a new transaction for {}", id, recipient);
+        currentUser = recipient;
+        recipient.tell(new Status.Success(null), sender);
+    }
+
     @Override
     public void doDelete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
         if (writeTx == null) {
@@ -56,6 +73,7 @@ public class RemoteOperationTxProcessorImpl implements RemoteOperationTxProcesso
 
     @Override
     public void doSubmit(final ActorRef recipient, final ActorRef sender) {
+        currentUser = null;
         if (writeTx != null) {
             CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
             Futures.addCallback(submitFuture, new FutureCallback<Void>() {
@@ -77,11 +95,13 @@ public class RemoteOperationTxProcessorImpl implements RemoteOperationTxProcesso
 
     @Override
     public void doCancel(final ActorRef recipient, final ActorRef sender) {
+        currentUser = null;
         boolean cancel = false;
         if (writeTx != null) {
             cancel = writeTx.cancel();
         }
         recipient.tell(cancel, sender);
+
     }
 
     @Override
@@ -148,6 +168,7 @@ public class RemoteOperationTxProcessorImpl implements RemoteOperationTxProcesso
 
     @Override
     public void close() throws Exception {
+        currentUser = null;
         if (readTx != null) {
             readTx.close();
         }