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.Verify.verifyNotNull;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
16 import org.opendaylight.yangtools.yang.data.tree.api.ModificationType;
17 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
18 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
21 * Mixin-type support class for subclasses of {@link ModificationApplyOperation} which need to provide automatic
22 * lifecycle management.
24 final class AutomaticLifecycleMixin {
26 * This is a capture of {@link ModificationApplyOperation#apply(ModifiedNode, TreeNode, Version)}.
30 @Nullable TreeNode apply(ModifiedNode modification, @Nullable TreeNode currentMeta, Version version);
34 * This is a capture of
35 * {@link SchemaAwareApplyOperation#applyWrite(ModifiedNode, NormalizedNode, TreeNode, Version)}.
38 interface ApplyWrite {
39 TreeNode applyWrite(ModifiedNode modification, NormalizedNode newValue, @Nullable TreeNode currentMeta,
43 private AutomaticLifecycleMixin() {
47 static @Nullable TreeNode apply(final Apply delegate, final ApplyWrite writeDelegate,
48 final NormalizedNode emptyNode, final ModifiedNode modification, final @Nullable TreeNode currentMeta,
49 final Version version) {
50 final @Nullable TreeNode ret;
51 if (modification.getOperation() == LogicalOperation.DELETE) {
52 if (modification.isEmpty()) {
53 return delegate.apply(modification, currentMeta, version);
55 // Delete with children, implies it really is an empty write
56 ret = verifyNotNull(writeDelegate.applyWrite(modification, emptyNode, currentMeta, version));
57 } else if (modification.getOperation() == LogicalOperation.TOUCH && currentMeta == null) {
58 ret = applyTouch(delegate, emptyNode, modification, null, version);
60 // No special handling required here, run normal apply operation
61 ret = delegate.apply(modification, currentMeta, version);
64 return ret == null ? null : disappearResult(modification, ret, currentMeta);
67 private static @Nullable TreeNode applyTouch(final Apply delegate, final NormalizedNode emptyNode,
68 final ModifiedNode modification, final @Nullable TreeNode currentMeta, final Version version) {
69 // Container is not present, let's take care of the 'magically appear' part of our job
70 final var 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 @Nullable TreeNode disappearResult(final ModifiedNode modification, final @NonNull TreeNode result,
80 final @Nullable TreeNode currentMeta) {
81 // Check if the result is in fact empty before pulling any tricks
82 final var data = result.data();
83 if (!(data instanceof NormalizedNodeContainer<?> container)) {
84 throw new IllegalStateException("Unhandled data " + data);
86 if (!container.isEmpty()) {
90 // We are pulling the 'disappear' trick, but what we report can be three different things
91 final ModificationType finalType;
92 if (currentMeta == null) {
93 // ... there was nothing in the datastore, no change
94 finalType = ModificationType.UNMODIFIED;
95 } else if (modification.getModificationType() == ModificationType.WRITE) {
96 // ... this was an empty write, possibly originally a delete
97 finalType = ModificationType.DELETE;
99 // ... it really disappeared
100 finalType = ModificationType.DISAPPEARED;
102 modification.resolveModificationType(finalType);
106 private static @NonNull TreeNode fakeMeta(final NormalizedNode emptyNode, final Version version) {
107 return TreeNode.of(emptyNode, version);