BUG-509: move snapshotCache get out of try block
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / data / InMemoryDataTreeModification.java
index df3ef8b7e128b2be29eb5473bd86d056cba218d2..7d0c81e39de8efe564c33925280c9dbb9c71ff22 100644 (file)
@@ -88,30 +88,32 @@ final class InMemoryDataTreeModification implements DataTreeModification {
 
     @Override
     public Optional<NormalizedNode<?, ?>> readNode(final InstanceIdentifier path) {
-        Entry<InstanceIdentifier, NodeModification> modification = TreeNodeUtils.findClosestsOrFirstMatch(rootNode, path, NodeModification.IS_TERMINAL_PREDICATE);
-
-        Optional<StoreMetadataNode> result = resolveSnapshot(modification);
+        /*
+         * Walk the tree from the top, looking for the first node between root and
+         * the requested path which has been modified. If no such node exists,
+         * we use the node itself.
+         */
+        final Entry<InstanceIdentifier, NodeModification> entry = TreeNodeUtils.findClosestsOrFirstMatch(rootNode, path, NodeModification.IS_TERMINAL_PREDICATE);
+        final InstanceIdentifier key = entry.getKey();
+        final NodeModification mod = entry.getValue();
+
+        final Optional<StoreMetadataNode> result = resolveSnapshot(key, mod);
         if (result.isPresent()) {
             NormalizedNode<?, ?> data = result.get().getData();
-            return NormalizedNodeUtils.findNode(modification.getKey(), data, path);
+            return NormalizedNodeUtils.findNode(key, data, path);
+        } else {
+            return Optional.absent();
         }
-        return Optional.absent();
-    }
-
-    private Optional<StoreMetadataNode> resolveSnapshot(
-            final Entry<InstanceIdentifier, NodeModification> keyModification) {
-        InstanceIdentifier path = keyModification.getKey();
-        NodeModification modification = keyModification.getValue();
-        return resolveSnapshot(path, modification);
     }
 
     private Optional<StoreMetadataNode> resolveSnapshot(final InstanceIdentifier path,
             final NodeModification modification) {
+        final Optional<Optional<StoreMetadataNode>> potentialSnapshot = modification.getSnapshotCache();
+        if(potentialSnapshot.isPresent()) {
+            return potentialSnapshot.get();
+        }
+
         try {
-            Optional<Optional<StoreMetadataNode>> potentialSnapshot = modification.getSnapshotCache();
-            if(potentialSnapshot.isPresent()) {
-                return potentialSnapshot.get();
-            }
             return resolveModificationStrategy(path).apply(modification, modification.getOriginal(),
                     StoreUtils.increase(snapshot.getRootNode().getSubtreeVersion()));
         } catch (Exception e) {