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());
111 @SuppressWarnings({ "rawtypes", "unchecked" })
112 private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
113 final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
115 for (ModifiedNode mod : modifications) {
116 final YangInstanceIdentifier.PathArgument id = mod.getIdentifier();
117 final Optional<TreeNode> cm = meta.getChild(id);
119 Optional<TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
120 if (result.isPresent()) {
121 final TreeNode tn = result.get();
123 data.addChild(tn.getData());
125 meta.removeChild(id);
126 data.removeChild(id);
130 meta.setData(data.build());
135 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
136 final Version version) {
137 // For Node Containers - merge is same as subtree change - we only replace children.
138 return applySubtreeChange(modification, currentMeta, version);
142 public TreeNode applySubtreeChange(final ModifiedNode modification,
143 final TreeNode currentMeta, final Version version) {
144 final MutableTreeNode newMeta = currentMeta.mutable();
145 newMeta.setSubtreeVersion(version);
147 @SuppressWarnings("rawtypes")
148 NormalizedNodeContainerBuilder dataBuilder = createBuilder(currentMeta.getData());
150 return mutateChildren(newMeta, dataBuilder, version, modification.getChildren());
154 protected void checkSubtreeModificationApplicable(final YangInstanceIdentifier path, final NodeModification modification,
155 final Optional<TreeNode> current) throws DataValidationFailedException {
156 if (!modification.getOriginal().isPresent() && !current.isPresent()) {
157 throw new ModifiedNodeDoesNotExistException(path, String.format("Node %s does not exist. Cannot apply modification to its children.", path));
160 SchemaAwareApplyOperation.checkConflicting(path, current.isPresent(), "Node was deleted by other transaction.");
161 checkChildPreconditions(path, modification, current);
164 private void checkChildPreconditions(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
165 final TreeNode currentMeta = current.get();
166 for (NodeModification childMod : modification.getChildren()) {
167 final YangInstanceIdentifier.PathArgument childId = childMod.getIdentifier();
168 final Optional<TreeNode> childMeta = currentMeta.getChild(childId);
170 YangInstanceIdentifier childPath = path.node(childId);
171 resolveChildOperation(childId).checkApplicable(childPath, childMod, childMeta);
176 protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
177 final Optional<TreeNode> current) throws DataValidationFailedException {
178 if(current.isPresent()) {
179 checkChildPreconditions(path, modification,current);
183 @SuppressWarnings("rawtypes")
184 protected abstract NormalizedNodeContainerBuilder createBuilder(NormalizedNode<?, ?> original);
186 public static class ChoiceModificationStrategy extends NormalizedNodeContainerModificationStrategy {
188 private final Map<YangInstanceIdentifier.PathArgument, ModificationApplyOperation> childNodes;
190 public ChoiceModificationStrategy(final ChoiceNode schemaNode) {
191 super(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode.class);
192 ImmutableMap.Builder<YangInstanceIdentifier.PathArgument, ModificationApplyOperation> child = ImmutableMap.builder();
194 for (ChoiceCaseNode caze : schemaNode.getCases()) {
195 for (DataSchemaNode cazeChild : caze.getChildNodes()) {
196 SchemaAwareApplyOperation childNode = SchemaAwareApplyOperation.from(cazeChild);
197 child.put(new YangInstanceIdentifier.NodeIdentifier(cazeChild.getQName()), childNode);
200 childNodes = child.build();
204 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument child) {
205 return Optional.fromNullable(childNodes.get(child));
209 @SuppressWarnings("rawtypes")
210 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
211 checkArgument(original instanceof org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode);
212 return ImmutableChoiceNodeBuilder.create((org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) original);
216 public static class OrderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
218 private final Optional<ModificationApplyOperation> entryStrategy;
220 @SuppressWarnings({ "unchecked", "rawtypes" })
221 protected OrderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
222 super((Class) LeafSetNode.class);
223 entryStrategy = Optional.<ModificationApplyOperation> of(new ValueNodeModificationStrategy.LeafSetEntryModificationStrategy(schema));
226 @SuppressWarnings("rawtypes")
228 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
229 checkArgument(original instanceof OrderedLeafSetNode<?>);
230 return ImmutableOrderedLeafSetNodeBuilder.create((OrderedLeafSetNode<?>) original);
234 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
235 if (identifier instanceof YangInstanceIdentifier.NodeWithValue) {
236 return entryStrategy;
238 return Optional.absent();
242 public static class OrderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
244 private final Optional<ModificationApplyOperation> entryStrategy;
246 protected OrderedMapModificationStrategy(final ListSchemaNode schema) {
247 super(OrderedMapNode.class);
248 entryStrategy = Optional.<ModificationApplyOperation> of(new DataNodeContainerModificationStrategy.ListEntryModificationStrategy(schema));
251 @SuppressWarnings("rawtypes")
253 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
254 checkArgument(original instanceof OrderedMapNode);
255 return ImmutableOrderedMapNodeBuilder.create((OrderedMapNode) original);
259 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
260 if (identifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
261 return entryStrategy;
263 return Optional.absent();
267 public String toString() {
268 return "OrderedMapModificationStrategy [entry=" + entryStrategy + "]";
272 public static class UnorderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
274 private final Optional<ModificationApplyOperation> entryStrategy;
276 @SuppressWarnings({ "unchecked", "rawtypes" })
277 protected UnorderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
278 super((Class) LeafSetNode.class);
279 entryStrategy = Optional.<ModificationApplyOperation> of(new ValueNodeModificationStrategy.LeafSetEntryModificationStrategy(schema));
282 @SuppressWarnings("rawtypes")
284 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
285 checkArgument(original instanceof LeafSetNode<?>);
286 return ImmutableLeafSetNodeBuilder.create((LeafSetNode<?>) original);
290 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
291 if (identifier instanceof YangInstanceIdentifier.NodeWithValue) {
292 return entryStrategy;
294 return Optional.absent();
298 public static class UnorderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
300 private final Optional<ModificationApplyOperation> entryStrategy;
302 protected UnorderedMapModificationStrategy(final ListSchemaNode schema) {
303 super(MapNode.class);
304 entryStrategy = Optional.<ModificationApplyOperation> of(new DataNodeContainerModificationStrategy.ListEntryModificationStrategy(schema));
307 @SuppressWarnings("rawtypes")
309 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
310 checkArgument(original instanceof MapNode);
311 return ImmutableMapNodeBuilder.create((MapNode) original);
315 public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
316 if (identifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
317 return entryStrategy;
319 return Optional.absent();
323 public String toString() {
324 return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]";