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 static com.google.common.base.Preconditions.checkArgument;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import java.util.Collection;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModifiedNodeDoesNotExistException;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.MutableTreeNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
28 abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareApplyOperation {
30 private final Class<? extends NormalizedNode<?, ?>> nodeClass;
31 private final boolean verifyChildrenStructure;
33 protected AbstractNodeContainerModificationStrategy(final Class<? extends NormalizedNode<?, ?>> nodeClass,
34 final TreeType treeType) {
35 this.nodeClass = Preconditions.checkNotNull(nodeClass , "nodeClass");
36 this.verifyChildrenStructure = (treeType == TreeType.CONFIGURATION);
39 @SuppressWarnings("rawtypes")
41 void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
42 checkArgument(nodeClass.isInstance(writtenValue), "Node %s is not of type %s", writtenValue, nodeClass);
43 checkArgument(writtenValue instanceof NormalizedNodeContainer);
44 if (verifyChildrenStructure && verifyChildren) {
45 final NormalizedNodeContainer container = (NormalizedNodeContainer) writtenValue;
46 for (final Object child : container.getValue()) {
47 checkArgument(child instanceof NormalizedNode);
48 final NormalizedNode<?, ?> castedChild = (NormalizedNode<?, ?>) child;
49 final Optional<ModificationApplyOperation> childOp = getChild(castedChild.getIdentifier());
50 if (childOp.isPresent()) {
51 childOp.get().verifyStructure(castedChild, verifyChildren);
53 throw new SchemaValidationFailedException(String.format(
54 "Child %s is not valid child according to schema.", castedChild.getIdentifier()));
61 protected TreeNode applyWrite(final ModifiedNode modification,
62 final Optional<TreeNode> currentMeta, final Version version) {
63 final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
64 final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version);
66 if (modification.getChildren().isEmpty()) {
71 * This is where things get interesting. The user has performed a write and
72 * then she applied some more modifications to it. So we need to make sense
73 * of that an apply the operations on top of the written value. We could have
74 * done it during the write, but this operation is potentially expensive, so
75 * we have left it out of the fast path.
77 * As it turns out, once we materialize the written data, we can share the
78 * code path with the subtree change. So let's create an unsealed TreeNode
79 * and run the common parts on it -- which end with the node being sealed.
81 * FIXME: this code needs to be moved out from the prepare() path and into
82 * the read() and seal() paths. Merging of writes needs to be charged
83 * to the code which originated this, not to the code which is
84 * attempting to make it visible.
86 final MutableTreeNode mutable = newValueMeta.mutable();
87 mutable.setSubtreeVersion(version);
89 @SuppressWarnings("rawtypes")
90 final NormalizedNodeContainerBuilder dataBuilder = createBuilder(newValue);
91 final TreeNode result = mutateChildren(mutable, dataBuilder, version, modification.getChildren());
93 // We are good to go except one detail: this is a single logical write, but
94 // we have a result TreeNode which has been forced to materialized, e.g. it
95 // is larger than it needs to be. Create a new TreeNode to host the data.
96 return TreeNodeFactory.createTreeNode(result.getData(), version);
100 * Applies write/remove diff operation for each modification child in modification subtree.
101 * Operation also sets the Data tree references for each Tree Node (Index Node) in meta (MutableTreeNode) structure.
103 * @param meta MutableTreeNode (IndexTreeNode)
104 * @param data DataBuilder
105 * @param nodeVersion Version of TreeNode
106 * @param modifications modification operations to apply
107 * @return Sealed immutable copy of TreeNode structure with all Data Node references set.
109 @SuppressWarnings({ "rawtypes", "unchecked" })
110 private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
111 final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
113 for (final ModifiedNode mod : modifications) {
114 final YangInstanceIdentifier.PathArgument id = mod.getIdentifier();
115 final Optional<TreeNode> cm = meta.getChild(id);
117 final Optional<TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
118 if (result.isPresent()) {
119 final TreeNode tn = result.get();
121 data.addChild(tn.getData());
123 meta.removeChild(id);
124 data.removeChild(id);
128 meta.setData(data.build());
133 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
134 final Version version) {
135 // For Node Containers - merge is same as subtree change - we only replace children.
136 return applyTouch(modification, currentMeta, version);
140 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
142 * The user may have issued an empty merge operation. In this case we do not perform
143 * a data tree mutation, do not pass GO, and do not collect useless garbage. It
144 * also means the ModificationType is UNMODIFIED.
146 final Collection<ModifiedNode> children = modification.getChildren();
147 if (!children.isEmpty()) {
148 @SuppressWarnings("rawtypes")
149 final NormalizedNodeContainerBuilder dataBuilder = createBuilder(currentMeta.getData());
150 final MutableTreeNode newMeta = currentMeta.mutable();
151 newMeta.setSubtreeVersion(version);
152 final TreeNode ret = mutateChildren(newMeta, dataBuilder, version, children);
155 * It is possible that the only modifications under this node were empty merges,
156 * which were turned into UNMODIFIED. If that is the case, we can turn this operation
157 * into UNMODIFIED, too, potentially cascading it up to root. This has the benefit
158 * of speeding up any users, who can skip processing child nodes.
160 * In order to do that, though, we have to check all child operations are UNMODIFIED.
161 * Let's do precisely that, stopping as soon we find a different result.
163 for (final ModifiedNode child : children) {
164 if (child.getModificationType() != ModificationType.UNMODIFIED) {
165 modification.resolveModificationType(ModificationType.SUBTREE_MODIFIED);
171 // The merge operation did not have any children, or all of them turned out to be UNMODIFIED, hence do not
172 // replace the metadata node.
173 modification.resolveModificationType(ModificationType.UNMODIFIED);
178 protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
179 final Optional<TreeNode> current) throws DataValidationFailedException {
180 if (!modification.getOriginal().isPresent() && !current.isPresent()) {
181 throw new ModifiedNodeDoesNotExistException(path, String.format("Node %s does not exist. Cannot apply modification to its children.", path));
184 if (!current.isPresent()) {
185 throw new ConflictingModificationAppliedException(path, "Node was deleted by other transaction.");
188 checkChildPreconditions(path, modification, current.get());
192 * Recursively check child preconditions.
194 * @param path current node path
195 * @param modification current modification
196 * @param current Current data tree node.
198 private void checkChildPreconditions(final YangInstanceIdentifier path, final NodeModification modification, final TreeNode current) throws DataValidationFailedException {
199 for (final NodeModification childMod : modification.getChildren()) {
200 final YangInstanceIdentifier.PathArgument childId = childMod.getIdentifier();
201 final Optional<TreeNode> childMeta = current.getChild(childId);
203 final YangInstanceIdentifier childPath = path.node(childId);
204 resolveChildOperation(childId).checkApplicable(childPath, childMod, childMeta);
209 protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
210 final Optional<TreeNode> current) throws DataValidationFailedException {
211 if (current.isPresent()) {
212 checkChildPreconditions(path, modification, current.get());
216 @SuppressWarnings("rawtypes")
217 protected abstract NormalizedNodeContainerBuilder createBuilder(NormalizedNode<?, ?> original);