Modernize DataTreeCandidateNode API
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / AbstractModifiedNodeBasedCandidateNode.java
1 /*
2  * Copyright (c) 2015, 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.tree.impl;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableList;
15 import java.util.Collection;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
22 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode;
23 import org.opendaylight.yangtools.yang.data.tree.api.ModificationType;
24 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
25 import org.opendaylight.yangtools.yang.data.tree.spi.DataTreeCandidateNodes;
26
27 abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandidateNode {
28     private final ModifiedNode mod;
29     private final TreeNode newMeta;
30     private final TreeNode oldMeta;
31
32     protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod, final TreeNode oldMeta,
33             final TreeNode newMeta) {
34         this.newMeta = newMeta;
35         this.oldMeta = oldMeta;
36         this.mod = requireNonNull(mod);
37     }
38
39     protected final ModifiedNode getMod() {
40         return mod;
41     }
42
43     protected final TreeNode getNewMeta() {
44         return newMeta;
45     }
46
47     protected final TreeNode getOldMeta() {
48         return oldMeta;
49     }
50
51     private static TreeNode childMeta(final TreeNode parent, final PathArgument id) {
52         return parent == null ? null : parent.childByArg(id);
53     }
54
55     private static boolean canHaveChildren(final @Nullable TreeNode oldMeta, final @Nullable TreeNode newMeta) {
56         if (oldMeta != null) {
57             return oldMeta.getData() instanceof NormalizedNodeContainer;
58         }
59         if (newMeta != null) {
60             return newMeta.getData() instanceof NormalizedNodeContainer;
61         }
62         return false;
63     }
64
65     @SuppressWarnings("unchecked")
66     private static DistinctNodeContainer<PathArgument, NormalizedNode> getContainer(
67             final @Nullable TreeNode meta) {
68         return meta == null ? null : (DistinctNodeContainer<PathArgument, NormalizedNode>)meta.getData();
69     }
70
71     private @NonNull ChildNode childNode(final ModifiedNode childMod) {
72         final var id = childMod.getIdentifier();
73         return new ChildNode(childMod, childMeta(oldMeta, id), childMeta(newMeta, id));
74     }
75
76     @Override
77     public Collection<DataTreeCandidateNode> childNodes() {
78         return switch (mod.getModificationType()) {
79             case APPEARED, DISAPPEARED, SUBTREE_MODIFIED -> Collections2.transform(mod.getChildren(), this::childNode);
80             case UNMODIFIED -> {
81                 // Unmodified node, but we still need to resolve potential children. canHaveChildren returns
82                 // false if both arguments are null.
83                 if (!canHaveChildren(oldMeta, newMeta)) {
84                     yield ImmutableList.of();
85                 }
86                 yield Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).body(),
87                     DataTreeCandidateNodes::unmodified);
88             }
89             case DELETE, WRITE -> {
90                 // This is unusual, the user is requesting we follow into an otherwise-terminal node.
91                 // We need to fudge things based on before/after data to correctly fake the expectations.
92                 if (!canHaveChildren(oldMeta, newMeta)) {
93                     yield ImmutableList.of();
94                 }
95                 yield DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta));
96             }
97         };
98     }
99
100     @Override
101     public ModificationType modificationType() {
102         return verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
103     }
104
105     @Override
106     public final NormalizedNode dataBefore() {
107         return data(oldMeta);
108     }
109
110     @Override
111     public final NormalizedNode dataAfter() {
112         return data(newMeta);
113     }
114
115     private static @Nullable NormalizedNode data(final TreeNode meta) {
116         return meta == null ? null : meta.getData();
117     }
118
119
120     @Override
121     public final DataTreeCandidateNode modifiedChild(final PathArgument childName) {
122         final var identifier = requireNonNull(childName);
123         return switch (mod.getModificationType()) {
124             case APPEARED, DISAPPEARED, SUBTREE_MODIFIED -> {
125                 final var child = mod.childByArg(identifier);
126                 yield child == null ? null : childNode(child);
127             }
128             case UNMODIFIED -> {
129                 if (!canHaveChildren(oldMeta, newMeta)) {
130                     yield null;
131                 }
132                 final var child = getContainer(newMeta != null ? newMeta : oldMeta).childByArg(identifier);
133                 yield child == null ? null : DataTreeCandidateNodes.unmodified(child);
134             }
135             case DELETE, WRITE -> {
136                 if (!canHaveChildren(oldMeta, newMeta)) {
137                     yield null;
138                 }
139                 yield DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta), identifier);
140             }
141         };
142     }
143
144     private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
145         ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
146             super(mod, oldMeta, newMeta);
147         }
148
149         @Override
150         public PathArgument name() {
151             return getMod().getIdentifier();
152         }
153     }
154
155     @Override
156     public String toString() {
157         return getClass().getSimpleName() + "{mod = " + mod + ", oldMeta = " + oldMeta + ", newMeta = " + newMeta + "}";
158     }
159 }