Move NormalizedNode builders
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractNodeContainerModificationStrategy.java
index 342745b735999cb1266d5b9b07b22f7601d89d59..2accd980be0c37f4213497f8db10ecc841a3ec05 100644 (file)
@@ -18,19 +18,20 @@ import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 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.DistinctNodeContainer;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
+import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeContainerBuilder;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModifiedNodeDoesNotExistException;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.MutableTreeNode;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
+import org.opendaylight.yangtools.yang.data.spi.tree.MutableTreeNode;
+import org.opendaylight.yangtools.yang.data.spi.tree.TreeNode;
+import org.opendaylight.yangtools.yang.data.spi.tree.TreeNodeFactory;
+import org.opendaylight.yangtools.yang.data.spi.tree.Version;
 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
 
 abstract class AbstractNodeContainerModificationStrategy<T extends WithStatus>
@@ -49,8 +50,8 @@ abstract class AbstractNodeContainerModificationStrategy<T extends WithStatus>
             return entryStrategy.getSchema();
         }
 
-        final Optional<ModificationApplyOperation> entryStrategy() {
-            return Optional.of(entryStrategy);
+        final @NonNull ModificationApplyOperation entryStrategy() {
+            return entryStrategy;
         }
 
         @Override
@@ -101,25 +102,24 @@ abstract class AbstractNodeContainerModificationStrategy<T extends WithStatus>
     }
 
     @Override
