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 return oldMeta != null && oldMeta.data() instanceof NormalizedNodeContainer
57 || newMeta != null && newMeta.data() instanceof NormalizedNodeContainer;
60 @SuppressWarnings("unchecked")
61 private static DistinctNodeContainer<PathArgument, NormalizedNode> getContainer(
62 final @Nullable TreeNode meta) {
63 return meta == null ? null : (DistinctNodeContainer<PathArgument, NormalizedNode>)meta.data();
66 private @NonNull ChildNode childNode(final ModifiedNode childMod) {
67 final var id = childMod.getIdentifier();
68 return new ChildNode(childMod, childMeta(oldMeta, id), childMeta(newMeta, id));
72 public Collection<DataTreeCandidateNode> childNodes() {
73 return switch (mod.getModificationType()) {
74 case APPEARED, DISAPPEARED, SUBTREE_MODIFIED -> Collections2.transform(mod.getChildren(), this::childNode);
76 // Unmodified node, but we still need to resolve potential children. canHaveChildren returns
77 // false if both arguments are null.
78 if (!canHaveChildren(oldMeta, newMeta)) {
79 yield ImmutableList.of();
81 yield Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).body(),
82 DataTreeCandidateNodes::unmodified);
84 case DELETE, WRITE -> {
85 // This is unusual, the user is requesting we follow into an otherwise-terminal node.
86 // We need to fudge things based on before/after data to correctly fake the expectations.
87 if (!canHaveChildren(oldMeta, newMeta)) {
88 yield ImmutableList.of();
90 yield DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta));
96 public ModificationType modificationType() {
97 return verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
101 public final NormalizedNode dataBefore() {
102 return data(oldMeta);
106 public final NormalizedNode dataAfter() {
107 return data(newMeta);
110 private static @Nullable NormalizedNode data(final TreeNode meta) {
111 return meta == null ? null : meta.data();
116 public final DataTreeCandidateNode modifiedChild(final PathArgument childName) {
117 final var identifier = requireNonNull(childName);
118 return switch (mod.getModificationType()) {
119 case APPEARED, DISAPPEARED, SUBTREE_MODIFIED -> {
120 final var child = mod.childByArg(identifier);
121 yield child == null ? null : childNode(child);
124 if (!canHaveChildren(oldMeta, newMeta)) {
127 final var child = getContainer(newMeta != null ? newMeta : oldMeta).childByArg(identifier);
128 yield child == null ? null : DataTreeCandidateNodes.unmodified(child);
130 case DELETE, WRITE -> {
131 if (!canHaveChildren(oldMeta, newMeta)) {
134 yield DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta), identifier);
139 private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
140 ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
141 super(mod, oldMeta, newMeta);
145 public PathArgument name() {
146 return getMod().getIdentifier();
151 public String toString() {
152 return getClass().getSimpleName() + "{mod = " + mod + ", oldMeta = " + oldMeta + ", newMeta = " + newMeta + "}";