Bug 499: Improved data change listener tree management
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / MutableDataTree.java
index ddf65b6d275389d98ac86e6a5971685ea6e683e1..f252744876f0b8da325523fdcce62c8bad3a7552 100644 (file)
@@ -7,7 +7,6 @@
  */
 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;
@@ -32,7 +31,7 @@ class MutableDataTree {
     final NodeModification rootModification;
     final ModificationApplyOperation strategyTree;
 
-    private  boolean sealed = false;
+    private boolean sealed = false;
 
     private MutableDataTree(final DataAndMetadataSnapshot snapshot, final ModificationApplyOperation strategyTree) {
         this.snapshot = snapshot;
@@ -51,50 +50,52 @@ 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()) {
+        if (result.isPresent()) {
             NormalizedNode<?, ?> data = result.get().getData();
-            return NormalizedNodeUtils.findNode(modification.getKey(),data, path);
+            return NormalizedNodeUtils.findNode(modification.getKey(), data, path);
         }
         return Optional.absent();
 
     }
 
-    private Optional<StoreMetadataNode> resolveSnapshot(final Entry<InstanceIdentifier, NodeModification> keyModification) {
+    private Optional<StoreMetadataNode> resolveSnapshot(
+            final Entry<InstanceIdentifier, NodeModification> keyModification) {
         InstanceIdentifier path = keyModification.getKey();
         NodeModification modification = keyModification.getValue();
-        return resolveSnapshot(path,modification);
+        return resolveSnapshot(path, modification);
     }
 
-    private Optional<StoreMetadataNode> resolveSnapshot(final InstanceIdentifier path, final NodeModification modification) {
+    private Optional<StoreMetadataNode> resolveSnapshot(final InstanceIdentifier path,
+            final NodeModification modification) {
         try {
-            return resolveModificationStrategy(path).apply(modification,modification.getOriginal());
+            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 NodeModification resolveModificationFor(final InstanceIdentifier path) {
+    private OperationWithModification resolveModificationFor(final InstanceIdentifier path) {
         NodeModification modification = rootModification;
         // We ensure strategy is present.
-        resolveModificationStrategy(path);
+        ModificationApplyOperation operation = resolveModificationStrategy(path);
         for (PathArgument pathArg : path.getPath()) {
             modification = modification.modifyChild(pathArg);
         }
-        return modification;
+        return OperationWithModification.from(operation, modification);
     }
 
     public static MutableDataTree from(final DataAndMetadataSnapshot snapshot, final ModificationApplyOperation resolver) {
@@ -107,6 +108,17 @@ class MutableDataTree {
     }
 
     private void checkSealed() {
-        checkState(!sealed , "Data Tree is sealed. No further modifications allowed.");
+        checkState(!sealed, "Data Tree is sealed. No further modifications allowed.");
+    }
+
+    protected NodeModification getRootModification() {
+        return rootModification;
     }
+
+    @Override
+    public String toString() {
+        return "MutableDataTree [modification=" + rootModification + "]";
+    }
+
+
 }