Bug 2690 - Yang-Data-Impl: write and then merge on same list in
[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
11 import com.google.common.base.Optional;
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         applyOperation.verifyStructure(modification);
32     }
33
34     private void recursiveMerge(final NormalizedNode<?,?> data) {
35         if (data instanceof NormalizedNodeContainer<?,?,?>) {
36             @SuppressWarnings({ "rawtypes", "unchecked" })
37             NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> dataContainer = (NormalizedNodeContainer) data;
38
39             /*
40              * if there was write before on this node and it is of NormalizedNodeContainer type
41              * merge would overwrite our changes. So we create write modifications from data children to
42              * retain children created by past write operation.
43              * These writes will then be pushed down in the tree while there are merge modifications on these children
44              */
45             if (modification.getOperation().equals(LogicalOperation.WRITE)) {
46                 @SuppressWarnings({ "rawtypes", "unchecked" })
47                 NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> odlDataContainer =
48                         (NormalizedNodeContainer) modification.getWrittenValue();
49                 for (NormalizedNode<PathArgument, ?> child : odlDataContainer.getValue()) {
50                     PathArgument childId = child.getIdentifier();
51                     forChild(childId).write(child);
52                 }
53             }
54             for (NormalizedNode<PathArgument, ?> child : dataContainer.getValue()) {
55                 PathArgument childId = child.getIdentifier();
56                 forChild(childId).recursiveMerge(child);
57             }
58         }
59
60         modification.merge(data);
61     }
62
63     void merge(final NormalizedNode<?, ?> data) {
64         /*
65          * A merge operation will end up overwriting parts of the tree, retaining others.
66          * We want to make sure we do not validate the complete resulting structure, but
67          * rather just what was written. In order to do that, we first pretend the data
68          * was written, run verification and then perform the merge -- with the explicit
69          * assumption that adding the newly-validated data with the previously-validated
70          * data will not result in invalid data.
71          */
72         applyOperation.verifyStructure(modification.asNewlyWritten(data));
73         recursiveMerge(data);
74     }
75
76     void delete() {
77         modification.delete();
78     }
79
80     public ModifiedNode getModification() {
81         return modification;
82     }
83
84     public ModificationApplyOperation getApplyOperation() {
85         return applyOperation;
86     }
87
88     public Optional<TreeNode> apply(final Optional<TreeNode> data, final Version version) {
89         return applyOperation.apply(modification, data, version);
90     }
91
92     public static OperationWithModification from(final ModificationApplyOperation operation,
93             final ModifiedNode modification) {
94         return new OperationWithModification(operation, modification);
95     }
96
97     private OperationWithModification forChild(final PathArgument childId) {
98         ModificationApplyOperation childOp = applyOperation.getChild(childId).get();
99         ModifiedNode childMod = modification.modifyChild(childId, childOp.getChildPolicy());
100
101         return from(childOp,childMod);
102     }
103 }