BUG-509: migrate to TreeNodes
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / data / InMemoryDataTreeCandidate.java
1 package org.opendaylight.controller.md.sal.dom.store.impl.tree.data;
2
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.controller.md.sal.dom.store.impl.tree.spi.TreeNode;
6 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
7 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
8 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
9
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
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;
20
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);
26         }
27
28         protected final ModifiedNode getMod() {
29             return mod;
30         }
31
32         protected final TreeNode getNewMeta() {
33             return newMeta;
34         }
35
36         protected final TreeNode getOldMeta() {
37             return oldMeta;
38         }
39
40         private static final TreeNode childMeta(final TreeNode parent, final PathArgument id) {
41             if (parent != null) {
42                 return parent.getChild(id).orNull();
43             } else {
44                 return null;
45             }
46         }
47
48         @Override
49         public Iterable<DataTreeCandidateNode> getChildNodes() {
50             return Iterables.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
51                 @Override
52                 public DataTreeCandidateNode apply(final ModifiedNode input) {
53                     final PathArgument id = input.getIdentifier();
54                     return new ChildNode(input, childMeta(oldMeta, id), childMeta(newMeta, id));
55                 }
56             });
57         }
58
59         @Override
60         public ModificationType getModificationType() {
61             return mod.getType();
62         }
63
64         private Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
65             if (meta != null) {
66                 return Optional.<NormalizedNode<?,?>>of(meta.getData());
67             } else {
68                 return Optional.absent();
69             }
70         }
71
72         @Override
73         public Optional<NormalizedNode<?, ?>> getDataAfter() {
74             return optionalData(newMeta);
75         }
76
77         @Override
78         public Optional<NormalizedNode<?, ?>> getDataBefore() {
79             return optionalData(oldMeta);
80         }
81     }
82
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);
86         }
87
88         @Override
89         public PathArgument getIdentifier() {
90             return getMod().getIdentifier();
91         }
92     }
93
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);
97         }
98
99         @Override
100         public PathArgument getIdentifier() {
101             throw new IllegalStateException("Attempted to get identifier of the root node");
102         }
103     }
104
105     private final RootNode root;
106
107     InMemoryDataTreeCandidate(final InstanceIdentifier rootPath, final ModifiedNode modificationRoot,
108             final TreeNode beforeRoot, final TreeNode afterRoot) {
109         super(rootPath);
110         this.root = new RootNode(modificationRoot, beforeRoot, afterRoot);
111     }
112
113     TreeNode getAfterRoot() {
114         return root.getNewMeta();
115     }
116
117     TreeNode getBeforeRoot() {
118         return root.getOldMeta();
119     }
120
121     @Override
122     public DataTreeCandidateNode getRootNode() {
123         return root;
124     }
125 }