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<TreeNode> apply(ModifiedNode modification, Optional<TreeNode> storeMeta, Version version);
33 * This is a capture of
34 * {@link SchemaAwareApplyOperation#applyWrite(ModifiedNode, NormalizedNode, Optional, Version)}.
37 interface ApplyWrite {
38 TreeNode applyWrite(ModifiedNode modification, NormalizedNode<?, ?> newValue, Optional<TreeNode> storeMeta,
42 private AutomaticLifecycleMixin() {
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
52 final Optional<TreeNode> ret;
53 if (modification.getOperation() == LogicalOperation.DELETE) {
54 if (modification.getChildren().isEmpty()) {
55 return delegate.apply(modification, storeMeta, version);
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);
62 // No special handling required here, run normal apply operation
63 ret = delegate.apply(modification, storeMeta, version);
66 return ret.isPresent() ? disappearResult(modification, ret.get(), storeMeta) : ret;
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);
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);
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);
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;
97 // ... it really disappeared
98 finalType = ModificationType.DISAPPEARED;
100 modification.resolveModificationType(finalType);
101 return Optional.empty();
104 private static Optional<TreeNode> fakeMeta(final NormalizedNode<?, ?> emptyNode, final Version version) {
105 return Optional.of(TreeNodeFactory.createTreeNode(emptyNode, version));
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();
113 if (data instanceof OrderedNodeContainer) {
114 return ((OrderedNodeContainer<?>) data).getSize() == 0;
116 throw new IllegalStateException("Unhandled data " + data);