Bug 5968: Mandatory leaf enforcement does not work in some cases
[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.tree.spi.TreeNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
16
17 final class OperationWithModification {
18     private final ModificationApplyOperation applyOperation;
19     private final ModifiedNode modification;
20
21     private OperationWithModification(final ModificationApplyOperation op, final ModifiedNode mod) {
22         this.applyOperation = Preconditions.checkNotNull(op);
23         this.modification = Preconditions.checkNotNull(mod);
24     }
25
26     void write(final NormalizedNode<?, ?> value) {
27         modification.write(value);
28         /**
29          * Fast validation of structure, full validation on written data will be run during seal.
30          */
31         applyOperation.verifyStructure(value, false);
32     }
33
34     void merge(final NormalizedNode<?, ?> data, final Version version) {
35         /*
36          * A merge operation will end up overwriting parts of the tree, retaining others. We want to
37          * make sure we do not validate the complete resulting structure, but rather just what was
38          * written. In order to do that, we first pretend the data was written, run verification and
39          * then perform the merge -- with the explicit assumption that adding the newly-validated
40          * data with the previously-validated data will not result in invalid data.
41          *
42          * Fast validation of structure, full validation on data will be run during seal.
43          */
44         applyOperation.verifyStructure(data, false);
45         applyOperation.mergeIntoModifiedNode(modification, data, version);
46     }
47
48     void delete() {
49         modification.delete();
50     }
51
52     /**
53      * Read a particular child. If the child has been modified and does not have a stable
54      * view, one will we instantiated with specified version.
55      *
56      * @param child
57      * @param version
58      * @return
59      */
60     Optional<NormalizedNode<?, ?>> read(final PathArgument child, final Version version) {
61         final Optional<ModifiedNode> maybeChild = modification.getChild(child);
62         if (maybeChild.isPresent()) {
63             final ModifiedNode childNode = maybeChild.get();
64
65             Optional<TreeNode> snapshot = childNode.getSnapshot();
66             if (snapshot == null) {
67                 // Snapshot is not present, force instantiation
68                 snapshot = applyOperation.getChild(child).get().apply(childNode, childNode.getOriginal(), version);
69             }
70
71             return snapshot.transform(TreeNode::getData);
72         }
73
74         Optional<TreeNode> snapshot = modification.getSnapshot();
75         if (snapshot == null) {
76             snapshot = apply(modification.getOriginal(), version);
77         }
78
79         if (snapshot.isPresent()) {
80             return snapshot.get().getChild(child).transform(TreeNode::getData);
81         }
82
83         return Optional.absent();
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 }