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