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