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