2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
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.data.impl.schema.tree.NormalizedNodeContainerSupport.Single;
27 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
29 final class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
30 private static final Single<NodeIdentifier, UnkeyedListEntryNode> ITEM_SUPPORT =
31 new Single<>(UnkeyedListEntryNode.class, ImmutableUnkeyedListEntryNodeBuilder::create,
32 ImmutableUnkeyedListEntryNodeBuilder::create);
34 private final Optional<ModificationApplyOperation> entryStrategy;
35 private final UnkeyedListNode emptyNode;
37 UnkeyedListModificationStrategy(final ListSchemaNode schema, final DataTreeConfiguration treeConfig) {
38 entryStrategy = Optional.of(new DataNodeContainerModificationStrategy<>(ITEM_SUPPORT, schema, treeConfig));
39 emptyNode = ImmutableNodes.listNode(schema.getQName());
43 Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> storeMeta,
44 final Version version) {
45 return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, storeMeta,
50 void checkApplicable(final ModificationPath path, final NodeModification modification,
51 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
52 AutomaticLifecycleMixin.checkApplicable(super::checkApplicable, emptyNode, path, modification, current,
57 protected ChildTrackingPolicy getChildPolicy() {
58 return ChildTrackingPolicy.ORDERED;
62 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
63 throw new IllegalStateException(String.format("Merge of modification %s on unkeyed list should never be called",
68 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
69 throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
73 protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode<?, ?> newValue,
74 final Optional<TreeNode> currentMeta, final Version version) {
75 final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version);
76 if (modification.getChildren().isEmpty()) {
81 * This is where things get interesting. The user has performed a write and
82 * then she applied some more modifications to it. So we need to make sense
83 * of that an apply the operations on top of the written value. We could have
84 * done it during the write, but this operation is potentially expensive, so
85 * we have left it out of the fast path.
87 * As it turns out, once we materialize the written data, we can share the
88 * code path with the subtree change. So let's create an unsealed TreeNode
89 * and run the common parts on it -- which end with the node being sealed.
91 final MutableTreeNode mutable = newValueMeta.mutable();
92 mutable.setSubtreeVersion(version);
94 @SuppressWarnings("rawtypes")
95 final NormalizedNodeContainerBuilder dataBuilder = ImmutableUnkeyedListEntryNodeBuilder
96 .create((UnkeyedListEntryNode) newValue);
98 return mutateChildren(mutable, dataBuilder, version, modification.getChildren());
102 * Applies write/remove diff operation for each modification child in modification subtree.
103 * Operation also sets the Data tree references for each Tree Node (Index Node) in meta (MutableTreeNode) structure.
105 * @param meta MutableTreeNode (IndexTreeNode)
106 * @param data DataBuilder
107 * @param nodeVersion Version of TreeNode
108 * @param modifications modification operations to apply
109 * @return Sealed immutable copy of TreeNode structure with all Data Node references set.
111 @SuppressWarnings({ "rawtypes", "unchecked" })
112 private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
113 final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
115 for (final ModifiedNode mod : modifications) {
116 final PathArgument id = mod.getIdentifier();
117 final Optional<TreeNode> cm = meta.getChild(id);
119 final Optional<TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
120 if (result.isPresent()) {
121 final TreeNode tn = result.get();
123 data.addChild(tn.getData());
125 meta.removeChild(id);
126 data.removeChild(id);
130 meta.setData(data.build());
135 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
136 return child instanceof NodeIdentifier ? entryStrategy : Optional.empty();
140 void verifyValue(final NormalizedNode<?, ?> value) {
145 void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
150 protected void checkTouchApplicable(final ModificationPath path, final NodeModification modification,
151 final Optional<TreeNode> current, final Version version) throws IncorrectDataStructureException {
152 throw new IncorrectDataStructureException(path.toInstanceIdentifier(), "Subtree modification is not allowed.");
156 void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
157 // Unkeyed lists are always replaced