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.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.builder.api.NormalizedNodeContainerBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
24 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
26 final class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
28 private final Optional<ModificationApplyOperation> entryStrategy;
30 UnkeyedListModificationStrategy(final ListSchemaNode schema) {
31 entryStrategy = Optional.<ModificationApplyOperation> of(new UnkeyedListItemModificationStrategy(schema));
35 protected ChildTrackingPolicy getChildPolicy() {
36 return ChildTrackingPolicy.ORDERED;
40 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
41 final Version version) {
42 modification.resolveModificationType(ModificationType.WRITE);
43 return applyWrite(modification, Optional.of(currentMeta), version);
47 protected TreeNode applyTouch(final ModifiedNode modification,
48 final TreeNode currentMeta, final Version version) {
49 throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
53 protected TreeNode applyWrite(final ModifiedNode modification,
54 final Optional<TreeNode> currentMeta, final Version version) {
55 final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
56 final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version);
58 if (modification.getChildren().isEmpty()) {
63 * This is where things get interesting. The user has performed a write and
64 * then she applied some more modifications to it. So we need to make sense
65 * of that an apply the operations on top of the written value. We could have
66 * done it during the write, but this operation is potentially expensive, so
67 * we have left it out of the fast path.
69 * As it turns out, once we materialize the written data, we can share the
70 * code path with the subtree change. So let's create an unsealed TreeNode
71 * and run the common parts on it -- which end with the node being sealed.
73 final MutableTreeNode mutable = newValueMeta.mutable();
74 mutable.setSubtreeVersion(version);
76 @SuppressWarnings("rawtypes")
77 final NormalizedNodeContainerBuilder dataBuilder = ImmutableUnkeyedListEntryNodeBuilder
78 .create((UnkeyedListEntryNode) newValue);
80 return mutateChildren(mutable, dataBuilder, version, modification.getChildren());
84 * Applies write/remove diff operation for each modification child in modification subtree.
85 * Operation also sets the Data tree references for each Tree Node (Index Node) in meta (MutableTreeNode) structure.
87 * @param meta MutableTreeNode (IndexTreeNode)
88 * @param data DataBuilder
89 * @param nodeVersion Version of TreeNode
90 * @param modifications modification operations to apply
91 * @return Sealed immutable copy of TreeNode structure with all Data Node references set.
93 @SuppressWarnings({ "rawtypes", "unchecked" })
94 private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
95 final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
97 for (ModifiedNode mod : modifications) {
98 final PathArgument id = mod.getIdentifier();
99 final Optional<TreeNode> cm = meta.getChild(id);
101 Optional<TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
102 if (result.isPresent()) {
103 final TreeNode tn = result.get();
105 data.addChild(tn.getData());
107 meta.removeChild(id);
108 data.removeChild(id);
112 meta.setData(data.build());
117 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
118 if (child instanceof NodeIdentifier) {
119 return entryStrategy;
121 return Optional.absent();
125 protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
130 protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
131 final Optional<TreeNode> current) throws IncorrectDataStructureException {
132 throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");