32d73c9b61876aeeae38438c08a2eb71e1e87034
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / UnkeyedListModificationStrategy.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 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.MutableTreeNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
25 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
26
27 final class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
28
29     private final Optional<ModificationApplyOperation> entryStrategy;
30
31     UnkeyedListModificationStrategy(final ListSchemaNode schema, final TreeType treeType) {
32         entryStrategy = Optional.<ModificationApplyOperation> of(new UnkeyedListItemModificationStrategy(schema, treeType));
33     }
34
35     @Override
36     protected ChildTrackingPolicy getChildPolicy() {
37         return ChildTrackingPolicy.ORDERED;
38     }
39
40     @Override
41     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
42             final Version version) {
43         modification.resolveModificationType(ModificationType.WRITE);
44         return applyWrite(modification, Optional.of(currentMeta), version);
45     }
46
47     @Override
48     protected TreeNode applyTouch(final ModifiedNode modification,
49             final TreeNode currentMeta, final Version version) {
50         throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
51     }
52
53     @Override
54     protected TreeNode applyWrite(final ModifiedNode modification,
55             final Optional<TreeNode> currentMeta, final Version version) {
56         final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
57         final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version);
58
59         if (modification.getChildren().isEmpty()) {
60             return newValueMeta;
61         }
62
63         /*
64          * This is where things get interesting. The user has performed a write and
65          * then she applied some more modifications to it. So we need to make sense
66          * of that an apply the operations on top of the written value. We could have
67          * done it during the write, but this operation is potentially expensive, so
68          * we have left it out of the fast path.
69          *
70          * As it turns out, once we materialize the written data, we can share the
71          * code path with the subtree change. So let's create an unsealed TreeNode
72          * and run the common parts on it -- which end with the node being sealed.
73          */
74         final MutableTreeNode mutable = newValueMeta.mutable();
75         mutable.setSubtreeVersion(version);
76
77         @SuppressWarnings("rawtypes")
78         final NormalizedNodeContainerBuilder dataBuilder = ImmutableUnkeyedListEntryNodeBuilder
79             .create((UnkeyedListEntryNode) newValue);
80
81         return mutateChildren(mutable, dataBuilder, version, modification.getChildren());
82     }
83
84     /**
85      * Applies write/remove diff operation for each modification child in modification subtree.
86      * Operation also sets the Data tree references for each Tree Node (Index Node) in meta (MutableTreeNode) structure.
87      *
88      * @param meta MutableTreeNode (IndexTreeNode)
89      * @param data DataBuilder
90      * @param nodeVersion Version of TreeNode
91      * @param modifications modification operations to apply
92      * @return Sealed immutable copy of TreeNode structure with all Data Node references set.
93      */
94     @SuppressWarnings({ "rawtypes", "unchecked" })
95     private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data,
96         final Version nodeVersion, final Iterable<ModifiedNode> modifications) {
97
98         for (final ModifiedNode mod : modifications) {
99             final PathArgument id = mod.getIdentifier();
100             final Optional<TreeNode> cm = meta.getChild(id);
101
102             final Optional<TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
103             if (result.isPresent()) {
104                 final TreeNode tn = result.get();
105                 meta.addChild(tn);
106                 data.addChild(tn.getData());
107             } else {
108                 meta.removeChild(id);
109                 data.removeChild(id);
110             }
111         }
112
113         meta.setData(data.build());
114         return meta.seal();
115     }
116
117     @Override
118     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
119         if (child instanceof NodeIdentifier) {
120             return entryStrategy;
121         }
122         return Optional.absent();
123     }
124
125     @Override
126     protected void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
127
128     }
129
130     @Override
131     protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
132             final Optional<TreeNode> current) throws IncorrectDataStructureException {
133         throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");
134     }
135 }