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_NODE = 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()) {
89 case SUBTREE_MODIFIED:
90 return Collections2.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
92 public DataTreeCandidateNode apply(final ModifiedNode input) {
93 return childNode(input);
97 // Unmodified node, but we still need to resolve potential children. canHaveChildren returns
98 // false if both arguments are null.
99 if (canHaveChildren(oldMeta, newMeta)) {
100 return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(), TO_UNMODIFIED_NODE);
102 return Collections.emptyList();
106 // This is unusual, the user is requesting we follow into an otherwise-terminal node.
107 // We need to fudge things based on before/after data to correctly fake the expectations.
108 if (canHaveChildren(oldMeta, newMeta)) {
109 return AbstractDataTreeCandidateNode.deltaChildren(getContainer(oldMeta), getContainer(newMeta));
111 return Collections.emptyList();
114 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
119 public ModificationType getModificationType() {
120 return Verify.verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
123 private static Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
125 return Optional.<NormalizedNode<?,?>>of(meta.getData());
127 return Optional.absent();
132 public final Optional<NormalizedNode<?, ?>> getDataAfter() {
133 return optionalData(newMeta);
137 public final Optional<NormalizedNode<?, ?>> getDataBefore() {
138 return optionalData(oldMeta);
142 public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
143 switch (mod.getModificationType()) {
146 case SUBTREE_MODIFIED:
147 final Optional<ModifiedNode> childMod = mod.getChild(identifier);
148 if (childMod.isPresent()) {
149 return childNode(childMod.get());
153 if (canHaveChildren(oldMeta, newMeta)) {
154 final Optional<NormalizedNode<?, ?>> maybeChild = getContainer(newMeta != null ? newMeta : oldMeta).getChild(identifier);
155 if (maybeChild.isPresent()) {
156 return TO_UNMODIFIED_NODE.apply(maybeChild.get());
165 if (canHaveChildren(oldMeta, newMeta)) {
166 return AbstractDataTreeCandidateNode.deltaChild(getContainer(oldMeta), getContainer(newMeta), identifier);
171 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
175 private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
176 ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
177 super(mod, oldMeta, newMeta);
181 public PathArgument getIdentifier() {
182 return getMod().getIdentifier();