Added more granular error reporting during commit validation.
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / yangtools / yang / util / YangSchemaUtils.java
1 package org.opendaylight.yangtools.yang.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         @Override
125         public YangNode getParent() {
126             // TODO Auto-generated method stub
127             return null;
128         }
129     
130         @Override
131         public Set<TypeDefinition<?>> getTypeDefinitions() {
132             // TODO Auto-generated method stub
133             return null;
134         }
135     
136         @Override
137         public Set<DataSchemaNode> getChildNodes() {
138             // TODO Auto-generated method stub
139             return null;
140         }
141     
142         @Override
143         public Set<GroupingDefinition> getGroupings() {
144             // TODO Auto-generated method stub
145             return null;
146         }
147     
148         @Override
149         public DataSchemaNode getDataChildByName(QName name) {
150             // TODO Auto-generated method stub
151             return null;
152         }
153     
154         @Override
155         public DataSchemaNode getDataChildByName(String name) {
156             // TODO Auto-generated method stub
157             return null;
158         }
159     
160         @Override
161         public Set<UsesNode> getUses() {
162             // TODO Auto-generated method stub
163             return null;
164         }
165     
166         @Override
167         public Set<AugmentationSchema> getAvailableAugmentations() {
168             // TODO Auto-generated method stub
169             return null;
170         }
171     
172         @Override
173         public boolean isAugmenting() {
174             // TODO Auto-generated method stub
175             return false;
176         }
177     
178         @Override
179         public boolean isAddedByUses() {
180             // TODO Auto-generated method stub
181             return false;
182         }
183     
184         @Override
185         public boolean isConfiguration() {
186             // TODO Auto-generated method stub
187             return false;
188         }
189     
190         @Override
191         public ConstraintDefinition getConstraints() {
192             // TODO Auto-generated method stub
193             return null;
194         }
195     
196         @Override
197         public QName getQName() {
198             // TODO Auto-generated method stub
199             return null;
200         }
201     
202         @Override
203         public SchemaPath getPath() {
204             // TODO Auto-generated method stub
205             return null;
206         }
207     
208         @Override
209         public String getDescription() {
210             // TODO Auto-generated method stub
211             return null;
212         }
213     
214         @Override
215         public String getReference() {
216             // TODO Auto-generated method stub
217             return null;
218         }
219     
220         @Override
221         public Status getStatus() {
222             // TODO Auto-generated method stub
223             return null;
224         }
225     
226         @Override
227         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
228             // TODO Auto-generated method stub
229             return null;
230         }
231     
232         @Override
233         public boolean isPresenceContainer() {
234             // TODO Auto-generated method stub
235             return false;
236         }
237     
238     }
239
240 }