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 java.util.Collection;
14 import java.util.Collections;
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) {
50 return parent.getChild(id).orNull();
56 private static boolean canHaveChildren(@Nullable final TreeNode oldMeta, @Nullable final 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 NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getContainer(@Nullable final TreeNode meta) {
68 return (meta == null ? null : (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>)meta.getData());
71 private ChildNode childNode(final ModifiedNode childMod) {
72 final PathArgument id = childMod.getIdentifier();
73 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 Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(),
89 AbstractRecursiveCandidateNode::unmodifiedNode);
91 return Collections.emptyList();
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 AbstractDataTreeCandidateNode.deltaChildren(getContainer(oldMeta), getContainer(newMeta));
100 return Collections.emptyList();
103 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
109 public ModificationType getModificationType() {
110 return Verify.verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
113 private static Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
115 return Optional.of(meta.getData());
117 return Optional.absent();
123 public final Optional<NormalizedNode<?, ?>> getDataAfter() {
124 return optionalData(newMeta);
129 public final Optional<NormalizedNode<?, ?>> getDataBefore() {
130 return optionalData(oldMeta);
134 public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
135 switch (mod.getModificationType()) {
138 case SUBTREE_MODIFIED:
139 final Optional<ModifiedNode> childMod = mod.getChild(identifier);
140 if (childMod.isPresent()) {
141 return childNode(childMod.get());
145 if (canHaveChildren(oldMeta, newMeta)) {
146 final Optional<NormalizedNode<?, ?>> maybeChild = getContainer(newMeta != null ? newMeta : oldMeta).getChild(identifier);
147 if (maybeChild.isPresent()) {
148 return AbstractRecursiveCandidateNode.unmodifiedNode(maybeChild.get());
157 if (canHaveChildren(oldMeta, newMeta)) {
158 return AbstractDataTreeCandidateNode.deltaChild(getContainer(oldMeta), getContainer(newMeta), identifier);
163 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
167 private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
168 ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
169 super(mod, oldMeta, newMeta);
174 public PathArgument getIdentifier() {
175 return getMod().getIdentifier();
180 public String toString() {
181 return this.getClass().getSimpleName() + "{mod = " + this.mod + ", oldMeta = " + this.oldMeta + ", newMeta = " +