Bug 3195: Cleanup on error paths and error handling
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LocalTransactionContext.java
index 01a778f8e43157730938490de9cd4a16ec1c8c21..276523e680a6389210edf290fff1a32fff252f23 100644 (file)
@@ -9,11 +9,14 @@ 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.identifiers.TransactionIdentifier;
-import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
+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;
@@ -24,39 +27,69 @@ import scala.concurrent.Future;
  *
  * @author Thomas Pantelis
  */
-final class LocalTransactionContext extends AbstractTransactionContext {
-    private final DOMStoreReadWriteTransaction delegate;
+abstract class LocalTransactionContext extends AbstractTransactionContext {
+    private final DOMStoreTransaction txDelegate;
+    private final LocalTransactionReadySupport readySupport;
+    private Exception operationError;
 
-    LocalTransactionContext(TransactionIdentifier identifier, DOMStoreReadWriteTransaction delegate) {
+    LocalTransactionContext(DOMStoreTransaction txDelegate, TransactionIdentifier identifier,
+            LocalTransactionReadySupport readySupport) {
         super(identifier);
-        this.delegate = delegate;
+        this.txDelegate = Preconditions.checkNotNull(txDelegate);
+        this.readySupport = readySupport;
     }
 
+    protected abstract DOMStoreWriteTransaction getWriteDelegate();
+
+    protected abstract DOMStoreReadTransaction getReadDelegate();
+
     @Override
     public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
-        delegate.write(path, data);
+        incrementModificationCount();
+        if(operationError == null) {
+            try {
+                getWriteDelegate().write(path, data);
+            } catch (Exception e) {
+                operationError = e;
+            }
+        }
+
     }
 
     @Override
     public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
-        delegate.merge(path, data);
+        incrementModificationCount();
+        if(operationError == null) {
+            try {
+                getWriteDelegate().merge(path, data);
+            } catch (Exception e) {
+                operationError = e;
+            }
+        }
     }
 
     @Override
     public void deleteData(YangInstanceIdentifier path) {
-        delegate.delete(path);
+        incrementModificationCount();
+        if(operationError == null) {
+            try {
+                getWriteDelegate().delete(path);
+            } catch (Exception e) {
+                operationError = e;
+            }
+        }
     }
 
     @Override
     public void readData(YangInstanceIdentifier path, final SettableFuture<Optional<NormalizedNode<?, ?>>> proxyFuture) {
-        Futures.addCallback(delegate.read(path), new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
+        Futures.addCallback(getReadDelegate().read(path), new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
             @Override
-            public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
+            public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
                 proxyFuture.set(result);
             }
 
             @Override
-            public void onFailure(Throwable t) {
+            public void onFailure(final Throwable t) {
                 proxyFuture.setException(t);
             }
         });
@@ -64,31 +97,36 @@ final class LocalTransactionContext extends AbstractTransactionContext {
 
     @Override
     public void dataExists(YangInstanceIdentifier path, final SettableFuture<Boolean> proxyFuture) {
-        Futures.addCallback(delegate.exists(path), new FutureCallback<Boolean>() {
+        Futures.addCallback(getReadDelegate().exists(path), new FutureCallback<Boolean>() {
             @Override
-            public void onSuccess(Boolean result) {
+            public void onSuccess(final Boolean result) {
                 proxyFuture.set(result);
             }
 
             @Override
-            public void onFailure(Throwable t) {
+            public void onFailure(final Throwable t) {
                 proxyFuture.setException(t);
             }
         });
     }
 
     private LocalThreePhaseCommitCohort ready() {
-        return (LocalThreePhaseCommitCohort) delegate.ready();
+        logModificationCount();
+        LocalThreePhaseCommitCohort cohort = readySupport.onTransactionReady(getWriteDelegate());
+        cohort.setOperationError(operationError);
+        return cohort;
     }
 
     @Override
     public Future<ActorSelection> readyTransaction() {
-        return ready().initiateCoordinatedCommit();
+        final LocalThreePhaseCommitCohort cohort = ready();
+        return cohort.initiateCoordinatedCommit();
     }
 
     @Override
     public Future<Object> directCommit() {
-        return ready().initiateDirectCommit();
+        final LocalThreePhaseCommitCohort cohort = ready();
+        return cohort.initiateDirectCommit();
     }
 
     @Override
@@ -98,6 +136,6 @@ final class LocalTransactionContext extends AbstractTransactionContext {
 
     @Override
     public void closeTransaction() {
-        delegate.close();
+        txDelegate.close();
     }
 }