Deprecate old MD-SAL APIs for removal
[controller.git] / opendaylight / md-sal / sal-dom-spi / src / main / java / org / opendaylight / controller / sal / core / spi / data / SnapshotBackedWriteTransaction.java
index dc600dd130213f6c4509c6fe4f8128975912b066..b95e3f1cf0b6364a338f33f311a4e44ad38e6d6e 100644 (file)
@@ -8,12 +8,14 @@
 package org.opendaylight.controller.sal.core.spi.data;
 
 import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.annotations.Beta;
 import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
 import com.google.common.base.Throwables;
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
@@ -27,16 +29,21 @@ import org.slf4j.LoggerFactory;
  * {@link org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction.TransactionReadyPrototype}.
  *
  * @param <T> Identifier type
+ * @deprecated Use {@link org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction} instead.
  */
+@Deprecated(forRemoval = true)
 @Beta
-public class SnapshotBackedWriteTransaction<T> extends AbstractDOMStoreTransaction<T> implements DOMStoreWriteTransaction {
+public class SnapshotBackedWriteTransaction<T> extends AbstractDOMStoreTransaction<T>
+        implements DOMStoreWriteTransaction {
     private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedWriteTransaction.class);
     @SuppressWarnings("rawtypes")
-    private static final AtomicReferenceFieldUpdater<SnapshotBackedWriteTransaction, TransactionReadyPrototype> READY_UPDATER =
-            AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedWriteTransaction.class, TransactionReadyPrototype.class, "readyImpl");
+    private static final AtomicReferenceFieldUpdater<SnapshotBackedWriteTransaction, TransactionReadyPrototype>
+        READY_UPDATER = AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedWriteTransaction.class,
+                TransactionReadyPrototype.class, "readyImpl");
     @SuppressWarnings("rawtypes")
-    private static final AtomicReferenceFieldUpdater<SnapshotBackedWriteTransaction, DataTreeModification> TREE_UPDATER =
-            AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedWriteTransaction.class, DataTreeModification.class, "mutableTree");
+    private static final AtomicReferenceFieldUpdater<SnapshotBackedWriteTransaction, DataTreeModification>
+        TREE_UPDATER = AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedWriteTransaction.class,
+                DataTreeModification.class, "mutableTree");
 
     // non-null when not ready
     private volatile TransactionReadyPrototype<T> readyImpl;
@@ -46,12 +53,13 @@ public class SnapshotBackedWriteTransaction<T> extends AbstractDOMStoreTransacti
     SnapshotBackedWriteTransaction(final T identifier, final boolean debug,
             final DataTreeSnapshot snapshot, final TransactionReadyPrototype<T> readyImpl) {
         super(identifier, debug);
-        this.readyImpl = Preconditions.checkNotNull(readyImpl, "readyImpl must not be null.");
+        this.readyImpl = requireNonNull(readyImpl, "readyImpl must not be null.");
         mutableTree = snapshot.newModification();
         LOG.debug("Write Tx: {} allocated with snapshot {}", identifier, snapshot);
     }
 
     @Override
+    @SuppressWarnings("checkstyle:IllegalCatch")
     public void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
         checkNotReady();
 
@@ -61,7 +69,7 @@ public class SnapshotBackedWriteTransaction<T> extends AbstractDOMStoreTransacti
         try {
             tree.write(path, data);
             // FIXME: Add checked exception
-        } catch (Exception e) {
+        } catch (RuntimeException e) {
             LOG.error("Tx: {}, failed to write {}:{} in {}", getIdentifier(), path, data, tree, e);
             // Rethrow original ones if they are subclasses of RuntimeException
             // or Error
@@ -72,6 +80,7 @@ public class SnapshotBackedWriteTransaction<T> extends AbstractDOMStoreTransacti
     }
 
     @Override
+    @SuppressWarnings("checkstyle:IllegalCatch")
     public void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
         checkNotReady();
 
@@ -81,7 +90,7 @@ public class SnapshotBackedWriteTransaction<T> extends AbstractDOMStoreTransacti
         try {
             tree.merge(path, data);
             // FIXME: Add checked exception
-        } catch (Exception e) {
+        } catch (RuntimeException e) {
             LOG.error("Tx: {}, failed to write {}:{} in {}", getIdentifier(), path, data, tree, e);
             // Rethrow original ones if they are subclasses of RuntimeException
             // or Error
@@ -92,6 +101,7 @@ public class SnapshotBackedWriteTransaction<T> extends AbstractDOMStoreTransacti
     }
 
     @Override
