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 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.builder.NormalizedNodeContainerBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
19 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
20 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
21 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
22 import org.opendaylight.yangtools.yang.data.tree.api.IncorrectDataStructureException;
23 import org.opendaylight.yangtools.yang.data.tree.impl.node.MutableTreeNode;
24 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
25 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
26 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
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);
33 private final DataNodeContainerModificationStrategy<ListSchemaNode> entryStrategy;
34 private final UnkeyedListNode emptyNode;
36 ListModificationStrategy(final ListSchemaNode schema, final DataTreeConfiguration treeConfig) {
37 entryStrategy = new DataNodeContainerModificationStrategy<>(ITEM_SUPPORT, schema, treeConfig);
38 emptyNode = ImmutableNodes.listNode(schema.getQName());
42 ListSchemaNode getSchema() {
43 return entryStrategy.getSchema();
47 Optional<? extends TreeNode> apply(final ModifiedNode modification, final TreeNode currentMeta,
48 final Version version) {
49 return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, currentMeta,
54 protected ChildTrackingPolicy getChildPolicy() {
55 return ChildTrackingPolicy.ORDERED;
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",
65 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
66 throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
70 protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue,
71 final TreeNode currentMeta, final Version version) {
72 final var newValueMeta = TreeNode.of(newValue, version);
73 if (modification.isEmpty()) {
78 * This is where things get interesting. The user has performed a write and then she applied some more
79 * modifications to it. So we need to make sense of that an apply the operations on top of the written value. We
80 * could have done it during the write, but this operation is potentially expensive, so we have left it out of
83 * As it turns out, once we materialize the written data, we can share the code path with the subtree change. So
84 * let's create an unsealed TreeNode and run the common parts on it -- which end with the node being sealed.
86 final var mutable = newValueMeta.mutable();
87 mutable.setSubtreeVersion(version);
89 return mutateChildren(mutable, ImmutableUnkeyedListNodeBuilder.create((UnkeyedListNode) newValue), version,
90 modification.getChildren());
94 * Applies write/remove diff operation for each modification child in modification subtree.
95 * Operation also sets the Data tree references for each Tree Node (Index Node) in meta (MutableTreeNode) structure.
97 * @param meta MutableTreeNode (IndexTreeNode)
98 * @param data DataBuilder
99 * @param nodeVersion Version of TreeNode
100 * @param modifications modification operations to apply
101 * @return Sealed immutable copy of TreeNode structure with all Data Node references set.
103 @SuppressWarnings({ "rawtypes", "unchecked" })
104 private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
105 final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
106 for (var mod : modifications) {
107 final var id = mod.getIdentifier();
108 final var cm = meta.childByArg(id);
110 final var result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
111 if (result.isPresent()) {
112 final TreeNode tn = result.orElseThrow();
114 data.addChild(tn.getData());
116 meta.removeChild(id);
117 data.removeChild(id);
121 meta.setData(data.build());
126 public ModificationApplyOperation childByArg(final PathArgument arg) {
127 return arg instanceof NodeIdentifier ? entryStrategy : null;
131 void verifyValue(final NormalizedNode value) {
136 void recursivelyVerifyStructure(final NormalizedNode value) {
141 protected void checkTouchApplicable(final ModificationPath path, final NodeModification modification,
142 final TreeNode currentMeta, final Version version) throws IncorrectDataStructureException {
143 throw new IncorrectDataStructureException(path.toInstanceIdentifier(), "Subtree modification is not allowed.");
147 void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode value, final Version version) {
148 // Unkeyed lists are always replaced
153 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
154 return helper.add("entry", entryStrategy);