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.Preconditions;
11 import java.util.List;
12 import java.util.Optional;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
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.TreeType;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 abstract class SchemaAwareApplyOperation extends ModificationApplyOperation {
36 private static final Logger LOG = LoggerFactory.getLogger(SchemaAwareApplyOperation.class);
38 public static ModificationApplyOperation from(final DataSchemaNode schemaNode,
39 final DataTreeConfiguration treeConfig) {
40 if (treeConfig.getTreeType() == TreeType.CONFIGURATION) {
41 Preconditions.checkArgument(schemaNode.isConfiguration(),
42 "Supplied %s does not belongs to configuration tree.", schemaNode.getPath());
44 if (schemaNode instanceof ContainerSchemaNode) {
45 final ContainerSchemaNode containerSchema = (ContainerSchemaNode) schemaNode;
46 return containerSchema.isPresenceContainer()
47 ? new PresenceContainerModificationStrategy(containerSchema, treeConfig)
48 : new StructuralContainerModificationStrategy(containerSchema, treeConfig);
49 } else if (schemaNode instanceof ListSchemaNode) {
50 return fromListSchemaNode((ListSchemaNode) schemaNode, treeConfig);
51 } else if (schemaNode instanceof ChoiceSchemaNode) {
52 return new ChoiceModificationStrategy((ChoiceSchemaNode) schemaNode, treeConfig);
53 } else if (schemaNode instanceof LeafListSchemaNode) {
54 return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode, treeConfig);
55 } else if (schemaNode instanceof LeafSchemaNode) {
56 return new LeafModificationStrategy((LeafSchemaNode) schemaNode);
58 throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
61 public static SchemaAwareApplyOperation from(final DataNodeContainer resolvedTree,
62 final AugmentationTarget augSchemas, final AugmentationIdentifier identifier,
63 final DataTreeConfiguration treeConfig) {
64 for (final AugmentationSchemaNode potential : augSchemas.getAvailableAugmentations()) {
65 for (final DataSchemaNode child : potential.getChildNodes()) {
66 if (identifier.getPossibleChildNames().contains(child.getQName())) {
67 return new AugmentationModificationStrategy(potential, resolvedTree, treeConfig);
75 static void checkConflicting(final ModificationPath path, final boolean condition, final String message)
76 throws ConflictingModificationAppliedException {
78 throw new ConflictingModificationAppliedException(path.toInstanceIdentifier(), message);
82 private static ModificationApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode,
83 final DataTreeConfiguration treeConfig) {
84 final List<QName> keyDefinition = schemaNode.getKeyDefinition();
85 final SchemaAwareApplyOperation op;
86 if (keyDefinition == null || keyDefinition.isEmpty()) {
87 op = new UnkeyedListModificationStrategy(schemaNode, treeConfig);
88 } else if (schemaNode.isUserOrdered()) {
89 op = new OrderedMapModificationStrategy(schemaNode, treeConfig);
91 op = new UnorderedMapModificationStrategy(schemaNode, treeConfig);
94 return MinMaxElementsValidation.from(op, schemaNode);
97 private static SchemaAwareApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode,
98 final DataTreeConfiguration treeConfig) {
99 final SchemaAwareApplyOperation op;
100 if (schemaNode.isUserOrdered()) {
101 op = new OrderedLeafSetModificationStrategy(schemaNode, treeConfig);
103 op = new UnorderedLeafSetModificationStrategy(schemaNode, treeConfig);
105 return MinMaxElementsValidation.from(op, schemaNode);
108 protected static void checkNotConflicting(final ModificationPath path, final TreeNode original,
109 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 final Optional<ModificationApplyOperation> potential = getChild(child);
118 Preconditions.checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
119 return potential.get();
123 void checkApplicable(final ModificationPath path, final NodeModification modification,
124 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
125 switch (modification.getOperation()) {
127 checkDeleteApplicable(modification, current);
130 checkTouchApplicable(path, modification, current, version);
133 checkWriteApplicable(path, modification, current, version);
136 checkMergeApplicable(path, modification, current, version);
141 throw new UnsupportedOperationException(
142 "Suplied modification type " + modification.getOperation() + " is not supported.");
146 protected void checkMergeApplicable(final ModificationPath path, final NodeModification modification,
147 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
148 final Optional<TreeNode> original = modification.getOriginal();
149 if (original.isPresent() && current.isPresent()) {
151 * We need to do conflict detection only and only if the value of leaf changed
152 * before two transactions. If value of leaf is unchanged between two transactions
153 * it should not cause transaction to fail, since result of this merge
154 * leads to same data.
156 final TreeNode orig = original.get();
157 final TreeNode cur = current.get();
158 if (!orig.getData().equals(cur.getData())) {
159 checkNotConflicting(path, orig, cur);
165 * Checks if write operation can be applied to current TreeNode.
166 * The operation checks if original tree node to which the modification is going to be applied exists and if
167 * current node in TreeNode structure exists.
169 * @param path Path from current node in TreeNode
170 * @param modification modification to apply
171 * @param current current node in TreeNode for modification to apply
172 * @throws DataValidationFailedException when a data dependency conflict is detected
174 protected void checkWriteApplicable(final ModificationPath path, final NodeModification modification,
175 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
176 final Optional<TreeNode> original = modification.getOriginal();
177 if (original.isPresent() && current.isPresent()) {
178 checkNotConflicting(path, original.get(), current.get());
180 checkConflicting(path, !original.isPresent(), "Node was deleted by other transaction.");
181 checkConflicting(path, !current.isPresent(), "Node was created by other transaction.");
185 private static void checkDeleteApplicable(final NodeModification modification, final Optional<TreeNode> current) {
186 // Delete is always applicable, we do not expose it to subclasses
187 if (!current.isPresent()) {
188 LOG.trace("Delete operation turned to no-op on missing node {}", modification);
193 protected ChildTrackingPolicy getChildPolicy() {
194 return ChildTrackingPolicy.UNORDERED;
198 Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
199 final Version version) {
200 switch (modification.getOperation()) {
202 // Deletion of a non-existing node is a no-op, report it as such
203 modification.resolveModificationType(currentMeta.isPresent() ? ModificationType.DELETE
204 : ModificationType.UNMODIFIED);
205 return modification.setSnapshot(Optional.empty());
207 Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification %s",
209 return modification.setSnapshot(Optional.of(applyTouch(modification, currentMeta.get(),
212 final TreeNode result;
214 if (!currentMeta.isPresent()) {
215 // This is a slight optimization: a merge on a non-existing node equals to a write. Written data
216 // structure is usually verified when the transaction is sealed. To preserve correctness, we have
217 // to run that validation here.
218 modification.resolveModificationType(ModificationType.WRITE);
219 result = applyWrite(modification, currentMeta, version);
220 verifyStructure(result.getData(), true);
222 result = applyMerge(modification, currentMeta.get(), version);
225 return modification.setSnapshot(Optional.of(result));
227 modification.resolveModificationType(ModificationType.WRITE);
228 return modification.setSnapshot(Optional.of(applyWrite(modification, currentMeta, version)));
230 modification.resolveModificationType(ModificationType.UNMODIFIED);
233 throw new IllegalArgumentException("Provided modification type is not supported.");
238 * Apply a merge operation. Since the result of merge differs based on the data type
239 * being modified, implementations of this method are responsible for calling
240 * {@link ModifiedNode#resolveModificationType(ModificationType)} as appropriate.
242 * @param modification Modified node
243 * @param currentMeta Store Metadata Node on which NodeModification should be applied
244 * @param version New subtree version of parent node
245 * @return A sealed TreeNode representing applied operation.
247 protected abstract TreeNode applyMerge(ModifiedNode modification, TreeNode currentMeta, Version version);
249 protected abstract TreeNode applyWrite(ModifiedNode modification, Optional<TreeNode> currentMeta, Version version);
252 * Apply a nested operation. Since there may not actually be a nested operation
253 * to be applied, implementations of this method are responsible for calling
254 * {@link ModifiedNode#resolveModificationType(ModificationType)} as appropriate.
256 * @param modification Modified node
257 * @param currentMeta Store Metadata Node on which NodeModification should be applied
258 * @param version New subtree version of parent node
259 * @return A sealed TreeNode representing applied operation.
261 protected abstract TreeNode applyTouch(ModifiedNode modification, TreeNode currentMeta, Version version);
264 * Checks is supplied {@link NodeModification} is applicable for Subtree Modification.
266 * @param path Path to current node
267 * @param modification Node modification which should be applied.
268 * @param current Current state of data tree
269 * @throws ConflictingModificationAppliedException If subtree was changed in conflicting way
270 * @throws org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException If subtree
271 * modification is not applicable (e.g. leaf node).
273 protected abstract void checkTouchApplicable(ModificationPath path, NodeModification modification,
274 Optional<TreeNode> current, Version version) throws DataValidationFailedException;
277 * Checks if supplied schema node belong to specified Data Tree type. All nodes belong to the operational tree,
278 * nodes in configuration tree are marked as such.
280 * @param treeType Tree Type
281 * @param node Schema node
282 * @return {@code true} if the node matches the tree type, {@code false} otherwise.
284 static boolean belongsToTree(final TreeType treeType, final DataSchemaNode node) {
285 return treeType == TreeType.OPERATIONAL || node.isConfiguration();