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(@Nullable final TreeNode meta) {
64 return (meta == null ? null : (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>)meta.getData());
67 private ChildNode childNode(final ModifiedNode childMod) {
68 final PathArgument id = childMod.getIdentifier();
69 return new ChildNode(childMod, childMeta(oldMeta, id), childMeta(newMeta, id));
74 public Collection<DataTreeCandidateNode> getChildNodes() {
75 switch (mod.getModificationType()) {
78 case SUBTREE_MODIFIED:
79 return 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 return ImmutableList.of();
87 return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(),
88 AbstractRecursiveCandidateNode::unmodifiedNode);
91 // This is unusual, the user is requesting we follow into an otherwise-terminal node.
92 // We need to fudge things based on before/after data to correctly fake the expectations.
93 if (!canHaveChildren(oldMeta, newMeta)) {
94 return ImmutableList.of();
96 return AbstractDataTreeCandidateNode.deltaChildren(getContainer(oldMeta), getContainer(newMeta));
98 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
104 public ModificationType getModificationType() {
105 return Verify.verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
108 private static Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
109 return meta == null ? Optional.absent() : Optional.of(meta.getData());
114 public final Optional<NormalizedNode<?, ?>> getDataAfter() {
115 return optionalData(newMeta);
120 public final Optional<NormalizedNode<?, ?>> getDataBefore() {
121 return optionalData(oldMeta);
125 public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
126 switch (mod.getModificationType()) {
129 case SUBTREE_MODIFIED:
130 final Optional<ModifiedNode> childMod = mod.getChild(identifier);
131 if (childMod.isPresent()) {
132 return childNode(childMod.get());
136 if (!canHaveChildren(oldMeta, newMeta)) {
139 final Optional<NormalizedNode<?, ?>> maybeChild = getContainer(newMeta != null ? newMeta : oldMeta)
140 .getChild(identifier);
141 return maybeChild.isPresent() ? AbstractRecursiveCandidateNode.unmodifiedNode(maybeChild.get()) : null;
144 if (!canHaveChildren(oldMeta, newMeta)) {
147 return AbstractDataTreeCandidateNode.deltaChild(getContainer(oldMeta), getContainer(newMeta), identifier);
149 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
153 private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
154 ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
155 super(mod, oldMeta, newMeta);
160 public PathArgument getIdentifier() {
161 return getMod().getIdentifier();
166 public String toString() {
167 return this.getClass().getSimpleName() + "{mod = " + this.mod + ", oldMeta = " + this.oldMeta + ", newMeta = " +