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