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.controller.md.sal.dom.store.impl.tree.data;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import java.util.List;
14 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ConflictingModificationAppliedException;
15 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataValidationFailedException;
16 import org.opendaylight.controller.md.sal.dom.store.impl.tree.IncorrectDataStructureException;
17 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ModificationType;
18 import org.opendaylight.controller.md.sal.dom.store.impl.tree.data.DataNodeContainerModificationStrategy.ContainerModificationStrategy;
19 import org.opendaylight.controller.md.sal.dom.store.impl.tree.data.DataNodeContainerModificationStrategy.UnkeyedListItemModificationStrategy;
20 import org.opendaylight.controller.md.sal.dom.store.impl.tree.data.NormalizedNodeContainerModificationStrategy.ChoiceModificationStrategy;
21 import org.opendaylight.controller.md.sal.dom.store.impl.tree.data.NormalizedNodeContainerModificationStrategy.OrderedLeafSetModificationStrategy;
22 import org.opendaylight.controller.md.sal.dom.store.impl.tree.data.NormalizedNodeContainerModificationStrategy.OrderedMapModificationStrategy;
23 import org.opendaylight.controller.md.sal.dom.store.impl.tree.data.NormalizedNodeContainerModificationStrategy.UnorderedLeafSetModificationStrategy;
24 import org.opendaylight.controller.md.sal.dom.store.impl.tree.data.ValueNodeModificationStrategy.LeafModificationStrategy;
25 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.TreeNode;
26 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.TreeNodeFactory;
27 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.Version;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
35 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
36 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
37 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
39 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 import com.google.common.base.Optional;
47 import com.google.common.base.Preconditions;
49 abstract class SchemaAwareApplyOperation implements ModificationApplyOperation {
50 private static final Logger LOG = LoggerFactory.getLogger(SchemaAwareApplyOperation.class);
52 public static SchemaAwareApplyOperation from(final DataSchemaNode schemaNode) {
53 if (schemaNode instanceof ContainerSchemaNode) {
54 return new ContainerModificationStrategy((ContainerSchemaNode) schemaNode);
55 } else if (schemaNode instanceof ListSchemaNode) {
56 return fromListSchemaNode((ListSchemaNode) schemaNode);
57 } else if (schemaNode instanceof ChoiceNode) {
58 return new ChoiceModificationStrategy((ChoiceNode) schemaNode);
59 } else if (schemaNode instanceof LeafListSchemaNode) {
60 return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode);
61 } else if (schemaNode instanceof LeafSchemaNode) {
62 return new LeafModificationStrategy((LeafSchemaNode) schemaNode);
64 throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
67 public static SchemaAwareApplyOperation from(final DataNodeContainer resolvedTree,
68 final AugmentationTarget augSchemas, final AugmentationIdentifier identifier) {
69 AugmentationSchema augSchema = null;
72 for (AugmentationSchema potential : augSchemas.getAvailableAugmentations()) {
73 for (DataSchemaNode child : potential.getChildNodes()) {
74 if (identifier.getPossibleChildNames().contains(child.getQName())) {
75 augSchema = potential;
81 if (augSchema != null) {
82 return new DataNodeContainerModificationStrategy.AugmentationModificationStrategy(augSchema, resolvedTree);
87 public static boolean checkConflicting(final InstanceIdentifier path, final boolean condition, final String message) throws ConflictingModificationAppliedException {
89 throw new ConflictingModificationAppliedException(path, message);
94 private static SchemaAwareApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode) {
95 List<QName> keyDefinition = schemaNode.getKeyDefinition();
96 if (keyDefinition == null || keyDefinition.isEmpty()) {
97 return new UnkeyedListModificationStrategy(schemaNode);
99 if (schemaNode.isUserOrdered()) {
100 return new OrderedMapModificationStrategy(schemaNode);
103 return new NormalizedNodeContainerModificationStrategy.UnorderedMapModificationStrategy(schemaNode);
106 private static SchemaAwareApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode) {
107 if(schemaNode.isUserOrdered()) {
108 return new OrderedLeafSetModificationStrategy(schemaNode);
110 return new UnorderedLeafSetModificationStrategy(schemaNode);
114 private static final void checkNotConflicting(final InstanceIdentifier path, final TreeNode original, final TreeNode current) throws ConflictingModificationAppliedException {
115 checkConflicting(path, original.getVersion().equals(current.getVersion()),
116 "Node was replaced by other transaction.");
117 checkConflicting(path, original.getSubtreeVersion().equals(current.getSubtreeVersion()),
118 "Node children was modified by other transaction");
121 protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) {
122 Optional<ModificationApplyOperation> potential = getChild(child);
123 checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
124 return potential.get();
128 public void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException {
129 if (modification.getType() == ModificationType.WRITE) {
130 verifyWrittenStructure(modification.getWrittenValue());
135 public final void checkApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
136 switch (modification.getType()) {
138 checkDeleteApplicable(modification, current);
139 case SUBTREE_MODIFIED:
140 checkSubtreeModificationApplicable(path, modification, current);
143 checkWriteApplicable(path, modification, current);
146 checkMergeApplicable(path, modification, current);
151 throw new UnsupportedOperationException("Suplied modification type "+ modification.getType()+ "is not supported.");
156 protected void checkMergeApplicable(final InstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
157 Optional<TreeNode> original = modification.getOriginal();
158 if (original.isPresent() && current.isPresent()) {
160 * We need to do conflict detection only and only if the value of leaf changed
161 * before two transactions. If value of leaf is unchanged between two transactions
162 * it should not cause transaction to fail, since result of this merge
163 * leads to same data.
165 if(!original.get().getData().equals(current.get().getData())) {
166 checkNotConflicting(path, original.get(), current.get());
171 protected void checkWriteApplicable(final InstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
172 Optional<TreeNode> original = modification.getOriginal();
173 if (original.isPresent() && current.isPresent()) {
174 checkNotConflicting(path, original.get(), current.get());
175 } else if(original.isPresent()) {
176 throw new ConflictingModificationAppliedException(path,"Node was deleted by other transaction.");
180 private void checkDeleteApplicable(final NodeModification modification, final Optional<TreeNode> current) {
181 // Delete is always applicable, we do not expose it to subclasses
182 if (current.isPresent()) {
183 LOG.trace("Delete operation turned to no-op on missing node {}", modification);
188 public final Optional<TreeNode> apply(final ModifiedNode modification,
189 final Optional<TreeNode> currentMeta, final Version version) {
191 switch (modification.getType()) {
193 return modification.storeSnapshot(Optional.<TreeNode> absent());
194 case SUBTREE_MODIFIED:
195 Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification",
197 return modification.storeSnapshot(Optional.of(applySubtreeChange(modification, currentMeta.get(),
200 if(currentMeta.isPresent()) {
201 return modification.storeSnapshot(Optional.of(applyMerge(modification,currentMeta.get(), version)));
202 } // Fallback to write is intentional - if node is not preexisting merge is same as write
204 return modification.storeSnapshot(Optional.of(applyWrite(modification, currentMeta, version)));
208 throw new IllegalArgumentException("Provided modification type is not supported.");
212 protected abstract TreeNode applyMerge(ModifiedNode modification,
213 TreeNode currentMeta, Version version);
215 protected abstract TreeNode applyWrite(ModifiedNode modification,
216 Optional<TreeNode> currentMeta, Version version);
218 protected abstract TreeNode applySubtreeChange(ModifiedNode modification,
219 TreeNode currentMeta, Version version);
223 * Checks is supplied {@link NodeModification} is applicable for Subtree Modification.
225 * @param path Path to current node
226 * @param modification Node modification which should be applied.
227 * @param current Current state of data tree
228 * @throws ConflictingModificationAppliedException If subtree was changed in conflicting way
229 * @throws IncorrectDataStructureException If subtree modification is not applicable (e.g. leaf node).
231 protected abstract void checkSubtreeModificationApplicable(InstanceIdentifier path, final NodeModification modification,
232 final Optional<TreeNode> current) throws DataValidationFailedException;
234 protected abstract void verifyWrittenStructure(NormalizedNode<?, ?> writtenValue);
236 public static class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
238 private final Optional<ModificationApplyOperation> entryStrategy;
240 protected UnkeyedListModificationStrategy(final ListSchemaNode schema) {
241 entryStrategy = Optional.<ModificationApplyOperation> of(new UnkeyedListItemModificationStrategy(schema));
245 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
246 final Version version) {
247 return applyWrite(modification, Optional.of(currentMeta), version);
251 protected TreeNode applySubtreeChange(final ModifiedNode modification,
252 final TreeNode currentMeta, final Version version) {
253 throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
257 protected TreeNode applyWrite(final ModifiedNode modification,
258 final Optional<TreeNode> currentMeta, final Version version) {
259 return TreeNodeFactory.createTreeNode(modification.getWrittenValue(), version);
263 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
264 if (child instanceof NodeIdentifier) {
265 return entryStrategy;
267 return Optional.absent();
271 protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
276 protected void checkSubtreeModificationApplicable(final InstanceIdentifier path, final NodeModification modification,
277 final Optional<TreeNode> current) throws IncorrectDataStructureException {
278 throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");