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.spi.TreeNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 abstract class SchemaAwareApplyOperation implements ModificationApplyOperation {
36 private static final Logger LOG = LoggerFactory.getLogger(SchemaAwareApplyOperation.class);
38 public static SchemaAwareApplyOperation from(final DataSchemaNode schemaNode) {
39 if (schemaNode instanceof ContainerSchemaNode) {
40 return new DataNodeContainerModificationStrategy.ContainerModificationStrategy((ContainerSchemaNode) schemaNode);
41 } else if (schemaNode instanceof ListSchemaNode) {
42 return fromListSchemaNode((ListSchemaNode) schemaNode);
43 } else if (schemaNode instanceof ChoiceSchemaNode) {
44 return new NormalizedNodeContainerModificationStrategy.ChoiceModificationStrategy((ChoiceSchemaNode) schemaNode);
45 } else if (schemaNode instanceof LeafListSchemaNode) {
46 return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode);
47 } else if (schemaNode instanceof LeafSchemaNode) {
48 return new ValueNodeModificationStrategy.LeafModificationStrategy((LeafSchemaNode) schemaNode);
50 throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
53 public static SchemaAwareApplyOperation from(final DataNodeContainer resolvedTree,
54 final AugmentationTarget augSchemas, final AugmentationIdentifier identifier) {
55 AugmentationSchema augSchema = null;
58 for (AugmentationSchema potential : augSchemas.getAvailableAugmentations()) {
59 for (DataSchemaNode child : potential.getChildNodes()) {
60 if (identifier.getPossibleChildNames().contains(child.getQName())) {
61 augSchema = potential;
67 if (augSchema != null) {
68 return new DataNodeContainerModificationStrategy.AugmentationModificationStrategy(augSchema, resolvedTree);
73 public static boolean checkConflicting(final YangInstanceIdentifier path, final boolean condition, final String message) throws ConflictingModificationAppliedException {
75 throw new ConflictingModificationAppliedException(path, message);
80 private static SchemaAwareApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode) {
81 List<QName> keyDefinition = schemaNode.getKeyDefinition();
82 if (keyDefinition == null || keyDefinition.isEmpty()) {
83 return new UnkeyedListModificationStrategy(schemaNode);
85 if (schemaNode.isUserOrdered()) {
86 return new NormalizedNodeContainerModificationStrategy.OrderedMapModificationStrategy(schemaNode);
89 return new NormalizedNodeContainerModificationStrategy.UnorderedMapModificationStrategy(schemaNode);
92 private static SchemaAwareApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode) {
93 if(schemaNode.isUserOrdered()) {
94 return new NormalizedNodeContainerModificationStrategy.OrderedLeafSetModificationStrategy(schemaNode);
96 return new NormalizedNodeContainerModificationStrategy.UnorderedLeafSetModificationStrategy(schemaNode);
100 private static final void checkNotConflicting(final YangInstanceIdentifier path, final TreeNode original, final TreeNode current) throws ConflictingModificationAppliedException {
101 checkConflicting(path, original.getVersion().equals(current.getVersion()),
102 "Node was replaced by other transaction.");
103 checkConflicting(path, original.getSubtreeVersion().equals(current.getSubtreeVersion()),
104 "Node children was modified by other transaction");
107 protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) {
108 Optional<ModificationApplyOperation> potential = getChild(child);
109 Preconditions.checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
110 return potential.get();
114 public void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException {
115 if (modification.getOperation() == LogicalOperation.WRITE) {
116 verifyWrittenStructure(modification.getWrittenValue());
121 public final void checkApplicable(final YangInstanceIdentifier path,final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
122 switch (modification.getOperation()) {
124 checkDeleteApplicable(modification, current);
126 checkSubtreeModificationApplicable(path, modification, current);
129 checkWriteApplicable(path, modification, current);
132 checkMergeApplicable(path, modification, current);
137 throw new UnsupportedOperationException("Suplied modification type "+ modification.getOperation()+ "is not supported.");
142 protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
143 Optional<TreeNode> original = modification.getOriginal();
144 if (original.isPresent() && current.isPresent()) {
146 * We need to do conflict detection only and only if the value of leaf changed
147 * before two transactions. If value of leaf is unchanged between two transactions
148 * it should not cause transaction to fail, since result of this merge
149 * leads to same data.
151 if(!original.get().getData().equals(current.get().getData())) {
152 checkNotConflicting(path, original.get(), current.get());
158 * Checks if write operation can be applied to current TreeNode.
159 * The operation checks if original tree node to which the modification is going to be applied exists and if
160 * current node in TreeNode structure exists.
162 * @param path Path from current node in TreeNode
163 * @param modification modification to apply
164 * @param current current node in TreeNode for modification to apply
165 * @throws DataValidationFailedException
167 protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification,
168 final Optional<TreeNode> current) throws DataValidationFailedException {
169 Optional<TreeNode> original = modification.getOriginal();
170 if (original.isPresent() && current.isPresent()) {
171 checkNotConflicting(path, original.get(), current.get());
172 } else if(original.isPresent()) {
173 throw new ConflictingModificationAppliedException(path,"Node was deleted by other transaction.");
174 } else if(current.isPresent()) {
175 throw new ConflictingModificationAppliedException(path,"Node was created by other transaction.");
179 private void checkDeleteApplicable(final NodeModification modification, final Optional<TreeNode> current) {
180 // Delete is always applicable, we do not expose it to subclasses
181 if (current.isPresent()) {
182 LOG.trace("Delete operation turned to no-op on missing node {}", modification);
186 boolean isOrdered() {
191 public final Optional<TreeNode> apply(final ModifiedNode modification,
192 final Optional<TreeNode> currentMeta, final Version version) {
194 switch (modification.getOperation()) {
196 return modification.setSnapshot(Optional.<TreeNode> absent());
198 Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification",
200 return modification.setSnapshot(Optional.of(applySubtreeChange(modification, currentMeta.get(),
203 final TreeNode result;
205 // This is a slight optimization: a merge on a non-existing node equals to a write
206 if (currentMeta.isPresent()) {
207 result = applyMerge(modification,currentMeta.get(), version);
209 result = applyWrite(modification, currentMeta, version);
212 return modification.setSnapshot(Optional.of(result));
214 return modification.setSnapshot(Optional.of(applyWrite(modification, currentMeta, version)));
218 throw new IllegalArgumentException("Provided modification type is not supported.");
222 protected abstract TreeNode applyMerge(ModifiedNode modification,
223 TreeNode currentMeta, Version version);
225 protected abstract TreeNode applyWrite(ModifiedNode modification,
226 Optional<TreeNode> currentMeta, Version version);
228 protected abstract TreeNode applySubtreeChange(ModifiedNode modification,
229 TreeNode currentMeta, Version version);
233 * Checks is supplied {@link NodeModification} is applicable for Subtree Modification.
235 * @param path Path to current node
236 * @param modification Node modification which should be applied.
237 * @param current Current state of data tree
238 * @throws ConflictingModificationAppliedException If subtree was changed in conflicting way
239 * @throws IncorrectDataStructureException If subtree modification is not applicable (e.g. leaf node).
241 protected abstract void checkSubtreeModificationApplicable(YangInstanceIdentifier path, final NodeModification modification,
242 final Optional<TreeNode> current) throws DataValidationFailedException;
244 protected abstract void verifyWrittenStructure(NormalizedNode<?, ?> writtenValue);