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