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 static com.google.common.base.Preconditions.checkState;
12 import java.util.Optional;
13 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
16 import org.opendaylight.yangtools.yang.data.spi.tree.TreeNode;
17 import org.opendaylight.yangtools.yang.data.spi.tree.TreeNodeFactory;
18 import org.opendaylight.yangtools.yang.data.spi.tree.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, Optional, Version)}.
30 Optional<? extends TreeNode> apply(ModifiedNode modification, Optional<? extends TreeNode> storeMeta,
35 * This is a capture of
36 * {@link SchemaAwareApplyOperation#applyWrite(ModifiedNode, NormalizedNode, Optional, Version)}.
39 interface ApplyWrite {
40 TreeNode applyWrite(ModifiedNode modification, NormalizedNode newValue,
41 Optional<? extends TreeNode> storeMeta, Version version);
44 private AutomaticLifecycleMixin() {
48 static Optional<? extends TreeNode> apply(final Apply delegate, final ApplyWrite writeDelegate,
49 final NormalizedNode emptyNode, final ModifiedNode modification,
50 final Optional<? extends TreeNode> storeMeta, final Version version) {
51 final Optional<? extends TreeNode> ret;
52 if (modification.getOperation() == LogicalOperation.DELETE) {
53 if (modification.getChildren().isEmpty()) {
54 return delegate.apply(modification, storeMeta, version);
56 // Delete with children, implies it really is an empty write
57 ret = Optional.of(writeDelegate.applyWrite(modification, emptyNode, storeMeta, version));
58 } else if (modification.getOperation() == LogicalOperation.TOUCH && !storeMeta.isPresent()) {
59 ret = applyTouch(delegate, emptyNode, modification, storeMeta, version);
61 // No special handling required here, run normal apply operation
62 ret = delegate.apply(modification, storeMeta, version);
65 return ret.isPresent() ? disappearResult(modification, ret.get(), storeMeta) : ret;
68 private static Optional<? extends TreeNode> applyTouch(final Apply delegate, final NormalizedNode emptyNode,
69 final ModifiedNode modification, final Optional<? extends TreeNode> storeMeta, final Version version) {
70 // Container is not present, let's take care of the 'magically appear' part of our job
71 final Optional<? extends TreeNode> 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 Optional<? extends TreeNode> storeMeta) {
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 (!storeMeta.isPresent()) {
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 Optional<TreeNode> fakeMeta(final NormalizedNode emptyNode, final Version version) {
104 return Optional.of(TreeNodeFactory.createTreeNode(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;