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.tree.impl;
10 import static com.google.common.base.Preconditions.checkState;
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;
22 * Mixin-type support class for subclasses of {@link ModificationApplyOperation} which need to provide automatic
23 * lifecycle management.
25 final class AutomaticLifecycleMixin {
27 * This is a capture of {@link ModificationApplyOperation#apply(ModifiedNode, TreeNode, Version)}.
31 Optional<? extends TreeNode> apply(ModifiedNode modification, @Nullable TreeNode currentMeta, Version version);
35 * This is a capture of
36 * {@link SchemaAwareApplyOperation#applyWrite(ModifiedNode, NormalizedNode, TreeNode, Version)}.
39 interface ApplyWrite {
40 TreeNode applyWrite(ModifiedNode modification, NormalizedNode newValue, @Nullable TreeNode currentMeta,
44 private AutomaticLifecycleMixin() {
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);
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);
61 // No special handling required here, run normal apply operation
62 ret = delegate.apply(modification, currentMeta, version);
65 return ret.isPresent() ? disappearResult(modification, ret.orElseThrow(), currentMeta) : ret;
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);
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);
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);
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;
96 // ... it really disappeared
97 finalType = ModificationType.DISAPPEARED;
99 modification.resolveModificationType(finalType);
100 return Optional.empty();
103 private static @NonNull TreeNode fakeMeta(final NormalizedNode emptyNode, final Version version) {
104 return TreeNode.of(emptyNode, version);
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;