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.Iterables;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.MutableTreeNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
22 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
24 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
26 final class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
28 private final Optional<ModificationApplyOperation> entryStrategy;
30 protected UnkeyedListModificationStrategy(final ListSchemaNode schema) {
31 entryStrategy = Optional.<ModificationApplyOperation> of(new DataNodeContainerModificationStrategy.UnkeyedListItemModificationStrategy(schema));
40 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
41 final Version version) {
42 return applyWrite(modification, Optional.of(currentMeta), version);
46 protected TreeNode applySubtreeChange(final ModifiedNode modification,
47 final TreeNode currentMeta, final Version version) {
48 throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
52 protected TreeNode applyWrite(final ModifiedNode modification,
53 final Optional<TreeNode> currentMeta, final Version version) {
54 final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
55 final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version);
57 if (Iterables.isEmpty(modification.getChildren())) {
62 * This is where things get interesting. The user has performed a write and
63 * then she applied some more modifications to it. So we need to make sense
64 * of that an apply the operations on top of the written value. We could have
65 * done it during the write, but this operation is potentially expensive, so
66 * we have left it out of the fast path.
68 * As it turns out, once we materialize the written data, we can share the
69 * code path with the subtree change. So let's create an unsealed TreeNode
70 * and run the common parts on it -- which end with the node being sealed.
72 final MutableTreeNode mutable = newValueMeta.mutable();
73 mutable.setSubtreeVersion(version);
75 @SuppressWarnings("rawtypes")
76 final NormalizedNodeContainerBuilder dataBuilder = ImmutableUnkeyedListEntryNodeBuilder
77 .create((UnkeyedListEntryNode) newValue);
79 return mutateChildren(mutable, dataBuilder, version, modification.getChildren());
83 * Applies write/remove diff operation for each modification child in modification subtree.
84 * Operation also sets the Data tree references for each Tree Node (Index Node) in meta (MutableTreeNode) structure.
86 * @param meta MutableTreeNode (IndexTreeNode)
87 * @param data DataBuilder
88 * @param nodeVersion Version of TreeNode
89 * @param modifications modification operations to apply
90 * @return Sealed immutable copy of TreeNode structure with all Data Node references set.
92 @SuppressWarnings({ "rawtypes", "unchecked" })
93 private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
94 final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
96 for (ModifiedNode mod : modifications) {
97 final PathArgument id = mod.getIdentifier();
98 final Optional<TreeNode> cm = meta.getChild(id);
100 Optional<TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
101 if (result.isPresent()) {
102 final TreeNode tn = result.get();
104 data.addChild(tn.getData());
106 meta.removeChild(id);
107 data.removeChild(id);
111 meta.setData(data.build());
116 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
117 if (child instanceof NodeIdentifier) {
118 return entryStrategy;
120 return Optional.absent();
124 protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
129 protected void checkSubtreeModificationApplicable(final YangInstanceIdentifier path, final NodeModification modification,
130 final Optional<TreeNode> current) throws IncorrectDataStructureException {
131 throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");