Clean up TreeNode API
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / AutomaticLifecycleMixin.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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 com.google.common.base.Verify.verifyNotNull;
11
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
16 import org.opendaylight.yangtools.yang.data.tree.api.ModificationType;
17 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
18 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
19
20 /**
21  * Mixin-type support class for subclasses of {@link ModificationApplyOperation} which need to provide automatic
22  * lifecycle management.
23  */
24 final class AutomaticLifecycleMixin {
25     /**
26      * This is a capture of {@link ModificationApplyOperation#apply(ModifiedNode, TreeNode, Version)}.
27      */
28     @FunctionalInterface
29     interface Apply {
30         @Nullable TreeNode apply(ModifiedNode modification, @Nullable TreeNode currentMeta, Version version);
31     }
32
33     /**
34      * This is a capture of
35      * {@link SchemaAwareApplyOperation#applyWrite(ModifiedNode, NormalizedNode, TreeNode, Version)}.
36      */
37     @FunctionalInterface
38     interface ApplyWrite {
39         TreeNode applyWrite(ModifiedNode modification, NormalizedNode newValue, @Nullable TreeNode currentMeta,
40             Version version);
41     }
42
43     private AutomaticLifecycleMixin() {
44         // Hidden on purpose
45     }
46
47     static @Nullable TreeNode apply(final Apply delegate, final ApplyWrite writeDelegate,
48             final NormalizedNode emptyNode, final ModifiedNode modification, final @Nullable TreeNode currentMeta,
49             final Version version) {
50         final @Nullable TreeNode ret;
51         if (modification.getOperation() == LogicalOperation.DELETE) {
52             if (modification.isEmpty()) {
53                 return delegate.apply(modification, currentMeta, version);
54             }
55             // Delete with children, implies it really is an empty write
56             ret = verifyNotNull(writeDelegate.applyWrite(modification, emptyNode, currentMeta, version));
57         } else if (modification.getOperation() == LogicalOperation.TOUCH && currentMeta == null) {
58             ret = applyTouch(delegate, emptyNode, modification, null, version);
59         } else {
60             // No special handling required here, run normal apply operation
61             ret = delegate.apply(modification, currentMeta, version);
62         }
63
64         return ret == null ? null : disappearResult(modification, ret, currentMeta);
65     }
66
67     private static @Nullable TreeNode applyTouch(final Apply delegate, final NormalizedNode emptyNode,
68             final ModifiedNode modification, final @Nullable TreeNode currentMeta, final Version version) {
69         // Container is not present, let's take care of the 'magically appear' part of our job
70         final var ret = delegate.apply(modification, fakeMeta(emptyNode, version), version);
71
72         // If the delegate indicated SUBTREE_MODIFIED, account for the fake and report APPEARED
73         if (modification.getModificationType() == ModificationType.SUBTREE_MODIFIED) {
74             modification.resolveModificationType(ModificationType.APPEARED);
75         }
76         return ret;
77     }
78
79     private static @Nullable TreeNode disappearResult(final ModifiedNode modification, final @NonNull TreeNode result,
80             final @Nullable TreeNode currentMeta) {
81         // Check if the result is in fact empty before pulling any tricks
82         final var data = result.data();
83         if (!(data instanceof NormalizedNodeContainer<?> container)) {
84             throw new IllegalStateException("Unhandled data " + data);
85         }
86         if (!container.isEmpty()) {
87             return result;
88         }
89
90         // We are pulling the 'disappear' trick, but what we report can be three different things
91         final ModificationType finalType;
92         if (currentMeta == null) {
93             // ... there was nothing in the datastore, no change
94             finalType = ModificationType.UNMODIFIED;
95         } else if (modification.getModificationType() == ModificationType.WRITE) {
96             // ... this was an empty write, possibly originally a delete
97             finalType = ModificationType.DELETE;
98         } else {
99             // ... it really disappeared
100             finalType = ModificationType.DISAPPEARED;
101         }
102         modification.resolveModificationType(finalType);
103         return null;
104     }
105
106     private static @NonNull TreeNode fakeMeta(final NormalizedNode emptyNode, final Version version) {
107         return TreeNode.of(emptyNode, version);
108     }
109 }