+    @SuppressWarnings("checkstyle:IllegalCatch")
     public void delete(final YangInstanceIdentifier path) {
         checkNotReady();
 
@@ -101,7 +111,7 @@ public class SnapshotBackedWriteTransaction<T> extends AbstractDOMStoreTransacti
         try {
             tree.delete(path);
             // FIXME: Add checked exception
-        } catch (Exception e) {
+        } catch (RuntimeException e) {
             LOG.error("Tx: {}, failed to delete {} in {}", getIdentifier(), path, tree, e);
             // Rethrow original ones if they are subclasses of RuntimeException
             // or Error
@@ -119,14 +129,16 @@ public class SnapshotBackedWriteTransaction<T> extends AbstractDOMStoreTransacti
      * @return null if the the transaction has been closed;
      */
     final Optional<NormalizedNode<?, ?>> readSnapshotNode(final YangInstanceIdentifier path) {
-        return readyImpl == null ? null : mutableTree.readNode(path);
+        return readyImpl == null ? null : Optional.fromJavaUtil(mutableTree.readNode(path));
     }
 
-    private final void checkNotReady() {
-        checkState(readyImpl != null, "Transaction %s is no longer open. No further modifications allowed.", getIdentifier());
+    private void checkNotReady() {
+        checkState(readyImpl != null, "Transaction %s is no longer open. No further modifications allowed.",
+                getIdentifier());
     }
 
     @Override
+    @SuppressWarnings("checkstyle:IllegalCatch")
     public DOMStoreThreePhaseCommitCohort ready() {
         @SuppressWarnings("unchecked")
         final TransactionReadyPrototype<T> wasReady = READY_UPDATER.getAndSet(this, null);
@@ -136,8 +148,13 @@ public class SnapshotBackedWriteTransaction<T> extends AbstractDOMStoreTransacti
 
         final DataTreeModification tree = mutableTree;
         TREE_UPDATER.lazySet(this, null);
-        tree.ready();
-        return wasReady.transactionReady(this, tree);
+        try {
+            tree.ready();
+            return wasReady.transactionReady(this, tree, null);
+        } catch (RuntimeException e) {
+            LOG.debug("Store transaction: {}: unexpected failure when readying", getIdentifier(), e);
+            return wasReady.transactionReady(this, tree, e);
+        }
     }
 
     @Override
@@ -159,12 +176,13 @@ public class SnapshotBackedWriteTransaction<T> extends AbstractDOMStoreTransacti
     }
 
     /**
-     * This class is intended to be implemented by Transaction factories
-     * responsible for allocation of {@link org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction} and
+     * This class is intended to be implemented by Transaction factories responsible for allocation of
+     * {@link org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction} and
      * providing underlying logic for applying implementation.
      *
      * @param <T> identifier type
      */
+    @Deprecated
     public abstract static class TransactionReadyPrototype<T> {
         /**
          * Called when a transaction is closed without being readied. This is not invoked for
@@ -172,19 +190,24 @@ public class SnapshotBackedWriteTransaction<T> extends AbstractDOMStoreTransacti
          *
          * @param tx Transaction which got aborted.
          */
-        protected abstract void transactionAborted(final SnapshotBackedWriteTransaction<T> tx);
+        protected abstract void transactionAborted(SnapshotBackedWriteTransaction<T> tx);
 
         /**
          * Returns a commit coordinator associated with supplied transactions.
          *
+         * <p>
          * This call must not fail.
          *
          * @param tx
          *            Transaction on which ready was invoked.
          * @param tree
          *            Modified data tree which has been constructed.
+         * @param readyError
+         *            Any error that has already happened when readying.
          * @return DOMStoreThreePhaseCommitCohort associated with transaction
          */
-        protected abstract DOMStoreThreePhaseCommitCohort transactionReady(SnapshotBackedWriteTransaction<T> tx, DataTreeModification tree);
+        protected abstract DOMStoreThreePhaseCommitCohort transactionReady(SnapshotBackedWriteTransaction<T> tx,
+                                                                           DataTreeModification tree,
+                                                                           @Nullable Exception readyError);
     }
 }