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 static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verifyNotNull;
13 import java.util.List;
14 import java.util.Optional;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 abstract class SchemaAwareApplyOperation extends ModificationApplyOperation {
39 private static final Logger LOG = LoggerFactory.getLogger(SchemaAwareApplyOperation.class);
41 public static ModificationApplyOperation from(final DataSchemaNode schemaNode,
42 final DataTreeConfiguration treeConfig) {
43 if (treeConfig.getTreeType() == TreeType.CONFIGURATION) {
44 checkArgument(schemaNode.isConfiguration(), "Supplied %s does not belongs to configuration tree.",
45 schemaNode.getPath());
47 if (schemaNode instanceof ContainerSchemaNode) {
48 return ContainerModificationStrategy.of((ContainerSchemaNode) schemaNode, 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);
89 op = MapModificationStrategy.of(schemaNode, treeConfig);
92 return MinMaxElementsValidation.from(op, schemaNode);
95 private static ModificationApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode,
96 final DataTreeConfiguration treeConfig) {
97 return MinMaxElementsValidation.from(new LeafSetModificationStrategy(schemaNode, treeConfig), schemaNode);
100 protected static void checkNotConflicting(final ModificationPath path, final TreeNode original,
101 final TreeNode current) throws ConflictingModificationAppliedException {
102 checkConflicting(path, original.getVersion().equals(current.getVersion()),
103 "Node was replaced by other transaction.");
104 checkConflicting(path, original.getSubtreeVersion().equals(current.getSubtreeVersion()),
105 "Node children was modified by other transaction");
108 protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) {
109 final Optional<ModificationApplyOperation> potential = getChild(child);
110 checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
111 return potential.get();
115 void checkApplicable(final ModificationPath path, final NodeModification modification,
116 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
117 switch (modification.getOperation()) {
119 checkDeleteApplicable(modification, current);
122 checkTouchApplicable(path, modification, current, version);
125 checkWriteApplicable(path, modification, current, version);
128 checkMergeApplicable(path, modification, current, version);
133 throw new UnsupportedOperationException(
134 "Suplied modification type " + modification.getOperation() + " is not supported.");
139 final void quickVerifyStructure(final NormalizedNode<?, ?> writtenValue) {
140 verifyValue(writtenValue);
144 final void fullVerifyStructure(final NormalizedNode<?, ?> writtenValue) {
145 verifyValue(writtenValue);
146 verifyValueChildren(writtenValue);
150 * Verify the a written value, without performing deeper tree validation.
152 * @param writtenValue Written value
154 abstract void verifyValue(NormalizedNode<?, ?> writtenValue);
157 * Verify the children implied by a written value after the value itself has been verified by
158 * {@link #verifyValue(NormalizedNode)}. Default implementation does nothing.
160 * @param writtenValue Written value
162 void verifyValueChildren(final NormalizedNode<?, ?> writtenValue) {
166 protected void checkMergeApplicable(final ModificationPath path, final NodeModification modification,
167 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
168 final Optional<TreeNode> original = modification.getOriginal();
169 if (original.isPresent() && current.isPresent()) {
171 * We need to do conflict detection only and only if the value of leaf changed
172 * before two transactions. If value of leaf is unchanged between two transactions
173 * it should not cause transaction to fail, since result of this merge
174 * leads to same data.
176 final TreeNode orig = original.get();
177 final TreeNode cur = current.get();
178 if (!orig.getData().equals(cur.getData())) {
179 checkNotConflicting(path, orig, cur);
185 * Checks if write operation can be applied to current TreeNode.
186 * The operation checks if original tree node to which the modification is going to be applied exists and if
187 * current node in TreeNode structure exists.
189 * @param path Path from current node in TreeNode
190 * @param modification modification to apply
191 * @param current current node in TreeNode for modification to apply
192 * @throws DataValidationFailedException when a data dependency conflict is detected
194 private static void checkWriteApplicable(final ModificationPath path, final NodeModification modification,
195 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
196 final Optional<TreeNode> original = modification.getOriginal();
197 if (original.isPresent() && current.isPresent()) {
198 checkNotConflicting(path, original.get(), current.get());
200 checkConflicting(path, !original.isPresent(), "Node was deleted by other transaction.");
201 checkConflicting(path, !current.isPresent(), "Node was created by other transaction.");
205 private static void checkDeleteApplicable(final NodeModification modification, final Optional<TreeNode> current) {
206 // Delete is always applicable, we do not expose it to subclasses
207 if (!current.isPresent()) {
208 LOG.trace("Delete operation turned to no-op on missing node {}", modification);
213 Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
214 final Version version) {
215 switch (modification.getOperation()) {
217 // Deletion of a non-existing node is a no-op, report it as such
218 modification.resolveModificationType(currentMeta.isPresent() ? ModificationType.DELETE
219 : ModificationType.UNMODIFIED);
220 return modification.setSnapshot(Optional.empty());
222 checkArgument(currentMeta.isPresent(), "Metadata not available for modification %s", modification);
223 return modification.setSnapshot(Optional.of(applyTouch(modification, currentMeta.get(),
226 final TreeNode result;
228 if (!currentMeta.isPresent()) {
229 // This is a slight optimization: a merge on a non-existing node equals to a write. Written data
230 // structure is usually verified when the transaction is sealed. To preserve correctness, we have
231 // to run that validation here.
232 modification.resolveModificationType(ModificationType.WRITE);
233 result = applyWrite(modification, modification.getWrittenValue(), currentMeta, version);
234 fullVerifyStructure(result.getData());
236 result = applyMerge(modification, currentMeta.get(), version);
239 return modification.setSnapshot(Optional.of(result));
241 modification.resolveModificationType(ModificationType.WRITE);
242 return modification.setSnapshot(Optional.of(applyWrite(modification,
243 verifyNotNull(modification.getWrittenValue()), currentMeta, version)));
245 modification.resolveModificationType(ModificationType.UNMODIFIED);
248 throw new IllegalArgumentException("Provided modification type is not supported.");
253 * Apply a merge operation. Since the result of merge differs based on the data type
254 * being modified, 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 applyMerge(ModifiedNode modification, TreeNode currentMeta, Version version);
264 protected abstract TreeNode applyWrite(ModifiedNode modification, NormalizedNode<?, ?> newValue,
265 Optional<TreeNode> currentMeta, Version version);
268 * Apply a nested operation. Since there may not actually be a nested operation
269 * to be applied, implementations of this method are responsible for calling
270 * {@link ModifiedNode#resolveModificationType(ModificationType)} as appropriate.
272 * @param modification Modified node
273 * @param currentMeta Store Metadata Node on which NodeModification should be applied
274 * @param version New subtree version of parent node
275 * @return A sealed TreeNode representing applied operation.
277 protected abstract TreeNode applyTouch(ModifiedNode modification, TreeNode currentMeta, Version version);
280 * Checks is supplied {@link NodeModification} is applicable for Subtree Modification.
282 * @param path Path to current node
283 * @param modification Node modification which should be applied.
284 * @param current Current state of data tree
285 * @throws ConflictingModificationAppliedException If subtree was changed in conflicting way
286 * @throws org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException If subtree
287 * modification is not applicable (e.g. leaf node).
289 protected abstract void checkTouchApplicable(ModificationPath path, NodeModification modification,
290 Optional<TreeNode> current, Version version) throws DataValidationFailedException;
293 * Checks if supplied schema node belong to specified Data Tree type. All nodes belong to the operational tree,
294 * nodes in configuration tree are marked as such.
296 * @param treeType Tree Type
297 * @param node Schema node
298 * @return {@code true} if the node matches the tree type, {@code false} otherwise.
300 static boolean belongsToTree(final TreeType treeType, final DataSchemaNode node) {
301 return treeType == TreeType.OPERATIONAL || node.isConfiguration();