Lower the size of ModifiedNode children maps
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / InMemoryDataTreeModification.java
index f86c71249bc53b20f2b3a5586cc16b49302c58cb..a40b13a78c4aa55a7da55ae8ea9859bf121073ba 100644 (file)
@@ -13,6 +13,7 @@ import com.google.common.collect.Iterables;
 import java.util.Collection;
 import java.util.Map.Entry;
 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -22,6 +23,7 @@ import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification
 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNodes;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -65,14 +67,14 @@ final class InMemoryDataTreeModification implements DataTreeModification {
     @Override
     public void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
         checkSealed();
-
+        checkIdentifierReferencesData(path, data);
         resolveModificationFor(path).write(data);
     }
 
     @Override
     public void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
         checkSealed();
-
+        checkIdentifierReferencesData(path, data);
         resolveModificationFor(path).merge(data);
     }
 
@@ -96,7 +98,7 @@ final class InMemoryDataTreeModification implements DataTreeModification {
 
         final Optional<TreeNode> result = resolveSnapshot(key, mod);
         if (result.isPresent()) {
-            NormalizedNode<?, ?> data = result.get().getData();
+            final NormalizedNode<?, ?> data = result.get().getData();
             return NormalizedNodes.findNode(key, data, path);
         } else {
             return Optional.absent();
@@ -112,7 +114,7 @@ final class InMemoryDataTreeModification implements DataTreeModification {
         try {
             return resolveModificationStrategy(path).apply(modification, modification.getOriginal(),
                     version);
-        } catch (Exception e) {
+        } catch (final Exception e) {
             LOG.error("Could not create snapshot for {}:{}", path, modification, e);
             throw e;
         }
@@ -148,10 +150,10 @@ final class InMemoryDataTreeModification implements DataTreeModification {
         ModifiedNode modification = rootNode;
 
         int i = 1;
-        for (PathArgument pathArg : path.getPathArguments()) {
-            Optional<ModificationApplyOperation> potential = operation.getChild(pathArg);
+        for(final PathArgument pathArg : path.getPathArguments()) {
+            final Optional<ModificationApplyOperation> potential = operation.getChild(pathArg);
             if (!potential.isPresent()) {
-                throw new IllegalArgumentException(String.format("Child %s is not present in schema tree.",
+                throw new SchemaValidationFailedException(String.format("Child %s is not present in schema tree.",
                         Iterables.toString(Iterables.limit(path.getPathArguments(), i))));
             }
             operation = potential.get();
@@ -185,10 +187,11 @@ final class InMemoryDataTreeModification implements DataTreeModification {
          * We will use preallocated version, this means returned snapshot will
          * have same version each time this method is called.
          */
-        TreeNode originalSnapshotRoot = snapshot.getRootNode();
-        Optional<TreeNode> tempRoot = strategyTree.apply(rootNode, Optional.of(originalSnapshotRoot), version);
+        final TreeNode originalSnapshotRoot = snapshot.getRootNode();
+        final Optional<TreeNode> tempRoot = strategyTree.apply(rootNode, Optional.of(originalSnapshotRoot), version);
+        Preconditions.checkState(tempRoot.isPresent(), "Data tree root is not present, possibly removed by previous modification");
 
-        InMemoryDataTreeSnapshot tempTree = new InMemoryDataTreeSnapshot(snapshot.getSchemaContext(), tempRoot.get(), strategyTree);
+        final InMemoryDataTreeSnapshot tempTree = new InMemoryDataTreeSnapshot(snapshot.getSchemaContext(), tempRoot.get(), strategyTree);
         return tempTree.newModification();
     }
 
@@ -200,7 +203,7 @@ final class InMemoryDataTreeModification implements DataTreeModification {
         final Collection<ModifiedNode> children = node.getChildren();
         if (!children.isEmpty()) {
             cursor.enter(node.getIdentifier());
-            for (ModifiedNode child : children) {
+            for (final ModifiedNode child : children) {
                 applyNode(cursor, child);
             }
             cursor.exit();
@@ -236,11 +239,23 @@ final class InMemoryDataTreeModification implements DataTreeModification {
 
     @Override
     public void applyToCursor(final DataTreeModificationCursor cursor) {
-        for (ModifiedNode child : rootNode.getChildren()) {
+        for (final ModifiedNode child : rootNode.getChildren()) {
             applyNode(cursor, child);
         }
     }
 
+    private static void checkIdentifierReferencesData(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
+        if (!path.isEmpty()) {
+            final PathArgument lastArg = path.getLastPathArgument();
+            Preconditions.checkArgument(lastArg != null, "Instance identifier %s has invalid null path argument", path);
+            Preconditions.checkArgument(lastArg.equals(data.getIdentifier()),
+                    "Instance identifier references %s but data identifier is %s", lastArg, data.getIdentifier());
+        } else {
+            final QName type = data.getNodeType();
+            Preconditions.checkArgument(SchemaContext.NAME.equals(type), "Incorrect name %s of root node", type);
+        }
+    }
+
     @Override
     public void ready() {
         final boolean wasRunning = UPDATER.compareAndSet(this, 0, 1);