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.base.Verify;
14 import com.google.common.collect.Collections2;
15 import java.util.Collection;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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;
23 final class InMemoryDataTreeCandidate extends AbstractDataTreeCandidate {
24 private abstract static class AbstractNode implements DataTreeCandidateNode {
25 private final ModifiedNode mod;
26 private final TreeNode newMeta;
27 private final TreeNode oldMeta;
29 protected AbstractNode(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 new ChildNode(input, childMeta(oldMeta, id), childMeta(newMeta, id));
62 public Collection<DataTreeCandidateNode> getChildNodes() {
63 return Collections2.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
65 public DataTreeCandidateNode apply(final ModifiedNode input) {
66 return childNode(input);
72 public ModificationType getModificationType() {
73 return Verify.verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
76 private Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
78 return Optional.<NormalizedNode<?,?>>of(meta.getData());
80 return Optional.absent();
85 public Optional<NormalizedNode<?, ?>> getDataAfter() {
86 return optionalData(newMeta);
90 public Optional<NormalizedNode<?, ?>> getDataBefore() {
91 return optionalData(oldMeta);
95 public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
96 final Optional<ModifiedNode> childMod = mod.getChild(identifier);
97 if (childMod.isPresent()) {
98 return childNode(childMod.get());
104 private static final class ChildNode extends AbstractNode {
105 public ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
106 super(mod, oldMeta, newMeta);
110 public PathArgument getIdentifier() {
111 return getMod().getIdentifier();
115 private static final class RootNode extends AbstractNode {
116 public RootNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
117 super(mod, oldMeta, newMeta);
121 public PathArgument getIdentifier() {
122 throw new IllegalStateException("Attempted to get identifier of the root node");
126 private final RootNode root;
128 InMemoryDataTreeCandidate(final YangInstanceIdentifier rootPath, final ModifiedNode modificationRoot,
129 final TreeNode beforeRoot, final TreeNode afterRoot) {
131 this.root = new RootNode(modificationRoot, beforeRoot, afterRoot);
134 TreeNode getAfterRoot() {
135 return root.getNewMeta();
138 TreeNode getBeforeRoot() {
139 return root.getOldMeta();
143 public DataTreeCandidateNode getRootNode() {