Convert yang-data-impl to a JPMS module
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / UnkeyedListModificationStrategy.java
index 23140b86a68cb86b597dabf44ce985fdb78b2e58..9e40b18a084e179f97692f302d8734b6f7dd3515 100644 (file)
@@ -7,52 +7,68 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
-import com.google.common.base.Optional;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import java.util.Optional;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
 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.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 
-final class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
+final class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation<ListSchemaNode> {
+    private static final NormalizedNodeContainerSupport<NodeIdentifier, UnkeyedListEntryNode> ITEM_SUPPORT =
+            new NormalizedNodeContainerSupport<>(UnkeyedListEntryNode.class,
+                    ImmutableUnkeyedListEntryNodeBuilder::create, ImmutableUnkeyedListEntryNodeBuilder::create);
 
-    private final Optional<ModificationApplyOperation> entryStrategy;
+    private final DataNodeContainerModificationStrategy<ListSchemaNode> entryStrategy;
+    private final UnkeyedListNode emptyNode;
 
-    protected UnkeyedListModificationStrategy(final ListSchemaNode schema) {
-        entryStrategy = Optional.<ModificationApplyOperation> of(new DataNodeContainerModificationStrategy.UnkeyedListItemModificationStrategy(schema));
+    UnkeyedListModificationStrategy(final ListSchemaNode schema, final DataTreeConfiguration treeConfig) {
+        entryStrategy = new DataNodeContainerModificationStrategy<>(ITEM_SUPPORT, schema, treeConfig);
+        emptyNode = ImmutableNodes.listNode(schema.getQName());
     }
 
     @Override
-    boolean isOrdered() {
-        return true;
+    ListSchemaNode getSchema() {
+        return entryStrategy.getSchema();
     }
 
     @Override
-    protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
+    Optional<? extends TreeNode> apply(final ModifiedNode modification, final Optional<? extends TreeNode> storeMeta,
             final Version version) {
-        return applyWrite(modification, Optional.of(currentMeta), version);
+        return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, storeMeta,
+            version);
     }
 
     @Override
-    protected TreeNode applySubtreeChange(final ModifiedNode modification,
-            final TreeNode currentMeta, final Version version) {
+    protected ChildTrackingPolicy getChildPolicy() {
+        return ChildTrackingPolicy.ORDERED;
+    }
+
+    @Override
+    protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
+        throw new IllegalStateException(String.format("Merge of modification %s on unkeyed list should never be called",
+            modification));
+    }
+
+    @Override
+    protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
         throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
     }
 
     @Override
-    protected TreeNode applyWrite(final ModifiedNode modification,
-            final Optional<TreeNode> currentMeta, final Version version) {
-        final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
+    protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode<?, ?> newValue,
+            final Optional<? extends TreeNode> currentMeta, final Version version) {
         final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version);
-
         if (modification.getChildren().isEmpty()) {
             return newValueMeta;
         }
@@ -90,13 +106,13 @@ final class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
      */
     @SuppressWarnings({ "rawtypes", "unchecked" })
     private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
-        final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
+            final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
 
-        for (ModifiedNode mod : modifications) {
+        for (final ModifiedNode mod : modifications) {
             final PathArgument id = mod.getIdentifier();
-            final Optional<TreeNode> cm = meta.getChild(id);
+            final Optional<? extends TreeNode> cm = meta.getChild(id);
 
-            Optional<TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
+            final Optional<? extends TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
             if (result.isPresent()) {
                 final TreeNode tn = result.get();
                 meta.addChild(tn);
@@ -113,20 +129,28 @@ final class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
 
     @Override
     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
-        if (child instanceof NodeIdentifier) {
-            return entryStrategy;
-        }
-        return Optional.absent();
+        return child instanceof NodeIdentifier ? Optional.of(entryStrategy) : Optional.empty();
+    }
+
+    @Override
+    void verifyValue(final NormalizedNode<?, ?> value) {
+        // NOOP
     }
 
     @Override
-    protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
+    void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
+        // NOOP
+    }
 
+    @Override
+    protected void checkTouchApplicable(final ModificationPath path, final NodeModification modification,
+            final Optional<? extends TreeNode> current, final Version version) throws IncorrectDataStructureException {
+        throw new IncorrectDataStructureException(path.toInstanceIdentifier(), "Subtree modification is not allowed.");
     }
 
     @Override
-    protected void checkSubtreeModificationApplicable(final YangInstanceIdentifier path, final NodeModification modification,
-            final Optional<TreeNode> current) throws IncorrectDataStructureException {
-        throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");
+    void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
+        // Unkeyed lists are always replaced
+        node.write(value);
     }
 }