Fix ListModificationStrategy child handling
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / ListModificationStrategy.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10 import com.google.common.base.MoreObjects.ToStringHelper;
11 import java.util.Optional;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.MutableTreeNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
23 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
25 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
27 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
28
29 final class ListModificationStrategy extends SchemaAwareApplyOperation<ListSchemaNode> {
30     private static final NormalizedNodeContainerSupport<NodeIdentifier, UnkeyedListEntryNode> ITEM_SUPPORT =
31             new NormalizedNodeContainerSupport<>(UnkeyedListEntryNode.class,
32                     ImmutableUnkeyedListEntryNodeBuilder::create, ImmutableUnkeyedListEntryNodeBuilder::create);
33
34     private final DataNodeContainerModificationStrategy<ListSchemaNode> entryStrategy;
35     private final UnkeyedListNode emptyNode;
36
37     ListModificationStrategy(final ListSchemaNode schema, final DataTreeConfiguration treeConfig) {
38         entryStrategy = new DataNodeContainerModificationStrategy<>(ITEM_SUPPORT, schema, treeConfig);
39         emptyNode = ImmutableNodes.listNode(schema.getQName());
40     }
41
42     @Override
43     ListSchemaNode getSchema() {
44         return entryStrategy.getSchema();
45     }
46
47     @Override
48     Optional<? extends TreeNode> apply(final ModifiedNode modification, final Optional<? extends TreeNode> storeMeta,
49             final Version version) {
50         return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, storeMeta,
51             version);
52     }
53
54     @Override
55     protected ChildTrackingPolicy getChildPolicy() {
56         return ChildTrackingPolicy.ORDERED;
57     }
58
59     @Override
60     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
61         throw new IllegalStateException(String.format("Merge of modification %s on unkeyed list should never be called",
62             modification));
63     }
64
65     @Override
66     protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
67         throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
68     }
69
70     @Override
71     protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode<?, ?> newValue,
72             final Optional<? extends TreeNode> currentMeta, final Version version) {
73         final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version);
74         if (modification.getChildren().isEmpty()) {
75             return newValueMeta;
76         }
77
78         /*
79          * This is where things get interesting. The user has performed a write and then she applied some more
80          * modifications to it. So we need to make sense of that an apply the operations on top of the written value. We
81          * could have done it during the write, but this operation is potentially expensive, so we have left it out of
82          * the fast path.
83          *
84          * As it turns out, once we materialize the written data, we can share the code path with the subtree change. So
85          * let's create an unsealed TreeNode and run the common parts on it -- which end with the node being sealed.
86          */
87         final MutableTreeNode mutable = newValueMeta.mutable();
88         mutable.setSubtreeVersion(version);
89
90         return mutateChildren(mutable, ImmutableUnkeyedListNodeBuilder.create((UnkeyedListNode) newValue), version,
91             modification.getChildren());
92     }
93
94     /**
95      * Applies write/remove diff operation for each modification child in modification subtree.
96      * Operation also sets the Data tree references for each Tree Node (Index Node) in meta (MutableTreeNode) structure.
97      *
98      * @param meta MutableTreeNode (IndexTreeNode)
99      * @param data DataBuilder
100      * @param nodeVersion Version of TreeNode
101      * @param modifications modification operations to apply
102      * @return Sealed immutable copy of TreeNode structure with all Data Node references set.
103      */
104     @SuppressWarnings({ "rawtypes", "unchecked" })
105     private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
106             final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
107
108         for (final ModifiedNode mod : modifications) {
109             final PathArgument id = mod.getIdentifier();
110             final Optional<? extends TreeNode> cm = meta.getChild(id);
111
112             final Optional<? extends TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
113             if (result.isPresent()) {
114                 final TreeNode tn = result.get();
115                 meta.addChild(tn);
116                 data.addChild(tn.getData());
117             } else {
118                 meta.removeChild(id);
119                 data.removeChild(id);
120             }
121         }
122
123         meta.setData(data.build());
124         return meta.seal();
125     }
126
127     @Override
128     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
129         return child instanceof NodeIdentifier ? Optional.of(entryStrategy) : Optional.empty();
130     }
131
132     @Override
133     void verifyValue(final NormalizedNode<?, ?> value) {
134         // NOOP
135     }
136
137     @Override
138     void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
139         // NOOP
140     }
141
142     @Override
143     protected void checkTouchApplicable(final ModificationPath path, final NodeModification modification,
144             final Optional<? extends TreeNode> current, final Version version) throws IncorrectDataStructureException {
145         throw new IncorrectDataStructureException(path.toInstanceIdentifier(), "Subtree modification is not allowed.");
146     }
147
148     @Override
149     void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
150         // Unkeyed lists are always replaced
151         node.write(value);
152     }
153
154     @Override
155     ToStringHelper addToStringAttributes(final ToStringHelper helper) {
156         return helper.add("entry", entryStrategy);
157     }
158 }