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;
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.DataTreeConfiguration;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
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,
40 final DataTreeConfiguration treeConfig) {
41 if (treeConfig.getTreeType() == TreeType.CONFIGURATION) {
42 Preconditions.checkArgument(schemaNode.isConfiguration(),
43 "Supplied %s does not belongs to configuration tree.", schemaNode.getPath());
45 if (schemaNode instanceof ContainerSchemaNode) {
46 final ContainerSchemaNode containerSchema = (ContainerSchemaNode) schemaNode;
47 if (containerSchema.isPresenceContainer()) {
48 return new PresenceContainerModificationStrategy(containerSchema, treeConfig);
51 return new StructuralContainerModificationStrategy(containerSchema, treeConfig);
52 } else if (schemaNode instanceof ListSchemaNode) {
53 return fromListSchemaNode((ListSchemaNode) schemaNode, treeConfig);
54 } else if (schemaNode instanceof ChoiceSchemaNode) {
55 return new ChoiceModificationStrategy((ChoiceSchemaNode) schemaNode, treeConfig);
56 } else if (schemaNode instanceof LeafListSchemaNode) {
57 return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode, treeConfig);
58 } else if (schemaNode instanceof LeafSchemaNode) {
59 return new LeafModificationStrategy((LeafSchemaNode) schemaNode);
61 throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
64 public static SchemaAwareApplyOperation from(final DataNodeContainer resolvedTree,
65 final AugmentationTarget augSchemas, final AugmentationIdentifier identifier,
66 final DataTreeConfiguration treeConfig) {
67 for (final AugmentationSchema potential : augSchemas.getAvailableAugmentations()) {
68 for (final DataSchemaNode child : potential.getChildNodes()) {
69 if (identifier.getPossibleChildNames().contains(child.getQName())) {
70 return new AugmentationModificationStrategy(potential, resolvedTree, treeConfig);
78 public static void checkConflicting(final YangInstanceIdentifier path, final boolean condition,
79 final String message) throws ConflictingModificationAppliedException {
81 throw new ConflictingModificationAppliedException(path, message);
85 private static SchemaAwareApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode,
86 final DataTreeConfiguration treeConfig) {
87 final List<QName> keyDefinition = schemaNode.getKeyDefinition();
88 final SchemaAwareApplyOperation op;
89 if (keyDefinition == null || keyDefinition.isEmpty()) {
90 op = new UnkeyedListModificationStrategy(schemaNode, treeConfig);
91 } else if (schemaNode.isUserOrdered()) {
92 op = new OrderedMapModificationStrategy(schemaNode, treeConfig);
94 op = new UnorderedMapModificationStrategy(schemaNode, treeConfig);
96 return MinMaxElementsValidation.from(op, schemaNode);
99 private static SchemaAwareApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode,
100 final DataTreeConfiguration treeConfig) {
101 final SchemaAwareApplyOperation op;
102 if (schemaNode.isUserOrdered()) {
103 op = new OrderedLeafSetModificationStrategy(schemaNode, treeConfig);
105 op = new UnorderedLeafSetModificationStrategy(schemaNode, treeConfig);
107 return MinMaxElementsValidation.from(op, schemaNode);
110 protected static void checkNotConflicting(final YangInstanceIdentifier path, final TreeNode original,
111 final TreeNode current) throws ConflictingModificationAppliedException {
112 checkConflicting(path, original.getVersion().equals(current.getVersion()),
113 "Node was replaced by other transaction.");
114 checkConflicting(path, original.getSubtreeVersion().equals(current.getSubtreeVersion()),
115 "Node children was modified by other transaction");
118 protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) {
119 final Optional<ModificationApplyOperation> potential = getChild(child);
120 Preconditions.checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
121 return potential.get();
125 final void checkApplicable(final YangInstanceIdentifier path,final NodeModification modification,
126 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
127 switch (modification.getOperation()) {
129 checkDeleteApplicable(modification, current);
132 checkTouchApplicable(path, modification, current, version);
135 checkWriteApplicable(path, modification, current, version);
138 checkMergeApplicable(path, modification, current, version);
143 throw new UnsupportedOperationException(
144 "Suplied modification type " + modification.getOperation() + " is not supported.");
148 protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
149 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
150 final Optional<TreeNode> original = modification.getOriginal();
151 if (original.isPresent() && current.isPresent()) {
153 * We need to do conflict detection only and only if the value of leaf changed
154 * before two transactions. If value of leaf is unchanged between two transactions
155 * it should not cause transaction to fail, since result of this merge
156 * leads to same data.
158 if (!original.get().getData().equals(current.get().getData())) {
159 checkNotConflicting(path, original.get(), current.get());
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 YangInstanceIdentifier 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());
179 } else if (original.isPresent()) {
180 throw new ConflictingModificationAppliedException(path, "Node was deleted by other transaction.");
181 } else if (current.isPresent()) {
182 throw new ConflictingModificationAppliedException(path, "Node was created by other transaction.");
186 private static void checkDeleteApplicable(final NodeModification modification, final Optional<TreeNode> current) {
187 // Delete is always applicable, we do not expose it to subclasses
188 if (!current.isPresent()) {
189 LOG.trace("Delete operation turned to no-op on missing node {}", modification);
194 protected ChildTrackingPolicy getChildPolicy() {
195 return ChildTrackingPolicy.UNORDERED;
199 final Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
200 final Version version) {
201 switch (modification.getOperation()) {
203 // Deletion of a non-existing node is a no-op, report it as such
204 modification.resolveModificationType(currentMeta.isPresent() ? ModificationType.DELETE
205 : ModificationType.UNMODIFIED);
206 return modification.setSnapshot(Optional.empty());
208 Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification %s",
210 return modification.setSnapshot(Optional.of(applyTouch(modification, currentMeta.get(),
213 final TreeNode result;
215 if (!currentMeta.isPresent()) {
216 // This is a slight optimization: a merge on a non-existing node equals to a write. Written data
217 // structure is usually verified when the transaction is sealed. To preserve correctness, we have
218 // to run that validation here.
219 modification.resolveModificationType(ModificationType.WRITE);
220 result = applyWrite(modification, currentMeta, version);
221 verifyStructure(result.getData(), true);
223 result = applyMerge(modification, currentMeta.get(), version);
226 return modification.setSnapshot(Optional.of(result));
228 modification.resolveModificationType(ModificationType.WRITE);
229 return modification.setSnapshot(Optional.of(applyWrite(modification, currentMeta, version)));
231 modification.resolveModificationType(ModificationType.UNMODIFIED);
234 throw new IllegalArgumentException("Provided modification type is not supported.");
239 * Apply a merge operation. Since the result of merge differs based on the data type
240 * being modified, 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 applyMerge(ModifiedNode modification, TreeNode currentMeta, Version version);
250 protected abstract TreeNode applyWrite(ModifiedNode modification, Optional<TreeNode> currentMeta, Version version);
253 * Apply a nested operation. Since there may not actually be a nested operation
254 * to be applied, implementations of this method are responsible for calling
255 * {@link ModifiedNode#resolveModificationType(ModificationType)} as appropriate.
257 * @param modification Modified node
258 * @param currentMeta Store Metadata Node on which NodeModification should be applied
259 * @param version New subtree version of parent node
260 * @return A sealed TreeNode representing applied operation.
262 protected abstract TreeNode applyTouch(ModifiedNode modification, TreeNode currentMeta, Version version);
265 * Checks is supplied {@link NodeModification} is applicable for Subtree Modification.
267 * @param path Path to current node
268 * @param modification Node modification which should be applied.
269 * @param current Current state of data tree
270 * @throws ConflictingModificationAppliedException If subtree was changed in conflicting way
271 * @throws org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException If subtree
272 * modification is not applicable (e.g. leaf node).
274 protected abstract void checkTouchApplicable(YangInstanceIdentifier path, NodeModification modification,
275 Optional<TreeNode> current, Version version) throws DataValidationFailedException;
278 * Checks if supplied schema node belong to specified Data Tree type. All nodes belong to the operational tree,
279 * nodes in configuration tree are marked as such.
281 * @param treeType Tree Type
282 * @param node Schema node
283 * @return {@code true} if the node matches the tree type, {@code false} otherwise.
285 static boolean belongsToTree(final TreeType treeType, final DataSchemaNode node) {
286 return treeType == TreeType.OPERATIONAL || node.isConfiguration();