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.collect.ImmutableMap;
12 import com.google.common.collect.Iterables;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
18 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModifiedNodeDoesNotExistException;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.MutableTreeNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
34 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
35 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
42 import static com.google.common.base.Preconditions.checkArgument;
44 abstract class NormalizedNodeContainerModificationStrategy extends SchemaAwareApplyOperation {
46 private final Class<? extends NormalizedNode<?, ?>> nodeClass;
48 protected NormalizedNodeContainerModificationStrategy(final Class<? extends NormalizedNode<?, ?>> nodeClass) {
49 this.nodeClass = nodeClass;
53 public void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException {
54 if (modification.getType() == ModificationType.WRITE) {
57 for (ModifiedNode childModification : modification.getChildren()) {
58 resolveChildOperation(childModification.getIdentifier()).verifyStructure(childModification);
62 @SuppressWarnings("rawtypes")
64 protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
65 checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
66 checkArgument(writtenValue instanceof NormalizedNodeContainer);
68 NormalizedNodeContainer container = (NormalizedNodeContainer) writtenValue;
69 for (Object child : container.getValue()) {
70 checkArgument(child instanceof NormalizedNode);
73 * FIXME: fail-fast semantics:
75 * We can validate the data structure here, aborting the commit
76 * before it ever progresses to being committed.
82 protected TreeNode applyWrite(final ModifiedNode modification,
83 final Optional<TreeNode> currentMeta, final Version version) {
84 final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
85 final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version);
87 if (Iterables.isEmpty(modification.getChildren())) {
92 * This is where things get interesting. The user has performed a write and
93 * then she applied some more modifications to it. So we need to make sense
94 * of that an apply the operations on top of the written value. We could have
95 * done it during the write, but this operation is potentially expensive, so
96 * we have left it out of the fast path.
98 * As it turns out, once we materialize the written data, we can share the
99 * code path with the subtree change. So let's create an unsealed TreeNode
100 * and run the common parts on it -- which end with the node being sealed.
102 final MutableTreeNode mutable = newValueMeta.mutable();
103 mutable.setSubtreeVersion(version);
105 @SuppressWarnings("rawtypes")
106 final NormalizedNodeContainerBuilder dataBuilder = createBuilder(newValue);
108 return mutateChildren(mutable, dataBuilder, version, modification.getChildren());
112 * Applies write/remove diff operation for each modification child in modification subtree.
113 * Operation also sets the Data tree references for each Tree Node (Index Node) in meta (MutableTreeNode) structure.
115 * @param meta MutableTreeNode (IndexTreeNode)
116 * @param data DataBuilder
117 * @param nodeVersion Version of TreeNode
118 * @param modifications modification operations to apply
119 * @return Sealed immutable copy of TreeNode structure with all Data Node references set.
121 @SuppressWarnings({ "rawtypes", "unchecked" })
122 private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
123 final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
125 for (ModifiedNode mod : modifications) {
126 final YangInstanceIdentifier.PathArgument id = mod.getIdentifier();
127 final Optional<TreeNode> cm = meta.getChild(id);
129 Optional<TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
130 if (result.isPresent()) {
131 final TreeNode tn = result.get();
133 data.addChild(tn.getData());
135 meta.removeChild(id);
136 data.removeChild(id);
140 meta.setData(data.build());
145 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
146 final Version version) {
147 // For Node Containers - merge is same as subtree change - we only replace children.
148 return applySubtreeChange(modification, currentMeta, version);
152 public TreeNode applySubtreeChange(final ModifiedNode modification,
153 final TreeNode currentMeta, final Version version) {
154 final MutableTreeNode newMeta = currentMeta.mutable();
155 newMeta.setSubtreeVersion(version);
157 @SuppressWarnings("rawtypes")
158 NormalizedNodeContainerBuilder dataBuilder = createBuilder(currentMeta.getData());
160 return mutateChildren(newMeta, dataBuilder, version, modification.getChildren());
164 protected void checkSubtreeModificationApplicable(final YangInstanceIdentifier path, final NodeModification modification,
165 final Optional<TreeNode> current) throws DataValidationFailedException {
166 if (!modification.getOriginal().isPresent() && !current.isPresent()) {
167 throw new ModifiedNodeDoesNotExistException(path, String.format("Node %s does not exist. Cannot apply modification to its children.", path));
170 SchemaAwareApplyOperation.checkConflicting(path, current.isPresent(), "Node was deleted by other transaction.");
171 checkChildPreconditions(path, modification, current);
174 private void checkChildPreconditions(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
175 final TreeNode currentMeta = current.get();
176 for (NodeModification childMod : modification.getChildren()) {
177 final YangInstanceIdentifier.PathArgument childId = childMod.getIdentifier();
178 final Optional<TreeNode> childMeta = currentMeta.getChild(childId);
180 YangInstanceIdentifier childPath = path.node(childId);
181 resolveChildOperation(childId).checkApplicable(childPath, childMod, childMeta);
186 protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
187 final Optional<TreeNode> current) throws DataValidationFailedException {
188 if(current.isPresent()) {
189 checkChildPreconditions(path, modification,current);
193 @SuppressWarnings("rawtypes")
194 protected abstract NormalizedNodeContainerBuilder createBuilder(NormalizedNode<?, ?> original);
196 public static class ChoiceModificationStrategy extends NormalizedNodeContainerModificationStrategy {
198 private final Map<YangInstanceIdentifier.PathArgument, ModificationApplyOperation> childNodes;
200 public ChoiceModificationStrategy(final ChoiceNode schemaNode) {
201 super(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode.class);
202 ImmutableMap.Builder<YangInstanceIdentifier.PathArgument, ModificationApplyOperation> child = ImmutableMap.builder();
204 for (ChoiceCaseNode caze : schemaNode.getCases()) {
205 for (DataSchemaNode cazeChild : caze.getChildNodes()) {
206 SchemaAwareApplyOperation childNode = SchemaAwareApplyOperation.from(cazeChild);
207 child.put(new YangInstanceIdentifier.NodeIdentifier(cazeChild.getQName()), childNode);
210 childNodes = child.build();
214 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument child) {
215 return Optional.fromNullable(childNodes.get(child));
219 @SuppressWarnings("rawtypes")
220 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
221 checkArgument(original instanceof org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode);
222 return ImmutableChoiceNodeBuilder.create((org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) original);
226 public static class OrderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
228 private final Optional<ModificationApplyOperation> entryStrategy;
230 @SuppressWarnings({ "unchecked", "rawtypes" })
231 protected OrderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
232 super((Class) LeafSetNode.class);
233 entryStrategy = Optional.<ModificationApplyOperation> of(new ValueNodeModificationStrategy.LeafSetEntryModificationStrategy(schema));
237 boolean isOrdered() {
241 @SuppressWarnings("rawtypes")
243 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
244 checkArgument(original instanceof OrderedLeafSetNode<?>);
245 return ImmutableOrderedLeafSetNodeBuilder.create((OrderedLeafSetNode<?>) original);
249 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
250 if (identifier instanceof YangInstanceIdentifier.NodeWithValue) {
251 return entryStrategy;
253 return Optional.absent();
257 public static class OrderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
259 private final Optional<ModificationApplyOperation> entryStrategy;
261 protected OrderedMapModificationStrategy(final ListSchemaNode schema) {
262 super(OrderedMapNode.class);
263 entryStrategy = Optional.<ModificationApplyOperation> of(new DataNodeContainerModificationStrategy.ListEntryModificationStrategy(schema));
267 boolean isOrdered() {
271 @SuppressWarnings("rawtypes")
273 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
274 checkArgument(original instanceof OrderedMapNode);
275 return ImmutableOrderedMapNodeBuilder.create((OrderedMapNode) original);
279 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
280 if (identifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
281 return entryStrategy;
283 return Optional.absent();
287 public String toString() {
288 return "OrderedMapModificationStrategy [entry=" + entryStrategy + "]";
292 public static class UnorderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
294 private final Optional<ModificationApplyOperation> entryStrategy;
296 @SuppressWarnings({ "unchecked", "rawtypes" })
297 protected UnorderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
298 super((Class) LeafSetNode.class);
299 entryStrategy = Optional.<ModificationApplyOperation> of(new ValueNodeModificationStrategy.LeafSetEntryModificationStrategy(schema));
302 @SuppressWarnings("rawtypes")
304 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
305 checkArgument(original instanceof LeafSetNode<?>);
306 return ImmutableLeafSetNodeBuilder.create((LeafSetNode<?>) original);
310 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
311 if (identifier instanceof YangInstanceIdentifier.NodeWithValue) {
312 return entryStrategy;
314 return Optional.absent();
318 public static class UnorderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
320 private final Optional<ModificationApplyOperation> entryStrategy;
322 protected UnorderedMapModificationStrategy(final ListSchemaNode schema) {
323 super(MapNode.class);
324 entryStrategy = Optional.<ModificationApplyOperation> of(new DataNodeContainerModificationStrategy.ListEntryModificationStrategy(schema));
327 @SuppressWarnings("rawtypes")
329 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
330 checkArgument(original instanceof MapNode);
331 return ImmutableMapNodeBuilder.create((MapNode) original);
335 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
336 if (identifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
337 return entryStrategy;
339 return Optional.absent();
343 public String toString() {
344 return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]";