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