2 * Copyright (c) 2015 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.Function;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
13 import com.google.common.collect.Collections2;
14 import java.util.Collection;
15 import java.util.Collections;
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 {
26 private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_UNMODIFIED_NODES = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
28 public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
29 return AbstractRecursiveCandidateNode.unmodifiedNode(input);
33 private final ModifiedNode mod;
34 private final TreeNode newMeta;
35 private final TreeNode oldMeta;
37 protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod,
38 final TreeNode oldMeta, final TreeNode newMeta) {
39 this.newMeta = newMeta;
40 this.oldMeta = oldMeta;
41 this.mod = Preconditions.checkNotNull(mod);
44 protected final ModifiedNode getMod() {
48 protected final TreeNode getNewMeta() {
52 protected final TreeNode getOldMeta() {
56 private static TreeNode childMeta(final TreeNode parent, final PathArgument id) {
58 return parent.getChild(id).orNull();
64 private static boolean canHaveChildren(@Nullable final TreeNode oldMeta, @Nullable final TreeNode newMeta) {
65 if (oldMeta != null) {
66 return oldMeta.getData() instanceof NormalizedNodeContainer;
68 if (newMeta != null) {
69 return newMeta.getData() instanceof NormalizedNodeContainer;
74 @SuppressWarnings("unchecked")
75 private static NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getContainer(@Nullable final TreeNode meta) {
76 return (meta == null ? null : (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>)meta.getData());
79 private ChildNode childNode(final ModifiedNode childMod) {
80 final PathArgument id = childMod.getIdentifier();
81 return new ChildNode(childMod, childMeta(oldMeta, id), childMeta(newMeta, id));
85 public Collection<DataTreeCandidateNode> getChildNodes() {
86 switch (mod.getModificationType()) {
87 case SUBTREE_MODIFIED:
88 return Collections2.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
90 public DataTreeCandidateNode apply(final ModifiedNode input) {
91 return childNode(input);
95 // Unmodified node, but we still need to resolve potential children. canHaveChildren returns
96 // false if both arguments are null.
97 if (canHaveChildren(oldMeta, newMeta)) {
98 return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(), TO_UNMODIFIED_NODES);
100 return Collections.emptyList();
104 // This is unusual, the user is requesting we follow into an otherwise-terminal node.
105 // We need to fudge things based on before/after data to correctly fake the expectations.
106 if (canHaveChildren(oldMeta, newMeta)) {
107 return AbstractDataTreeCandidateNode.deltaChildren(getContainer(oldMeta), getContainer(newMeta));
109 return Collections.emptyList();
112 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
117 public ModificationType getModificationType() {
118 return Verify.verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
121 private static Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
123 return Optional.<NormalizedNode<?,?>>of(meta.getData());
125 return Optional.absent();
130 public final Optional<NormalizedNode<?, ?>> getDataAfter() {
131 return optionalData(newMeta);
135 public final Optional<NormalizedNode<?, ?>> getDataBefore() {
136 return optionalData(oldMeta);
140 public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
141 switch (mod.getModificationType()) {
142 case SUBTREE_MODIFIED:
143 final Optional<ModifiedNode> childMod = mod.getChild(identifier);
144 if (childMod.isPresent()) {
145 return childNode(childMod.get());
151 // FIXME: this is a linear walk. We need a Map of these in order to
152 // do something like getChildMap().get(identifier);
153 for (DataTreeCandidateNode c : getChildNodes()) {
154 if (identifier.equals(c.getIdentifier())) {
160 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
164 private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
165 ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
166 super(mod, oldMeta, newMeta);
170 public PathArgument getIdentifier() {
171 return getMod().getIdentifier();