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 com.google.common.base.Optional;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.Iterables;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
20 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModifiedNodeDoesNotExistException;
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.DataContainerNodeBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
35 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
36 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
41 abstract class NormalizedNodeContainerModificationStrategy extends SchemaAwareApplyOperation {
43 private final Class<? extends NormalizedNode<?, ?>> nodeClass;
45 protected NormalizedNodeContainerModificationStrategy(final Class<? extends NormalizedNode<?, ?>> nodeClass) {
46 this.nodeClass = nodeClass;
50 public void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException {
51 for (ModifiedNode childModification : modification.getChildren()) {
52 resolveChildOperation(childModification.getIdentifier()).verifyStructure(childModification);
56 @SuppressWarnings("rawtypes")
58 protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
59 checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
60 checkArgument(writtenValue instanceof NormalizedNodeContainer);
62 NormalizedNodeContainer container = (NormalizedNodeContainer) writtenValue;
63 for (Object child : container.getValue()) {
64 checkArgument(child instanceof NormalizedNode);
67 * FIXME: fail-fast semantics:
69 * We can validate the data structure here, aborting the commit
70 * before it ever progresses to being committed.
76 protected TreeNode applyWrite(final ModifiedNode modification,
77 final Optional<TreeNode> currentMeta, final Version version) {
78 final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
79 final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version);
81 if (Iterables.isEmpty(modification.getChildren())) {
86 * This is where things get interesting. The user has performed a write and
87 * then she applied some more modifications to it. So we need to make sense
88 * of that an apply the operations on top of the written value. We could have
89 * done it during the write, but this operation is potentially expensive, so
90 * we have left it out of the fast path.
92 * As it turns out, once we materialize the written data, we can share the
93 * code path with the subtree change. So let's create an unsealed TreeNode
94 * and run the common parts on it -- which end with the node being sealed.
96 final MutableTreeNode mutable = newValueMeta.mutable();
97 mutable.setSubtreeVersion(version);
99 @SuppressWarnings("rawtypes")
100 final NormalizedNodeContainerBuilder dataBuilder = createBuilder(newValue);
102 return mutateChildren(mutable, dataBuilder, version, modification.getChildren());
106 * Applies write/remove diff operation for each modification child in modification subtree.
107 * Operation also sets the Data tree references for each Tree Node (Index Node) in meta (MutableTreeNode) structure.
109 * @param meta MutableTreeNode (IndexTreeNode)
110 * @param data DataBuilder
111 * @param nodeVersion Version of TreeNode
112 * @param modifications modification operations to apply
113 * @return Sealed immutable copy of TreeNode structure with all Data Node references set.
115 @SuppressWarnings({ "rawtypes", "unchecked" })
116 private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
117 final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
119 for (ModifiedNode mod : modifications) {
120 final YangInstanceIdentifier.PathArgument id = mod.getIdentifier();
121 final Optional<TreeNode> cm = meta.getChild(id);
123 Optional<TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
124 if (result.isPresent()) {
125 final TreeNode tn = result.get();
127 data.addChild(tn.getData());
129 meta.removeChild(id);
130 data.removeChild(id);
134 meta.setData(data.build());
139 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
140 final Version version) {
141 // For Node Containers - merge is same as subtree change - we only replace children.
142 return applySubtreeChange(modification, currentMeta, version);
146 public TreeNode applySubtreeChange(final ModifiedNode modification,
147 final TreeNode currentMeta, final Version version) {
148 final MutableTreeNode newMeta = currentMeta.mutable();
149 newMeta.setSubtreeVersion(version);
151 @SuppressWarnings("rawtypes")
152 NormalizedNodeContainerBuilder dataBuilder = createBuilder(currentMeta.getData());
154 return mutateChildren(newMeta, dataBuilder, version, modification.getChildren());
158 protected void checkSubtreeModificationApplicable(final YangInstanceIdentifier path, final NodeModification modification,
159 final Optional<TreeNode> current) throws DataValidationFailedException {
160 if (!modification.getOriginal().isPresent() && !current.isPresent()) {
161 throw new ModifiedNodeDoesNotExistException(path, String.format("Node %s does not exist. Cannot apply modification to its children.", path));
164 SchemaAwareApplyOperation.checkConflicting(path, current.isPresent(), "Node was deleted by other transaction.");
165 checkChildPreconditions(path, modification, current);
168 private void checkChildPreconditions(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
169 final TreeNode currentMeta = current.get();
170 for (NodeModification childMod : modification.getChildren()) {
171 final YangInstanceIdentifier.PathArgument childId = childMod.getIdentifier();
172 final Optional<TreeNode> childMeta = currentMeta.getChild(childId);
174 YangInstanceIdentifier childPath = path.node(childId);
175 resolveChildOperation(childId).checkApplicable(childPath, childMod, childMeta);
180 protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
181 final Optional<TreeNode> current) throws DataValidationFailedException {
182 if(current.isPresent()) {
183 checkChildPreconditions(path, modification,current);
187 @SuppressWarnings("rawtypes")
188 protected abstract NormalizedNodeContainerBuilder createBuilder(NormalizedNode<?, ?> original);
190 public static class ChoiceModificationStrategy extends NormalizedNodeContainerModificationStrategy {
192 private final Map<YangInstanceIdentifier.PathArgument, ModificationApplyOperation> childNodes;
194 public ChoiceModificationStrategy(final ChoiceNode schemaNode) {
195 super(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode.class);
196 ImmutableMap.Builder<YangInstanceIdentifier.PathArgument, ModificationApplyOperation> child = ImmutableMap.builder();
198 for (ChoiceCaseNode caze : schemaNode.getCases()) {
199 for (DataSchemaNode cazeChild : caze.getChildNodes()) {
200 SchemaAwareApplyOperation childNode = SchemaAwareApplyOperation.from(cazeChild);
201 child.put(new YangInstanceIdentifier.NodeIdentifier(cazeChild.getQName()), childNode);
204 childNodes = child.build();
208 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument child) {
209 return Optional.fromNullable(childNodes.get(child));
213 @SuppressWarnings("rawtypes")
214 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
215 checkArgument(original instanceof org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode);
216 return ImmutableChoiceNodeBuilder.create((org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) original);
220 public static class OrderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
222 private final Optional<ModificationApplyOperation> entryStrategy;
224 @SuppressWarnings({ "unchecked", "rawtypes" })
225 protected OrderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
226 super((Class) LeafSetNode.class);
227 entryStrategy = Optional.<ModificationApplyOperation> of(new ValueNodeModificationStrategy.LeafSetEntryModificationStrategy(schema));
231 boolean isOrdered() {
235 @SuppressWarnings("rawtypes")
237 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
238 checkArgument(original instanceof OrderedLeafSetNode<?>);
239 return ImmutableOrderedLeafSetNodeBuilder.create((OrderedLeafSetNode<?>) original);
243 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
244 if (identifier instanceof YangInstanceIdentifier.NodeWithValue) {
245 return entryStrategy;
247 return Optional.absent();
251 public static class OrderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
253 private final Optional<ModificationApplyOperation> entryStrategy;
255 protected OrderedMapModificationStrategy(final ListSchemaNode schema) {
256 super(OrderedMapNode.class);
257 entryStrategy = Optional.<ModificationApplyOperation> of(new DataNodeContainerModificationStrategy.ListEntryModificationStrategy(schema));
261 boolean isOrdered() {
265 @SuppressWarnings("rawtypes")
267 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
268 checkArgument(original instanceof OrderedMapNode);
269 return ImmutableOrderedMapNodeBuilder.create((OrderedMapNode) original);
273 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
274 if (identifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
275 return entryStrategy;
277 return Optional.absent();
281 public String toString() {
282 return "OrderedMapModificationStrategy [entry=" + entryStrategy + "]";
286 public static class UnorderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
288 private final Optional<ModificationApplyOperation> entryStrategy;
290 @SuppressWarnings({ "unchecked", "rawtypes" })
291 protected UnorderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
292 super((Class) LeafSetNode.class);
293 entryStrategy = Optional.<ModificationApplyOperation> of(new ValueNodeModificationStrategy.LeafSetEntryModificationStrategy(schema));
296 @SuppressWarnings("rawtypes")
298 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
299 checkArgument(original instanceof LeafSetNode<?>);
300 return ImmutableLeafSetNodeBuilder.create((LeafSetNode<?>) original);
304 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
305 if (identifier instanceof YangInstanceIdentifier.NodeWithValue) {
306 return entryStrategy;
308 return Optional.absent();
312 public static class UnorderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
314 private final Optional<ModificationApplyOperation> entryStrategy;
316 protected UnorderedMapModificationStrategy(final ListSchemaNode schema) {
317 super(MapNode.class);
318 entryStrategy = Optional.<ModificationApplyOperation> of(new DataNodeContainerModificationStrategy.ListEntryModificationStrategy(schema));
321 @SuppressWarnings("rawtypes")
323 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
324 checkArgument(original instanceof MapNode);
325 return ImmutableMapNodeBuilder.create((MapNode) original);
329 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
330 if (identifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
331 return entryStrategy;
333 return Optional.absent();
337 public String toString() {
338 return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]";