2 * Copyright (c) 2014 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 com.google.common.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Collections2;
14 import java.util.Collection;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
22 final class InMemoryDataTreeCandidate extends AbstractDataTreeCandidate {
23 private static abstract class AbstractNode implements DataTreeCandidateNode {
24 private final ModifiedNode mod;
25 private final TreeNode newMeta;
26 private final TreeNode oldMeta;
28 protected AbstractNode(final ModifiedNode mod,
29 final TreeNode oldMeta, final TreeNode newMeta) {
30 this.newMeta = newMeta;
31 this.oldMeta = oldMeta;
32 this.mod = Preconditions.checkNotNull(mod);
35 protected final ModifiedNode getMod() {
39 protected final TreeNode getNewMeta() {
43 protected final TreeNode getOldMeta() {
47 private static final TreeNode childMeta(final TreeNode parent, final PathArgument id) {
49 return parent.getChild(id).orNull();
55 private DataTreeCandidateNode childNode(final ModifiedNode input) {
56 final PathArgument id = input.getIdentifier();
57 return new ChildNode(input, childMeta(oldMeta, id), childMeta(newMeta, id));
61 public Collection<DataTreeCandidateNode> getChildNodes() {
62 return Collections2.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
64 public DataTreeCandidateNode apply(final ModifiedNode input) {
65 return childNode(input);
71 public ModificationType getModificationType() {
72 return mod.modificationType();
75 private Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
77 return Optional.<NormalizedNode<?,?>>of(meta.getData());
79 return Optional.absent();
84 public Optional<NormalizedNode<?, ?>> getDataAfter() {
85 return optionalData(newMeta);
89 public Optional<NormalizedNode<?, ?>> getDataBefore() {
90 return optionalData(oldMeta);
94 public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
95 final Optional<ModifiedNode> childMod = mod.getChild(identifier);
96 if (childMod.isPresent()) {
97 return childNode(mod);
103 private static final class ChildNode extends AbstractNode {
104 public ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
105 super(mod, oldMeta, newMeta);
109 public PathArgument getIdentifier() {
110 return getMod().getIdentifier();
114 private static final class RootNode extends AbstractNode {
115 public RootNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
116 super(mod, oldMeta, newMeta);
120 public PathArgument getIdentifier() {
121 throw new IllegalStateException("Attempted to get identifier of the root node");
125 private final RootNode root;
127 InMemoryDataTreeCandidate(final YangInstanceIdentifier rootPath, final ModifiedNode modificationRoot,
128 final TreeNode beforeRoot, final TreeNode afterRoot) {
130 this.root = new RootNode(modificationRoot, beforeRoot, afterRoot);
133 TreeNode getAfterRoot() {
134 return root.getNewMeta();
137 TreeNode getBeforeRoot() {
138 return root.getOldMeta();
142 public DataTreeCandidateNode getRootNode() {