BUG-2882: implement DataTreeModificationCursor
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / OperationWithModification.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.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
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.NormalizedNodeContainer;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
18
19 final class OperationWithModification {
20     private static final Function<TreeNode, NormalizedNode<?, ?>> READ_DATA = new Function<TreeNode, NormalizedNode<?, ?>>() {
21         @Override
22         public NormalizedNode<?, ?> apply(final TreeNode input) {
23             return input.getData();
24         }
25     };
26
27     private final ModifiedNode modification;
28     private final ModificationApplyOperation applyOperation;
29
30     private OperationWithModification(final ModificationApplyOperation op, final ModifiedNode mod) {
31         this.modification = Preconditions.checkNotNull(mod);
32         this.applyOperation = Preconditions.checkNotNull(op);
33     }
34
35     void write(final NormalizedNode<?, ?> value) {
36         modification.write(value);
37         /**
38          * Fast validation of structure, full validation on written data will be run during seal.
39          */
40         applyOperation.verifyStructure(value, false);
41     }
42
43     private void recursiveMerge(final NormalizedNode<?,?> data) {
44         if (data instanceof NormalizedNodeContainer) {
45             @SuppressWarnings({ "rawtypes", "unchecked" })
46             final
47             NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> dataContainer = (NormalizedNodeContainer) data;
48
49             /*
50              * if there was write before on this node and it is of NormalizedNodeContainer type
51              * merge would overwrite our changes. So we create write modifications from data children to
52              * retain children created by past write operation.
53              * These writes will then be pushed down in the tree while there are merge modifications on these children
54              */
55             if (modification.getOperation() == LogicalOperation.WRITE) {
56                 @SuppressWarnings({ "rawtypes", "unchecked" })
57                 final
58                 NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> odlDataContainer =
59                         (NormalizedNodeContainer) modification.getWrittenValue();
60                 for (final NormalizedNode<PathArgument, ?> child : odlDataContainer.getValue()) {
61                     final PathArgument childId = child.getIdentifier();
62                     forChild(childId).write(child);
63                 }
64             }
65             for (final NormalizedNode<PathArgument, ?> child : dataContainer.getValue()) {
66                 final PathArgument childId = child.getIdentifier();
67                 forChild(childId).recursiveMerge(child);
68             }
69         }
70
71         modification.merge(data);
72     }
73
74     void merge(final NormalizedNode<?, ?> data) {
75         /*
76          * A merge operation will end up overwriting parts of the tree, retaining others. We want to
77          * make sure we do not validate the complete resulting structure, but rather just what was
78          * written. In order to do that, we first pretend the data was written, run verification and
79          * then perform the merge -- with the explicit assumption that adding the newly-validated
80          * data with the previously-validated data will not result in invalid data.
81          *
82          * FIXME: Should be this moved to recursive merge and run for each node?
83          */
84         applyOperation.verifyStructure(data, false);
85         recursiveMerge(data);
86     }
87
88     void delete() {
89         modification.delete();
90     }
91
92     /**
93      * Read a particular child. If the child has been modified and does not have a stable
94      * view, one will we instantiated with specified version.
95      *
96      * @param child
97      * @param version
98      * @return
99      */
100     Optional<NormalizedNode<?, ?>> read(final PathArgument child, final Version version) {
101         final Optional<ModifiedNode> maybeChild = modification.getChild(child);
102         if (maybeChild.isPresent()) {
103             final ModifiedNode childNode = maybeChild.get();
104
105             Optional<TreeNode> snapshot = childNode.getSnapshot();
106             if (snapshot == null) {
107                 // Snapshot is not present, force instantiation
108                 snapshot = applyOperation.getChild(child).get().apply(childNode, childNode.getOriginal(), version);
109             }
110
111             return snapshot.transform(READ_DATA);
112         }
113
114         Optional<TreeNode> snapshot = modification.getSnapshot();
115         if (snapshot == null) {
116             snapshot = apply(modification.getOriginal(), version);
117         }
118
119         if (snapshot.isPresent()) {
120             return snapshot.get().getChild(child).transform(READ_DATA);
121         }
122
123         return Optional.absent();
124     }
125
126     public ModifiedNode getModification() {
127         return modification;
128     }
129
130     public ModificationApplyOperation getApplyOperation() {
131         return applyOperation;
132     }
133
134     public Optional<TreeNode> apply(final Optional<TreeNode> data, final Version version) {
135         return applyOperation.apply(modification, data, version);
136     }
137
138     public static OperationWithModification from(final ModificationApplyOperation operation,
139             final ModifiedNode modification) {
140         return new OperationWithModification(operation, modification);
141     }
142
143     private OperationWithModification forChild(final PathArgument childId) {
144         final Optional<ModificationApplyOperation> maybeChildOp = applyOperation.getChild(childId);
145         Preconditions.checkArgument(maybeChildOp.isPresent(), "Attempted to apply operation to non-existent child %s", childId);
146
147         final ModificationApplyOperation childOp = maybeChildOp.get();
148         final ModifiedNode childMod = modification.modifyChild(childId, childOp.getChildPolicy());
149
150         return from(childOp, childMod);
151     }
152 }