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.impl.schema.tree;
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 java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNodes;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
26 import org.opendaylight.yangtools.yang.data.spi.tree.TreeNode;
28 abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandidateNode {
29 private final ModifiedNode mod;
30 private final TreeNode newMeta;
31 private final TreeNode oldMeta;
33 protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod, final TreeNode oldMeta,
34 final TreeNode newMeta) {
35 this.newMeta = newMeta;
36 this.oldMeta = oldMeta;
37 this.mod = requireNonNull(mod);
40 protected final ModifiedNode getMod() {
44 protected final TreeNode getNewMeta() {
48 protected final TreeNode getOldMeta() {
52 private static TreeNode childMeta(final TreeNode parent, final PathArgument id) {
53 return parent == null ? null : parent.childByArg(id);
56 private static boolean canHaveChildren(final @Nullable TreeNode oldMeta, final @Nullable TreeNode newMeta) {
57 if (oldMeta != null) {
58 return oldMeta.getData() instanceof NormalizedNodeContainer;
60 if (newMeta != null) {
61 return newMeta.getData() instanceof NormalizedNodeContainer;
66 @SuppressWarnings("unchecked")
67 private static DistinctNodeContainer<PathArgument, NormalizedNode> getContainer(
68 final @Nullable TreeNode meta) {
69 return meta == null ? null : (DistinctNodeContainer<PathArgument, NormalizedNode>)meta.getData();
72 private ChildNode childNode(final ModifiedNode childMod) {
73 final PathArgument id = childMod.getIdentifier();
74 return new ChildNode(childMod, childMeta(oldMeta, id), childMeta(newMeta, id));
78 public Collection<DataTreeCandidateNode> getChildNodes() {
79 switch (mod.getModificationType()) {
82 case SUBTREE_MODIFIED:
83 return Collections2.transform(mod.getChildren(), this::childNode);
85 // Unmodified node, but we still need to resolve potential children. canHaveChildren returns
86 // false if both arguments are null.
87 if (!canHaveChildren(oldMeta, newMeta)) {
88 return ImmutableList.of();
91 return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).body(),
92 DataTreeCandidateNodes::unmodified);
95 // This is unusual, the user is requesting we follow into an otherwise-terminal node.
96 // We need to fudge things based on before/after data to correctly fake the expectations.
97 if (!canHaveChildren(oldMeta, newMeta)) {
98 return ImmutableList.of();
100 return DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta));
102 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
107 public ModificationType getModificationType() {
108 return verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
111 private static @NonNull Optional<NormalizedNode> optionalData(final TreeNode meta) {
112 return meta == null ? Optional.empty() : Optional.of(meta.getData());
116 public final Optional<NormalizedNode> getDataAfter() {
117 return optionalData(newMeta);
121 public final Optional<NormalizedNode> getDataBefore() {
122 return optionalData(oldMeta);
126 public final Optional<DataTreeCandidateNode> getModifiedChild(final PathArgument identifier) {
127 switch (mod.getModificationType()) {
130 case SUBTREE_MODIFIED:
131 final ModifiedNode child = mod.childByArg(identifier);
132 return child == null ? Optional.empty() : Optional.of(childNode(child));
134 if (!canHaveChildren(oldMeta, newMeta)) {
135 return Optional.empty();
137 return getContainer(newMeta != null ? newMeta : oldMeta).findChildByArg(identifier)
138 .map(DataTreeCandidateNodes::unmodified);
141 if (!canHaveChildren(oldMeta, newMeta)) {
142 return Optional.empty();
144 return DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta), identifier);
146 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
150 private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
151 ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
152 super(mod, oldMeta, newMeta);
156 public PathArgument getIdentifier() {
157 return getMod().getIdentifier();
162 public String toString() {
163 return this.getClass().getSimpleName() + "{mod = " + this.mod + ", oldMeta = " + this.oldMeta + ", newMeta = "
164 + this.newMeta + "}";