Bug 319: Fixed two-phase commit verification of data.
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / util / YangSchemaUtils.java
1 package org.opendaylight.controller.sal.dom.broker.util;
2
3
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Set;
7
8 import org.opendaylight.yangtools.yang.common.QName;
9 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
10 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
11 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
12 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
13 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
14 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
15 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
19 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.Status;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.UsesNode;
27 import org.opendaylight.yangtools.yang.model.api.YangNode;
28
29 import static com.google.common.base.Preconditions.*;
30
31 import com.google.common.base.Function;
32 import com.google.common.collect.FluentIterable;
33
34 public class YangSchemaUtils {
35
36     private static final Function<PathArgument, QName> QNAME_FROM_PATH_ARGUMENT = new Function<PathArgument, QName>(){
37         
38         @Override
39         public QName apply(PathArgument input) {
40             if(input == null) {
41                 return null;
42             }
43             return input.getNodeType();
44         }
45     };
46
47     private  YangSchemaUtils() {
48         throw new UnsupportedOperationException("Utility class.");
49     }
50     
51     
52     public static DataSchemaNode getSchemaNode(SchemaContext schema,InstanceIdentifier path) {
53         checkArgument(schema != null,"YANG Schema must not be null.");
54         checkArgument(path != null,"Path must not be null.");
55         return getSchemaNode(schema, FluentIterable.from(path.getPath()).transform(QNAME_FROM_PATH_ARGUMENT));
56     }
57     
58     public static DataSchemaNode getSchemaNode(SchemaContext schema,Iterable<QName> path) {
59         checkArgument(schema != null,"YANG Schema must not be null.");
60         checkArgument(path != null,"Path must not be null.");
61         if(!path.iterator().hasNext()){
62             return toRootDataNode(schema);
63         }
64         
65         QName firstNode = path.iterator().next();
66         DataNodeContainer previous = schema.findModuleByNamespaceAndRevision(firstNode.getNamespace(),
67                 firstNode.getRevision());
68         Iterator<QName> iterator = path.iterator();
69         
70         while (iterator.hasNext()) {
71             checkArgument(previous!= null, "Supplied path does not resolve into valid schema node.");
72             QName arg = iterator.next();
73             DataSchemaNode currentNode = previous.getDataChildByName(arg);
74             if (currentNode == null && previous instanceof DataNodeContainer) {
75                 currentNode = searchInChoices(previous, arg);
76             }
77             if (currentNode instanceof DataNodeContainer) {
78                 previous = (DataNodeContainer) currentNode;
79             } else if (currentNode instanceof LeafSchemaNode || currentNode instanceof LeafListSchemaNode) {
80                 checkArgument(!iterator.hasNext(), "Path nests inside leaf node, which is not allowed.");
81                 return currentNode;
82             }
83             checkState(currentNode != null, "Current node should not be null for %s",path);
84         }
85         checkState(previous instanceof DataSchemaNode, "Schema node for %s should be instance of DataSchemaNode. Found %s",path,previous);
86         return (DataSchemaNode) previous;
87     }
88
89     private static DataSchemaNode searchInChoices(DataNodeContainer node, QName arg) {
90         Set<DataSchemaNode> children = node.getChildNodes();
91         for (DataSchemaNode child : children) {
92             if (child instanceof ChoiceNode) {
93                 ChoiceNode choiceNode = (ChoiceNode) child;
94                 DataSchemaNode potential = searchInCases(choiceNode, arg);
95                 if (potential != null) {
96                     return potential;
97                 }
98             }
99         }
100         return null;
101     }
102
103     private static DataSchemaNode searchInCases(ChoiceNode choiceNode, QName arg) {
104         Set<ChoiceCaseNode> cases = choiceNode.getCases();
105         for (ChoiceCaseNode caseNode : cases) {
106             DataSchemaNode node = caseNode.getDataChildByName(arg);
107             if (node != null) {
108                 return node;
109             }
110         }
111         return null;
112     }
113
114     private static ContainerSchemaNode toRootDataNode(SchemaContext schema) {
115         return new NetconfDataRootNode(schema);
116     }
117
118     private static final class NetconfDataRootNode implements ContainerSchemaNode {
119     
120         public NetconfDataRootNode(SchemaContext schema) {
121             // TODO Auto-generated constructor stub
122         }
123
124         public YangNode getParent() {
125             // TODO Auto-generated method stub
126             return null;
127         }
128     
129         @Override
130         public Set<TypeDefinition<?>> getTypeDefinitions() {
131             // TODO Auto-generated method stub
132             return null;
133         }
134     
135         @Override
136         public Set<DataSchemaNode> getChildNodes() {
137             // TODO Auto-generated method stub
138             return null;
139         }
140     
141         @Override
142         public Set<GroupingDefinition> getGroupings() {
143             // TODO Auto-generated method stub
144             return null;
145         }
146     
147         @Override
148         public DataSchemaNode getDataChildByName(QName name) {
149             // TODO Auto-generated method stub
150             return null;
151         }
152     
153         @Override
154         public DataSchemaNode getDataChildByName(String name) {
155             // TODO Auto-generated method stub
156             return null;
157         }
158     
159         @Override
160         public Set<UsesNode> getUses() {
161             // TODO Auto-generated method stub
162             return null;
163         }
164     
165         @Override
166         public Set<AugmentationSchema> getAvailableAugmentations() {
167             // TODO Auto-generated method stub
168             return null;
169         }
170     
171         @Override
172         public boolean isAugmenting() {
173             // TODO Auto-generated method stub
174             return false;
175         }
176     
177         @Override
178         public boolean isAddedByUses() {
179             // TODO Auto-generated method stub
180             return false;
181         }
182     
183         @Override
184         public boolean isConfiguration() {
185             // TODO Auto-generated method stub
186             return false;
187         }
188     
189         @Override
190         public ConstraintDefinition getConstraints() {
191             // TODO Auto-generated method stub
192             return null;
193         }
194     
195         @Override
196         public QName getQName() {
197             // TODO Auto-generated method stub
198             return null;
199         }
200     
201         @Override
202         public SchemaPath getPath() {
203             // TODO Auto-generated method stub
204             return null;
205         }
206     
207         @Override
208         public String getDescription() {
209             // TODO Auto-generated method stub
210             return null;
211         }
212     
213         @Override
214         public String getReference() {
215             // TODO Auto-generated method stub
216             return null;
217         }
218     
219         @Override
220         public Status getStatus() {
221             // TODO Auto-generated method stub
222             return null;
223         }
224     
225         @Override
226         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
227             // TODO Auto-generated method stub
228             return null;
229         }
230     
231         @Override
232         public boolean isPresenceContainer() {
233             // TODO Auto-generated method stub
234             return false;
235         }
236     
237     }
238
239 }