1 package org.opendaylight.controller.md.sal.dom.store.impl.tree.data;
3 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataTreeCandidateNode;
4 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ModificationType;
5 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
6 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
7 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
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.collect.Iterables;
14 final class InMemoryDataTreeCandidate extends AbstractDataTreeCandidate {
15 private static abstract class AbstractNode implements DataTreeCandidateNode {
16 private final StoreMetadataNode newMeta;
17 private final StoreMetadataNode oldMeta;
18 private final NodeModification mod;
20 protected AbstractNode(final NodeModification mod,
21 final StoreMetadataNode oldMeta, final StoreMetadataNode newMeta) {
22 this.newMeta = newMeta;
23 this.oldMeta = oldMeta;
24 this.mod = Preconditions.checkNotNull(mod);
27 protected final NodeModification getMod() {
31 protected final StoreMetadataNode getNewMeta() {
35 protected final StoreMetadataNode getOldMeta() {
39 private static final StoreMetadataNode childMeta(final StoreMetadataNode parent, final PathArgument id) {
40 return parent == null ? null : parent.getChild(id).orNull();
44 public Iterable<DataTreeCandidateNode> getChildNodes() {
45 return Iterables.transform(mod.getModifications(), new Function<NodeModification, DataTreeCandidateNode>() {
47 public DataTreeCandidateNode apply(final NodeModification input) {
48 final PathArgument id = input.getIdentifier();
49 return new ChildNode(input, childMeta(oldMeta, id), childMeta(newMeta, id));
55 public ModificationType getModificationType() {
56 return mod.getModificationType();
59 private Optional<NormalizedNode<?, ?>> optionalData(StoreMetadataNode meta) {
61 return Optional.absent();
63 return Optional.<NormalizedNode<?,?>>of(meta.getData());
67 public Optional<NormalizedNode<?, ?>> getDataAfter() {
68 return optionalData(newMeta);
72 public Optional<NormalizedNode<?, ?>> getDataBefore() {
73 return optionalData(oldMeta);
77 private static final class ChildNode extends AbstractNode {
78 public ChildNode(NodeModification mod, StoreMetadataNode oldMeta, StoreMetadataNode newMeta) {
79 super(mod, oldMeta, newMeta);
83 public PathArgument getIdentifier() {
84 return getMod().getIdentifier();
88 private static final class RootNode extends AbstractNode {
89 public RootNode(NodeModification mod, StoreMetadataNode oldMeta, StoreMetadataNode newMeta) {
90 super(mod, oldMeta, newMeta);
94 public PathArgument getIdentifier() {
95 throw new IllegalStateException("Attempted to get identifier of the root node");
99 private final RootNode root;
101 InMemoryDataTreeCandidate(final InstanceIdentifier rootPath, final NodeModification modificationRoot,
102 final StoreMetadataNode oldRoot, final StoreMetadataNode newRoot) {
104 this.root = new RootNode(modificationRoot, oldRoot, newRoot);
107 StoreMetadataNode getAfterRoot() {
108 return root.getNewMeta();
111 StoreMetadataNode getBeforeRoot() {
112 return root.getOldMeta();
116 public DataTreeCandidateNode getRootNode() {