2 * Copyright (c) 2015, 2016 Cisco Systems, Inc. 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;
11 import static java.util.Objects.requireNonNull;
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;
27 abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandidateNode {
28 private final ModifiedNode mod;
29 private final TreeNode newMeta;
30 private final TreeNode oldMeta;
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);
39 protected final ModifiedNode getMod() {
43 protected final TreeNode getNewMeta() {
47 protected final TreeNode getOldMeta() {
51 private static TreeNode childMeta(final TreeNode parent, final PathArgument id) {
52 return parent == null ? null : parent.childByArg(id);
55 private static boolean canHaveChildren(final @Nullable TreeNode oldMeta, final @Nullable TreeNode newMeta) {
56 if (oldMeta != null) {
57 return oldMeta.getData() instanceof NormalizedNodeContainer;
59 if (newMeta != null) {
60 return newMeta.getData() instanceof NormalizedNodeContainer;
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();
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));
77 public Collection<DataTreeCandidateNode> childNodes() {
78 return switch (mod.getModificationType()) {
79 case APPEARED, DISAPPEARED, SUBTREE_MODIFIED -> Collections2.transform(mod.getChildren(), this::childNode);
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();
86 yield Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).body(),
87 DataTreeCandidateNodes::unmodified);
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();
95 yield DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta));
101 public ModificationType modificationType() {
102 return verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
106 public final NormalizedNode dataBefore() {
107 return data(oldMeta);
111 public final NormalizedNode dataAfter() {
112 return data(newMeta);
115 private static @Nullable NormalizedNode data(final TreeNode meta) {
116 return meta == null ? null : meta.getData();
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);
129 if (!canHaveChildren(oldMeta, newMeta)) {
132 final var child = getContainer(newMeta != null ? newMeta : oldMeta).childByArg(identifier);
133 yield child == null ? null : DataTreeCandidateNodes.unmodified(child);
135 case DELETE, WRITE -> {
136 if (!canHaveChildren(oldMeta, newMeta)) {
139 yield DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta), identifier);
144 private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
145 ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
146 super(mod, oldMeta, newMeta);
150 public PathArgument name() {
151 return getMod().getIdentifier();
156 public String toString() {
157 return getClass().getSimpleName() + "{mod = " + mod + ", oldMeta = " + oldMeta + ", newMeta = " + newMeta + "}";