BUG-5280: add frontend state lifecycle
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LocalTransactionContext.java
index 9b0accd455117f464d556fa8fc8c73f1990d0b23..76bafb75d3003a911f1b1207f55a919b0c08e2f0 100644 (file)
@@ -8,17 +8,16 @@
 package org.opendaylight.controller.cluster.datastore;
 
 import akka.actor.ActorSelection;
-import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.SettableFuture;
-import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
+import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
+import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
+import org.opendaylight.controller.cluster.datastore.modification.AbstractModification;
 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import scala.concurrent.Future;
 
 /**
@@ -29,10 +28,14 @@ import scala.concurrent.Future;
  */
 abstract class LocalTransactionContext extends AbstractTransactionContext {
     private final DOMStoreTransaction txDelegate;
+    private final LocalTransactionReadySupport readySupport;
+    private Exception operationError;
 
-    LocalTransactionContext(DOMStoreTransaction txDelegate, OperationLimiter limiter) {
-        super(limiter);
+    LocalTransactionContext(DOMStoreTransaction txDelegate, TransactionIdentifier identifier,
+            LocalTransactionReadySupport readySupport) {
+        super(identifier);
         this.txDelegate = Preconditions.checkNotNull(txDelegate);
+        this.readySupport = readySupport;
     }
 
     protected abstract DOMStoreWriteTransaction getWriteDelegate();
@@ -40,92 +43,52 @@ abstract class LocalTransactionContext extends AbstractTransactionContext {
     protected abstract DOMStoreReadTransaction getReadDelegate();
 
     @Override
-    public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    public void executeModification(AbstractModification modification) {
         incrementModificationCount();
-        getWriteDelegate().write(path, data);
-        releaseOperation();
-    }
-
-    @Override
-    public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
-        incrementModificationCount();
-        getWriteDelegate().merge(path, data);
-        releaseOperation();
-    }
-
-    @Override
-    public void deleteData(YangInstanceIdentifier path) {
-        incrementModificationCount();
-        getWriteDelegate().delete(path);
-        releaseOperation();
-    }
-
-    @Override
-    public void readData(YangInstanceIdentifier path, final SettableFuture<Optional<NormalizedNode<?, ?>>> proxyFuture) {
-        Futures.addCallback(getReadDelegate().read(path), new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
-            @Override
-            public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
-                proxyFuture.set(result);
-                releaseOperation();
-            }
-
-            @Override
-            public void onFailure(final Throwable t) {
-                proxyFuture.setException(t);
-                releaseOperation();
+        if (operationError == null) {
+            try {
+                modification.apply(getWriteDelegate());
+            } catch (Exception e) {
+                operationError = e;
             }
-        });
+        }
     }
 
     @Override
-    public void dataExists(YangInstanceIdentifier path, final SettableFuture<Boolean> proxyFuture) {
-        Futures.addCallback(getReadDelegate().exists(path), new FutureCallback<Boolean>() {
+    public <T> void executeRead(AbstractRead<T> readCmd, final SettableFuture<T> proxyFuture) {
+        Futures.addCallback(readCmd.apply(getReadDelegate()), new FutureCallback<T>() {
             @Override
-            public void onSuccess(final Boolean result) {
+            public void onSuccess(final T result) {
                 proxyFuture.set(result);
-                releaseOperation();
             }
 
             @Override
-            public void onFailure(final Throwable t) {
-                proxyFuture.setException(t);
-                releaseOperation();
+            public void onFailure(final Throwable failure) {
+                proxyFuture.setException(failure);
             }
         });
     }
 
     private LocalThreePhaseCommitCohort ready() {
         logModificationCount();
-        acquireOperation();
-        return (LocalThreePhaseCommitCohort) getWriteDelegate().ready();
-    }
-
-    @SuppressWarnings({ "unchecked", "rawtypes" })
-    private <T extends Future> T completeOperation(final ActorContext actorContext, final T operationFuture) {
-        operationFuture.onComplete(getLimiter(), actorContext.getClientDispatcher());
-        return operationFuture;
+        return readySupport.onTransactionReady(getWriteDelegate(), operationError);
     }
 
     @Override
     public Future<ActorSelection> readyTransaction() {
         final LocalThreePhaseCommitCohort cohort = ready();
-        return completeOperation(cohort.getActorContext(), cohort.initiateCoordinatedCommit());
+        return cohort.initiateCoordinatedCommit();
     }
 
     @Override
     public Future<Object> directCommit() {
         final LocalThreePhaseCommitCohort cohort = ready();
-        return completeOperation(cohort.getActorContext(), cohort.initiateDirectCommit());
-    }
-
-    @Override
-    public boolean supportsDirectCommit() {
-        return true;
+        return cohort.initiateDirectCommit();
     }
 
     @Override
     public void closeTransaction() {
         txDelegate.close();
-        releaseOperation();
     }
 }