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.tree.impl;
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;
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);
29 private final DataNodeContainerModificationStrategy<ListSchemaNode> entryStrategy;
30 private final UnkeyedListNode emptyNode;
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()))
40 ListSchemaNode getSchema() {
41 return entryStrategy.getSchema();
45 TreeNode apply(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
46 return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, currentMeta,
51 protected ChildTrackingPolicy getChildPolicy() {
52 return ChildTrackingPolicy.ORDERED;
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",
62 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
63 throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
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()) {
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
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.
83 final var mutable = newValueMeta.toMutable();
84 mutable.setSubtreeVersion(version);
86 return mutateChildren(mutable, BUILDER_FACTORY.newUnkeyedListBuilder((UnkeyedListNode) newValue), version,
87 modification.getChildren());
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.
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.
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);
107 final var result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
108 if (result != null) {
109 meta.putChild(result);
110 data.addChild(result.data());
112 meta.removeChild(id);
113 data.removeChild(id);
117 meta.setData(data.build());
122 public ModificationApplyOperation childByArg(final PathArgument arg) {
123 return arg instanceof NodeIdentifier ? entryStrategy : null;
127 void verifyValue(final NormalizedNode value) {
132 void recursivelyVerifyStructure(final NormalizedNode value) {
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.");
143 void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode value, final Version version) {
144 // Unkeyed lists are always replaced
149 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
150 return helper.add("entry", entryStrategy);