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.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.Nonnull;
17 import javax.annotation.Nullable;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
25 abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandidateNode {
27 private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_UNMODIFIED_NODE = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
29 public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
30 return AbstractRecursiveCandidateNode.unmodifiedNode(input);
34 private final ModifiedNode mod;
35 private final TreeNode newMeta;
36 private final TreeNode oldMeta;
38 protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod,
39 final TreeNode oldMeta, final TreeNode newMeta) {
40 this.newMeta = newMeta;
41 this.oldMeta = oldMeta;
42 this.mod = Preconditions.checkNotNull(mod);
45 protected final ModifiedNode getMod() {
49 protected final TreeNode getNewMeta() {
53 protected final TreeNode getOldMeta() {
57 private static TreeNode childMeta(final TreeNode parent, final PathArgument id) {
59 return parent.getChild(id).orNull();
65 private static boolean canHaveChildren(@Nullable final TreeNode oldMeta, @Nullable final TreeNode newMeta) {
66 if (oldMeta != null) {
67 return oldMeta.getData() instanceof NormalizedNodeContainer;
69 if (newMeta != null) {
70 return newMeta.getData() instanceof NormalizedNodeContainer;
75 @SuppressWarnings("unchecked")
76 private static NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getContainer(@Nullable final TreeNode meta) {
77 return (meta == null ? null : (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>)meta.getData());
80 private ChildNode childNode(final ModifiedNode childMod) {
81 final PathArgument id = childMod.getIdentifier();
82 return new ChildNode(childMod, childMeta(oldMeta, id), childMeta(newMeta, id));
87 public Collection<DataTreeCandidateNode> getChildNodes() {
88 switch (mod.getModificationType()) {
91 case SUBTREE_MODIFIED:
92 return Collections2.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
94 public DataTreeCandidateNode apply(final ModifiedNode input) {
95 return childNode(input);
99 // Unmodified node, but we still need to resolve potential children. canHaveChildren returns
100 // false if both arguments are null.
101 if (canHaveChildren(oldMeta, newMeta)) {
102 return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(), TO_UNMODIFIED_NODE);
104 return Collections.emptyList();
108 // This is unusual, the user is requesting we follow into an otherwise-terminal node.
109 // We need to fudge things based on before/after data to correctly fake the expectations.
110 if (canHaveChildren(oldMeta, newMeta)) {
111 return AbstractDataTreeCandidateNode.deltaChildren(getContainer(oldMeta), getContainer(newMeta));
113 return Collections.emptyList();
116 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
122 public ModificationType getModificationType() {
123 return Verify.verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
126 private static Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
128 return Optional.of(meta.getData());
130 return Optional.absent();
136 public final Optional<NormalizedNode<?, ?>> getDataAfter() {
137 return optionalData(newMeta);
142 public final Optional<NormalizedNode<?, ?>> getDataBefore() {
143 return optionalData(oldMeta);
147 public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
148 switch (mod.getModificationType()) {
151 case SUBTREE_MODIFIED:
152 final Optional<ModifiedNode> childMod = mod.getChild(identifier);
153 if (childMod.isPresent()) {
154 return childNode(childMod.get());
158 if (canHaveChildren(oldMeta, newMeta)) {
159 final Optional<NormalizedNode<?, ?>> maybeChild = getContainer(newMeta != null ? newMeta : oldMeta).getChild(identifier);
160 if (maybeChild.isPresent()) {
161 return TO_UNMODIFIED_NODE.apply(maybeChild.get());
170 if (canHaveChildren(oldMeta, newMeta)) {
171 return AbstractDataTreeCandidateNode.deltaChild(getContainer(oldMeta), getContainer(newMeta), identifier);
176 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
180 private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
181 ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
182 super(mod, oldMeta, newMeta);
187 public PathArgument getIdentifier() {
188 return getMod().getIdentifier();
193 public String toString() {
194 return this.getClass().getSimpleName() + "{mod = " + this.mod + ", oldMeta = " + this.oldMeta + ", newMeta = " +