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;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableList;
15 import java.util.Collection;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNodes;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
27 abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandidateNode {
28 private final ModifiedNode mod;
29 private final TreeNode newMeta;
30 private final TreeNode oldMeta;
32 protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod, final TreeNode oldMeta,
33 final TreeNode newMeta) {
34 this.newMeta = newMeta;
35 this.oldMeta = oldMeta;
36 this.mod = requireNonNull(mod);
39 protected final ModifiedNode getMod() {
43 protected final TreeNode getNewMeta() {
47 protected final TreeNode getOldMeta() {
51 private static TreeNode childMeta(final TreeNode parent, final PathArgument id) {
52 return parent == null ? null : parent.childByArg(id);
55 private static boolean canHaveChildren(final @Nullable TreeNode oldMeta, final @Nullable TreeNode newMeta) {
56 if (oldMeta != null) {
57 return oldMeta.getData() instanceof NormalizedNodeContainer;
59 if (newMeta != null) {
60 return newMeta.getData() instanceof NormalizedNodeContainer;
65 @SuppressWarnings("unchecked")
66 private static NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getContainer(
67 final @Nullable 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));
77 public Collection<DataTreeCandidateNode> getChildNodes() {
78 switch (mod.getModificationType()) {
81 case SUBTREE_MODIFIED:
82 return Collections2.transform(mod.getChildren(), this::childNode);
84 // Unmodified node, but we still need to resolve potential children. canHaveChildren returns
85 // false if both arguments are null.
86 if (!canHaveChildren(oldMeta, newMeta)) {
87 return ImmutableList.of();
90 return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(),
91 DataTreeCandidateNodes::unmodified);
94 // This is unusual, the user is requesting we follow into an otherwise-terminal node.
95 // We need to fudge things based on before/after data to correctly fake the expectations.
96 if (!canHaveChildren(oldMeta, newMeta)) {
97 return ImmutableList.of();
99 return DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta));
101 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
106 public ModificationType getModificationType() {
107 return verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
110 private static @NonNull Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
111 return meta == null ? Optional.empty() : Optional.of(meta.getData());
115 public final Optional<NormalizedNode<?, ?>> getDataAfter() {
116 return optionalData(newMeta);
120 public final Optional<NormalizedNode<?, ?>> getDataBefore() {
121 return optionalData(oldMeta);
125 public final Optional<DataTreeCandidateNode> getModifiedChild(final PathArgument identifier) {
126 switch (mod.getModificationType()) {
129 case SUBTREE_MODIFIED:
130 final ModifiedNode child = mod.childByArg(identifier);
131 return child == null ? Optional.empty() : Optional.of(childNode(child));
133 if (!canHaveChildren(oldMeta, newMeta)) {
134 return Optional.empty();
136 return getContainer(newMeta != null ? newMeta : oldMeta).getChild(identifier)
137 .map(DataTreeCandidateNodes::unmodified);
140 if (!canHaveChildren(oldMeta, newMeta)) {
141 return Optional.empty();
143 return DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta), identifier);
145 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
149 private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
150 ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
151 super(mod, oldMeta, newMeta);
155 public PathArgument getIdentifier() {
156 return getMod().getIdentifier();
161 public String toString() {
162 return this.getClass().getSimpleName() + "{mod = " + this.mod + ", oldMeta = " + this.oldMeta + ", newMeta = "
163 + this.newMeta + "}";