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 org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
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.IncorrectDataStructureException;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import java.util.List;
39 abstract class SchemaAwareApplyOperation implements ModificationApplyOperation {
40 private static final Logger LOG = LoggerFactory.getLogger(SchemaAwareApplyOperation.class);
42 public static SchemaAwareApplyOperation from(final DataSchemaNode schemaNode) {
43 if (schemaNode instanceof ContainerSchemaNode) {
44 return new DataNodeContainerModificationStrategy.ContainerModificationStrategy((ContainerSchemaNode) schemaNode);
45 } else if (schemaNode instanceof ListSchemaNode) {
46 return fromListSchemaNode((ListSchemaNode) schemaNode);
47 } else if (schemaNode instanceof ChoiceNode) {
48 return new NormalizedNodeContainerModificationStrategy.ChoiceModificationStrategy((ChoiceNode) schemaNode);
49 } else if (schemaNode instanceof LeafListSchemaNode) {
50 return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode);
51 } else if (schemaNode instanceof LeafSchemaNode) {
52 return new ValueNodeModificationStrategy.LeafModificationStrategy((LeafSchemaNode) schemaNode);
54 throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
57 public static SchemaAwareApplyOperation from(final DataNodeContainer resolvedTree,
58 final AugmentationTarget augSchemas, final AugmentationIdentifier identifier) {
59 AugmentationSchema augSchema = null;
62 for (AugmentationSchema potential : augSchemas.getAvailableAugmentations()) {
63 for (DataSchemaNode child : potential.getChildNodes()) {
64 if (identifier.getPossibleChildNames().contains(child.getQName())) {
65 augSchema = potential;
71 if (augSchema != null) {
72 return new DataNodeContainerModificationStrategy.AugmentationModificationStrategy(augSchema, resolvedTree);
77 public static boolean checkConflicting(final InstanceIdentifier path, final boolean condition, final String message) throws ConflictingModificationAppliedException {
79 throw new ConflictingModificationAppliedException(path, message);
84 private static SchemaAwareApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode) {
85 List<QName> keyDefinition = schemaNode.getKeyDefinition();
86 if (keyDefinition == null || keyDefinition.isEmpty()) {
87 return new UnkeyedListModificationStrategy(schemaNode);
89 if (schemaNode.isUserOrdered()) {
90 return new NormalizedNodeContainerModificationStrategy.OrderedMapModificationStrategy(schemaNode);
93 return new NormalizedNodeContainerModificationStrategy.UnorderedMapModificationStrategy(schemaNode);
96 private static SchemaAwareApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode) {
97 if(schemaNode.isUserOrdered()) {
98 return new NormalizedNodeContainerModificationStrategy.OrderedLeafSetModificationStrategy(schemaNode);
100 return new NormalizedNodeContainerModificationStrategy.UnorderedLeafSetModificationStrategy(schemaNode);
104 private static final void checkNotConflicting(final InstanceIdentifier path, final TreeNode original, final TreeNode current) throws ConflictingModificationAppliedException {
105 checkConflicting(path, original.getVersion().equals(current.getVersion()),
106 "Node was replaced by other transaction.");
107 checkConflicting(path, original.getSubtreeVersion().equals(current.getSubtreeVersion()),
108 "Node children was modified by other transaction");
111 protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) {
112 Optional<ModificationApplyOperation> potential = getChild(child);
113 Preconditions.checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
114 return potential.get();
118 public void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException {
119 if (modification.getType() == ModificationType.WRITE) {
120 verifyWrittenStructure(modification.getWrittenValue());
125 public final void checkApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
126 switch (modification.getType()) {
128 checkDeleteApplicable(modification, current);
129 case SUBTREE_MODIFIED:
130 checkSubtreeModificationApplicable(path, modification, current);
133 checkWriteApplicable(path, modification, current);
136 checkMergeApplicable(path, modification, current);
141 throw new UnsupportedOperationException("Suplied modification type "+ modification.getType()+ "is not supported.");
146 protected void checkMergeApplicable(final InstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
147 Optional<TreeNode> original = modification.getOriginal();
148 if (original.isPresent() && current.isPresent()) {
150 * We need to do conflict detection only and only if the value of leaf changed
151 * before two transactions. If value of leaf is unchanged between two transactions
152 * it should not cause transaction to fail, since result of this merge
153 * leads to same data.
155 if(!original.get().getData().equals(current.get().getData())) {
156 checkNotConflicting(path, original.get(), current.get());
161 protected void checkWriteApplicable(final InstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
162 Optional<TreeNode> original = modification.getOriginal();
163 if (original.isPresent() && current.isPresent()) {
164 checkNotConflicting(path, original.get(), current.get());
165 } else if(original.isPresent()) {
166 throw new ConflictingModificationAppliedException(path,"Node was deleted by other transaction.");
170 private void checkDeleteApplicable(final NodeModification modification, final Optional<TreeNode> current) {
171 // Delete is always applicable, we do not expose it to subclasses
172 if (current.isPresent()) {
173 LOG.trace("Delete operation turned to no-op on missing node {}", modification);
178 public final Optional<TreeNode> apply(final ModifiedNode modification,
179 final Optional<TreeNode> currentMeta, final Version version) {
181 switch (modification.getType()) {
183 return modification.storeSnapshot(Optional.<TreeNode> absent());
184 case SUBTREE_MODIFIED:
185 Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification",
187 return modification.storeSnapshot(Optional.of(applySubtreeChange(modification, currentMeta.get(),
190 if(currentMeta.isPresent()) {
191 return modification.storeSnapshot(Optional.of(applyMerge(modification,currentMeta.get(), version)));
192 } // Fallback to write is intentional - if node is not preexisting merge is same as write
194 return modification.storeSnapshot(Optional.of(applyWrite(modification, currentMeta, version)));
198 throw new IllegalArgumentException("Provided modification type is not supported.");
202 protected abstract TreeNode applyMerge(ModifiedNode modification,
203 TreeNode currentMeta, Version version);
205 protected abstract TreeNode applyWrite(ModifiedNode modification,
206 Optional<TreeNode> currentMeta, Version version);
208 protected abstract TreeNode applySubtreeChange(ModifiedNode modification,
209 TreeNode currentMeta, Version version);
213 * Checks is supplied {@link NodeModification} is applicable for Subtree Modification.
215 * @param path Path to current node
216 * @param modification Node modification which should be applied.
217 * @param current Current state of data tree
218 * @throws ConflictingModificationAppliedException If subtree was changed in conflicting way
219 * @throws IncorrectDataStructureException If subtree modification is not applicable (e.g. leaf node).
221 protected abstract void checkSubtreeModificationApplicable(InstanceIdentifier path, final NodeModification modification,
222 final Optional<TreeNode> current) throws DataValidationFailedException;
224 protected abstract void verifyWrittenStructure(NormalizedNode<?, ?> writtenValue);
226 public static class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
228 private final Optional<ModificationApplyOperation> entryStrategy;
230 protected UnkeyedListModificationStrategy(final ListSchemaNode schema) {
231 entryStrategy = Optional.<ModificationApplyOperation> of(new DataNodeContainerModificationStrategy.UnkeyedListItemModificationStrategy(schema));
235 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
236 final Version version) {
237 return applyWrite(modification, Optional.of(currentMeta), version);
241 protected TreeNode applySubtreeChange(final ModifiedNode modification,
242 final TreeNode currentMeta, final Version version) {
243 throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
247 protected TreeNode applyWrite(final ModifiedNode modification,
248 final Optional<TreeNode> currentMeta, final Version version) {
249 return TreeNodeFactory.createTreeNode(modification.getWrittenValue(), version);
253 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
254 if (child instanceof NodeIdentifier) {
255 return entryStrategy;
257 return Optional.absent();
261 protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
266 protected void checkSubtreeModificationApplicable(final InstanceIdentifier path, final NodeModification modification,
267 final Optional<TreeNode> current) throws IncorrectDataStructureException {
268 throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");