BUG-1014: Moved recursive verify of written data to ready()
[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.Optional;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
13 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
17
18 final class OperationWithModification {
19
20     private final ModifiedNode modification;
21
22     private final ModificationApplyOperation applyOperation;
23
24     private OperationWithModification(final ModificationApplyOperation op, final ModifiedNode mod) {
25         this.modification = mod;
26         this.applyOperation = op;
27     }
28
29     void write(final NormalizedNode<?, ?> value) {
30         modification.write(value);
31         /**
32          * Fast validation of structure, full validation on written data will be run during seal.
33          */
34         applyOperation.verifyStructure(value, false);
35     }
36
37     private void recursiveMerge(final NormalizedNode<?,?> data) {
38         if (data instanceof NormalizedNodeContainer<?,?,?>) {
39             @SuppressWarnings({ "rawtypes", "unchecked" })
40             final
41             NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> dataContainer = (NormalizedNodeContainer) data;
42
43             /*
44              * if there was write before on this node and it is of NormalizedNodeContainer type
45              * merge would overwrite our changes. So we create write modifications from data children to
46              * retain children created by past write operation.
47              * These writes will then be pushed down in the tree while there are merge modifications on these children
48              */
49             if (modification.getOperation().equals(LogicalOperation.WRITE)) {
50                 @SuppressWarnings({ "rawtypes", "unchecked" })
51                 final
52                 NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> odlDataContainer =
53                         (NormalizedNodeContainer) modification.getWrittenValue();
54                 for (final NormalizedNode<PathArgument, ?> child : odlDataContainer.getValue()) {
55                     final PathArgument childId = child.getIdentifier();
56                     forChild(childId).write(child);
57                 }
58             }
59             for (final NormalizedNode<PathArgument, ?> child : dataContainer.getValue()) {
60                 final PathArgument childId = child.getIdentifier();
61                 forChild(childId).recursiveMerge(child);
62             }
63         }
64
65         modification.merge(data);
66     }
67
68     void merge(final NormalizedNode<?, ?> data) {
69         /*
70          * A merge operation will end up overwriting parts of the tree, retaining others. We want to
71          * make sure we do not validate the complete resulting structure, but rather just what was
72          * written. In order to do that, we first pretend the data was written, run verification and
73          * then perform the merge -- with the explicit assumption that adding the newly-validated
74          * data with the previously-validated data will not result in invalid data.
75          *
76          * FIXME: Should be this moved to recursive merge and run for each node?
77          */
78         applyOperation.verifyStructure(data, false);
79         recursiveMerge(data);
80     }
81
82     void delete() {
83         modification.delete();
84     }
85
86     public ModifiedNode getModification() {
87         return modification;
88     }
89
90     public ModificationApplyOperation getApplyOperation() {
91         return applyOperation;
92     }
93
94     public Optional<TreeNode> apply(final Optional<TreeNode> data, final Version version) {
95         return applyOperation.apply(modification, data, version);
96     }
97
98     public static OperationWithModification from(final ModificationApplyOperation operation,
99             final ModifiedNode modification) {
100         return new OperationWithModification(operation, modification);
101     }
102
103     private OperationWithModification forChild(final PathArgument childId) {
104         final Optional<ModificationApplyOperation> maybeChildOp = applyOperation.getChild(childId);
105         Preconditions.checkArgument(maybeChildOp.isPresent(), "Attempted to apply operation to non-existent child %s", childId);
106
107         final ModificationApplyOperation childOp = maybeChildOp.get();
108         final ModifiedNode childMod = modification.modifyChild(childId, childOp.getChildPolicy());
109
110         return from(childOp, childMod);
111     }
112 }