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.NormalizedNode;
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.AugmentationSchemaNode;
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 return ContainerModificationStrategy.of((ContainerSchemaNode) schemaNode, treeConfig);
47 } else if (schemaNode instanceof ListSchemaNode) {
48 return fromListSchemaNode((ListSchemaNode) schemaNode, treeConfig);
49 } else if (schemaNode instanceof ChoiceSchemaNode) {
50 return new ChoiceModificationStrategy((ChoiceSchemaNode) schemaNode, treeConfig);
51 } else if (schemaNode instanceof LeafListSchemaNode) {
52 return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode, treeConfig);
53 } else if (schemaNode instanceof LeafSchemaNode) {
54 return new LeafModificationStrategy((LeafSchemaNode) schemaNode);
56 throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
59 public static SchemaAwareApplyOperation from(final DataNodeContainer resolvedTree,
60 final AugmentationTarget augSchemas, final AugmentationIdentifier identifier,
61 final DataTreeConfiguration treeConfig) {
62 for (final AugmentationSchemaNode potential : augSchemas.getAvailableAugmentations()) {
63 for (final DataSchemaNode child : potential.getChildNodes()) {
64 if (identifier.getPossibleChildNames().contains(child.getQName())) {
65 return new AugmentationModificationStrategy(potential, resolvedTree, treeConfig);
73 static void checkConflicting(final ModificationPath path, final boolean condition, final String message)
74 throws ConflictingModificationAppliedException {
76 throw new ConflictingModificationAppliedException(path.toInstanceIdentifier(), message);
80 private static ModificationApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode,
81 final DataTreeConfiguration treeConfig) {
82 final List<QName> keyDefinition = schemaNode.getKeyDefinition();
83 final SchemaAwareApplyOperation op;
84 if (keyDefinition == null || keyDefinition.isEmpty()) {
85 op = new UnkeyedListModificationStrategy(schemaNode, treeConfig);
86 } else if (schemaNode.isUserOrdered()) {
87 op = new OrderedMapModificationStrategy(schemaNode, treeConfig);
89 op = new UnorderedMapModificationStrategy(schemaNode, treeConfig);
91 return MinMaxElementsValidation.from(op, schemaNode);
94 private static ModificationApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode,
95 final DataTreeConfiguration treeConfig) {
96 final SchemaAwareApplyOperation op;
97 if (schemaNode.isUserOrdered()) {
98 op = new OrderedLeafSetModificationStrategy(schemaNode, treeConfig);
100 op = new UnorderedLeafSetModificationStrategy(schemaNode, treeConfig);
102 return MinMaxElementsValidation.from(op, schemaNode);
105 protected static void checkNotConflicting(final ModificationPath path, final TreeNode original,
106 final TreeNode current) throws ConflictingModificationAppliedException {
107 checkConflicting(path, original.getVersion().equals(current.getVersion()),
108 "Node was replaced by other transaction.");
109 checkConflicting(path, original.getSubtreeVersion().equals(current.getSubtreeVersion()),
110 "Node children was modified by other transaction");
113 protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) {
114 final Optional<ModificationApplyOperation> potential = getChild(child);
115 Preconditions.checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
116 return potential.get();
120 final void checkApplicable(final ModificationPath path, final NodeModification modification,
121 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
122 switch (modification.getOperation()) {
124 checkDeleteApplicable(modification, current);
127 checkTouchApplicable(path, modification, current, version);
130 checkWriteApplicable(path, modification, current, version);
133 checkMergeApplicable(path, modification, current, version);
138 throw new UnsupportedOperationException(
139 "Suplied modification type " + modification.getOperation() + " is not supported.");
143 protected void checkMergeApplicable(final ModificationPath path, final NodeModification modification,
144 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
145 final Optional<TreeNode> original = modification.getOriginal();
146 if (original.isPresent() && current.isPresent()) {
148 * We need to do conflict detection only and only if the value of leaf changed
149 * before two transactions. If value of leaf is unchanged between two transactions
150 * it should not cause transaction to fail, since result of this merge
151 * leads to same data.
153 final TreeNode orig = original.get();
154 final TreeNode cur = current.get();
155 if (!orig.getData().equals(cur.getData())) {
156 checkNotConflicting(path, orig, cur);
162 * Checks if write operation can be applied to current TreeNode.
163 * The operation checks if original tree node to which the modification is going to be applied exists and if
164 * current node in TreeNode structure exists.
166 * @param path Path from current node in TreeNode
167 * @param modification modification to apply
168 * @param current current node in TreeNode for modification to apply
169 * @throws DataValidationFailedException when a data dependency conflict is detected
171 private static void checkWriteApplicable(final ModificationPath path, final NodeModification modification,
172 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
173 final Optional<TreeNode> original = modification.getOriginal();
174 if (original.isPresent() && current.isPresent()) {
175 checkNotConflicting(path, original.get(), current.get());
177 checkConflicting(path, !original.isPresent(), "Node was deleted by other transaction.");
178 checkConflicting(path, !current.isPresent(), "Node was created by other transaction.");
182 private static void checkDeleteApplicable(final NodeModification modification, final Optional<TreeNode> current) {
183 // Delete is always applicable, we do not expose it to subclasses
184 if (!current.isPresent()) {
185 LOG.trace("Delete operation turned to no-op on missing node {}", modification);
190 protected ChildTrackingPolicy getChildPolicy() {
191 return ChildTrackingPolicy.UNORDERED;
195 final Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
196 final Version version) {
197 switch (modification.getOperation()) {
199 // Deletion of a non-existing node is a no-op, report it as such
200 modification.resolveModificationType(currentMeta.isPresent() ? ModificationType.DELETE
201 : ModificationType.UNMODIFIED);
202 return modification.setSnapshot(Optional.empty());
204 Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification %s",
206 return modification.setSnapshot(Optional.of(applyTouch(modification, currentMeta.get(),
209 final TreeNode result;
211 if (!currentMeta.isPresent()) {
212 // This is a slight optimization: a merge on a non-existing node equals to a write. Written data
213 // structure is usually verified when the transaction is sealed. To preserve correctness, we have
214 // to run that validation here.
215 modification.resolveModificationType(ModificationType.WRITE);
216 result = applyWrite(modification, modification.getWrittenValue(), currentMeta, version);
217 verifyStructure(result.getData(), true);
219 result = applyMerge(modification, currentMeta.get(), version);
222 return modification.setSnapshot(Optional.of(result));
224 modification.resolveModificationType(ModificationType.WRITE);
225 return modification.setSnapshot(Optional.of(applyWrite(modification, modification.getWrittenValue(),
226 currentMeta, version)));
228 modification.resolveModificationType(ModificationType.UNMODIFIED);
231 throw new IllegalArgumentException("Provided modification type is not supported.");
236 * Apply a merge operation. Since the result of merge differs based on the data type
237 * being modified, implementations of this method are responsible for calling
238 * {@link ModifiedNode#resolveModificationType(ModificationType)} as appropriate.
240 * @param modification Modified node
241 * @param currentMeta Store Metadata Node on which NodeModification should be applied
242 * @param version New subtree version of parent node
243 * @return A sealed TreeNode representing applied operation.
245 protected abstract TreeNode applyMerge(ModifiedNode modification, TreeNode currentMeta, Version version);
247 protected abstract TreeNode applyWrite(ModifiedNode modification, NormalizedNode<?, ?> newValue,
248 Optional<TreeNode> currentMeta, Version version);
251 * Apply a nested operation. Since there may not actually be a nested operation
252 * to be applied, implementations of this method are responsible for calling
253 * {@link ModifiedNode#resolveModificationType(ModificationType)} as appropriate.
255 * @param modification Modified node
256 * @param currentMeta Store Metadata Node on which NodeModification should be applied
257 * @param version New subtree version of parent node
258 * @return A sealed TreeNode representing applied operation.
260 protected abstract TreeNode applyTouch(ModifiedNode modification, TreeNode currentMeta, Version version);
263 * Checks is supplied {@link NodeModification} is applicable for Subtree Modification.
265 * @param path Path to current node
266 * @param modification Node modification which should be applied.
267 * @param current Current state of data tree
268 * @throws ConflictingModificationAppliedException If subtree was changed in conflicting way
269 * @throws org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException If subtree
270 * modification is not applicable (e.g. leaf node).
272 protected abstract void checkTouchApplicable(ModificationPath path, NodeModification modification,
273 Optional<TreeNode> current, Version version) throws DataValidationFailedException;
276 * Checks if supplied schema node belong to specified Data Tree type. All nodes belong to the operational tree,
277 * nodes in configuration tree are marked as such.
279 * @param treeType Tree Type
280 * @param node Schema node
281 * @return {@code true} if the node matches the tree type, {@code false} otherwise.
283 static boolean belongsToTree(final TreeType treeType, final DataSchemaNode node) {
284 return treeType == TreeType.OPERATIONAL || node.isConfiguration();