Merge "Bug 1258: Implement DataTree partial indexing"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / SchemaAwareApplyOperation.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Iterables;
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.NodeIdentifier;
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.UnkeyedListEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.MutableTreeNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
30 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
31 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
32 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
33 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import java.util.List;
43
44 abstract class SchemaAwareApplyOperation implements ModificationApplyOperation {
45     private static final Logger LOG = LoggerFactory.getLogger(SchemaAwareApplyOperation.class);
46
47     public static SchemaAwareApplyOperation from(final DataSchemaNode schemaNode) {
48         if (schemaNode instanceof ContainerSchemaNode) {
49             return new DataNodeContainerModificationStrategy.ContainerModificationStrategy((ContainerSchemaNode) schemaNode);
50         } else if (schemaNode instanceof ListSchemaNode) {
51             return fromListSchemaNode((ListSchemaNode) schemaNode);
52         } else if (schemaNode instanceof ChoiceNode) {
53             return new NormalizedNodeContainerModificationStrategy.ChoiceModificationStrategy((ChoiceNode) schemaNode);
54         } else if (schemaNode instanceof LeafListSchemaNode) {
55             return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode);
56         } else if (schemaNode instanceof LeafSchemaNode) {
57             return new ValueNodeModificationStrategy.LeafModificationStrategy((LeafSchemaNode) schemaNode);
58         }
59         throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
60     }
61
62     public static SchemaAwareApplyOperation from(final DataNodeContainer resolvedTree,
63             final AugmentationTarget augSchemas, final AugmentationIdentifier identifier) {
64         AugmentationSchema augSchema = null;
65
66         allAugments:
67             for (AugmentationSchema potential : augSchemas.getAvailableAugmentations()) {
68                 for (DataSchemaNode child : potential.getChildNodes()) {
69                     if (identifier.getPossibleChildNames().contains(child.getQName())) {
70                         augSchema = potential;
71                         break allAugments;
72                     }
73                 }
74             }
75
76         if (augSchema != null) {
77             return new DataNodeContainerModificationStrategy.AugmentationModificationStrategy(augSchema, resolvedTree);
78         }
79         return null;
80     }
81
82     public static boolean checkConflicting(final YangInstanceIdentifier path, final boolean condition, final String message) throws ConflictingModificationAppliedException {
83         if(!condition) {
84             throw new ConflictingModificationAppliedException(path, message);
85         }
86         return condition;
87     }
88
89     private static SchemaAwareApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode) {
90         List<QName> keyDefinition = schemaNode.getKeyDefinition();
91         if (keyDefinition == null || keyDefinition.isEmpty()) {
92             return new UnkeyedListModificationStrategy(schemaNode);
93         }
94         if (schemaNode.isUserOrdered()) {
95             return new NormalizedNodeContainerModificationStrategy.OrderedMapModificationStrategy(schemaNode);
96         }
97
98         return new NormalizedNodeContainerModificationStrategy.UnorderedMapModificationStrategy(schemaNode);
99     }
100
101     private static SchemaAwareApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode) {
102         if(schemaNode.isUserOrdered()) {
103             return new NormalizedNodeContainerModificationStrategy.OrderedLeafSetModificationStrategy(schemaNode);
104         } else {
105             return new NormalizedNodeContainerModificationStrategy.UnorderedLeafSetModificationStrategy(schemaNode);
106         }
107     }
108
109     private static final void checkNotConflicting(final YangInstanceIdentifier path, final TreeNode original, final TreeNode current) throws ConflictingModificationAppliedException {
110         checkConflicting(path, original.getVersion().equals(current.getVersion()),
111                 "Node was replaced by other transaction.");
112         checkConflicting(path, original.getSubtreeVersion().equals(current.getSubtreeVersion()),
113                 "Node children was modified by other transaction");
114     }
115
116     protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) {
117         Optional<ModificationApplyOperation> potential = getChild(child);
118         Preconditions.checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
119         return potential.get();
120     }
121
122     @Override
123     public void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException {
124         if (modification.getType() == ModificationType.WRITE) {
125             verifyWrittenStructure(modification.getWrittenValue());
126         }
127     }
128
129     @Override
130     public final void checkApplicable(final YangInstanceIdentifier path,final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
131         switch (modification.getType()) {
132         case DELETE:
133             checkDeleteApplicable(modification, current);
134         case SUBTREE_MODIFIED:
135             checkSubtreeModificationApplicable(path, modification, current);
136             return;
137         case WRITE:
138             checkWriteApplicable(path, modification, current);
139             return;
140         case MERGE:
141             checkMergeApplicable(path, modification, current);
142             return;
143         case UNMODIFIED:
144             return;
145         default:
146             throw new UnsupportedOperationException("Suplied modification type "+ modification.getType()+ "is not supported.");
147         }
148
149     }
150
151     protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
152         Optional<TreeNode> original = modification.getOriginal();
153         if (original.isPresent() && current.isPresent()) {
154             /*
155              * We need to do conflict detection only and only if the value of leaf changed
156              * before two transactions. If value of leaf is unchanged between two transactions
157              * it should not cause transaction to fail, since result of this merge
158              * leads to same data.
159              */
160             if(!original.get().getData().equals(current.get().getData())) {
161                 checkNotConflicting(path, original.get(), current.get());
162             }
163         }
164     }
165
166     /**
167      * Checks if write operation can be applied to current TreeNode.
168      * The operation checks if original tree node to which the modification is going to be applied exists and if
169      * current node in TreeNode structure exists.
170      *
171      * @param path Path from current node in TreeNode
172      * @param modification modification to apply
173      * @param current current node in TreeNode for modification to apply
174      * @throws DataValidationFailedException
175      */
176     protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification,
177         final Optional<TreeNode> current) throws DataValidationFailedException {
178         Optional<TreeNode> original = modification.getOriginal();
179         if (original.isPresent() && current.isPresent()) {
180             checkNotConflicting(path, original.get(), current.get());
181         } else if(original.isPresent()) {
182             throw new ConflictingModificationAppliedException(path,"Node was deleted by other transaction.");
183         } else if(current.isPresent()) {
184             throw new ConflictingModificationAppliedException(path,"Node was created by other transaction.");
185         }
186     }
187
188     private void checkDeleteApplicable(final NodeModification modification, final Optional<TreeNode> current) {
189         // Delete is always applicable, we do not expose it to subclasses
190         if (current.isPresent()) {
191             LOG.trace("Delete operation turned to no-op on missing node {}", modification);
192         }
193     }
194
195     @Override
196     public final Optional<TreeNode> apply(final ModifiedNode modification,
197             final Optional<TreeNode> currentMeta, final Version version) {
198
199         switch (modification.getType()) {
200         case DELETE:
201             return modification.storeSnapshot(Optional.<TreeNode> absent());
202         case SUBTREE_MODIFIED:
203             Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification",
204                     modification);
205             return modification.storeSnapshot(Optional.of(applySubtreeChange(modification, currentMeta.get(),
206                     version)));
207         case MERGE:
208             if(currentMeta.isPresent()) {
209                 return modification.storeSnapshot(Optional.of(applyMerge(modification,currentMeta.get(), version)));
210             }
211             // intentional fall-through: if the node does not exist a merge is same as a write
212         case WRITE:
213             return modification.storeSnapshot(Optional.of(applyWrite(modification, currentMeta, version)));
214         case UNMODIFIED:
215             return currentMeta;
216         default:
217             throw new IllegalArgumentException("Provided modification type is not supported.");
218         }
219     }
220
221     protected abstract TreeNode applyMerge(ModifiedNode modification,
222             TreeNode currentMeta, Version version);
223
224     protected abstract TreeNode applyWrite(ModifiedNode modification,
225             Optional<TreeNode> currentMeta, Version version);
226
227     protected abstract TreeNode applySubtreeChange(ModifiedNode modification,
228             TreeNode currentMeta, Version version);
229
230     /**
231      *
232      * Checks is supplied {@link NodeModification} is applicable for Subtree Modification.
233      *
234      * @param path Path to current node
235      * @param modification Node modification which should be applied.
236      * @param current Current state of data tree
237      * @throws ConflictingModificationAppliedException If subtree was changed in conflicting way
238      * @throws IncorrectDataStructureException If subtree modification is not applicable (e.g. leaf node).
239      */
240     protected abstract void checkSubtreeModificationApplicable(YangInstanceIdentifier path, final NodeModification modification,
241             final Optional<TreeNode> current) throws DataValidationFailedException;
242
243     protected abstract void verifyWrittenStructure(NormalizedNode<?, ?> writtenValue);
244
245     public static class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
246
247         private final Optional<ModificationApplyOperation> entryStrategy;
248
249         protected UnkeyedListModificationStrategy(final ListSchemaNode schema) {
250             entryStrategy = Optional.<ModificationApplyOperation> of(new DataNodeContainerModificationStrategy.UnkeyedListItemModificationStrategy(schema));
251         }
252
253         @Override
254         protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
255                 final Version version) {
256             return applyWrite(modification, Optional.of(currentMeta), version);
257         }
258
259         @Override
260         protected TreeNode applySubtreeChange(final ModifiedNode modification,
261                 final TreeNode currentMeta, final Version version) {
262             throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
263         }
264
265         @Override
266         protected TreeNode applyWrite(final ModifiedNode modification,
267                 final Optional<TreeNode> currentMeta, final Version version) {
268             final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
269             final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version);
270
271             if (Iterables.isEmpty(modification.getChildren())) {
272                 return newValueMeta;
273             }
274
275             /*
276              * This is where things get interesting. The user has performed a write and
277              * then she applied some more modifications to it. So we need to make sense
278              * of that an apply the operations on top of the written value. We could have
279              * done it during the write, but this operation is potentially expensive, so
280              * we have left it out of the fast path.
281              *
282              * As it turns out, once we materialize the written data, we can share the
283              * code path with the subtree change. So let's create an unsealed TreeNode
284              * and run the common parts on it -- which end with the node being sealed.
285              */
286             final MutableTreeNode mutable = newValueMeta.mutable();
287             mutable.setSubtreeVersion(version);
288
289             @SuppressWarnings("rawtypes")
290             final NormalizedNodeContainerBuilder dataBuilder = ImmutableUnkeyedListEntryNodeBuilder
291                 .create((UnkeyedListEntryNode) newValue);
292
293             return mutateChildren(mutable, dataBuilder, version, modification.getChildren());
294         }
295
296         /**
297          * Applies write/remove diff operation for each modification child in modification subtree.
298          * Operation also sets the Data tree references for each Tree Node (Index Node) in meta (MutableTreeNode) structure.
299          *
300          * @param meta MutableTreeNode (IndexTreeNode)
301          * @param data DataBuilder
302          * @param nodeVersion Version of TreeNode
303          * @param modifications modification operations to apply
304          * @return Sealed immutable copy of TreeNode structure with all Data Node references set.
305          */
306         @SuppressWarnings({ "rawtypes", "unchecked" })
307         private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
308             final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
309
310             for (ModifiedNode mod : modifications) {
311                 final PathArgument id = mod.getIdentifier();
312                 final Optional<TreeNode> cm = meta.getChild(id);
313
314                 Optional<TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
315                 if (result.isPresent()) {
316                     final TreeNode tn = result.get();
317                     meta.addChild(tn);
318                     data.addChild(tn.getData());
319                 } else {
320                     meta.removeChild(id);
321                     data.removeChild(id);
322                 }
323             }
324
325             meta.setData(data.build());
326             return meta.seal();
327         }
328
329         @Override
330         public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
331             if (child instanceof NodeIdentifier) {
332                 return entryStrategy;
333             }
334             return Optional.absent();
335         }
336
337         @Override
338         protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
339
340         }
341
342         @Override
343         protected void checkSubtreeModificationApplicable(final YangInstanceIdentifier path, final NodeModification modification,
344                 final Optional<TreeNode> current) throws IncorrectDataStructureException {
345             throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");
346         }
347     }
348 }