45227c88b549cce7a4395cfe964df8e56e4cd90e
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / InMemoryDataTreeCandidate.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
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 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;
20
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;
26
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);
32         }
33
34         protected final ModifiedNode getMod() {
35             return mod;
36         }
37
38         protected final TreeNode getNewMeta() {
39             return newMeta;
40         }
41
42         protected final TreeNode getOldMeta() {
43             return oldMeta;
44         }
45
46         private static final TreeNode childMeta(final TreeNode parent, final PathArgument id) {
47             if (parent != null) {
48                 return parent.getChild(id).orNull();
49             } else {
50                 return null;
51             }
52         }
53
54         @Override
55         public Iterable<DataTreeCandidateNode> getChildNodes() {
56             return Iterables.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
57                 @Override
58                 public DataTreeCandidateNode apply(final ModifiedNode input) {
59                     final PathArgument id = input.getIdentifier();
60                     return new ChildNode(input, childMeta(oldMeta, id), childMeta(newMeta, id));
61                 }
62             });
63         }
64
65         @Override
66         public ModificationType getModificationType() {
67             return mod.getType();
68         }
69
70         private Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
71             if (meta != null) {
72                 return Optional.<NormalizedNode<?,?>>of(meta.getData());
73             } else {
74                 return Optional.absent();
75             }
76         }
77
78         @Override
79         public Optional<NormalizedNode<?, ?>> getDataAfter() {
80             return optionalData(newMeta);
81         }
82
83         @Override
84         public Optional<NormalizedNode<?, ?>> getDataBefore() {
85             return optionalData(oldMeta);
86         }
87     }
88
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);
92         }
93
94         @Override
95         public PathArgument getIdentifier() {
96             return getMod().getIdentifier();
97         }
98     }
99
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);
103         }
104
105         @Override
106         public PathArgument getIdentifier() {
107             throw new IllegalStateException("Attempted to get identifier of the root node");
108         }
109     }
110
111     private final RootNode root;
112
113     InMemoryDataTreeCandidate(final YangInstanceIdentifier rootPath, final ModifiedNode modificationRoot,
114             final TreeNode beforeRoot, final TreeNode afterRoot) {
115         super(rootPath);
116         this.root = new RootNode(modificationRoot, beforeRoot, afterRoot);
117     }
118
119     TreeNode getAfterRoot() {
120         return root.getNewMeta();
121     }
122
123     TreeNode getBeforeRoot() {
124         return root.getOldMeta();
125     }
126
127     @Override
128     public DataTreeCandidateNode getRootNode() {
129         return root;
130     }
131 }