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