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.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
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 SchemaAwareApplyOperation from(final DataSchemaNode schemaNode) {
40 if (schemaNode instanceof ContainerSchemaNode) {
41 return new ContainerModificationStrategy((ContainerSchemaNode) schemaNode);
42 } else if (schemaNode instanceof ListSchemaNode) {
43 return fromListSchemaNode((ListSchemaNode) schemaNode);
44 } else if (schemaNode instanceof ChoiceSchemaNode) {
45 return new ChoiceModificationStrategy((ChoiceSchemaNode) schemaNode);
46 } else if (schemaNode instanceof LeafListSchemaNode) {
47 return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode);
48 } else if (schemaNode instanceof LeafSchemaNode) {
49 return new LeafModificationStrategy((LeafSchemaNode) schemaNode);
51 throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
54 public static SchemaAwareApplyOperation from(final DataNodeContainer resolvedTree,
55 final AugmentationTarget augSchemas, final AugmentationIdentifier identifier) {
56 for (AugmentationSchema potential : augSchemas.getAvailableAugmentations()) {
57 for (DataSchemaNode child : potential.getChildNodes()) {
58 if (identifier.getPossibleChildNames().contains(child.getQName())) {
59 return new AugmentationModificationStrategy(potential, resolvedTree);
67 public static boolean checkConflicting(final YangInstanceIdentifier path, final boolean condition, final String message) throws ConflictingModificationAppliedException {
69 throw new ConflictingModificationAppliedException(path, message);
74 private static SchemaAwareApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode) {
75 List<QName> keyDefinition = schemaNode.getKeyDefinition();
76 if (keyDefinition == null || keyDefinition.isEmpty()) {
77 return new UnkeyedListModificationStrategy(schemaNode);
79 if (schemaNode.isUserOrdered()) {
80 return new OrderedMapModificationStrategy(schemaNode);
83 return new UnorderedMapModificationStrategy(schemaNode);
86 private static SchemaAwareApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode) {
87 if(schemaNode.isUserOrdered()) {
88 return new OrderedLeafSetModificationStrategy(schemaNode);
90 return new UnorderedLeafSetModificationStrategy(schemaNode);
94 private static final void checkNotConflicting(final YangInstanceIdentifier path, final TreeNode original, final TreeNode current) throws ConflictingModificationAppliedException {
95 checkConflicting(path, original.getVersion().equals(current.getVersion()),
96 "Node was replaced by other transaction.");
97 checkConflicting(path, original.getSubtreeVersion().equals(current.getSubtreeVersion()),
98 "Node children was modified by other transaction");
101 protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) {
102 Optional<ModificationApplyOperation> potential = getChild(child);
103 Preconditions.checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
104 return potential.get();
108 void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException {
109 if (modification.getOperation() == LogicalOperation.WRITE) {
110 verifyWrittenStructure(modification.getWrittenValue());
115 final void checkApplicable(final YangInstanceIdentifier path,final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
116 switch (modification.getOperation()) {
118 checkDeleteApplicable(modification, current);
121 checkTouchApplicable(path, modification, current);
124 checkWriteApplicable(path, modification, current);
127 checkMergeApplicable(path, modification, current);
132 throw new UnsupportedOperationException("Suplied modification type "+ modification.getOperation()+ " is not supported.");
136 protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
137 Optional<TreeNode> original = modification.getOriginal();
138 if (original.isPresent() && current.isPresent()) {
140 * We need to do conflict detection only and only if the value of leaf changed
141 * before two transactions. If value of leaf is unchanged between two transactions
142 * it should not cause transaction to fail, since result of this merge
143 * leads to same data.
145 if(!original.get().getData().equals(current.get().getData())) {
146 checkNotConflicting(path, original.get(), current.get());
152 * Checks if write operation can be applied to current TreeNode.
153 * The operation checks if original tree node to which the modification is going to be applied exists and if
154 * current node in TreeNode structure exists.
156 * @param path Path from current node in TreeNode
157 * @param modification modification to apply
158 * @param current current node in TreeNode for modification to apply
159 * @throws DataValidationFailedException
161 protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification,
162 final Optional<TreeNode> current) throws DataValidationFailedException {
163 Optional<TreeNode> original = modification.getOriginal();
164 if (original.isPresent() && current.isPresent()) {
165 checkNotConflicting(path, original.get(), current.get());
166 } else if(original.isPresent()) {
167 throw new ConflictingModificationAppliedException(path,"Node was deleted by other transaction.");
168 } else if(current.isPresent()) {
169 throw new ConflictingModificationAppliedException(path,"Node was created by other transaction.");
173 private void checkDeleteApplicable(final NodeModification modification, final Optional<TreeNode> current) {
174 // Delete is always applicable, we do not expose it to subclasses
175 if (current.isPresent()) {
176 LOG.trace("Delete operation turned to no-op on missing node {}", modification);
181 protected ChildTrackingPolicy getChildPolicy() {
182 return ChildTrackingPolicy.UNORDERED;
186 final Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> currentMeta, final Version version) {
187 switch (modification.getOperation()) {
189 modification.resolveModificationType(ModificationType.DELETE);
190 return modification.setSnapshot(Optional.<TreeNode> absent());
192 Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification",
194 return modification.setSnapshot(Optional.of(applyTouch(modification, currentMeta.get(),
197 final TreeNode result;
199 // This is a slight optimization: a merge on a non-existing node equals to a write
200 if (currentMeta.isPresent()) {
201 result = applyMerge(modification,currentMeta.get(), version);
203 modification.resolveModificationType(ModificationType.WRITE);
204 result = applyWrite(modification, currentMeta, version);
207 return modification.setSnapshot(Optional.of(result));
209 modification.resolveModificationType(ModificationType.WRITE);
210 return modification.setSnapshot(Optional.of(applyWrite(modification, currentMeta, version)));
212 modification.resolveModificationType(ModificationType.UNMODIFIED);
215 throw new IllegalArgumentException("Provided modification type is not supported.");
220 * Apply a merge operation. Since the result of merge differs based on the data type
221 * being modified, implementations of this method are responsible for calling
222 * {@link ModifiedNode#resolveModificationType(ModificationType)} as appropriate.
224 * @param modification Modified node
225 * @param currentMeta Store Metadata Node on which NodeModification should be applied
226 * @param version New subtree version of parent node
227 * @return A sealed TreeNode representing applied operation.
229 protected abstract TreeNode applyMerge(ModifiedNode modification, TreeNode currentMeta, Version version);
231 protected abstract TreeNode applyWrite(ModifiedNode modification, Optional<TreeNode> currentMeta, Version version);
234 * Apply a nested operation. Since there may not actually be a nested operation
235 * to be applied, implementations of this method are responsible for calling
236 * {@link ModifiedNode#resolveModificationType(ModificationType)} as appropriate.
238 * @param modification Modified node
239 * @param currentMeta Store Metadata Node on which NodeModification should be applied
240 * @param version New subtree version of parent node
241 * @return A sealed TreeNode representing applied operation.
243 protected abstract TreeNode applyTouch(ModifiedNode modification, TreeNode currentMeta, Version version);
247 * Checks is supplied {@link NodeModification} is applicable for Subtree Modification.
249 * @param path Path to current node
250 * @param modification Node modification which should be applied.
251 * @param current Current state of data tree
252 * @throws ConflictingModificationAppliedException If subtree was changed in conflicting way
253 * @throws IncorrectDataStructureException If subtree modification is not applicable (e.g. leaf node).
255 protected abstract void checkTouchApplicable(YangInstanceIdentifier path, final NodeModification modification,
256 final Optional<TreeNode> current) throws DataValidationFailedException;
258 protected abstract void verifyWrittenStructure(NormalizedNode<?, ?> writtenValue);