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;
9 import com.google.common.base.Optional;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.ImmutableList;
14 import java.util.Collection;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
24 abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandidateNode {
25 private final ModifiedNode mod;
26 private final TreeNode newMeta;
27 private final TreeNode oldMeta;
29 protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod, final TreeNode oldMeta,
30 final TreeNode newMeta) {
31 this.newMeta = newMeta;
32 this.oldMeta = oldMeta;
33 this.mod = Preconditions.checkNotNull(mod);
36 protected final ModifiedNode getMod() {
40 protected final TreeNode getNewMeta() {
44 protected final TreeNode getOldMeta() {
48 private static TreeNode childMeta(final TreeNode parent, final PathArgument id) {
49 return parent == null ? null : parent.getChild(id).orNull();
52 private static boolean canHaveChildren(@Nullable final TreeNode oldMeta, @Nullable final TreeNode newMeta) {
53 if (oldMeta != null) {
54 return oldMeta.getData() instanceof NormalizedNodeContainer;
56 if (newMeta != null) {
57 return newMeta.getData() instanceof NormalizedNodeContainer;
62 @SuppressWarnings("unchecked")
63 private static NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getContainer(
64 @Nullable final TreeNode meta) {
65 return meta == null ? null : (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>)meta.getData();
68 private ChildNode childNode(final ModifiedNode childMod) {
69 final PathArgument id = childMod.getIdentifier();
70 return new ChildNode(childMod, childMeta(oldMeta, id), childMeta(newMeta, id));
75 public Collection<DataTreeCandidateNode> getChildNodes() {
76 switch (mod.getModificationType()) {
79 case SUBTREE_MODIFIED:
80 return Collections2.transform(mod.getChildren(), this::childNode);
82 // Unmodified node, but we still need to resolve potential children. canHaveChildren returns
83 // false if both arguments are null.
84 if (!canHaveChildren(oldMeta, newMeta)) {
85 return ImmutableList.of();
88 return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(),
89 AbstractRecursiveCandidateNode::unmodifiedNode);
92 // This is unusual, the user is requesting we follow into an otherwise-terminal node.
93 // We need to fudge things based on before/after data to correctly fake the expectations.
94 if (!canHaveChildren(oldMeta, newMeta)) {
95 return ImmutableList.of();
97 return AbstractDataTreeCandidateNode.deltaChildren(getContainer(oldMeta), getContainer(newMeta));
99 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
105 public ModificationType getModificationType() {
106 return Verify.verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
109 private static Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
110 return meta == null ? Optional.absent() : Optional.of(meta.getData());
115 public final Optional<NormalizedNode<?, ?>> getDataAfter() {
116 return optionalData(newMeta);
121 public final Optional<NormalizedNode<?, ?>> getDataBefore() {
122 return optionalData(oldMeta);
126 public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
127 switch (mod.getModificationType()) {
130 case SUBTREE_MODIFIED:
131 final Optional<ModifiedNode> childMod = mod.getChild(identifier);
132 if (childMod.isPresent()) {
133 return childNode(childMod.get());
137 if (!canHaveChildren(oldMeta, newMeta)) {
140 final Optional<NormalizedNode<?, ?>> maybeChild = getContainer(newMeta != null ? newMeta : oldMeta)
141 .getChild(identifier);
142 return maybeChild.isPresent() ? AbstractRecursiveCandidateNode.unmodifiedNode(maybeChild.get()) : null;
145 if (!canHaveChildren(oldMeta, newMeta)) {
148 return AbstractDataTreeCandidateNode.deltaChild(getContainer(oldMeta), getContainer(newMeta),
151 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
155 private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
156 ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
157 super(mod, oldMeta, newMeta);
162 public PathArgument getIdentifier() {
163 return getMod().getIdentifier();
168 public String toString() {
169 return this.getClass().getSimpleName() + "{mod = " + this.mod + ", oldMeta = " + this.oldMeta + ", newMeta = "
170 + this.newMeta + "}";