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.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
28 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
35 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 abstract class SchemaAwareApplyOperation<T extends WithStatus> extends ModificationApplyOperation {
42 private static final Logger LOG = LoggerFactory.getLogger(SchemaAwareApplyOperation.class);
44 public static ModificationApplyOperation from(final DataSchemaNode schemaNode,
45 final DataTreeConfiguration treeConfig) {
46 if (treeConfig.getTreeType() == TreeType.CONFIGURATION) {
47 checkArgument(schemaNode.isConfiguration(), "Supplied %s does not belongs to configuration tree.",
48 schemaNode.getPath());
50 if (schemaNode instanceof ContainerSchemaNode) {
51 return ContainerModificationStrategy.of((ContainerSchemaNode) schemaNode, 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 ValueNodeModificationStrategy<>(LeafNode.class, (LeafSchemaNode) schemaNode);
61 throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
64 public static AugmentationModificationStrategy from(final DataNodeContainer resolvedTree,
65 final AugmentationTarget augSchemas, final AugmentationIdentifier identifier,
66 final DataTreeConfiguration treeConfig) {
67 for (final AugmentationSchemaNode 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 static void checkConflicting(final ModificationPath path, final boolean condition, final String message)
79 throws ConflictingModificationAppliedException {
81 throw new ConflictingModificationAppliedException(path.toInstanceIdentifier(), message);
85 private static ModificationApplyOperation 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);
92 op = MapModificationStrategy.of(schemaNode, treeConfig);
95 return MinMaxElementsValidation.from(op, schemaNode);
98 private static ModificationApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode,
99 final DataTreeConfiguration treeConfig) {
100 return MinMaxElementsValidation.from(new LeafSetModificationStrategy(schemaNode, treeConfig), schemaNode);
103 protected static void checkNotConflicting(final ModificationPath path, final TreeNode original,
104 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 checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
114 return potential.get();
118 void checkApplicable(final ModificationPath path, final NodeModification modification,
119 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
120 switch (modification.getOperation()) {
122 checkDeleteApplicable(modification, current);
125 checkTouchApplicable(path, modification, current, version);
128 checkWriteApplicable(path, modification, current, version);
131 checkMergeApplicable(path, modification, current, version);
136 throw new UnsupportedOperationException(
137 "Suplied modification type " + modification.getOperation() + " is not supported.");
142 final void quickVerifyStructure(final NormalizedNode<?, ?> writtenValue) {
143 verifyValue(writtenValue);
147 final void fullVerifyStructure(final NormalizedNode<?, ?> writtenValue) {
148 verifyValue(writtenValue);
149 verifyValueChildren(writtenValue);
153 * Verify the a written value, without performing deeper tree validation.
155 * @param writtenValue Written value
157 abstract void verifyValue(NormalizedNode<?, ?> writtenValue);
160 * Verify the children implied by a written value after the value itself has been verified by
161 * {@link #verifyValue(NormalizedNode)}. Default implementation does nothing.
163 * @param writtenValue Written value
165 void verifyValueChildren(final NormalizedNode<?, ?> writtenValue) {
169 protected void checkMergeApplicable(final ModificationPath path, final NodeModification modification,
170 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
171 final Optional<TreeNode> original = modification.getOriginal();
172 if (original.isPresent() && current.isPresent()) {
174 * We need to do conflict detection only and only if the value of leaf changed
175 * before two transactions. If value of leaf is unchanged between two transactions
176 * it should not cause transaction to fail, since result of this merge
177 * leads to same data.
179 final TreeNode orig = original.get();
180 final TreeNode cur = current.get();
181 if (!orig.getData().equals(cur.getData())) {
182 checkNotConflicting(path, orig, cur);
188 * Checks if write operation can be applied to current TreeNode.
189 * The operation checks if original tree node to which the modification is going to be applied exists and if
190 * current node in TreeNode structure exists.
192 * @param path Path from current node in TreeNode
193 * @param modification modification to apply
194 * @param current current node in TreeNode for modification to apply
195 * @throws DataValidationFailedException when a data dependency conflict is detected
197 private static void checkWriteApplicable(final ModificationPath path, final NodeModification modification,
198 final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
199 final Optional<TreeNode> original = modification.getOriginal();
200 if (original.isPresent() && current.isPresent()) {
201 checkNotConflicting(path, original.get(), current.get());
203 checkConflicting(path, !original.isPresent(), "Node was deleted by other transaction.");
204 checkConflicting(path, !current.isPresent(), "Node was created by other transaction.");
208 private static void checkDeleteApplicable(final NodeModification modification, final Optional<TreeNode> current) {
209 // Delete is always applicable, we do not expose it to subclasses
210 if (!current.isPresent()) {
211 LOG.trace("Delete operation turned to no-op on missing node {}", modification);
216 Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
217 final Version version) {
218 switch (modification.getOperation()) {
220 // Deletion of a non-existing node is a no-op, report it as such
221 modification.resolveModificationType(currentMeta.isPresent() ? ModificationType.DELETE
222 : ModificationType.UNMODIFIED);
223 return modification.setSnapshot(Optional.empty());
225 checkArgument(currentMeta.isPresent(), "Metadata not available for modification %s", modification);
226 return modification.setSnapshot(Optional.of(applyTouch(modification, currentMeta.get(),
229 final TreeNode result;
231 if (!currentMeta.isPresent()) {
232 // This is a slight optimization: a merge on a non-existing node equals to a write. Written data
233 // structure is usually verified when the transaction is sealed. To preserve correctness, we have
234 // to run that validation here.
235 modification.resolveModificationType(ModificationType.WRITE);
236 result = applyWrite(modification, modification.getWrittenValue(), currentMeta, version);
237 fullVerifyStructure(result.getData());
239 result = applyMerge(modification, currentMeta.get(), version);
242 return modification.setSnapshot(Optional.of(result));
244 modification.resolveModificationType(ModificationType.WRITE);
245 return modification.setSnapshot(Optional.of(applyWrite(modification,
246 verifyNotNull(modification.getWrittenValue()), currentMeta, version)));
248 modification.resolveModificationType(ModificationType.UNMODIFIED);
251 throw new IllegalArgumentException("Provided modification type is not supported.");
256 * Apply a merge operation. Since the result of merge differs based on the data type
257 * being modified, implementations of this method are responsible for calling
258 * {@link ModifiedNode#resolveModificationType(ModificationType)} as appropriate.
260 * @param modification Modified node
261 * @param currentMeta Store Metadata Node on which NodeModification should be applied
262 * @param version New subtree version of parent node
263 * @return A sealed TreeNode representing applied operation.
265 protected abstract TreeNode applyMerge(ModifiedNode modification, TreeNode currentMeta, Version version);
267 protected abstract TreeNode applyWrite(ModifiedNode modification, NormalizedNode<?, ?> newValue,
268 Optional<TreeNode> currentMeta, Version version);
271 * Apply a nested operation. Since there may not actually be a nested operation
272 * to be applied, implementations of this method are responsible for calling
273 * {@link ModifiedNode#resolveModificationType(ModificationType)} as appropriate.
275 * @param modification Modified node
276 * @param currentMeta Store Metadata Node on which NodeModification should be applied
277 * @param version New subtree version of parent node
278 * @return A sealed TreeNode representing applied operation.
280 protected abstract TreeNode applyTouch(ModifiedNode modification, TreeNode currentMeta, Version version);
283 * Checks is supplied {@link NodeModification} is applicable for Subtree Modification.
285 * @param path Path to current node
286 * @param modification Node modification which should be applied.
287 * @param current Current state of data tree
288 * @throws ConflictingModificationAppliedException If subtree was changed in conflicting way
289 * @throws org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException If subtree
290 * modification is not applicable (e.g. leaf node).
292 protected abstract void checkTouchApplicable(ModificationPath path, NodeModification modification,
293 Optional<TreeNode> current, Version version) throws DataValidationFailedException;
296 * Return the {@link WithStatus}-subclass schema associated with this operation.
297 * @return A model node
299 abstract @NonNull T getSchema();
302 * Checks if supplied schema node belong to specified Data Tree type. All nodes belong to the operational tree,
303 * nodes in configuration tree are marked as such.
305 * @param treeType Tree Type
306 * @param node Schema node
307 * @return {@code true} if the node matches the tree type, {@code false} otherwise.
309 static boolean belongsToTree(final TreeType treeType, final DataSchemaNode node) {
310 return treeType == TreeType.OPERATIONAL || node.isConfiguration();