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 com.google.common.base.Preconditions;
12 import com.google.common.collect.Iterables;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.MutableTreeNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
30 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
31 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
32 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
33 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import java.util.List;
44 abstract class SchemaAwareApplyOperation implements ModificationApplyOperation {
45 private static final Logger LOG = LoggerFactory.getLogger(SchemaAwareApplyOperation.class);
47 public static SchemaAwareApplyOperation from(final DataSchemaNode schemaNode) {
48 if (schemaNode instanceof ContainerSchemaNode) {
49 return new DataNodeContainerModificationStrategy.ContainerModificationStrategy((ContainerSchemaNode) schemaNode);
50 } else if (schemaNode instanceof ListSchemaNode) {
51 return fromListSchemaNode((ListSchemaNode) schemaNode);
52 } else if (schemaNode instanceof ChoiceNode) {
53 return new NormalizedNodeContainerModificationStrategy.ChoiceModificationStrategy((ChoiceNode) schemaNode);
54 } else if (schemaNode instanceof LeafListSchemaNode) {
55 return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode);
56 } else if (schemaNode instanceof LeafSchemaNode) {
57 return new ValueNodeModificationStrategy.LeafModificationStrategy((LeafSchemaNode) schemaNode);
59 throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
62 public static SchemaAwareApplyOperation from(final DataNodeContainer resolvedTree,
63 final AugmentationTarget augSchemas, final AugmentationIdentifier identifier) {
64 AugmentationSchema augSchema = null;
67 for (AugmentationSchema potential : augSchemas.getAvailableAugmentations()) {
68 for (DataSchemaNode child : potential.getChildNodes()) {
69 if (identifier.getPossibleChildNames().contains(child.getQName())) {
70 augSchema = potential;
76 if (augSchema != null) {
77 return new DataNodeContainerModificationStrategy.AugmentationModificationStrategy(augSchema, resolvedTree);
82 public static boolean checkConflicting(final YangInstanceIdentifier path, final boolean condition, final String message) throws ConflictingModificationAppliedException {
84 throw new ConflictingModificationAppliedException(path, message);
89 private static SchemaAwareApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode) {
90 List<QName> keyDefinition = schemaNode.getKeyDefinition();
91 if (keyDefinition == null || keyDefinition.isEmpty()) {
92 return new UnkeyedListModificationStrategy(schemaNode);
94 if (schemaNode.isUserOrdered()) {
95 return new NormalizedNodeContainerModificationStrategy.OrderedMapModificationStrategy(schemaNode);
98 return new NormalizedNodeContainerModificationStrategy.UnorderedMapModificationStrategy(schemaNode);
101 private static SchemaAwareApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode) {
102 if(schemaNode.isUserOrdered()) {
103 return new NormalizedNodeContainerModificationStrategy.OrderedLeafSetModificationStrategy(schemaNode);
105 return new NormalizedNodeContainerModificationStrategy.UnorderedLeafSetModificationStrategy(schemaNode);
109 private static final void checkNotConflicting(final YangInstanceIdentifier path, final TreeNode original, final TreeNode current) throws ConflictingModificationAppliedException {
110 checkConflicting(path, original.getVersion().equals(current.getVersion()),
111 "Node was replaced by other transaction.");
112 checkConflicting(path, original.getSubtreeVersion().equals(current.getSubtreeVersion()),
113 "Node children was modified by other transaction");
116 protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) {
117 Optional<ModificationApplyOperation> potential = getChild(child);
118 Preconditions.checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
119 return potential.get();
123 public void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException {
124 if (modification.getType() == ModificationType.WRITE) {
125 verifyWrittenStructure(modification.getWrittenValue());
130 public final void checkApplicable(final YangInstanceIdentifier path,final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
131 switch (modification.getType()) {
133 checkDeleteApplicable(modification, current);
134 case SUBTREE_MODIFIED:
135 checkSubtreeModificationApplicable(path, modification, current);
138 checkWriteApplicable(path, modification, current);
141 checkMergeApplicable(path, modification, current);
146 throw new UnsupportedOperationException("Suplied modification type "+ modification.getType()+ "is not supported.");
151 protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
152 Optional<TreeNode> original = modification.getOriginal();
153 if (original.isPresent() && current.isPresent()) {
155 * We need to do conflict detection only and only if the value of leaf changed
156 * before two transactions. If value of leaf is unchanged between two transactions
157 * it should not cause transaction to fail, since result of this merge
158 * leads to same data.
160 if(!original.get().getData().equals(current.get().getData())) {
161 checkNotConflicting(path, original.get(), current.get());
167 * Checks if write operation can be applied to current TreeNode.
168 * The operation checks if original tree node to which the modification is going to be applied exists and if
169 * current node in TreeNode structure exists.
171 * @param path Path from current node in TreeNode
172 * @param modification modification to apply
173 * @param current current node in TreeNode for modification to apply
174 * @throws DataValidationFailedException
176 protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification,
177 final Optional<TreeNode> current) throws DataValidationFailedException {
178 Optional<TreeNode> original = modification.getOriginal();
179 if (original.isPresent() && current.isPresent()) {
180 checkNotConflicting(path, original.get(), current.get());
181 } else if(original.isPresent()) {
182 throw new ConflictingModificationAppliedException(path,"Node was deleted by other transaction.");
183 } else if(current.isPresent()) {
184 throw new ConflictingModificationAppliedException(path,"Node was created by other transaction.");
188 private void checkDeleteApplicable(final NodeModification modification, final Optional<TreeNode> current) {
189 // Delete is always applicable, we do not expose it to subclasses
190 if (current.isPresent()) {
191 LOG.trace("Delete operation turned to no-op on missing node {}", modification);
196 public final Optional<TreeNode> apply(final ModifiedNode modification,
197 final Optional<TreeNode> currentMeta, final Version version) {
199 switch (modification.getType()) {
201 return modification.storeSnapshot(Optional.<TreeNode> absent());
202 case SUBTREE_MODIFIED:
203 Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification",
205 return modification.storeSnapshot(Optional.of(applySubtreeChange(modification, currentMeta.get(),
208 if(currentMeta.isPresent()) {
209 return modification.storeSnapshot(Optional.of(applyMerge(modification,currentMeta.get(), version)));
211 // intentional fall-through: if the node does not exist a merge is same as a write
213 return modification.storeSnapshot(Optional.of(applyWrite(modification, currentMeta, version)));
217 throw new IllegalArgumentException("Provided modification type is not supported.");
221 protected abstract TreeNode applyMerge(ModifiedNode modification,
222 TreeNode currentMeta, Version version);
224 protected abstract TreeNode applyWrite(ModifiedNode modification,
225 Optional<TreeNode> currentMeta, Version version);
227 protected abstract TreeNode applySubtreeChange(ModifiedNode modification,
228 TreeNode currentMeta, Version version);
232 * Checks is supplied {@link NodeModification} is applicable for Subtree Modification.
234 * @param path Path to current node
235 * @param modification Node modification which should be applied.
236 * @param current Current state of data tree
237 * @throws ConflictingModificationAppliedException If subtree was changed in conflicting way
238 * @throws IncorrectDataStructureException If subtree modification is not applicable (e.g. leaf node).
240 protected abstract void checkSubtreeModificationApplicable(YangInstanceIdentifier path, final NodeModification modification,
241 final Optional<TreeNode> current) throws DataValidationFailedException;
243 protected abstract void verifyWrittenStructure(NormalizedNode<?, ?> writtenValue);
245 public static class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
247 private final Optional<ModificationApplyOperation> entryStrategy;
249 protected UnkeyedListModificationStrategy(final ListSchemaNode schema) {
250 entryStrategy = Optional.<ModificationApplyOperation> of(new DataNodeContainerModificationStrategy.UnkeyedListItemModificationStrategy(schema));
254 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
255 final Version version) {
256 return applyWrite(modification, Optional.of(currentMeta), version);
260 protected TreeNode applySubtreeChange(final ModifiedNode modification,
261 final TreeNode currentMeta, final Version version) {
262 throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
266 protected TreeNode applyWrite(final ModifiedNode modification,
267 final Optional<TreeNode> currentMeta, final Version version) {
268 final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
269 final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version);
271 if (Iterables.isEmpty(modification.getChildren())) {
276 * This is where things get interesting. The user has performed a write and
277 * then she applied some more modifications to it. So we need to make sense
278 * of that an apply the operations on top of the written value. We could have
279 * done it during the write, but this operation is potentially expensive, so
280 * we have left it out of the fast path.
282 * As it turns out, once we materialize the written data, we can share the
283 * code path with the subtree change. So let's create an unsealed TreeNode
284 * and run the common parts on it -- which end with the node being sealed.
286 final MutableTreeNode mutable = newValueMeta.mutable();
287 mutable.setSubtreeVersion(version);
289 @SuppressWarnings("rawtypes")
290 final NormalizedNodeContainerBuilder dataBuilder = ImmutableUnkeyedListEntryNodeBuilder
291 .create((UnkeyedListEntryNode) newValue);
293 return mutateChildren(mutable, dataBuilder, version, modification.getChildren());
297 * Applies write/remove diff operation for each modification child in modification subtree.
298 * Operation also sets the Data tree references for each Tree Node (Index Node) in meta (MutableTreeNode) structure.
300 * @param meta MutableTreeNode (IndexTreeNode)
301 * @param data DataBuilder
302 * @param nodeVersion Version of TreeNode
303 * @param modifications modification operations to apply
304 * @return Sealed immutable copy of TreeNode structure with all Data Node references set.
306 @SuppressWarnings({ "rawtypes", "unchecked" })
307 private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
308 final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
310 for (ModifiedNode mod : modifications) {
311 final PathArgument id = mod.getIdentifier();
312 final Optional<TreeNode> cm = meta.getChild(id);
314 Optional<TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
315 if (result.isPresent()) {
316 final TreeNode tn = result.get();
318 data.addChild(tn.getData());
320 meta.removeChild(id);
321 data.removeChild(id);
325 meta.setData(data.build());
330 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
331 if (child instanceof NodeIdentifier) {
332 return entryStrategy;
334 return Optional.absent();
338 protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
343 protected void checkSubtreeModificationApplicable(final YangInstanceIdentifier path, final NodeModification modification,
344 final Optional<TreeNode> current) throws IncorrectDataStructureException {
345 throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");