1 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
4 import com.google.common.base.Function;
5 import com.google.common.base.Optional;
6 import com.google.common.base.Preconditions;
7 import com.google.common.collect.Iterables;
8 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
9 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
10 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
11 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
12 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
13 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
15 final class InMemoryDataTreeCandidate extends AbstractDataTreeCandidate {
16 private static abstract class AbstractNode implements DataTreeCandidateNode {
17 private final ModifiedNode mod;
18 private final TreeNode newMeta;
19 private final TreeNode oldMeta;
21 protected AbstractNode(final ModifiedNode mod,
22 final TreeNode oldMeta, final TreeNode newMeta) {
23 this.newMeta = newMeta;
24 this.oldMeta = oldMeta;
25 this.mod = Preconditions.checkNotNull(mod);
28 protected final ModifiedNode getMod() {
32 protected final TreeNode getNewMeta() {
36 protected final TreeNode getOldMeta() {
40 private static final TreeNode childMeta(final TreeNode parent, final PathArgument id) {
42 return parent.getChild(id).orNull();
49 public Iterable<DataTreeCandidateNode> getChildNodes() {
50 return Iterables.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
52 public DataTreeCandidateNode apply(final ModifiedNode input) {
53 final PathArgument id = input.getIdentifier();
54 return new ChildNode(input, childMeta(oldMeta, id), childMeta(newMeta, id));
60 public ModificationType getModificationType() {
64 private Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
66 return Optional.<NormalizedNode<?,?>>of(meta.getData());
68 return Optional.absent();
73 public Optional<NormalizedNode<?, ?>> getDataAfter() {
74 return optionalData(newMeta);
78 public Optional<NormalizedNode<?, ?>> getDataBefore() {
79 return optionalData(oldMeta);
83 private static final class ChildNode extends AbstractNode {
84 public ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
85 super(mod, oldMeta, newMeta);
89 public PathArgument getIdentifier() {
90 return getMod().getIdentifier();
94 private static final class RootNode extends AbstractNode {
95 public RootNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
96 super(mod, oldMeta, newMeta);
100 public PathArgument getIdentifier() {
101 throw new IllegalStateException("Attempted to get identifier of the root node");
105 private final RootNode root;
107 InMemoryDataTreeCandidate(final InstanceIdentifier rootPath, final ModifiedNode modificationRoot,
108 final TreeNode beforeRoot, final TreeNode afterRoot) {
110 this.root = new RootNode(modificationRoot, beforeRoot, afterRoot);
113 TreeNode getAfterRoot() {
114 return root.getNewMeta();
117 TreeNode getBeforeRoot() {
118 return root.getOldMeta();
122 public DataTreeCandidateNode getRootNode() {