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 javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
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.api.schema.tree.DataTreeCandidateNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
26 abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandidateNode {
27 private final ModifiedNode mod;
28 private final TreeNode newMeta;
29 private final TreeNode oldMeta;
31 protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod, final TreeNode oldMeta,
32 final TreeNode newMeta) {
33 this.newMeta = newMeta;
34 this.oldMeta = oldMeta;
35 this.mod = requireNonNull(mod);
38 protected final ModifiedNode getMod() {
42 protected final TreeNode getNewMeta() {
46 protected final TreeNode getOldMeta() {
50 private static TreeNode childMeta(final TreeNode parent, final PathArgument id) {
51 return parent == null ? null : parent.getChild(id).orElse(null);
54 private static boolean canHaveChildren(@Nullable final TreeNode oldMeta, @Nullable final TreeNode newMeta) {
55 if (oldMeta != null) {
56 return oldMeta.getData() instanceof NormalizedNodeContainer;
58 if (newMeta != null) {
59 return newMeta.getData() instanceof NormalizedNodeContainer;
64 @SuppressWarnings("unchecked")
65 private static NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getContainer(
66 @Nullable final TreeNode meta) {
67 return meta == null ? null : (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>)meta.getData();
70 private ChildNode childNode(final ModifiedNode childMod) {
71 final PathArgument id = childMod.getIdentifier();
72 return new ChildNode(childMod, childMeta(oldMeta, id), childMeta(newMeta, id));
77 public Collection<DataTreeCandidateNode> getChildNodes() {
78 switch (mod.getModificationType()) {
81 case SUBTREE_MODIFIED:
82 return Collections2.transform(mod.getChildren(), this::childNode);
84 // Unmodified node, but we still need to resolve potential children. canHaveChildren returns
85 // false if both arguments are null.
86 if (!canHaveChildren(oldMeta, newMeta)) {
87 return ImmutableList.of();
90 return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(),
91 AbstractRecursiveCandidateNode::unmodifiedNode);
94 // This is unusual, the user is requesting we follow into an otherwise-terminal node.
95 // We need to fudge things based on before/after data to correctly fake the expectations.
96 if (!canHaveChildren(oldMeta, newMeta)) {
97 return ImmutableList.of();
99 return AbstractDataTreeCandidateNode.deltaChildren(getContainer(oldMeta), getContainer(newMeta));
101 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 Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
112 return meta == null ? Optional.empty() : Optional.of(meta.getData());
117 public final Optional<NormalizedNode<?, ?>> getDataAfter() {
118 return optionalData(newMeta);
123 public final Optional<NormalizedNode<?, ?>> getDataBefore() {
124 return optionalData(oldMeta);
128 public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
129 switch (mod.getModificationType()) {
132 case SUBTREE_MODIFIED:
133 final Optional<ModifiedNode> childMod = mod.getChild(identifier);
134 if (childMod.isPresent()) {
135 return childNode(childMod.get());
139 if (!canHaveChildren(oldMeta, newMeta)) {
142 final Optional<NormalizedNode<?, ?>> maybeChild = getContainer(newMeta != null ? newMeta : oldMeta)
143 .getChild(identifier);
144 return maybeChild.isPresent() ? AbstractRecursiveCandidateNode.unmodifiedNode(maybeChild.get()) : null;
147 if (!canHaveChildren(oldMeta, newMeta)) {
150 return AbstractDataTreeCandidateNode.deltaChild(getContainer(oldMeta), getContainer(newMeta),
153 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
157 private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
158 ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
159 super(mod, oldMeta, newMeta);
164 public PathArgument getIdentifier() {
165 return getMod().getIdentifier();
170 public String toString() {
171 return this.getClass().getSimpleName() + "{mod = " + this.mod + ", oldMeta = " + this.oldMeta + ", newMeta = "
172 + this.newMeta + "}";