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 com.google.common.base.Optional;
11 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.tree.IncorrectDataStructureException;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
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.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 UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
29 private final Optional<ModificationApplyOperation> entryStrategy;
31 UnkeyedListModificationStrategy(final ListSchemaNode schema, final TreeType treeType) {
32 entryStrategy = Optional.<ModificationApplyOperation> of(new UnkeyedListItemModificationStrategy(schema, treeType));
36 protected ChildTrackingPolicy getChildPolicy() {
37 return ChildTrackingPolicy.ORDERED;
41 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
42 final Version version) {
43 modification.resolveModificationType(ModificationType.WRITE);
44 return applyWrite(modification, Optional.of(currentMeta), version);
48 protected TreeNode applyTouch(final ModifiedNode modification,
49 final TreeNode currentMeta, final Version version) {
50 throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
54 protected TreeNode applyWrite(final ModifiedNode modification,
55 final Optional<TreeNode> currentMeta, final Version version) {
56 final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
57 final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version);
59 if (modification.getChildren().isEmpty()) {
64 * This is where things get interesting. The user has performed a write and
65 * then she applied some more modifications to it. So we need to make sense
66 * of that an apply the operations on top of the written value. We could have
67 * done it during the write, but this operation is potentially expensive, so
68 * we have left it out of the fast path.
70 * As it turns out, once we materialize the written data, we can share the
71 * code path with the subtree change. So let's create an unsealed TreeNode
72 * and run the common parts on it -- which end with the node being sealed.
74 final MutableTreeNode mutable = newValueMeta.mutable();
75 mutable.setSubtreeVersion(version);
77 @SuppressWarnings("rawtypes")
78 final NormalizedNodeContainerBuilder dataBuilder = ImmutableUnkeyedListEntryNodeBuilder
79 .create((UnkeyedListEntryNode) newValue);
81 return mutateChildren(mutable, dataBuilder, version, modification.getChildren());
85 * Applies write/remove diff operation for each modification child in modification subtree.
86 * Operation also sets the Data tree references for each Tree Node (Index Node) in meta (MutableTreeNode) structure.
88 * @param meta MutableTreeNode (IndexTreeNode)
89 * @param data DataBuilder
90 * @param nodeVersion Version of TreeNode
91 * @param modifications modification operations to apply
92 * @return Sealed immutable copy of TreeNode structure with all Data Node references set.
94 @SuppressWarnings({ "rawtypes", "unchecked" })
95 private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
96 final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
98 for (final ModifiedNode mod : modifications) {
99 final PathArgument id = mod.getIdentifier();
100 final Optional<TreeNode> cm = meta.getChild(id);
102 final Optional<TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
103 if (result.isPresent()) {
104 final TreeNode tn = result.get();
106 data.addChild(tn.getData());
108 meta.removeChild(id);
109 data.removeChild(id);
113 meta.setData(data.build());
118 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
119 if (child instanceof NodeIdentifier) {
120 return entryStrategy;
122 return Optional.absent();
126 protected void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
131 protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
132 final Optional<TreeNode> current) throws IncorrectDataStructureException {
133 throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");