Fixed relative/absolute yang files directory resolving.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / impl / AugmentationSchemaBuilderImpl.java
1 /*
2  * Copyright (c) 2013 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.controller.yang.parser.builder.impl;
9
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.Map;
14 import java.util.Set;
15
16 import org.opendaylight.controller.yang.common.QName;
17 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
18 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
19 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
20 import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
21 import org.opendaylight.controller.yang.model.api.SchemaPath;
22 import org.opendaylight.controller.yang.model.api.Status;
23 import org.opendaylight.controller.yang.model.api.TypeDefinition;
24 import org.opendaylight.controller.yang.model.api.UsesNode;
25 import org.opendaylight.controller.yang.model.util.RevisionAwareXPathImpl;
26 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
27 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
28 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
29 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
30 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
31 import org.opendaylight.controller.yang.parser.util.YangModelBuilderUtil;
32
33 public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder {
34     private final AugmentationSchemaImpl instance;
35     private final int line;
36     private final String augmentTargetStr;
37     private SchemaPath augmentTarget;
38     private SchemaPath finalAugmentTarget;
39     private String whenCondition;
40     private final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
41     private final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
42     private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
43     private boolean resolved;
44
45     AugmentationSchemaBuilderImpl(final String augmentTargetStr, final int line) {
46         this.augmentTargetStr = augmentTargetStr;
47         this.line = line;
48         final SchemaPath targetPath = YangModelBuilderUtil
49                 .parseAugmentPath(augmentTargetStr);
50         augmentTarget = targetPath;
51         instance = new AugmentationSchemaImpl(targetPath);
52     }
53
54     @Override
55     public int getLine() {
56         return line;
57     }
58
59     @Override
60     public void addChildNode(DataSchemaNodeBuilder childNode) {
61         childNodes.add(childNode);
62     }
63
64     @Override
65     public Set<DataSchemaNodeBuilder> getChildNodes() {
66         return childNodes;
67     }
68
69     @Override
70     public void addGrouping(GroupingBuilder grouping) {
71         groupings.add(grouping);
72     }
73
74     @Override
75     public void addUsesNode(UsesNodeBuilder usesBuilder) {
76         usesNodes.add(usesBuilder);
77     }
78
79     /**
80      * Always returns null.
81      */
82     @Override
83     public QName getQName() {
84         return null;
85     }
86
87     /**
88      * Always returns null.
89      */
90     @Override
91     public SchemaPath getPath() {
92         return null;
93     }
94
95     @Override
96     public AugmentationSchema build() {
97         instance.setTargetPath(finalAugmentTarget);
98
99         RevisionAwareXPath whenStmt;
100         if (whenCondition == null) {
101             whenStmt = null;
102         } else {
103             whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
104         }
105         instance.setWhenCondition(whenStmt);
106
107         // CHILD NODES
108         final Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
109         for (DataSchemaNodeBuilder node : childNodes) {
110             childs.put(node.getQName(), node.build());
111         }
112         instance.setChildNodes(childs);
113
114         // GROUPINGS
115         final Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
116         for (GroupingBuilder builder : groupings) {
117             groupingDefinitions.add(builder.build());
118         }
119         instance.setGroupings(groupingDefinitions);
120
121         // USES
122         final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
123         for (UsesNodeBuilder builder : usesNodes) {
124             usesNodeDefinitions.add(builder.build());
125         }
126         instance.setUses(usesNodeDefinitions);
127
128         return instance;
129     }
130
131     @Override
132     public boolean isResolved() {
133         return resolved;
134     }
135
136     @Override
137     public void setResolved(boolean resolved) {
138         this.resolved = resolved;
139     }
140
141     public String getWhenCondition() {
142         return whenCondition;
143     }
144
145     public void addWhenCondition(String whenCondition) {
146         this.whenCondition = whenCondition;
147     }
148
149     @Override
150     public void addTypedef(TypeDefinitionBuilder type) {
151         throw new UnsupportedOperationException(
152                 "Augmentation can not contains type definitions");
153     }
154
155     @Override
156     public void setDescription(String description) {
157         instance.setDescription(description);
158     }
159
160     @Override
161     public void setReference(String reference) {
162         instance.setReference(reference);
163     }
164
165     @Override
166     public void setStatus(Status status) {
167         instance.setStatus(status);
168     }
169
170     @Override
171     public SchemaPath getTargetPath() {
172         return augmentTarget;
173     }
174
175     @Override
176     public void setTargetPath(SchemaPath path) {
177         this.finalAugmentTarget = path;
178     }
179
180     @Override
181     public String getTargetPathAsString() {
182         return augmentTargetStr;
183     }
184
185     @Override
186     public int hashCode() {
187         final int prime = 17;
188         int result = 1;
189         result = prime * result
190                 + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
191         result = prime * result
192                 + ((childNodes == null) ? 0 : childNodes.hashCode());
193         result = prime * result
194                 + ((groupings == null) ? 0 : groupings.hashCode());
195         result = prime * result + ((usesNodes == null) ? 0 : usesNodes.hashCode());
196         result = prime * result
197                 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
198         return result;
199     }
200
201     @Override
202     public boolean equals(Object obj) {
203         if (this == obj) {
204             return true;
205         }
206         if (obj == null) {
207             return false;
208         }
209         if (getClass() != obj.getClass()) {
210             return false;
211         }
212         AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
213         if (augmentTargetStr == null) {
214             if (other.augmentTargetStr != null) {
215                 return false;
216             }
217         } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
218             return false;
219         }
220         if (childNodes == null) {
221             if (other.childNodes != null) {
222                 return false;
223             }
224         } else if (!childNodes.equals(other.childNodes)) {
225             return false;
226         }
227         if (groupings == null) {
228             if (other.groupings != null) {
229                 return false;
230             }
231         } else if (!groupings.equals(other.groupings)) {
232             return false;
233         }
234         if (usesNodes == null) {
235             if (other.usesNodes != null) {
236                 return false;
237             }
238         } else if (!usesNodes.equals(other.usesNodes)) {
239             return false;
240         }
241         if (whenCondition == null) {
242             if (other.whenCondition != null) {
243                 return false;
244             }
245         } else if (!whenCondition.equals(other.whenCondition)) {
246             return false;
247         }
248         return true;
249     }
250
251
252     private static class AugmentationSchemaImpl implements AugmentationSchema {
253         private SchemaPath targetPath;
254         private RevisionAwareXPath whenCondition;
255         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
256         private Set<GroupingDefinition> groupings = Collections.emptySet();
257         private Set<UsesNode> uses = Collections.emptySet();
258
259         private String description;
260         private String reference;
261         private Status status;
262
263         private AugmentationSchemaImpl(SchemaPath targetPath) {
264             this.targetPath = targetPath;
265         }
266
267         @Override
268         public SchemaPath getTargetPath() {
269             return targetPath;
270         }
271
272         private void setTargetPath(SchemaPath path) {
273             this.targetPath = path;
274         }
275
276         @Override
277         public RevisionAwareXPath getWhenCondition() {
278             return whenCondition;
279         }
280
281         private void setWhenCondition(RevisionAwareXPath whenCondition) {
282             this.whenCondition = whenCondition;
283         }
284
285         @Override
286         public Set<DataSchemaNode> getChildNodes() {
287             return new HashSet<DataSchemaNode>(childNodes.values());
288         }
289
290         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
291             if (childNodes != null) {
292                 this.childNodes = childNodes;
293             }
294         }
295
296         @Override
297         public Set<GroupingDefinition> getGroupings() {
298             return groupings;
299         }
300
301         private void setGroupings(Set<GroupingDefinition> groupings) {
302             if (groupings != null) {
303                 this.groupings = groupings;
304             }
305         }
306
307         @Override
308         public Set<UsesNode> getUses() {
309             return uses;
310         }
311
312         private void setUses(Set<UsesNode> uses) {
313             if (uses != null) {
314                 this.uses = uses;
315             }
316         }
317
318         /**
319          * Always returns an empty set, because augmentation can not contains
320          * type definitions.
321          */
322         @Override
323         public Set<TypeDefinition<?>> getTypeDefinitions() {
324             return Collections.emptySet();
325         }
326
327         @Override
328         public String getDescription() {
329             return description;
330         }
331
332         private void setDescription(String description) {
333             this.description = description;
334         }
335
336         @Override
337         public String getReference() {
338             return reference;
339         }
340
341         private void setReference(String reference) {
342             this.reference = reference;
343         }
344
345         @Override
346         public Status getStatus() {
347             return status;
348         }
349
350         private void setStatus(Status status) {
351             this.status = status;
352         }
353
354         @Override
355         public DataSchemaNode getDataChildByName(QName name) {
356             return childNodes.get(name);
357         }
358
359         @Override
360         public DataSchemaNode getDataChildByName(String name) {
361             DataSchemaNode result = null;
362             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
363                 if (entry.getKey().getLocalName().equals(name)) {
364                     result = entry.getValue();
365                     break;
366                 }
367             }
368             return result;
369         }
370
371         @Override
372         public int hashCode() {
373             final int prime = 17;
374             int result = 1;
375             result = prime * result
376                     + ((targetPath == null) ? 0 : targetPath.hashCode());
377             result = prime * result
378                     + ((childNodes == null) ? 0 : childNodes.hashCode());
379             result = prime * result
380                     + ((groupings == null) ? 0 : groupings.hashCode());
381             result = prime * result + ((uses == null) ? 0 : uses.hashCode());
382             result = prime * result
383                     + ((whenCondition == null) ? 0 : whenCondition.hashCode());
384             return result;
385         }
386
387         @Override
388         public boolean equals(Object obj) {
389             if (this == obj) {
390                 return true;
391             }
392             if (obj == null) {
393                 return false;
394             }
395             if (getClass() != obj.getClass()) {
396                 return false;
397             }
398             AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
399             if (targetPath == null) {
400                 if (other.targetPath != null) {
401                     return false;
402                 }
403             } else if (!targetPath.equals(other.targetPath)) {
404                 return false;
405             }
406             if (childNodes == null) {
407                 if (other.childNodes != null) {
408                     return false;
409                 }
410             } else if (!childNodes.equals(other.childNodes)) {
411                 return false;
412             }
413             if (groupings == null) {
414                 if (other.groupings != null) {
415                     return false;
416                 }
417             } else if (!groupings.equals(other.groupings)) {
418                 return false;
419             }
420             if (uses == null) {
421                 if (other.uses != null) {
422                     return false;
423                 }
424             } else if (!uses.equals(other.uses)) {
425                 return false;
426             }
427             if (whenCondition == null) {
428                 if (other.whenCondition != null) {
429                     return false;
430                 }
431             } else if (!whenCondition.equals(other.whenCondition)) {
432                 return false;
433             }
434             return true;
435         }
436
437         @Override
438         public String toString() {
439             StringBuilder sb = new StringBuilder(
440                     AugmentationSchemaImpl.class.getSimpleName());
441             sb.append("[");
442             sb.append("targetPath=" + targetPath);
443             sb.append(", childNodes=" + childNodes.values());
444             sb.append(", groupings=" + groupings);
445             sb.append(", uses=" + uses);
446             sb.append("]");
447             return sb.toString();
448         }
449     }
450
451 }