Split out yang-data-tree-{api,spi}
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / 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.tree.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Optional;
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.spi.tree.TreeNode;
16 import org.opendaylight.yangtools.yang.data.spi.tree.Version;
17
18 final class OperationWithModification {
19     private final ModificationApplyOperation applyOperation;
20     private final ModifiedNode modification;
21
22     private OperationWithModification(final ModificationApplyOperation op, final ModifiedNode mod) {
23         this.applyOperation = requireNonNull(op);
24         this.modification = requireNonNull(mod);
25     }
26
27     void write(final NormalizedNode value) {
28         modification.write(value);
29         /**
30          * Fast validation of structure, full validation on written data will be run during seal.
31          */
32         applyOperation.quickVerifyStructure(value);
33     }
34
35     void merge(final NormalizedNode data, final Version version) {
36         /*
37          * A merge operation will end up overwriting parts of the tree, retaining others. We want to
38          * make sure we do not validate the complete resulting structure, but rather just what was
39          * written. In order to do that, we first pretend the data was written, run verification and
40          * then perform the merge -- with the explicit assumption that adding the newly-validated
41          * data with the previously-validated data will not result in invalid data.
42          *
43          * We perform only quick validation here, full validation will be applied as-needed during
44          * preparation, as the merge is reconciled with current state.
45          */
46         applyOperation.quickVerifyStructure(data);
47         applyOperation.mergeIntoModifiedNode(modification, data, version);
48     }
49
50     void delete() {
51         modification.delete();
52     }
53
54     /**
55      * Read a particular child. If the child has been modified and does not have a stable
56      * view, one will we instantiated with specified version.
57      */
58     Optional<NormalizedNode> read(final PathArgument child, final Version version) {
59         final ModifiedNode childNode = modification.childByArg(child);
60         if (childNode != null) {
61             Optional<? extends TreeNode> snapshot = childNode.getSnapshot();
62             if (snapshot == null) {
63                 // Snapshot is not present, force instantiation
64                 snapshot = applyOperation.getChildByArg(child).apply(childNode, childNode.getOriginal(), version);
65             }
66
67             return snapshot.map(TreeNode::getData);
68         }
69
70         Optional<? extends TreeNode> snapshot = modification.getSnapshot();
71         if (snapshot == null) {
72             snapshot = apply(modification.getOriginal(), version);
73         }
74
75         if (snapshot.isPresent()) {
76             return snapshot.get().findChildByArg(child).map(TreeNode::getData);
77         }
78
79         return Optional.empty();
80     }
81
82     public ModifiedNode getModification() {
83         return modification;
84     }
85
86     public ModificationApplyOperation getApplyOperation() {
87         return applyOperation;
88     }
89
90     public Optional<? extends TreeNode> apply(final Optional<? extends 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 }