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