Merge "Added more Rpc markers to yang-binding."
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / GroupingBuilderImpl.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.yangtools.yang.parser.builder.impl;
9
10 import java.util.Collections;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Set;
14 import java.util.TreeSet;
15
16 import org.opendaylight.yangtools.yang.common.QName;
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.SchemaPath;
20 import org.opendaylight.yangtools.yang.model.api.Status;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.UsesNode;
24 import org.opendaylight.yangtools.yang.model.api.YangNode;
25 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.util.Comparators;
31 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
32
33 public final class GroupingBuilderImpl extends AbstractDataNodeContainerBuilder implements GroupingBuilder {
34     private boolean isBuilt;
35     private final GroupingDefinitionImpl instance;
36     private SchemaPath schemaPath;
37     private String description;
38     private String reference;
39     private Status status = Status.CURRENT;
40     private boolean addedByUses;
41
42     public GroupingBuilderImpl(final String moduleName, final int line, final QName qname) {
43         super(moduleName, line, qname);
44         instance = new GroupingDefinitionImpl(qname);
45     }
46
47     @Override
48     public GroupingDefinition build(YangNode parent) {
49         if (!isBuilt) {
50             instance.setParent(parent);
51             instance.setPath(schemaPath);
52             instance.setDescription(description);
53             instance.setReference(reference);
54             instance.setStatus(status);
55             instance.setAddedByUses(addedByUses);
56
57             // CHILD NODES
58             for (DataSchemaNodeBuilder node : addedChildNodes) {
59                 DataSchemaNode child = node.build(instance);
60                 childNodes.put(child.getQName(), child);
61             }
62             instance.setChildNodes(childNodes);
63
64             // GROUPINGS
65             for (GroupingBuilder builder : addedGroupings) {
66                 groupings.add(builder.build(instance));
67             }
68             instance.setGroupings(groupings);
69
70             // TYPEDEFS
71             for (TypeDefinitionBuilder entry : addedTypedefs) {
72                 typedefs.add(entry.build(instance));
73             }
74             instance.setTypeDefinitions(typedefs);
75
76             // USES
77             for (UsesNodeBuilder builder : addedUsesNodes) {
78                 usesNodes.add(builder.build(instance));
79             }
80             instance.setUses(usesNodes);
81
82             // UNKNOWN NODES
83             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
84                 unknownNodes.add(b.build(instance));
85             }
86             Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
87             instance.setUnknownSchemaNodes(unknownNodes);
88
89             isBuilt = true;
90         }
91
92         return instance;
93     }
94
95
96     @Override
97     public void setQName(QName qname) {
98         this.qname = qname;
99     }
100
101     @Override
102     public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
103         return addedTypedefs;
104     }
105
106     @Override
107     public void addTypedef(final TypeDefinitionBuilder type) {
108         String typeName = type.getQName().getLocalName();
109         for (TypeDefinitionBuilder addedTypedef : addedTypedefs) {
110             throw new YangParseException(moduleName, type.getLine(), "Can not add typedef '" + typeName
111                     + "': typedef with same name already declared at line " + addedTypedef.getLine());
112         }
113         addedTypedefs.add(type);
114     }
115
116     public void setTypedefs(final Set<TypeDefinition<?>> typedefs) {
117         this.typedefs = typedefs;
118     }
119
120     @Override
121     public SchemaPath getPath() {
122         return schemaPath;
123     }
124
125     @Override
126     public void setPath(SchemaPath schemaPath) {
127         this.schemaPath = schemaPath;
128     }
129
130     @Override
131     public String getDescription() {
132         return description;
133     }
134
135     @Override
136     public void setDescription(final String description) {
137         this.description = description;
138     }
139
140     @Override
141     public String getReference() {
142         return reference;
143     }
144
145     @Override
146     public void setReference(final String reference) {
147         this.reference = reference;
148     }
149
150     @Override
151     public Status getStatus() {
152         return status;
153     }
154
155     @Override
156     public void setStatus(final Status status) {
157         this.status = status;
158     }
159
160     @Override
161     public boolean isAddedByUses() {
162         return addedByUses;
163     }
164
165     @Override
166     public void setAddedByUses(final boolean addedByUses) {
167         this.addedByUses = addedByUses;
168     }
169
170     @Override
171     public String toString() {
172         return "grouping " + qname.getLocalName();
173     }
174
175     @Override
176     public int hashCode() {
177         final int prime = 31;
178         int result = 1;
179         result = prime * result + ((parentBuilder == null) ? 0 : parentBuilder.hashCode());
180         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
181         return result;
182     }
183
184     @Override
185     public boolean equals(Object obj) {
186         if (this == obj) {
187             return true;
188         }
189         if (obj == null) {
190             return false;
191         }
192         if (getClass() != obj.getClass()) {
193             return false;
194         }
195         if (!super.equals(obj)) {
196             return false;
197         }
198         final GroupingBuilderImpl other = (GroupingBuilderImpl) obj;
199         if (parentBuilder == null) {
200             if (other.parentBuilder != null) {
201                 return false;
202             }
203         } else if (!parentBuilder.equals(other.parentBuilder)) {
204             return false;
205         }
206         if (schemaPath == null) {
207             if (other.schemaPath != null) {
208                 return false;
209             }
210         } else if (!schemaPath.equals(other.schemaPath)) {
211             return false;
212         }
213         return true;
214     }
215
216
217     private final class GroupingDefinitionImpl implements GroupingDefinition {
218         private final QName qname;
219         private SchemaPath path;
220         private YangNode parent;
221         private String description;
222         private String reference;
223         private Status status;
224         private boolean addedByUses;
225         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
226         private Set<GroupingDefinition> groupings = Collections.emptySet();
227         private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
228         private Set<UsesNode> uses = Collections.emptySet();
229         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
230
231         private GroupingDefinitionImpl(final QName qname) {
232             this.qname = qname;
233         }
234
235         @Override
236         public QName getQName() {
237             return qname;
238         }
239
240         @Override
241         public SchemaPath getPath() {
242             return path;
243         }
244
245         private void setPath(SchemaPath path) {
246             this.path = path;
247         }
248
249         @Override
250         public YangNode getParent() {
251             return parent;
252         }
253
254         private void setParent(YangNode parent) {
255             this.parent = parent;
256         }
257
258         @Override
259         public String getDescription() {
260             return description;
261         }
262
263         private void setDescription(String description) {
264             this.description = description;
265         }
266
267         @Override
268         public String getReference() {
269             return reference;
270         }
271
272         private void setReference(String reference) {
273             this.reference = reference;
274         }
275
276         @Override
277         public Status getStatus() {
278             return status;
279         }
280
281         private void setStatus(Status status) {
282             this.status = status;
283         }
284
285         @Override
286         public boolean isAddedByUses() {
287             return addedByUses;
288         }
289
290         private void setAddedByUses(final boolean addedByUses) {
291             this.addedByUses = addedByUses;
292         }
293
294         @Override
295         public Set<DataSchemaNode> getChildNodes() {
296             final Set<DataSchemaNode> result = new TreeSet<DataSchemaNode>(Comparators.SCHEMA_NODE_COMP);
297             result.addAll(childNodes.values());
298             return result;
299         }
300
301         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
302             this.childNodes = childNodes;
303         }
304
305         @Override
306         public Set<GroupingDefinition> getGroupings() {
307             return groupings;
308         }
309
310         private void setGroupings(Set<GroupingDefinition> groupings) {
311             this.groupings = groupings;
312         }
313
314         @Override
315         public Set<UsesNode> getUses() {
316             return uses;
317         }
318
319         private void setUses(Set<UsesNode> uses) {
320             this.uses = uses;
321         }
322
323         @Override
324         public Set<TypeDefinition<?>> getTypeDefinitions() {
325             return typeDefinitions;
326         }
327
328         private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
329             this.typeDefinitions = typeDefinitions;
330         }
331
332         @Override
333         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
334             return unknownNodes;
335         }
336
337         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
338             if (unknownNodes != null) {
339                 this.unknownNodes = unknownNodes;
340             }
341         }
342
343         @Override
344         public DataSchemaNode getDataChildByName(QName name) {
345             return childNodes.get(name);
346         }
347
348         @Override
349         public DataSchemaNode getDataChildByName(String name) {
350             DataSchemaNode result = null;
351             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
352                 if (entry.getKey().getLocalName().equals(name)) {
353                     result = entry.getValue();
354                     break;
355                 }
356             }
357             return result;
358         }
359
360         @Override
361         public int hashCode() {
362             final int prime = 31;
363             int result = 1;
364             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
365             result = prime * result + ((path == null) ? 0 : path.hashCode());
366             return result;
367         }
368
369         @Override
370         public boolean equals(Object obj) {
371             if (this == obj) {
372                 return true;
373             }
374             if (obj == null) {
375                 return false;
376             }
377             if (getClass() != obj.getClass()) {
378                 return false;
379             }
380             final GroupingDefinitionImpl other = (GroupingDefinitionImpl) obj;
381             if (qname == null) {
382                 if (other.qname != null) {
383                     return false;
384                 }
385             } else if (!qname.equals(other.qname)) {
386                 return false;
387             }
388             if (path == null) {
389                 if (other.path != null) {
390                     return false;
391                 }
392             } else if (!path.equals(other.path)) {
393                 return false;
394             }
395             return true;
396         }
397
398         @Override
399         public String toString() {
400             StringBuilder sb = new StringBuilder(GroupingDefinitionImpl.class.getSimpleName());
401             sb.append("[");
402             sb.append("qname=" + qname);
403             sb.append("]");
404             return sb.toString();
405         }
406     }
407
408 }