Merge "Fix for Bug 934 - issue in validations for nuetronPort creation and updatePort"
[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.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;
8
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;
13
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;
19
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);
25         }
26
27         protected final NodeModification getMod() {
28             return mod;
29         }
30
31         protected final StoreMetadataNode getNewMeta() {
32             return newMeta;
33         }
34
35         protected final StoreMetadataNode getOldMeta() {
36             return oldMeta;
37         }
38
39         private static final StoreMetadataNode childMeta(final StoreMetadataNode parent, final PathArgument id) {
40             return parent == null ? null : parent.getChild(id).orNull();
41         }
42
43         @Override
44         public Iterable<DataTreeCandidateNode> getChildNodes() {
45             return Iterables.transform(mod.getModifications(), new Function<NodeModification, DataTreeCandidateNode>() {
46                 @Override
47                 public DataTreeCandidateNode apply(final NodeModification input) {
48                     final PathArgument id = input.getIdentifier();
49                     return new ChildNode(input, childMeta(oldMeta, id), childMeta(newMeta, id));
50                 }
51             });
52         }
53
54         @Override
55         public ModificationType getModificationType() {
56             return mod.getModificationType();
57         }
58
59         private Optional<NormalizedNode<?, ?>> optionalData(StoreMetadataNode meta) {
60             if (meta == null) {
61                 return Optional.absent();
62             }
63             return Optional.<NormalizedNode<?,?>>of(meta.getData());
64         }
65
66         @Override
67         public Optional<NormalizedNode<?, ?>> getDataAfter() {
68             return optionalData(newMeta);
69         }
70
71         @Override
72         public Optional<NormalizedNode<?, ?>> getDataBefore() {
73             return optionalData(oldMeta);
74         }
75     }
76
77     private static final class ChildNode extends AbstractNode {
78         public ChildNode(NodeModification mod, StoreMetadataNode oldMeta, StoreMetadataNode newMeta) {
79             super(mod, oldMeta, newMeta);
80         }
81
82         @Override
83         public PathArgument getIdentifier() {
84             return getMod().getIdentifier();
85         }
86     }
87
88     private static final class RootNode extends AbstractNode {
89         public RootNode(NodeModification mod, StoreMetadataNode oldMeta, StoreMetadataNode newMeta) {
90             super(mod, oldMeta, newMeta);
91         }
92
93         @Override
94         public PathArgument getIdentifier() {
95             throw new IllegalStateException("Attempted to get identifier of the root node");
96         }
97     }
98
99     private final RootNode root;
100
101     InMemoryDataTreeCandidate(final InstanceIdentifier rootPath, final NodeModification modificationRoot,
102             final StoreMetadataNode oldRoot, final StoreMetadataNode newRoot) {
103         super(rootPath);
104         this.root = new RootNode(modificationRoot, oldRoot, newRoot);
105     }
106
107     StoreMetadataNode getAfterRoot() {
108         return root.getNewMeta();
109     }
110
111     StoreMetadataNode getBeforeRoot() {
112         return root.getOldMeta();
113     }
114
115     @Override
116     public DataTreeCandidateNode getRootNode() {
117         return root;
118     }
119 }