Populate data/ hierarchy
[yangtools.git] / data / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / 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.impl.schema.tree;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import java.util.Optional;
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.ModificationType;
16 import org.opendaylight.yangtools.yang.data.spi.tree.TreeNode;
17 import org.opendaylight.yangtools.yang.data.spi.tree.TreeNodeFactory;
18 import org.opendaylight.yangtools.yang.data.spi.tree.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, Optional, Version)}.
27      */
28     @FunctionalInterface
29     interface Apply {
30         Optional<? extends TreeNode> apply(ModifiedNode modification, Optional<? extends TreeNode> storeMeta,
31                 Version version);
32     }
33
34     /**
35      * This is a capture of
36      * {@link SchemaAwareApplyOperation#applyWrite(ModifiedNode, NormalizedNode, Optional, Version)}.
37      */
38     @FunctionalInterface
39     interface ApplyWrite {
40         TreeNode applyWrite(ModifiedNode modification, NormalizedNode newValue,
41                 Optional<? extends TreeNode> storeMeta, Version version);
42     }
43
44     private AutomaticLifecycleMixin() {
45
46     }
47
48     static Optional<? extends TreeNode> apply(final Apply delegate, final ApplyWrite writeDelegate,
49             final NormalizedNode emptyNode, final ModifiedNode modification,
50             final Optional<? extends TreeNode> storeMeta, final Version version) {
51         final Optional<? extends TreeNode> ret;
52         if (modification.getOperation() == LogicalOperation.DELETE) {
53             if (modification.getChildren().isEmpty()) {
54                 return delegate.apply(modification, storeMeta, version);
55             }
56             // Delete with children, implies it really is an empty write
57             ret = Optional.of(writeDelegate.applyWrite(modification, emptyNode, storeMeta, version));
58         } else if (modification.getOperation() == LogicalOperation.TOUCH && !storeMeta.isPresent()) {
59             ret = applyTouch(delegate, emptyNode, modification, storeMeta, version);
60         } else {
61             // No special handling required here, run normal apply operation
62             ret = delegate.apply(modification, storeMeta, version);
63         }
64
65         return ret.isPresent() ? disappearResult(modification, ret.get(), storeMeta) : ret;
66     }
67
68     private static Optional<? extends TreeNode> applyTouch(final Apply delegate, final NormalizedNode emptyNode,
69             final ModifiedNode modification, final Optional<? extends TreeNode> storeMeta, final Version version) {
70         // Container is not present, let's take care of the 'magically appear' part of our job
71         final Optional<? extends TreeNode> 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 Optional<? extends TreeNode> storeMeta) {
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 (!storeMeta.isPresent()) {
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 Optional<TreeNode> fakeMeta(final NormalizedNode emptyNode, final Version version) {
104         return Optional.of(TreeNodeFactory.createTreeNode(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 }