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