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 java.util.List;
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.PathArgument;
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.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.AugmentationSchema;
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, final TreeType treeType) {
39 if (treeType == TreeType.CONFIGURATION) {
40 Preconditions.checkArgument(schemaNode.isConfiguration(), "Supplied %s does not belongs to configuration tree.", schemaNode.getPath());
42 if (schemaNode instanceof ContainerSchemaNode) {
43 final ContainerSchemaNode containerSchema = (ContainerSchemaNode) schemaNode;
44 if (containerSchema.isPresenceContainer()) {
45 return new PresenceContainerModificationStrategy(containerSchema, treeType);
47 return new StructuralContainerModificationStrategy(containerSchema, treeType);
49 } else if (schemaNode instanceof ListSchemaNode) {
50 return fromListSchemaNode((ListSchemaNode) schemaNode, treeType);
51 } else if (schemaNode instanceof ChoiceSchemaNode) {
52 return new ChoiceModificationStrategy((ChoiceSchemaNode) schemaNode, treeType);
53 } else if (schemaNode instanceof LeafListSchemaNode) {
54 return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode, treeType);
55 } else if (schemaNode instanceof LeafSchemaNode) {
56 return new LeafModificationStrategy((LeafSchemaNode) schemaNode, treeType);
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, final TreeType treeType) {
63 for (final AugmentationSchema potential : augSchemas.getAvailableAugmentations()) {
64 for (final DataSchemaNode child : potential.getChildNodes()) {
65 if (identifier.getPossibleChildNames().contains(child.getQName())) {
66 return new AugmentationModificationStrategy(potential, resolvedTree, treeType);
74 public static boolean checkConflicting(final YangInstanceIdentifier path, final boolean condition, final String message) throws ConflictingModificationAppliedException {
76 throw new ConflictingModificationAppliedException(path, message);
81 private static SchemaAwareApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode, final TreeType treeType) {
82 final List<QName> keyDefinition = schemaNode.getKeyDefinition();
83 final SchemaAwareApplyOperation op;
84 if (keyDefinition == null || keyDefinition.isEmpty()) {
85 op = new UnkeyedListModificationStrategy(schemaNode, treeType);
86 } else if (schemaNode.isUserOrdered()) {
87 op = new OrderedMapModificationStrategy(schemaNode, treeType);
89 op = new UnorderedMapModificationStrategy(schemaNode, treeType);
91 return MinMaxElementsValidation.from(op, schemaNode);
94 private static SchemaAwareApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode, final TreeType treeType) {
95 final SchemaAwareApplyOperation op;
96 if(schemaNode.isUserOrdered()) {
97 op = new OrderedLeafSetModificationStrategy(schemaNode, treeType);
99 op = new UnorderedLeafSetModificationStrategy(schemaNode, treeType);
101 return MinMaxElementsValidation.from(op, schemaNode);
104 protected static final void checkNotConflicting(final YangInstanceIdentifier 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 final Optional<ModificationApplyOperation> potential = getChild(child);
113 Preconditions.checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
114 return potential.get();
118 final void checkApplicable(final YangInstanceIdentifier path,final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
119 switch (modification.getOperation()) {
121 checkDeleteApplicable(modification, current);
124 checkTouchApplicable(path, modification, current);
127 checkWriteApplicable(path, modification, current);
130 checkMergeApplicable(path, modification, current);
135 throw new UnsupportedOperationException("Suplied modification type "+ modification.getOperation()+ " is not supported.");
139 protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
140 final Optional<TreeNode> original = modification.getOriginal();
141 if (original.isPresent() && current.isPresent()) {
143 * We need to do conflict detection only and only if the value of leaf changed
144 * before two transactions. If value of leaf is unchanged between two transactions
145 * it should not cause transaction to fail, since result of this merge
146 * leads to same data.
148 if(!original.get().getData().equals(current.get().getData())) {
149 checkNotConflicting(path, original.get(), current.get());
155 * Checks if write operation can be applied to current TreeNode.
156 * The operation checks if original tree node to which the modification is going to be applied exists and if
157 * current node in TreeNode structure exists.
159 * @param path Path from current node in TreeNode
160 * @param modification modification to apply
161 * @param current current node in TreeNode for modification to apply
162 * @throws DataValidationFailedException
164 protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification,
165 final Optional<TreeNode> current) throws DataValidationFailedException {
166 final Optional<TreeNode> original = modification.getOriginal();
167 if (original.isPresent() && current.isPresent()) {
168 checkNotConflicting(path, original.get(), current.get());
169 } else if(original.isPresent()) {
170 throw new ConflictingModificationAppliedException(path,"Node was deleted by other transaction.");
171 } else if (current.isPresent()) {
172 throw new ConflictingModificationAppliedException(path, "Node was created by other transaction.");
176 private static void checkDeleteApplicable(final NodeModification modification, final Optional<TreeNode> current) {
177 // Delete is always applicable, we do not expose it to subclasses
178 if (!current.isPresent()) {
179 LOG.trace("Delete operation turned to no-op on missing node {}", modification);
184 protected ChildTrackingPolicy getChildPolicy() {
185 return ChildTrackingPolicy.UNORDERED;
189 final Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> currentMeta, final Version version) {
190 switch (modification.getOperation()) {
192 // Deletion of a non-existing node is a no-op, report it as such
193 modification.resolveModificationType(currentMeta.isPresent() ? ModificationType.DELETE : ModificationType.UNMODIFIED);
194 return modification.setSnapshot(Optional.<TreeNode> absent());
196 Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification %s",
198 return modification.setSnapshot(Optional.of(applyTouch(modification, currentMeta.get(),
201 final TreeNode result;
203 // This is a slight optimization: a merge on a non-existing node equals to a write
204 if (currentMeta.isPresent()) {
205 result = applyMerge(modification, currentMeta.get(), version);
207 modification.resolveModificationType(ModificationType.WRITE);
208 result = applyWrite(modification, currentMeta, version);
211 return modification.setSnapshot(Optional.of(result));
213 modification.resolveModificationType(ModificationType.WRITE);
214 return modification.setSnapshot(Optional.of(applyWrite(modification, currentMeta, version)));
216 modification.resolveModificationType(ModificationType.UNMODIFIED);
219 throw new IllegalArgumentException("Provided modification type is not supported.");
224 * Apply a merge operation. Since the result of merge differs based on the data type
225 * being modified, implementations of this method are responsible for calling
226 * {@link ModifiedNode#resolveModificationType(ModificationType)} as appropriate.
228 * @param modification Modified node
229 * @param currentMeta Store Metadata Node on which NodeModification should be applied
230 * @param version New subtree version of parent node
231 * @return A sealed TreeNode representing applied operation.
233 protected abstract TreeNode applyMerge(ModifiedNode modification, TreeNode currentMeta, Version version);
235 protected abstract TreeNode applyWrite(ModifiedNode modification, Optional<TreeNode> currentMeta, Version version);
238 * Apply a nested operation. Since there may not actually be a nested operation
239 * to be applied, 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 applyTouch(ModifiedNode modification, TreeNode currentMeta, Version version);
251 * Checks is supplied {@link NodeModification} is applicable for Subtree Modification.
253 * @param path Path to current node
254 * @param modification Node modification which should be applied.
255 * @param current Current state of data tree
256 * @throws ConflictingModificationAppliedException If subtree was changed in conflicting way
257 * @throws IncorrectDataStructureException If subtree modification is not applicable (e.g. leaf node).
259 protected abstract void checkTouchApplicable(YangInstanceIdentifier path, final NodeModification modification,
260 final Optional<TreeNode> current) throws DataValidationFailedException;
263 * Checks if supplied schema node belong to specified Data Tree type. All nodes belong to the operational tree,
264 * nodes in configuration tree are marked as such.
266 * @param treeType Tree Type
267 * @param node Schema node
270 static boolean belongsToTree(final TreeType treeType, final DataSchemaNode node) {
271 return treeType == TreeType.OPERATIONAL || node.isConfiguration();