2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
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;
20 * Mixin-type support class for subclasses of {@link ModificationApplyOperation} which need to provide automatic
21 * lifecycle management.
23 final class AutomaticLifecycleMixin {
25 * This is a capture of {@link ModificationApplyOperation#apply(ModifiedNode, Optional, Version)}.
29 Optional<? extends TreeNode> apply(ModifiedNode modification, Optional<? extends TreeNode> storeMeta,
34 * This is a capture of
35 * {@link SchemaAwareApplyOperation#applyWrite(ModifiedNode, NormalizedNode, Optional, Version)}.
38 interface ApplyWrite {
39 TreeNode applyWrite(ModifiedNode modification, NormalizedNode<?, ?> newValue,
40 Optional<? extends TreeNode> storeMeta, Version version);
43 private AutomaticLifecycleMixin() {
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);
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);
60 // No special handling required here, run normal apply operation
61 ret = delegate.apply(modification, storeMeta, version);
64 return ret.isPresent() ? disappearResult(modification, ret.get(), storeMeta) : ret;
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);
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);
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);
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;
95 // ... it really disappeared
96 finalType = ModificationType.DISAPPEARED;
98 modification.resolveModificationType(finalType);
99 return Optional.empty();
102 private static Optional<TreeNode> fakeMeta(final NormalizedNode<?, ?> emptyNode, final Version version) {
103 return Optional.of(TreeNodeFactory.createTreeNode(emptyNode, version));
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();
111 if (data instanceof OrderedNodeContainer) {
112 return ((OrderedNodeContainer<?>) data).getSize() == 0;
114 throw new IllegalStateException("Unhandled data " + data);