BUG-509: improve safety of MutableDataTree
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / MutableDataTree.java
index 9fa32a68e5741d350747487791649fd59be4ab63..14d82799d76ec3d0a58b7797ab9d718163072c6c 100644 (file)
@@ -7,10 +7,10 @@
  */
 package org.opendaylight.controller.md.sal.dom.store.impl;
 
-import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkState;
 
 import java.util.Map.Entry;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification;
 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
@@ -23,16 +23,18 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 
+/*
+ * FIXME: the thread safety of concurrent write/delete/read/seal operations
+ *        needs to be evaluated.
+ */
 class MutableDataTree {
-
-    private static final Logger log = LoggerFactory.getLogger(MutableDataTree.class);
-
-    final DataAndMetadataSnapshot snapshot;
-    final NodeModification rootModification;
-    final ModificationApplyOperation strategyTree;
-
-    private boolean sealed = false;
+    private static final Logger LOG = LoggerFactory.getLogger(MutableDataTree.class);
+    private final AtomicBoolean sealed = new AtomicBoolean();
+    private final ModificationApplyOperation strategyTree;
+    private final DataAndMetadataSnapshot snapshot;
+    private final NodeModification rootModification;
 
     private MutableDataTree(final DataAndMetadataSnapshot snapshot, final ModificationApplyOperation strategyTree) {
         this.snapshot = snapshot;
@@ -51,19 +53,14 @@ class MutableDataTree {
     }
 
     public Optional<NormalizedNode<?, ?>> read(final InstanceIdentifier path) {
-        Entry<InstanceIdentifier, NodeModification> modification = TreeNodeUtils.findClosest(rootModification, path);
-        return getModifiedVersion(path, modification);
-    }
+        Entry<InstanceIdentifier, NodeModification> modification = TreeNodeUtils.findClosestsOrFirstMatch(rootModification, path, NodeModification.IS_TERMINAL_PREDICATE);
 
-    private Optional<NormalizedNode<?, ?>> getModifiedVersion(final InstanceIdentifier path,
-            final Entry<InstanceIdentifier, NodeModification> modification) {
         Optional<StoreMetadataNode> result = resolveSnapshot(modification);
         if (result.isPresent()) {
             NormalizedNode<?, ?> data = result.get().getData();
             return NormalizedNodeUtils.findNode(modification.getKey(), data, path);
         }
         return Optional.absent();
-
     }
 
     private Optional<StoreMetadataNode> resolveSnapshot(
@@ -76,20 +73,21 @@ class MutableDataTree {
     private Optional<StoreMetadataNode> resolveSnapshot(final InstanceIdentifier path,
             final NodeModification modification) {
         try {
+            Optional<Optional<StoreMetadataNode>> potentialSnapshot = modification.getSnapshotCache();
+            if(potentialSnapshot.isPresent()) {
+                return potentialSnapshot.get();
+            }
             return resolveModificationStrategy(path).apply(modification, modification.getOriginal(),
                     StoreUtils.increase(snapshot.getMetadataTree().getSubtreeVersion()));
         } catch (Exception e) {
-            log.error("Could not create snapshot for {},", e);
+            LOG.error("Could not create snapshot for {}", path,e);
             throw e;
         }
     }
 
     private ModificationApplyOperation resolveModificationStrategy(final InstanceIdentifier path) {
-        log.trace("Resolving modification apply strategy for {}", path);
-        Optional<ModificationApplyOperation> strategy = TreeNodeUtils.findNode(strategyTree, path);
-        checkArgument(strategy.isPresent(),
-                "Provided path %s is not supported by data store. No schema available for it.", path);
-        return strategy.get();
+        LOG.trace("Resolving modification apply strategy for {}", path);
+        return TreeNodeUtils.findNodeChecked(strategyTree, path);
     }
 
     private OperationWithModification resolveModificationFor(final InstanceIdentifier path) {
@@ -107,15 +105,21 @@ class MutableDataTree {
     }
 
     public void seal() {
-        sealed = true;
+        final boolean success = sealed.compareAndSet(false, true);
+        Preconditions.checkState(success, "Attempted to seal an already-sealed Data Tree.");
         rootModification.seal();
     }
 
     private void checkSealed() {
-        checkState(!sealed, "Data Tree is sealed. No further modifications allowed.");
+        checkState(!sealed.get(), "Data Tree is sealed. No further modifications allowed.");
     }
 
     protected NodeModification getRootModification() {
         return rootModification;
     }
+
+    @Override
+    public String toString() {
+        return "MutableDataTree [modification=" + rootModification + "]";
+    }
 }