Merge "Fixed for bug : 1171 - issue while creating subnet"
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / DOMForwardedWriteTransaction.java
index eeb345e9ce663e7340300f779ab789805be86a7f..5d4ad4d803ac90d75c3769b76ef5133e6283cf17 100644 (file)
@@ -13,16 +13,19 @@ import javax.annotation.concurrent.GuardedBy;
 
 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.controller.md.sal.common.impl.service.AbstractDataTransaction;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
 import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.ListenableFuture;
 
 /**
@@ -33,13 +36,13 @@ import com.google.common.util.concurrent.ListenableFuture;
  * {@link LogicalDatastoreType} type parameter in:
  *
  * <ul>
- * <li>{@link #put(LogicalDatastoreType, InstanceIdentifier, NormalizedNode)}
- * <li>{@link #delete(LogicalDatastoreType, InstanceIdentifier)}
- * <li>{@link #merge(LogicalDatastoreType, InstanceIdentifier, NormalizedNode)}
+ * <li>{@link #put(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
+ * <li>{@link #delete(LogicalDatastoreType, YangInstanceIdentifier)}
+ * <li>{@link #merge(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
  * </ul>
  * <p>
  * {@link #commit()} will result in invocation of
- * {@link DOMDataCommitImplementation#commit(org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction, Iterable)}
+ * {@link DOMDataCommitImplementation#submit(org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction, Iterable)}
  * invocation with all {@link org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort} for underlying
  * transactions.
  *
@@ -50,13 +53,31 @@ import com.google.common.util.concurrent.ListenableFuture;
 class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction> extends
         AbstractDOMForwardedCompositeTransaction<LogicalDatastoreType, T> implements DOMDataWriteTransaction {
 
+    /**
+     *  Implementation of real commit.
+     *
+     *  Transaction can not be commited if commitImpl is null,
+     *  so this seting this property to null is also used to
+     *  prevent write to
+     *  already commited / canceled transaction {@link #checkNotCanceled()
+     *
+     *
+     */
     @GuardedBy("this")
-    private DOMDataCommitImplementation commitImpl;
-
-    @GuardedBy("this")
-    private boolean canceled;
+    private volatile DOMDataCommitImplementation commitImpl;
+
+    /**
+     *
+     * Future task of transaction commit.
+     *
+     * This value is initially null, and is once updated if transaction
+     * is commited {@link #commit()}.
+     * If this future exists, transaction MUST not be commited again
+     * and all modifications should fail. See {@link #checkNotCommited()}.
+     *
+     */
     @GuardedBy("this")
-    private ListenableFuture<RpcResult<TransactionStatus>> commitFuture;
+    private volatile CheckedFuture<Void, TransactionCommitFailedException> commitFuture;
 
     protected DOMForwardedWriteTransaction(final Object identifier,
             final ImmutableMap<LogicalDatastoreType, T> backingTxs, final DOMDataCommitImplementation commitImpl) {
@@ -65,38 +86,47 @@ class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction> extends
     }
 
     @Override
-    public void put(final LogicalDatastoreType store, final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
+    public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
         checkNotReady();
         getSubtransaction(store).write(path, data);
     }
 
     @Override
-    public void delete(final LogicalDatastoreType store, final InstanceIdentifier path) {
+    public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
         checkNotReady();
         getSubtransaction(store).delete(path);
     }
 
     @Override
-    public void merge(final LogicalDatastoreType store, final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
+    public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
         checkNotReady();
         getSubtransaction(store).merge(path, data);
     }
 
     @Override
-    public synchronized void cancel() {
-        checkState(!canceled, "Transaction was canceled.");
-        if (commitFuture != null) {
-            // FIXME: Implement cancelation of commit future
-            // when Broker impl will support cancelation.
-            throw new UnsupportedOperationException("Not implemented yet.");
+    public synchronized boolean cancel() {
+        // Transaction is already canceled, we are safe to return true
+        final boolean cancelationResult;
+        if (commitImpl == null && commitFuture != null) {
+            // Transaction is submitted, we try to cancel future.
+            cancelationResult = commitFuture.cancel(false);
+        } else if(commitImpl == null) {
+            return true;
+        } else {
+            cancelationResult = true;
+            commitImpl = null;
         }
-        canceled = true;
-        commitImpl = null;
+        return cancelationResult;
 
     }
 
     @Override
     public synchronized ListenableFuture<RpcResult<TransactionStatus>> commit() {
+        return AbstractDataTransaction.convertToLegacyCommitFuture(submit());
+    }
+
+    @Override
+    public CheckedFuture<Void,TransactionCommitFailedException> submit() {
         checkNotReady();
 
         ImmutableList.Builder<DOMStoreThreePhaseCommitCohort> cohortsBuilder = ImmutableList.builder();
@@ -104,21 +134,26 @@ class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction> extends
             cohortsBuilder.add(subTx.ready());
         }
         ImmutableList<DOMStoreThreePhaseCommitCohort> cohorts = cohortsBuilder.build();
-        commitFuture = commitImpl.commit(this, cohorts);
+        commitFuture = commitImpl.submit(this, cohorts);
+
+        /*
+         *We remove reference to Commit Implementation in order
+         *to prevent memory leak
+         */
+        commitImpl = null;
         return commitFuture;
     }
 
     private void checkNotReady() {
-        checkNotCanceled();
         checkNotCommited();
+        checkNotCanceled();
     }
 
     private void checkNotCanceled() {
-        Preconditions.checkState(!canceled, "Transaction was canceled.");
+        Preconditions.checkState(commitImpl != null, "Transaction was canceled.");
     }
 
     private void checkNotCommited() {
-        checkState(commitFuture == null, "Transaction was already commited.");
+        checkState(commitFuture == null, "Transaction was already submited.");
     }
-
 }
\ No newline at end of file