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.Iterables;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
21 final class InMemoryDataTreeCandidate extends AbstractDataTreeCandidate {
22 private static abstract class AbstractNode implements DataTreeCandidateNode {
23 private final ModifiedNode mod;
24 private final TreeNode newMeta;
25 private final TreeNode oldMeta;
27 protected AbstractNode(final ModifiedNode mod,
28 final TreeNode oldMeta, final TreeNode newMeta) {
29 this.newMeta = newMeta;
30 this.oldMeta = oldMeta;
31 this.mod = Preconditions.checkNotNull(mod);
34 protected final ModifiedNode getMod() {
38 protected final TreeNode getNewMeta() {
42 protected final TreeNode getOldMeta() {
46 private static final TreeNode childMeta(final TreeNode parent, final PathArgument id) {
48 return parent.getChild(id).orNull();
55 public Iterable<DataTreeCandidateNode> getChildNodes() {
56 return Iterables.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
58 public DataTreeCandidateNode apply(final ModifiedNode input) {
59 final PathArgument id = input.getIdentifier();
60 return new ChildNode(input, childMeta(oldMeta, id), childMeta(newMeta, id));
66 public ModificationType getModificationType() {
70 private Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
72 return Optional.<NormalizedNode<?,?>>of(meta.getData());
74 return Optional.absent();
79 public Optional<NormalizedNode<?, ?>> getDataAfter() {
80 return optionalData(newMeta);
84 public Optional<NormalizedNode<?, ?>> getDataBefore() {
85 return optionalData(oldMeta);
89 private static final class ChildNode extends AbstractNode {
90 public ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
91 super(mod, oldMeta, newMeta);
95 public PathArgument getIdentifier() {
96 return getMod().getIdentifier();
100 private static final class RootNode extends AbstractNode {
101 public RootNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
102 super(mod, oldMeta, newMeta);
106 public PathArgument getIdentifier() {
107 throw new IllegalStateException("Attempted to get identifier of the root node");
111 private final RootNode root;
113 InMemoryDataTreeCandidate(final YangInstanceIdentifier rootPath, final ModifiedNode modificationRoot,
114 final TreeNode beforeRoot, final TreeNode afterRoot) {
116 this.root = new RootNode(modificationRoot, beforeRoot, afterRoot);
119 TreeNode getAfterRoot() {
120 return root.getNewMeta();
123 TreeNode getBeforeRoot() {
124 return root.getOldMeta();
128 public DataTreeCandidateNode getRootNode() {