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.IncorrectDataStructureException;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 abstract class SchemaAwareApplyOperation extends ModificationApplyOperation {
37 private static final Logger LOG = LoggerFactory.getLogger(SchemaAwareApplyOperation.class);
39 public static ModificationApplyOperation from(final DataSchemaNode schemaNode, final TreeType treeType) {
40 if (treeType == TreeType.CONFIGURATION) {
41 Preconditions.checkArgument(schemaNode.isConfiguration(), "Supplied %s does not belongs to configuration tree.", schemaNode.getPath());
43 if (schemaNode instanceof ContainerSchemaNode) {
44 final ContainerSchemaNode containerSchema = (ContainerSchemaNode) schemaNode;
45 if (containerSchema.isPresenceContainer()) {
46 return new PresenceContainerModificationStrategy(containerSchema, treeType);
48 return new StructuralContainerModificationStrategy(containerSchema, treeType);
50 } else if (schemaNode instanceof ListSchemaNode) {
51 return fromListSchemaNode((ListSchemaNode) schemaNode, treeType);
52 } else if (schemaNode instanceof ChoiceSchemaNode) {
53 return new ChoiceModificationStrategy((ChoiceSchemaNode) schemaNode, treeType);
54 } else if (schemaNode instanceof LeafListSchemaNode) {
55 return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode, treeType);
56 } else if (schemaNode instanceof LeafSchemaNode) {
57 return new LeafModificationStrategy((LeafSchemaNode) schemaNode, treeType);
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, final TreeType treeType) {
64 for (final AugmentationSchema potential : augSchemas.getAvailableAugmentations()) {
65 for (final DataSchemaNode child : potential.getChildNodes()) {
66 if (identifier.getPossibleChildNames().contains(child.getQName())) {
67 return new AugmentationModificationStrategy(potential, resolvedTree, treeType);
75 public static boolean checkConflicting(final YangInstanceIdentifier path, final boolean condition, final String message) throws ConflictingModificationAppliedException {
77 throw new ConflictingModificationAppliedException(path, message);
82 private static SchemaAwareApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode, final TreeType treeType) {
83 final List<QName> keyDefinition = schemaNode.getKeyDefinition();
84 final SchemaAwareApplyOperation op;
85 if (keyDefinition == null || keyDefinition.isEmpty()) {
86 op = new UnkeyedListModificationStrategy(schemaNode, treeType);
87 } else if (schemaNode.isUserOrdered()) {
88 op = new OrderedMapModificationStrategy(schemaNode, treeType);
90 op = new UnorderedMapModificationStrategy(schemaNode, treeType);
92 return MinMaxElementsValidation.from(op, schemaNode);
95 private static SchemaAwareApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode, final TreeType treeType) {
96 final SchemaAwareApplyOperation op;
97 if(schemaNode.isUserOrdered()) {
98 op = new OrderedLeafSetModificationStrategy(schemaNode, treeType);
100 op = new UnorderedLeafSetModificationStrategy(schemaNode, treeType);
102 return MinMaxElementsValidation.from(op, schemaNode);
105 protected static final void checkNotConflicting(final YangInstanceIdentifier path, final TreeNode original, final TreeNode current) throws ConflictingModificationAppliedException {
106 checkConflicting(path, original.getVersion().equals(current.getVersion()),
107 "Node was replaced by other transaction.");
108 checkConflicting(path, original.getSubtreeVersion().equals(current.getSubtreeVersion()),
109 "Node children was modified by other transaction");
112 protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) {
113 final Optional<ModificationApplyOperation> potential = getChild(child);
114 Preconditions.checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
115 return potential.get();
119 final void checkApplicable(final YangInstanceIdentifier path,final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
120 switch (modification.getOperation()) {
122 checkDeleteApplicable(modification, current);
125 checkTouchApplicable(path, modification, current);
128 checkWriteApplicable(path, modification, current);
131 checkMergeApplicable(path, modification, current);
136 throw new UnsupportedOperationException("Suplied modification type "+ modification.getOperation()+ " is not supported.");
140 protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
141 final Optional<TreeNode> original = modification.getOriginal();
142 if (original.isPresent() && current.isPresent()) {
144 * We need to do conflict detection only and only if the value of leaf changed
145 * before two transactions. If value of leaf is unchanged between two transactions
146 * it should not cause transaction to fail, since result of this merge
147 * leads to same data.
149 if(!original.get().getData().equals(current.get().getData())) {
150 checkNotConflicting(path, original.get(), current.get());
156 * Checks if write operation can be applied to current TreeNode.
157 * The operation checks if original tree node to which the modification is going to be applied exists and if
158 * current node in TreeNode structure exists.
160 * @param path Path from current node in TreeNode
161 * @param modification modification to apply
162 * @param current current node in TreeNode for modification to apply
163 * @throws DataValidationFailedException
165 protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification,
166 final Optional<TreeNode> current) throws DataValidationFailedException {
167 final Optional<TreeNode> original = modification.getOriginal();
168 if (original.isPresent() && current.isPresent()) {
169 checkNotConflicting(path, original.get(), current.get());
170 } else if(original.isPresent()) {
171 throw new ConflictingModificationAppliedException(path,"Node was deleted by other transaction.");
172 } else if(current.isPresent()) {
173 throw new ConflictingModificationAppliedException(path,"Node was created by other transaction.");
177 private static void checkDeleteApplicable(final NodeModification modification, final Optional<TreeNode> current) {
178 // Delete is always applicable, we do not expose it to subclasses
179 if (!current.isPresent()) {
180 LOG.trace("Delete operation turned to no-op on missing node {}", modification);
185 protected ChildTrackingPolicy getChildPolicy() {
186 return ChildTrackingPolicy.UNORDERED;
190 final Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> currentMeta, final Version version) {
191 switch (modification.getOperation()) {
193 // Deletion of a non-existing node is a no-op, report it as such
194 modification.resolveModificationType(currentMeta.isPresent() ? ModificationType.DELETE : ModificationType.UNMODIFIED);
195 return modification.setSnapshot(Optional.<TreeNode> absent());
197 Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification %s",
199 return modification.setSnapshot(Optional.of(applyTouch(modification, currentMeta.get(),
202 final TreeNode result;
204 // This is a slight optimization: a merge on a non-existing node equals to a write
205 if (currentMeta.isPresent()) {
206 result = applyMerge(modification,currentMeta.get(), version);
208 modification.resolveModificationType(ModificationType.WRITE);
209 result = applyWrite(modification, currentMeta, version);
212 return modification.setSnapshot(Optional.of(result));
214 modification.resolveModificationType(ModificationType.WRITE);
215 return modification.setSnapshot(Optional.of(applyWrite(modification, currentMeta, version)));
217 modification.resolveModificationType(ModificationType.UNMODIFIED);
220 throw new IllegalArgumentException("Provided modification type is not supported.");
225 * Apply a merge operation. Since the result of merge differs based on the data type
226 * being modified, implementations of this method are responsible for calling
227 * {@link ModifiedNode#resolveModificationType(ModificationType)} as appropriate.
229 * @param modification Modified node
230 * @param currentMeta Store Metadata Node on which NodeModification should be applied
231 * @param version New subtree version of parent node
232 * @return A sealed TreeNode representing applied operation.
234 protected abstract TreeNode applyMerge(ModifiedNode modification, TreeNode currentMeta, Version version);
236 protected abstract TreeNode applyWrite(ModifiedNode modification, Optional<TreeNode> currentMeta, Version version);
239 * Apply a nested operation. Since there may not actually be a nested operation
240 * to be applied, implementations of this method are responsible for calling
241 * {@link ModifiedNode#resolveModificationType(ModificationType)} as appropriate.
243 * @param modification Modified node
244 * @param currentMeta Store Metadata Node on which NodeModification should be applied
245 * @param version New subtree version of parent node
246 * @return A sealed TreeNode representing applied operation.
248 protected abstract TreeNode applyTouch(ModifiedNode modification, TreeNode currentMeta, Version version);
252 * Checks is supplied {@link NodeModification} is applicable for Subtree Modification.
254 * @param path Path to current node
255 * @param modification Node modification which should be applied.
256 * @param current Current state of data tree
257 * @throws ConflictingModificationAppliedException If subtree was changed in conflicting way
258 * @throws IncorrectDataStructureException If subtree modification is not applicable (e.g. leaf node).
260 protected abstract void checkTouchApplicable(YangInstanceIdentifier path, final NodeModification modification,
261 final Optional<TreeNode> current) throws DataValidationFailedException;
264 * Checks if supplied schema node belong to specified Data Tree type. All nodes belong to the operational tree,
265 * nodes in configuration tree are marked as such.
267 * @param treeType Tree Type
268 * @param node Schema node
271 static boolean belongsToTree(final TreeType treeType, final DataSchemaNode node) {
272 return treeType == TreeType.OPERATIONAL || node.isConfiguration();