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 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.tree.DataTreeCandidateNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
21 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,
30 final TreeNode oldMeta, 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 final TreeNode childMeta(final TreeNode parent, final PathArgument id) {
50 return parent.getChild(id).orNull();
56 private DataTreeCandidateNode childNode(final ModifiedNode input) {
57 final PathArgument id = input.getIdentifier();
58 return createChildNode(input, childMeta(oldMeta, id), childMeta(newMeta, id));
61 private static DataTreeCandidateNode createChildNode(@Nonnull final ModifiedNode input, @Nullable final TreeNode oldMeta, @Nullable final TreeNode newMeta) {
63 return RecursiveModificationCandidateNode.initialWriteNode(newMeta.getData());
66 return RecursiveModificationCandidateNode.deleteNode(oldMeta.getData());
68 return new ChildNode(input, oldMeta, newMeta);
72 public Collection<DataTreeCandidateNode> getChildNodes() {
73 return Collections2.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
75 public DataTreeCandidateNode apply(final ModifiedNode input) {
76 return childNode(input);
82 public ModificationType getModificationType() {
83 return Verify.verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
86 private Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
88 return Optional.<NormalizedNode<?,?>>of(meta.getData());
90 return Optional.absent();
95 public Optional<NormalizedNode<?, ?>> getDataAfter() {
96 return optionalData(newMeta);
100 public Optional<NormalizedNode<?, ?>> getDataBefore() {
101 return optionalData(oldMeta);
105 public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
106 final Optional<ModifiedNode> childMod = mod.getChild(identifier);
107 if (childMod.isPresent()) {
108 return childNode(childMod.get());
113 static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode {
114 public ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
115 super(mod, oldMeta, newMeta);
119 public PathArgument getIdentifier() {
120 return getMod().getIdentifier();