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.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;
27 final class ListModificationStrategy extends SchemaAwareApplyOperation<ListSchemaNode> {
28 private static final NormalizedNodeContainerSupport<NodeIdentifier, UnkeyedListEntryNode> ITEM_SUPPORT =
29 new NormalizedNodeContainerSupport<>(UnkeyedListEntryNode.class,
30 ImmutableUnkeyedListEntryNodeBuilder::create, ImmutableUnkeyedListEntryNodeBuilder::create);
32 private final DataNodeContainerModificationStrategy<ListSchemaNode> entryStrategy;
33 private final UnkeyedListNode emptyNode;
35 ListModificationStrategy(final ListSchemaNode schema, final DataTreeConfiguration treeConfig) {
36 entryStrategy = new DataNodeContainerModificationStrategy<>(ITEM_SUPPORT, schema, treeConfig);
37 emptyNode = ImmutableNodes.listNode(schema.getQName());
41 ListSchemaNode getSchema() {
42 return entryStrategy.getSchema();
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,
53 protected ChildTrackingPolicy getChildPolicy() {
54 return ChildTrackingPolicy.ORDERED;
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",
64 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
65 throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
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()) {
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.
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.
87 final MutableTreeNode mutable = newValueMeta.mutable();
88 mutable.setSubtreeVersion(version);
90 @SuppressWarnings("rawtypes")
91 final NormalizedNodeContainerBuilder dataBuilder = ImmutableUnkeyedListEntryNodeBuilder
92 .create((UnkeyedListEntryNode) newValue);
94 return mutateChildren(mutable, dataBuilder, version, modification.getChildren());
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.
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.
107 @SuppressWarnings({ "rawtypes", "unchecked" })
108 private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
109 final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
111 for (final ModifiedNode mod : modifications) {
112 final PathArgument id = mod.getIdentifier();
113 final Optional<? extends TreeNode> cm = meta.getChild(id);
115 final Optional<? extends TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
116 if (result.isPresent()) {
117 final TreeNode tn = result.get();
119 data.addChild(tn.getData());
121 meta.removeChild(id);
122 data.removeChild(id);
126 meta.setData(data.build());
131 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
132 return child instanceof NodeIdentifier ? Optional.of(entryStrategy) : Optional.empty();
136 void verifyValue(final NormalizedNode<?, ?> value) {
141 void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
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.");
152 void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
153 // Unkeyed lists are always replaced