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