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;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import java.util.Collection;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModifiedNodeDoesNotExistException;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.MutableTreeNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
29 abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareApplyOperation {
31 private final Class<? extends NormalizedNode<?, ?>> nodeClass;
32 private final boolean verifyChildrenStructure;
34 protected AbstractNodeContainerModificationStrategy(final Class<? extends NormalizedNode<?, ?>> nodeClass,
35 final TreeType treeType) {
36 this.nodeClass = Preconditions.checkNotNull(nodeClass , "nodeClass");
37 this.verifyChildrenStructure = (treeType == TreeType.CONFIGURATION);
40 @SuppressWarnings("rawtypes")
42 void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
43 checkArgument(nodeClass.isInstance(writtenValue), "Node %s is not of type %s", writtenValue, nodeClass);
44 checkArgument(writtenValue instanceof NormalizedNodeContainer);
45 if (verifyChildrenStructure && verifyChildren) {
46 final NormalizedNodeContainer container = (NormalizedNodeContainer) writtenValue;
47 for (final Object child : container.getValue()) {
48 checkArgument(child instanceof NormalizedNode);
49 final NormalizedNode<?, ?> castedChild = (NormalizedNode<?, ?>) child;
50 final Optional<ModificationApplyOperation> childOp = getChild(castedChild.getIdentifier());
51 if (childOp.isPresent()) {
52 childOp.get().verifyStructure(castedChild, verifyChildren);
54 throw new SchemaValidationFailedException(String.format(
55 "Child %s is not valid child according to schema.", castedChild.getIdentifier()));
62 protected TreeNode applyWrite(final ModifiedNode modification,
63 final Optional<TreeNode> currentMeta, final Version version) {
64 final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
65 final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version);
67 if (modification.getChildren().isEmpty()) {
72 * This is where things get interesting. The user has performed a write and
73 * then she applied some more modifications to it. So we need to make sense
74 * of that an apply the operations on top of the written value. We could have
75 * done it during the write, but this operation is potentially expensive, so
76 * we have left it out of the fast path.
78 * As it turns out, once we materialize the written data, we can share the
79 * code path with the subtree change. So let's create an unsealed TreeNode
80 * and run the common parts on it -- which end with the node being sealed.
82 * FIXME: this code needs to be moved out from the prepare() path and into
83 * the read() and seal() paths. Merging of writes needs to be charged
84 * to the code which originated this, not to the code which is
85 * attempting to make it visible.
87 final MutableTreeNode mutable = newValueMeta.mutable();
88 mutable.setSubtreeVersion(version);
90 @SuppressWarnings("rawtypes")
91 final NormalizedNodeContainerBuilder dataBuilder = createBuilder(newValue);
93 return mutateChildren(mutable, dataBuilder, version, modification.getChildren());
97 * Applies write/remove diff operation for each modification child in modification subtree.
98 * Operation also sets the Data tree references for each Tree Node (Index Node) in meta (MutableTreeNode) structure.
100 * @param meta MutableTreeNode (IndexTreeNode)
101 * @param data DataBuilder
102 * @param nodeVersion Version of TreeNode
103 * @param modifications modification operations to apply
104 * @return Sealed immutable copy of TreeNode structure with all Data Node references set.
106 @SuppressWarnings({ "rawtypes", "unchecked" })
107 private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
108 final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
110 for (final ModifiedNode mod : modifications) {
111 final YangInstanceIdentifier.PathArgument id = mod.getIdentifier();
112 final Optional<TreeNode> cm = meta.getChild(id);
114 final Optional<TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
115 if (result.isPresent()) {
116 final TreeNode tn = result.get();
118 data.addChild(tn.getData());
120 meta.removeChild(id);
121 data.removeChild(id);
125 meta.setData(data.build());
130 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
131 final Version version) {
132 // For Node Containers - merge is same as subtree change - we only replace children.
133 return applyTouch(modification, currentMeta, version);
137 public TreeNode applyTouch(final ModifiedNode modification,
138 final TreeNode currentMeta, final Version version) {
139 final MutableTreeNode newMeta = currentMeta.mutable();
140 newMeta.setSubtreeVersion(version);
143 * The user has issued an empty merge operation. In this case we do not perform
144 * a data tree mutation, do not pass GO, and do not collect useless garbage. It
145 * also means the ModificationType is UNMODIFIED.
147 final Collection<ModifiedNode> children = modification.getChildren();
148 if (children.isEmpty()) {
149 modification.resolveModificationType(ModificationType.UNMODIFIED);
150 newMeta.setData(currentMeta.getData());
151 return newMeta.seal();
154 @SuppressWarnings("rawtypes")
155 final NormalizedNodeContainerBuilder dataBuilder = createBuilder(currentMeta.getData());
156 final TreeNode ret = mutateChildren(newMeta, dataBuilder, version, children);
159 * It is possible that the only modifications under this node were empty merges,
160 * which were turned into UNMODIFIED. If that is the case, we can turn this operation
161 * into UNMODIFIED, too, potentially cascading it up to root. This has the benefit
162 * of speeding up any users, who can skip processing child nodes.
164 * In order to do that, though, we have to check all child operations are UNMODIFIED.
165 * Let's do precisely that, stopping as soon we find a different result.
167 for (final ModifiedNode child : children) {
168 if (child.getModificationType() != ModificationType.UNMODIFIED) {
169 modification.resolveModificationType(ModificationType.SUBTREE_MODIFIED);
174 modification.resolveModificationType(ModificationType.UNMODIFIED);
179 protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
180 final Optional<TreeNode> current) throws DataValidationFailedException {
181 if (!modification.getOriginal().isPresent() && !current.isPresent()) {
182 throw new ModifiedNodeDoesNotExistException(path, String.format("Node %s does not exist. Cannot apply modification to its children.", path));
185 if (!current.isPresent()) {
186 throw new ConflictingModificationAppliedException(path, "Node was deleted by other transaction.");
189 checkChildPreconditions(path, modification, current.get());
193 * Recursively check child preconditions.
195 * @param path current node path
196 * @param modification current modification
197 * @param current Current data tree node.
199 private void checkChildPreconditions(final YangInstanceIdentifier path, final NodeModification modification, final TreeNode current) throws DataValidationFailedException {
200 for (final NodeModification childMod : modification.getChildren()) {
201 final YangInstanceIdentifier.PathArgument childId = childMod.getIdentifier();
202 final Optional<TreeNode> childMeta = current.getChild(childId);
204 final YangInstanceIdentifier childPath = path.node(childId);
205 resolveChildOperation(childId).checkApplicable(childPath, childMod, childMeta);
210 protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
211 final Optional<TreeNode> current) throws DataValidationFailedException {
212 if (current.isPresent()) {
213 checkChildPreconditions(path, modification, current.get());
217 @SuppressWarnings("rawtypes")
218 protected abstract NormalizedNodeContainerBuilder createBuilder(NormalizedNode<?, ?> original);