Merge "Bug 981: Make sure QName is not null in RPC case."
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / ListSchemaNodeBuilder.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.net.URI;
11 import java.util.ArrayList;
12 import java.util.Date;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
18 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
21 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
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.parser.builder.api.AbstractDataNodeContainerBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
34 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
35 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
36
37 import com.google.common.base.Preconditions;
38 import com.google.common.collect.ImmutableList;
39 import com.google.common.collect.ImmutableSet;
40
41 public final class ListSchemaNodeBuilder extends AbstractDataNodeContainerBuilder implements DataSchemaNodeBuilder,
42         AugmentationTargetBuilder {
43     private ListSchemaNodeImpl instance;
44     private boolean userOrdered;
45     private List<String> keys;
46     private List<QName> keyDefinition;
47     // SchemaNode args
48     private SchemaPath schemaPath;
49     private String description;
50     private String reference;
51     private Status status = Status.CURRENT;
52     // DataSchemaNode args
53     private boolean augmenting;
54     private boolean addedByUses;
55     private boolean configuration;
56     private final ConstraintsBuilder constraints;
57     // AugmentationTarget args
58     private final List<AugmentationSchema> augmentations = new ArrayList<>();
59     private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
60
61     public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
62         super(moduleName, line, qname);
63         this.schemaPath = path;
64         constraints = new ConstraintsBuilder(moduleName, line);
65     }
66
67     public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
68             final ListSchemaNode base) {
69         super(moduleName, line, qname);
70         schemaPath = path;
71         constraints = new ConstraintsBuilder(moduleName, line, base.getConstraints());
72
73         keyDefinition = ImmutableList.copyOf(base.getKeyDefinition());
74         userOrdered = base.isUserOrdered();
75
76         description = base.getDescription();
77         reference = base.getReference();
78         status = base.getStatus();
79         augmenting = base.isAugmenting();
80         addedByUses = base.isAddedByUses();
81         configuration = base.isConfiguration();
82
83         URI ns = qname.getNamespace();
84         Date rev = qname.getRevision();
85         String pref = qname.getPrefix();
86         addedChildNodes.addAll(ParserUtils.wrapChildNodes(moduleName, line, base.getChildNodes(), path, ns, rev, pref));
87         addedGroupings.addAll(ParserUtils.wrapGroupings(moduleName, line, base.getGroupings(), path, ns, rev, pref));
88         addedTypedefs.addAll(ParserUtils.wrapTypedefs(moduleName, line, base, path, ns, rev, pref));
89         addedUnknownNodes.addAll(ParserUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path, ns,
90                 rev, pref));
91
92         augmentations.addAll(base.getAvailableAugmentations());
93         usesNodes.addAll(base.getUses());
94     }
95
96     @Override
97     public ListSchemaNode build() {
98         if (instance != null) {
99             return instance;
100         }
101
102         instance = new ListSchemaNodeImpl(qname, schemaPath);
103
104         instance.description = description;
105         instance.reference = reference;
106         instance.status = status;
107         instance.augmenting = augmenting;
108         instance.addedByUses = addedByUses;
109         instance.configuration = configuration;
110         instance.constraints = constraints.build();
111         instance.userOrdered = userOrdered;
112
113         // CHILD NODES
114         for (DataSchemaNodeBuilder node : addedChildNodes) {
115             childNodes.add(node.build());
116         }
117         instance.childNodes = ImmutableSet.copyOf(childNodes);
118
119         // KEY
120         if (keys == null) {
121             instance.keyDefinition = ImmutableList.of();
122         } else {
123             keyDefinition = new ArrayList<>();
124             for (String key : keys) {
125                 keyDefinition.add(instance.getDataChildByName(key).getQName());
126             }
127             instance.keyDefinition = ImmutableList.copyOf(keyDefinition);
128         }
129
130         // GROUPINGS
131         for (GroupingBuilder builder : addedGroupings) {
132             groupings.add(builder.build());
133         }
134         instance.groupings = ImmutableSet.copyOf(groupings);
135
136         // TYPEDEFS
137         for (TypeDefinitionBuilder entry : addedTypedefs) {
138             typedefs.add(entry.build());
139         }
140         instance.typeDefinitions = ImmutableSet.copyOf(typedefs);
141
142         // USES
143         for (UsesNodeBuilder builder : addedUsesNodes) {
144             usesNodes.add(builder.build());
145         }
146         instance.uses = ImmutableSet.copyOf(usesNodes);
147
148         // AUGMENTATIONS
149         for (AugmentationSchemaBuilder builder : augmentationBuilders) {
150             augmentations.add(builder.build());
151         }
152         instance.augmentations = ImmutableSet.copyOf(augmentations);
153
154         // UNKNOWN NODES
155         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
156             unknownNodes.add(b.build());
157         }
158         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
159
160         return instance;
161     }
162
163     @Override
164     public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
165         return addedTypedefs;
166     }
167
168     @Override
169     public void addTypedef(final TypeDefinitionBuilder type) {
170         String typeName = type.getQName().getLocalName();
171         for (TypeDefinitionBuilder addedTypedef : addedTypedefs) {
172             if (addedTypedef.getQName().getLocalName().equals(typeName)) {
173                 throw new YangParseException(moduleName, type.getLine(), "Can not add typedef '" + typeName
174                         + "': typedef with same name already declared at line " + addedTypedef.getLine());
175             }
176         }
177         addedTypedefs.add(type);
178     }
179
180     @Override
181     public SchemaPath getPath() {
182         return schemaPath;
183     }
184
185     @Override
186     public void setPath(SchemaPath path) {
187         this.schemaPath = path;
188     }
189
190     @Override
191     public String getDescription() {
192         return description;
193     }
194
195     @Override
196     public void setDescription(final String description) {
197         this.description = description;
198     }
199
200     @Override
201     public String getReference() {
202         return reference;
203     }
204
205     @Override
206     public void setReference(final String reference) {
207         this.reference = reference;
208     }
209
210     @Override
211     public Status getStatus() {
212         return status;
213     }
214
215     @Override
216     public void setStatus(Status status) {
217         this.status = Preconditions.checkNotNull(status, "status cannot be null");
218     }
219
220     @Override
221     public void addAugmentation(final AugmentationSchemaBuilder augment) {
222         augmentationBuilders.add(augment);
223     }
224
225     public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
226         return augmentationBuilders;
227     }
228
229     public List<String> getKeys() {
230         return keys;
231     }
232
233     public void setKeys(final List<String> keys) {
234         this.keys = keys;
235     }
236
237     @Override
238     public boolean isAugmenting() {
239         return augmenting;
240     }
241
242     @Override
243     public void setAugmenting(boolean augmenting) {
244         this.augmenting = augmenting;
245     }
246
247     @Override
248     public boolean isAddedByUses() {
249         return addedByUses;
250     }
251
252     @Override
253     public void setAddedByUses(final boolean addedByUses) {
254         this.addedByUses = addedByUses;
255     }
256
257     @Override
258     public boolean isConfiguration() {
259         return configuration;
260     }
261
262     @Override
263     public void setConfiguration(boolean configuration) {
264         this.configuration = configuration;
265     }
266
267     @Override
268     public ConstraintsBuilder getConstraints() {
269         return constraints;
270     }
271
272     public boolean isUserOrdered() {
273         return userOrdered;
274     }
275
276     public void setUserOrdered(final boolean userOrdered) {
277         this.userOrdered = userOrdered;
278     }
279
280     @Override
281     public int hashCode() {
282         final int prime = 31;
283         int result = 1;
284         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
285         return result;
286     }
287
288     @Override
289     public boolean equals(final Object obj) {
290         if (this == obj) {
291             return true;
292         }
293         if (obj == null) {
294             return false;
295         }
296         if (getClass() != obj.getClass()) {
297             return false;
298         }
299         ListSchemaNodeBuilder other = (ListSchemaNodeBuilder) obj;
300         if (schemaPath == null) {
301             if (other.schemaPath != null) {
302                 return false;
303             }
304         } else if (!schemaPath.equals(other.schemaPath)) {
305             return false;
306         }
307         if (parentBuilder == null) {
308             if (other.parentBuilder != null) {
309                 return false;
310             }
311         } else if (!parentBuilder.equals(other.parentBuilder)) {
312             return false;
313         }
314         return true;
315     }
316
317     @Override
318     public String toString() {
319         return "list " + qname.getLocalName();
320     }
321
322     private static final class ListSchemaNodeImpl implements ListSchemaNode {
323         private final QName qname;
324         private final SchemaPath path;
325         private String description;
326         private String reference;
327         private Status status;
328         private ImmutableList<QName> keyDefinition;
329         private boolean augmenting;
330         private boolean addedByUses;
331         private boolean configuration;
332         private ConstraintDefinition constraints;
333         private ImmutableSet<AugmentationSchema> augmentations;
334         private ImmutableSet<DataSchemaNode> childNodes;
335         private ImmutableSet<GroupingDefinition> groupings;
336         private ImmutableSet<TypeDefinition<?>> typeDefinitions;
337         private ImmutableSet<UsesNode> uses;
338         private ImmutableList<UnknownSchemaNode> unknownNodes;
339         private boolean userOrdered;
340
341         private ListSchemaNodeImpl(final QName qname, final SchemaPath path) {
342             this.qname = qname;
343             this.path = path;
344         }
345
346         @Override
347         public QName getQName() {
348             return qname;
349         }
350
351         @Override
352         public SchemaPath getPath() {
353             return path;
354         }
355
356         @Override
357         public String getDescription() {
358             return description;
359         }
360
361         @Override
362         public String getReference() {
363             return reference;
364         }
365
366         @Override
367         public Status getStatus() {
368             return status;
369         }
370
371         @Override
372         public List<QName> getKeyDefinition() {
373             return keyDefinition;
374         }
375
376         @Override
377         public boolean isAugmenting() {
378             return augmenting;
379         }
380
381         @Override
382         public boolean isAddedByUses() {
383             return addedByUses;
384         }
385
386         @Override
387         public boolean isConfiguration() {
388             return configuration;
389         }
390
391         @Override
392         public ConstraintDefinition getConstraints() {
393             return constraints;
394         }
395
396         @Override
397         public Set<AugmentationSchema> getAvailableAugmentations() {
398             return augmentations;
399         }
400
401         @Override
402         public Set<DataSchemaNode> getChildNodes() {
403             return childNodes;
404         }
405
406         @Override
407         public Set<GroupingDefinition> getGroupings() {
408             return groupings;
409         }
410
411         @Override
412         public Set<TypeDefinition<?>> getTypeDefinitions() {
413             return typeDefinitions;
414         }
415
416         @Override
417         public Set<UsesNode> getUses() {
418             return uses;
419         }
420
421         @Override
422         public DataSchemaNode getDataChildByName(final QName name) {
423             return getChildNode(childNodes, name);
424         }
425
426         @Override
427         public DataSchemaNode getDataChildByName(final String name) {
428             return getChildNode(childNodes, name);
429         }
430
431         @Override
432         public boolean isUserOrdered() {
433             return userOrdered;
434         }
435
436         @Override
437         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
438             return unknownNodes;
439         }
440
441         @Override
442         public int hashCode() {
443             final int prime = 31;
444             int result = 1;
445             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
446             result = prime * result + ((path == null) ? 0 : path.hashCode());
447             return result;
448         }
449
450         @Override
451         public boolean equals(final Object obj) {
452             if (this == obj) {
453                 return true;
454             }
455             if (obj == null) {
456                 return false;
457             }
458             if (getClass() != obj.getClass()) {
459                 return false;
460             }
461             final ListSchemaNodeImpl other = (ListSchemaNodeImpl) obj;
462             if (qname == null) {
463                 if (other.qname != null) {
464                     return false;
465                 }
466             } else if (!qname.equals(other.qname)) {
467                 return false;
468             }
469             if (path == null) {
470                 if (other.path != null) {
471                     return false;
472                 }
473             } else if (!path.equals(other.path)) {
474                 return false;
475             }
476             return true;
477         }
478
479         @Override
480         public String toString() {
481             return "list " + qname.getLocalName();
482         }
483     }
484
485 }