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