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.model.api.ListSchemaNode;
28 final class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
29 private static final NormalizedNodeContainerSupport<NodeIdentifier, UnkeyedListEntryNode> ITEM_SUPPORT =
30 new NormalizedNodeContainerSupport<>(UnkeyedListEntryNode.class,
31 ImmutableUnkeyedListEntryNodeBuilder::create, ImmutableUnkeyedListEntryNodeBuilder::create);
33 private final Optional<ModificationApplyOperation> entryStrategy;
34 private final UnkeyedListNode emptyNode;
36 UnkeyedListModificationStrategy(final ListSchemaNode schema, final DataTreeConfiguration treeConfig) {
37 entryStrategy = Optional.of(new DataNodeContainerModificationStrategy<>(ITEM_SUPPORT, schema, treeConfig));
38 emptyNode = ImmutableNodes.listNode(schema.getQName());
42 Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> storeMeta,
43 final Version version) {
44 return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, storeMeta,
49 void checkApplicable(final ModificationPath path, final NodeModification modification,
50 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
51 AutomaticLifecycleMixin.checkApplicable(super::checkApplicable, emptyNode, path, modification, current,
56 protected ChildTrackingPolicy getChildPolicy() {
57 return ChildTrackingPolicy.ORDERED;
61 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
62 throw new IllegalStateException(String.format("Merge of modification %s on unkeyed list should never be called",
67 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
68 throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
72 protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode<?, ?> newValue,
73 final Optional<TreeNode> currentMeta, final Version version) {
74 final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version);
75 if (modification.getChildren().isEmpty()) {
80 * This is where things get interesting. The user has performed a write and
81 * then she applied some more modifications to it. So we need to make sense
82 * of that an apply the operations on top of the written value. We could have
83 * done it during the write, but this operation is potentially expensive, so
84 * we have left it out of the fast path.
86 * As it turns out, once we materialize the written data, we can share the
87 * code path with the subtree change. So let's create an unsealed TreeNode
88 * and run the common parts on it -- which end with the node being sealed.
90 final MutableTreeNode mutable = newValueMeta.mutable();
91 mutable.setSubtreeVersion(version);
93 @SuppressWarnings("rawtypes")
94 final NormalizedNodeContainerBuilder dataBuilder = ImmutableUnkeyedListEntryNodeBuilder
95 .create((UnkeyedListEntryNode) newValue);
97 return mutateChildren(mutable, dataBuilder, version, modification.getChildren());
101 * Applies write/remove diff operation for each modification child in modification subtree.
102 * Operation also sets the Data tree references for each Tree Node (Index Node) in meta (MutableTreeNode) structure.
104 * @param meta MutableTreeNode (IndexTreeNode)
105 * @param data DataBuilder
106 * @param nodeVersion Version of TreeNode
107 * @param modifications modification operations to apply
108 * @return Sealed immutable copy of TreeNode structure with all Data Node references set.
110 @SuppressWarnings({ "rawtypes", "unchecked" })
111 private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
112 final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
114 for (final ModifiedNode mod : modifications) {
115 final PathArgument id = mod.getIdentifier();
116 final Optional<TreeNode> cm = meta.getChild(id);
118 final Optional<TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
119 if (result.isPresent()) {
120 final TreeNode tn = result.get();
122 data.addChild(tn.getData());
124 meta.removeChild(id);
125 data.removeChild(id);
129 meta.setData(data.build());
134 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
135 return child instanceof NodeIdentifier ? entryStrategy : Optional.empty();
139 void verifyValue(final NormalizedNode<?, ?> value) {
144 void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
149 protected void checkTouchApplicable(final ModificationPath path, final NodeModification modification,
150 final Optional<TreeNode> current, final Version version) throws IncorrectDataStructureException {
151 throw new IncorrectDataStructureException(path.toInstanceIdentifier(), "Subtree modification is not allowed.");
155 void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
156 // Unkeyed lists are always replaced