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