-    final void verifyValue(final NormalizedNode<?, ?> writtenValue) {
+    final void verifyValue(final NormalizedNode writtenValue) {
         final Class<?> nodeClass = support.requiredClass;
         checkArgument(nodeClass.isInstance(writtenValue), "Node %s is not of type %s", writtenValue, nodeClass);
         checkArgument(writtenValue instanceof NormalizedNodeContainer);
     }
 
     @Override
-    final void verifyValueChildren(final NormalizedNode<?, ?> writtenValue) {
+    final void verifyValueChildren(final NormalizedNode writtenValue) {
         if (verifyChildrenStructure) {
-            final NormalizedNodeContainer<?, ?, ?> container = (NormalizedNodeContainer<?, ?, ?>) writtenValue;
-            for (final NormalizedNode<?, ?> child : container.getValue()) {
-                final Optional<ModificationApplyOperation> childOp = getChild(child.getIdentifier());
-                if (childOp.isPresent()) {
-                    childOp.get().fullVerifyStructure(child);
-                } else {
+            final DistinctNodeContainer<?, ?> container = (DistinctNodeContainer<?, ?>) writtenValue;
+            for (final NormalizedNode child : container.body()) {
+                final ModificationApplyOperation childOp = childByArg(child.getIdentifier());
+                if (childOp == null) {
                     throw new SchemaValidationFailedException(String.format(
-                            "Node %s is not a valid child of %s according to the schema.",
-                            child.getIdentifier(), container.getIdentifier()));
+                        "Node %s is not a valid child of %s according to the schema.",
+                        child.getIdentifier(), container.getIdentifier()));
                 }
+                childOp.fullVerifyStructure(child);
             }
 
             optionalVerifyValueChildren(writtenValue);
@@ -133,7 +133,7 @@ abstract class AbstractNodeContainerModificationStrategy<T extends WithStatus>
      *
      * @param writtenValue Effective written value
      */
-    void optionalVerifyValueChildren(final NormalizedNode<?, ?> writtenValue) {
+    void optionalVerifyValueChildren(final NormalizedNode writtenValue) {
         // Defaults to no-op
     }
 
@@ -143,27 +143,27 @@ abstract class AbstractNodeContainerModificationStrategy<T extends WithStatus>
      *
      * @param writtenValue Effective written value
      */
-    void mandatoryVerifyValueChildren(final NormalizedNode<?, ?> writtenValue) {
+    void mandatoryVerifyValueChildren(final NormalizedNode writtenValue) {
         // Defaults to no-op
     }
 
     @Override
-    protected final void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
-        final NormalizedNodeContainer<?, ?, ?> container = (NormalizedNodeContainer<?, ?, ?>) value;
-        for (final NormalizedNode<?, ?> child : container.getValue()) {
-            final Optional<ModificationApplyOperation> childOp = getChild(child.getIdentifier());
-            if (!childOp.isPresent()) {
+    protected final void recursivelyVerifyStructure(final NormalizedNode value) {
+        final NormalizedNodeContainer<?> container = (NormalizedNodeContainer<?>) value;
+        for (final NormalizedNode child : container.body()) {
+            final ModificationApplyOperation childOp = childByArg(child.getIdentifier());
+            if (childOp == null) {
                 throw new SchemaValidationFailedException(
                     String.format("Node %s is not a valid child of %s according to the schema.",
                         child.getIdentifier(), container.getIdentifier()));
             }
 
-            childOp.get().recursivelyVerifyStructure(child);
+            childOp.recursivelyVerifyStructure(child);
         }
     }
 
     @Override
-    protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode<?, ?> newValue,
+    protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue,
             final Optional<? extends TreeNode> currentMeta, final Version version) {
         final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version);
 
@@ -172,20 +172,18 @@ abstract class AbstractNodeContainerModificationStrategy<T extends WithStatus>
         }
 
         /*
-         * This is where things get interesting. The user has performed a write and
-         * then she applied some more modifications to it. So we need to make sense
-         * of that an apply the operations on top of the written value. We could have
-         * done it during the write, but this operation is potentially expensive, so
-         * we have left it out of the fast path.
+         * This is where things get interesting. The user has performed a write and then she applied some more
+         * modifications to it. So we need to make sense of that and apply the operations on top of the written value.
+         *
+         * We could have done it during the write, but this operation is potentially expensive, so we have left it out
+         * of the fast path.
          *
-         * As it turns out, once we materialize the written data, we can share the
-         * code path with the subtree change. So let's create an unsealed TreeNode
-         * and run the common parts on it -- which end with the node being sealed.
+         * As it turns out, once we materialize the written data, we can share the code path with the subtree change. So
+         * let's create an unsealed TreeNode and run the common parts on it -- which end with the node being sealed.
          *
-         * FIXME: this code needs to be moved out from the prepare() path and into
-         *        the read() and seal() paths. Merging of writes needs to be charged
-         *        to the code which originated this, not to the code which is
-         *        attempting to make it visible.
+         * FIXME: this code needs to be moved out from the prepare() path and into the read() and seal() paths. Merging
+         *        of writes needs to be charged to the code which originated this, not to the code which is attempting
+         *        to make it visible.
          */
         final MutableTreeNode mutable = newValueMeta.mutable();
         mutable.setSubtreeVersion(version);
@@ -216,12 +214,12 @@ abstract class AbstractNodeContainerModificationStrategy<T extends WithStatus>
 
         for (final ModifiedNode mod : modifications) {
             final PathArgument id = mod.getIdentifier();
-            final Optional<? extends TreeNode> cm = meta.getChild(id);
+            final Optional<? extends TreeNode> cm = meta.findChildByArg(id);
 
             final Optional<? extends TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
             if (result.isPresent()) {
                 final TreeNode tn = result.get();
-                meta.addChild(tn);
+                meta.putChild(tn);
                 data.addChild(tn.getData());
             } else {
                 meta.removeChild(id);
@@ -240,10 +238,10 @@ abstract class AbstractNodeContainerModificationStrategy<T extends WithStatus>
          * we do that, ModifiedNode children will look like this node were a TOUCH and we will let applyTouch() do the
          * heavy lifting of applying the children recursively (either through here or through applyWrite().
          */
-        final NormalizedNode<?, ?> value = modification.getWrittenValue();
+        final NormalizedNode value = modification.getWrittenValue();
 
-        Verify.verify(value instanceof NormalizedNodeContainer, "Attempted to merge non-container %s", value);
-        for (final NormalizedNode<?, ?> c : ((NormalizedNodeContainer<?, ?, ?>) value).getValue()) {
+        Verify.verify(value instanceof DistinctNodeContainer, "Attempted to merge non-container %s", value);
+        for (final NormalizedNode c : ((DistinctNodeContainer<?, ?>) value).body()) {
             final PathArgument id = c.getIdentifier();
             modification.modifyChild(id, resolveChildOperation(id), version);
         }
@@ -251,8 +249,8 @@ abstract class AbstractNodeContainerModificationStrategy<T extends WithStatus>
     }
 
     private void mergeChildrenIntoModification(final ModifiedNode modification,
-            final Collection<? extends NormalizedNode<?, ?>> children, final Version version) {
-        for (final NormalizedNode<?, ?> c : children) {
+            final Collection<? extends NormalizedNode> children, final Version version) {
+        for (final NormalizedNode c : children) {
             final ModificationApplyOperation childOp = resolveChildOperation(c.getIdentifier());
             final ModifiedNode childNode = modification.modifyChild(c.getIdentifier(), childOp, version);
             childOp.mergeIntoModifiedNode(childNode, c, version);
@@ -260,11 +258,9 @@ abstract class AbstractNodeContainerModificationStrategy<T extends WithStatus>
     }
 
     @Override
-    final void mergeIntoModifiedNode(final ModifiedNode modification, final NormalizedNode<?, ?> value,
+    final void mergeIntoModifiedNode(final ModifiedNode modification, final NormalizedNode value,
             final Version version) {
-        final Collection<? extends NormalizedNode<?, ?>> children =
-                ((NormalizedNodeContainer<?, ?, ?>)value).getValue();
-
+        final Collection<? extends NormalizedNode> children = ((DistinctNodeContainer<?, ?>)value).body();
         switch (modification.getOperation()) {
             case NONE:
                 // Fresh node, just record a MERGE with a value
@@ -274,12 +270,10 @@ abstract class AbstractNodeContainerModificationStrategy<T extends WithStatus>
             case TOUCH:
 
                 mergeChildrenIntoModification(modification, children, version);
-                // We record empty merge value, since real children merges
-                // are already expanded. This is needed to satisfy non-null for merge
-                // original merge value can not be used since it mean different
-                // order of operation - parent changes are always resolved before
-                // children ones, and having node in TOUCH means children was modified
-                // before.
+                // We record empty merge value, since real children merges are already expanded. This is needed to
+                // satisfy non-null for merge original merge value can not be used since it mean different order of
+                // operation - parent changes are always resolved before children ones, and having node in TOUCH means
+                // children was modified before.
                 modification.updateValue(LogicalOperation.MERGE, support.createEmptyValue(value));
                 return;
             case MERGE:
@@ -320,9 +314,11 @@ abstract class AbstractNodeContainerModificationStrategy<T extends WithStatus>
     @Override
     protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
         /*
-         * The user may have issued an empty merge operation. In this case we do not perform
-         * a data tree mutation, do not pass GO, and do not collect useless garbage. It
-         * also means the ModificationType is UNMODIFIED.
+         * The user may have issued an empty merge operation. In this case we:
+         * - do not perform a data tree mutation
+         * - do not pass GO, and
+         * - do not collect useless garbage.
+         * It also means the ModificationType is UNMODIFIED.
          */
         final Collection<ModifiedNode> children = modification.getChildren();
         if (!children.isEmpty()) {
@@ -333,12 +329,12 @@ abstract class AbstractNodeContainerModificationStrategy<T extends WithStatus>
             final TreeNode ret = mutateChildren(newMeta, dataBuilder, version, children);
 
             /*
-             * It is possible that the only modifications under this node were empty merges,
-             * which were turned into UNMODIFIED. If that is the case, we can turn this operation
-             * into UNMODIFIED, too, potentially cascading it up to root. This has the benefit
-             * of speeding up any users, who can skip processing child nodes.
+             * It is possible that the only modifications under this node were empty merges, which were turned into
+             * UNMODIFIED. If that is the case, we can turn this operation into UNMODIFIED, too, potentially cascading
+             * it up to root. This has the benefit of speeding up any users, who can skip processing child nodes.
              *
              * In order to do that, though, we have to check all child operations are UNMODIFIED.
+             *
              * Let's do precisely that, stopping as soon we find a different result.
              */
             for (final ModifiedNode child : children) {
@@ -389,7 +385,7 @@ abstract class AbstractNodeContainerModificationStrategy<T extends WithStatus>
         return null;
     }
 
-    static final TreeNode defaultTreeNode(final NormalizedNode<?, ?> emptyNode) {
+    static final TreeNode defaultTreeNode(final NormalizedNode emptyNode) {
         return TreeNodeFactory.createTreeNode(emptyNode, FAKE_VERSION);
     }
 
@@ -412,7 +408,7 @@ abstract class AbstractNodeContainerModificationStrategy<T extends WithStatus>
             final TreeNode current, final Version version) throws DataValidationFailedException {
         for (final NodeModification childMod : modification.getChildren()) {
             final PathArgument childId = childMod.getIdentifier();
-            final Optional<? extends TreeNode> childMeta = current.getChild(childId);
+            final Optional<? extends TreeNode> childMeta = current.findChildByArg(childId);
 
             path.push(childId);
             try {