Fix NONE operation node with automatic lifecycle
[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<TreeNode> apply(ModifiedNode modification, Optional<TreeNode> storeMeta, Version version);
30     }
31
32     /**
33      * This is a capture of
34      * {@link SchemaAwareApplyOperation#applyWrite(ModifiedNode, NormalizedNode, Optional, Version)}.
35      */
36     @FunctionalInterface
37     interface ApplyWrite {
38         TreeNode applyWrite(ModifiedNode modification, NormalizedNode<?, ?> newValue, Optional<TreeNode> storeMeta,
39                 Version version);
40     }
41
42     private AutomaticLifecycleMixin() {
43
44     }
45
46     static Optional<TreeNode> apply(final Apply delegate, final ApplyWrite writeDelegate,
47             final NormalizedNode<?, ?> emptyNode, final ModifiedNode modification, final Optional<TreeNode> storeMeta,
48             final Version version) {
49         // The only way a tree node can disappear is through delete (which we handle here explicitly) or through
50         // actions of disappearResult(). It is therefore safe to perform Optional.get() on the results of
51         // delegate.apply()
52         final Optional<TreeNode> ret;
53         if (modification.getOperation() == LogicalOperation.DELETE) {
54             if (modification.getChildren().isEmpty()) {
55                 return delegate.apply(modification, storeMeta, version);
56             }
57             // Delete with children, implies it really is an empty write
58             ret = Optional.of(writeDelegate.applyWrite(modification, emptyNode, storeMeta, version));
59         } else if (modification.getOperation() == LogicalOperation.TOUCH && !storeMeta.isPresent()) {
60             ret = applyTouch(delegate, emptyNode, modification, storeMeta, version);
61         } else {
62             // No special handling required here, run normal apply operation
63             ret = delegate.apply(modification, storeMeta, version);
64         }
65
66         return ret.isPresent() ? disappearResult(modification, ret.get(), storeMeta) : ret;
67     }
68
69     private static Optional<TreeNode> applyTouch(final Apply delegate, final NormalizedNode<?, ?> emptyNode,
70             final ModifiedNode modification, final Optional<TreeNode> storeMeta, final Version version) {
71         // Container is not present, let's take care of the 'magically appear' part of our job
72         final Optional<TreeNode> ret = delegate.apply(modification, fakeMeta(emptyNode, version), version);
73
74         // If the delegate indicated SUBTREE_MODIFIED, account for the fake and report APPEARED
75         if (modification.getModificationType() == ModificationType.SUBTREE_MODIFIED) {
76             modification.resolveModificationType(ModificationType.APPEARED);
77         }
78         return ret;
79     }
80
81     private static Optional<TreeNode> disappearResult(final ModifiedNode modification, final TreeNode result,
82             final Optional<TreeNode> storeMeta) {
83         // Check if the result is in fact empty before pulling any tricks
84         if (!isEmpty(result)) {
85             return Optional.of(result);
86         }
87
88         // We are pulling the 'disappear' trick, but what we report can be three different things
89         final ModificationType finalType;
90         if (!storeMeta.isPresent()) {
91             // ... there was nothing in the datastore, no change
92             finalType = ModificationType.UNMODIFIED;
93         } else if (modification.getModificationType() == ModificationType.WRITE) {
94             // ... this was an empty write, possibly originally a delete
95             finalType = ModificationType.DELETE;
96         } else {
97             // ... it really disappeared
98             finalType = ModificationType.DISAPPEARED;
99         }
100         modification.resolveModificationType(finalType);
101         return Optional.empty();
102     }
103
104     private static Optional<TreeNode> fakeMeta(final NormalizedNode<?, ?> emptyNode, final Version version) {
105         return Optional.of(TreeNodeFactory.createTreeNode(emptyNode, version));
106     }
107
108     private static boolean isEmpty(final TreeNode treeNode) {
109         final NormalizedNode<?, ?> data = treeNode.getData();
110         if (data instanceof NormalizedNodeContainer) {
111             return ((NormalizedNodeContainer<?, ?, ?>) data).getValue().isEmpty();
112         }
113         if (data instanceof OrderedNodeContainer) {
114             return ((OrderedNodeContainer<?>) data).getSize() == 0;
115         }
116         throw new IllegalStateException("Unhandled data " + data);
117     }
118 }