01ee714694d3e198988329d344e9460b2cadb701
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / ModuleBuilder.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.Collections;
13 import java.util.Date;
14 import java.util.HashSet;
15 import java.util.LinkedHashSet;
16 import java.util.LinkedList;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.TreeMap;
21 import java.util.TreeSet;
22
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Deviation;
27 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
28 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
29 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
30 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
33 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
34 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
35 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.UsesNode;
39 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
40 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
41 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
42 import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder;
43 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
44 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
45 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
46 import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
47 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
48 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
49 import org.opendaylight.yangtools.yang.parser.util.Comparators;
50 import org.opendaylight.yangtools.yang.parser.util.RefineHolder;
51 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
52
53 /**
54  * Builder of Module object. If this module is dependent on external
55  * module/modules, these dependencies must be resolved before module is built,
56  * otherwise result may not be valid.
57  */
58 public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
59     private final ModuleImpl instance;
60     private final String name;
61     private final SchemaPath schemaPath;
62     private URI namespace;
63     private String prefix;
64     private Date revision;
65
66     private final LinkedList<Builder> actualPath = new LinkedList<Builder>();
67     private final Set<TypeAwareBuilder> dirtyNodes = new HashSet<TypeAwareBuilder>();
68
69     private final Set<ModuleImport> imports = new HashSet<ModuleImport>();
70     private final List<AugmentationSchemaBuilder> addedAugments = new ArrayList<AugmentationSchemaBuilder>();
71     private final List<AugmentationSchemaBuilder> allAugments = new ArrayList<AugmentationSchemaBuilder>();
72     private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
73     private final List<UsesNodeBuilder> allUsesNodes = new ArrayList<UsesNodeBuilder>();
74     private final Set<RpcDefinitionBuilder> addedRpcs = new HashSet<RpcDefinitionBuilder>();
75     private final Set<NotificationBuilder> addedNotifications = new HashSet<NotificationBuilder>();
76     private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<IdentitySchemaNodeBuilder>();
77     private final Set<FeatureBuilder> addedFeatures = new HashSet<FeatureBuilder>();
78     private final Set<DeviationBuilder> addedDeviations = new HashSet<DeviationBuilder>();
79     private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
80     private final List<ExtensionBuilder> addedExtensions = new ArrayList<ExtensionBuilder>();
81     private final List<UnknownSchemaNodeBuilder> allUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
82
83     public ModuleBuilder(final String name) {
84         super(name, 0, null);
85         this.name = name;
86         schemaPath = new SchemaPath(Collections.<QName>emptyList(), true);
87         instance = new ModuleImpl(name);
88         actualPath.push(this);
89     }
90
91     /**
92      * Build new Module object based on this builder.
93      */
94     @Override
95     public Module build() {
96         instance.setPrefix(prefix);
97         instance.setRevision(revision);
98         instance.setImports(imports);
99         instance.setNamespace(namespace);
100
101         // TYPEDEFS
102         final Set<TypeDefinition<?>> typedefs = new TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
103         for (TypeDefinitionBuilder tdb : addedTypedefs) {
104             typedefs.add(tdb.build());
105         }
106         instance.setTypeDefinitions(typedefs);
107
108         // CHILD NODES
109         final Map<QName, DataSchemaNode> children = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
110         for (DataSchemaNodeBuilder child : addedChildNodes) {
111             children.put(child.getQName(), child.build());
112         }
113         instance.setChildNodes(children);
114
115         // GROUPINGS
116         final Set<GroupingDefinition> groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
117         for (GroupingBuilder gb : addedGroupings) {
118             groupings.add(gb.build());
119         }
120         instance.setGroupings(groupings);
121
122         // USES
123         final Set<UsesNode> usesDefinitions = new HashSet<UsesNode>();
124         for (UsesNodeBuilder unb : addedUsesNodes) {
125             usesDefinitions.add(unb.build());
126         }
127         instance.setUses(usesDefinitions);
128
129         // FEATURES
130         final Set<FeatureDefinition> features = new TreeSet<FeatureDefinition>(Comparators.SCHEMA_NODE_COMP);
131         for (FeatureBuilder fb : addedFeatures) {
132             features.add(fb.build());
133         }
134         instance.setFeatures(features);
135
136         // NOTIFICATIONS
137         final Set<NotificationDefinition> notifications = new TreeSet<NotificationDefinition>(
138                 Comparators.SCHEMA_NODE_COMP);
139         for (NotificationBuilder entry : addedNotifications) {
140             notifications.add(entry.build());
141         }
142         instance.setNotifications(notifications);
143
144         // AUGMENTATIONS
145         final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
146         for (AugmentationSchemaBuilder builder : addedAugments) {
147             augmentations.add(builder.build());
148         }
149         instance.setAugmentations(augmentations);
150
151         // RPCs
152         final Set<RpcDefinition> rpcs = new TreeSet<RpcDefinition>(Comparators.SCHEMA_NODE_COMP);
153         for (RpcDefinitionBuilder rpc : addedRpcs) {
154             rpcs.add(rpc.build());
155         }
156         instance.setRpcs(rpcs);
157
158         // DEVIATIONS
159         final Set<Deviation> deviations = new HashSet<Deviation>();
160         for (DeviationBuilder entry : addedDeviations) {
161             deviations.add(entry.build());
162         }
163         instance.setDeviations(deviations);
164
165         // EXTENSIONS
166         final List<ExtensionDefinition> extensions = new ArrayList<ExtensionDefinition>();
167         for (ExtensionBuilder eb : addedExtensions) {
168             extensions.add(eb.build());
169         }
170         Collections.sort(extensions, Comparators.SCHEMA_NODE_COMP);
171         instance.setExtensionSchemaNodes(extensions);
172
173         // IDENTITIES
174         final Set<IdentitySchemaNode> identities = new TreeSet<IdentitySchemaNode>(Comparators.SCHEMA_NODE_COMP);
175         for (IdentitySchemaNodeBuilder id : addedIdentities) {
176             identities.add(id.build());
177         }
178         instance.setIdentities(identities);
179
180         // UNKNOWN NODES
181         final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
182         for (UnknownSchemaNodeBuilder unb : addedUnknownNodes) {
183             unknownNodes.add(unb.build());
184         }
185         instance.setUnknownSchemaNodes(unknownNodes);
186
187         return instance;
188     }
189
190     public boolean isAllUsesDataCollected() {
191         for(UsesNodeBuilder usesNode : allUsesNodes) {
192             if(!usesNode.isDataCollected()) {
193                 return false;
194             }
195         }
196         return true;
197     }
198
199     @Override
200     public void setParent(Builder parent) {
201         throw new YangParseException(name, 0, "Can not set parent to module");
202     }
203
204     @Override
205     public SchemaPath getPath() {
206         return schemaPath;
207     }
208
209     @Override
210     public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
211         return addedTypedefs;
212     }
213
214     public void enterNode(final Builder node) {
215         actualPath.push(node);
216     }
217
218     public void exitNode() {
219         actualPath.pop();
220     }
221
222     public Builder getActualNode() {
223         if (actualPath.isEmpty()) {
224             return null;
225         } else {
226             return actualPath.get(0);
227         }
228     }
229
230     public Builder getActualParent() {
231         if (actualPath.size() < 2) {
232             return null;
233         } else {
234             return actualPath.get(1);
235         }
236     }
237
238     public Set<TypeAwareBuilder> getDirtyNodes() {
239         return dirtyNodes;
240     }
241
242     public List<AugmentationSchemaBuilder> getAllAugments() {
243         return allAugments;
244     }
245
246     public Set<IdentitySchemaNodeBuilder> getIdentities() {
247         return addedIdentities;
248     }
249
250     public List<UsesNodeBuilder> getAllUsesNodes() {
251         return allUsesNodes;
252     }
253
254     public Set<DeviationBuilder> getDeviations() {
255         return addedDeviations;
256     }
257
258     public List<ExtensionBuilder> getExtensions() {
259         return addedExtensions;
260     }
261
262     public List<UnknownSchemaNodeBuilder> getAllUnknownNodes() {
263         return allUnknownNodes;
264     }
265
266     public String getName() {
267         return name;
268     }
269
270     public URI getNamespace() {
271         return namespace;
272     }
273
274     public void setNamespace(final URI namespace) {
275         this.namespace = namespace;
276     }
277
278     public String getPrefix() {
279         return prefix;
280     }
281
282     public Date getRevision() {
283         return revision;
284     }
285
286     public void markActualNodeDirty() {
287         final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) getActualNode();
288         dirtyNodes.add(nodeBuilder);
289     }
290
291     public void setRevision(final Date revision) {
292         this.revision = revision;
293     }
294
295     public void setPrefix(final String prefix) {
296         this.prefix = prefix;
297     }
298
299     public void setYangVersion(final String yangVersion) {
300         instance.setYangVersion(yangVersion);
301     }
302
303     public void setDescription(final String description) {
304         instance.setDescription(description);
305     }
306
307     public void setReference(final String reference) {
308         instance.setReference(reference);
309     }
310
311     public void setOrganization(final String organization) {
312         instance.setOrganization(organization);
313     }
314
315     public void setContact(final String contact) {
316         instance.setContact(contact);
317     }
318
319     public boolean addModuleImport(final String moduleName, final Date revision, final String prefix) {
320         final ModuleImport moduleImport = createModuleImport(moduleName, revision, prefix);
321         return imports.add(moduleImport);
322     }
323
324     public Set<ModuleImport> getModuleImports() {
325         return imports;
326     }
327
328     public ExtensionBuilder addExtension(final QName qname, final int line) {
329         final String extName = qname.getLocalName();
330         for (ExtensionBuilder addedExtension : addedExtensions) {
331             if (addedExtension.getQName().getLocalName().equals(extName)) {
332                 throw new YangParseException(moduleName, line, "Can not add extension '" + extName
333                         + "': extension with same name already declared at line " + addedExtension.getLine());
334             }
335         }
336         final ExtensionBuilder builder = new ExtensionBuilder(name, line, qname);
337         addedExtensions.add(builder);
338         return builder;
339     }
340
341     public ContainerSchemaNodeBuilder addContainerNode(final int line, final QName qname, final SchemaPath schemaPath) {
342         final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
343
344         Builder parent = getActualNode();
345         builder.setParent(parent);
346         addChildToParent(parent, builder, qname.getLocalName());
347
348         return builder;
349     }
350
351     public ListSchemaNodeBuilder addListNode(final int line, final QName qname, final SchemaPath schemaPath) {
352         final ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(name, line, qname, schemaPath);
353
354         Builder parent = getActualNode();
355         builder.setParent(parent);
356         addChildToParent(parent, builder, qname.getLocalName());
357
358         return builder;
359     }
360
361     public LeafSchemaNodeBuilder addLeafNode(final int line, final QName qname, final SchemaPath schemaPath) {
362         final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(name, line, qname, schemaPath);
363
364         Builder parent = getActualNode();
365         builder.setParent(parent);
366         addChildToParent(parent, builder, qname.getLocalName());
367
368         return builder;
369     }
370
371     public LeafListSchemaNodeBuilder addLeafListNode(final int line, final QName qname, final SchemaPath schemaPath) {
372         final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(name, line, qname, schemaPath);
373
374         Builder parent = getActualNode();
375         builder.setParent(parent);
376         addChildToParent(parent, builder, qname.getLocalName());
377
378         return builder;
379     }
380
381     public GroupingBuilder addGrouping(final int line, final QName qname) {
382         final GroupingBuilder builder = new GroupingBuilderImpl(name, line, qname);
383
384         Builder parent = getActualNode();
385         builder.setParent(parent);
386
387         String groupingName = qname.getLocalName();
388         if (parent.equals(this)) {
389             for (GroupingBuilder addedGrouping : addedGroupings) {
390                 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
391                     throw new YangParseException(name, line, "grouping with same name '" + groupingName
392                             + "' already declared at line " + addedGrouping.getLine());
393                 }
394             }
395             addedGroupings.add(builder);
396         } else {
397             if (parent instanceof DataNodeContainerBuilder) {
398                 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
399                 for (GroupingBuilder addedGrouping : parentNode.getGroupingBuilders()) {
400                     if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
401                         throw new YangParseException(name, line, "grouping with same name '" + groupingName
402                                 + "' already declared at line " + addedGrouping.getLine());
403                     }
404                 }
405                 parentNode.addGrouping(builder);
406             } else if (parent instanceof RpcDefinitionBuilder) {
407                 RpcDefinitionBuilder parentNode = (RpcDefinitionBuilder) parent;
408                 for (GroupingBuilder child : parentNode.getGroupings()) {
409                     if (child.getQName().getLocalName().equals(groupingName)) {
410                         throw new YangParseException(name, line, "grouping with same name '" + groupingName
411                                 + "' already declared at line " + child.getLine());
412                     }
413                 }
414                 parentNode.addGrouping(builder);
415             } else {
416                 throw new YangParseException(name, line, "Unresolved parent of grouping " + groupingName);
417             }
418         }
419
420         return builder;
421     }
422
423     public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr) {
424         final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr);
425
426         Builder parent = getActualNode();
427         builder.setParent(parent);
428
429         if (parent.equals(this)) {
430             // augment can be declared only under 'module' ...
431             addedAugments.add(builder);
432         } else {
433             // ... or 'uses' statement
434             if (parent instanceof UsesNodeBuilder) {
435                 ((UsesNodeBuilder) parent).addAugment(builder);
436             } else {
437                 throw new YangParseException(name, line, "Augment can be declared only under module or uses statement.");
438             }
439         }
440         allAugments.add(builder);
441
442         return builder;
443     }
444
445     @Override
446     public Set<UsesNodeBuilder> getUsesNodes() {
447         return addedUsesNodes;
448     }
449
450     @Override
451     public void addUsesNode(UsesNodeBuilder usesBuilder) {
452         addedUsesNodes.add(usesBuilder);
453         allUsesNodes.add(usesBuilder);
454     }
455
456     public UsesNodeBuilder addUsesNode(final int line, final String groupingPathStr) {
457         final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(name, line, groupingPathStr);
458
459         Builder parent = getActualNode();
460         usesBuilder.setParent(parent);
461
462         if (parent.equals(this)) {
463             addedUsesNodes.add(usesBuilder);
464         } else {
465             if (!(parent instanceof DataNodeContainerBuilder)) {
466                 throw new YangParseException(name, line, "Unresolved parent of uses '" + groupingPathStr + "'.");
467             }
468             ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder);
469         }
470         allUsesNodes.add(usesBuilder);
471         return usesBuilder;
472     }
473
474     public void addRefine(final RefineHolder refine) {
475         final Builder parent = getActualNode();
476         if (!(parent instanceof UsesNodeBuilder)) {
477             throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
478         }
479         ((UsesNodeBuilder) parent).addRefine(refine);
480         refine.setParent(parent);
481     }
482
483     public RpcDefinitionBuilder addRpc(final int line, final QName qname) {
484         Builder parent = getActualNode();
485         if (!(parent.equals(this))) {
486             throw new YangParseException(name, line, "rpc can be defined only in module or submodule");
487         }
488
489         final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(name, line, qname);
490         rpcBuilder.setParent(parent);
491
492         String rpcName = qname.getLocalName();
493         for (RpcDefinitionBuilder rpc : addedRpcs) {
494             if (rpc.getQName().getLocalName().equals(rpcName)) {
495                 throw new YangParseException(name, line, "rpc with same name '" + rpcName
496                         + "' already declared at line " + rpc.getLine());
497             }
498         }
499         for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
500             if (addedChild.getQName().getLocalName().equals(rpcName)) {
501                 throw new YangParseException(name, line, "Can not add rpc: node with same name '" + rpcName
502                         + "' already declared at line " + addedChild.getLine());
503             }
504         }
505         for (NotificationBuilder addedNotification : addedNotifications) {
506             if (addedNotification.getQName().getLocalName().equals(rpcName)) {
507                 throw new YangParseException(name, line, "Can not add rpc: notification with same name '" + rpcName
508                         + "' already declared at line " + addedNotification.getLine());
509             }
510         }
511         addedRpcs.add(rpcBuilder);
512         return rpcBuilder;
513     }
514
515     public ContainerSchemaNodeBuilder addRpcInput(final int line, final QName qname, final SchemaPath schemaPath) {
516         final Builder parent = getActualNode();
517         if (!(parent instanceof RpcDefinitionBuilder)) {
518             throw new YangParseException(name, line, "input can be defined only in rpc statement");
519         }
520         final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
521
522         final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
523         inputBuilder.setParent(rpc);
524
525         rpc.setInput(inputBuilder);
526         return inputBuilder;
527     }
528
529     public ContainerSchemaNodeBuilder addRpcOutput(final SchemaPath schemaPath, final QName qname, final int line) {
530         final Builder parent = getActualNode();
531         if (!(parent instanceof RpcDefinitionBuilder)) {
532             throw new YangParseException(name, line, "output can be defined only in rpc statement");
533         }
534         final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
535
536         final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
537         outputBuilder.setParent(rpc);
538
539         rpc.setOutput(outputBuilder);
540         return outputBuilder;
541     }
542
543     public NotificationBuilder addNotification(final int line, final QName qname) {
544         final Builder parent = getActualNode();
545         if (!(parent.equals(this))) {
546             throw new YangParseException(name, line, "notification can be defined only in module or submodule");
547         }
548
549         String notificationName = qname.getLocalName();
550         for (NotificationBuilder nb : addedNotifications) {
551             if (nb.getQName().equals(qname)) {
552                 throw new YangParseException(name, line, "notification with same name '" + notificationName
553                         + "' already declared at line " + nb.getLine());
554             }
555         }
556         for (RpcDefinitionBuilder rpc : addedRpcs) {
557             if (rpc.getQName().getLocalName().equals(notificationName)) {
558                 throw new YangParseException(name, line, "Can not add notification: rpc with same name '"
559                         + notificationName + "' already declared at line " + rpc.getLine());
560             }
561         }
562         for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
563             if (addedChild.getQName().getLocalName().equals(notificationName)) {
564                 throw new YangParseException(name, line, "Can not add notification: node with same name '"
565                         + notificationName + "' already declared at line " + addedChild.getLine());
566             }
567         }
568
569         final NotificationBuilder builder = new NotificationBuilder(name, line, qname);
570         builder.setParent(parent);
571         addedNotifications.add(builder);
572
573         return builder;
574     }
575
576     public FeatureBuilder addFeature(final int line, final QName qname) {
577         Builder parent = getActualNode();
578         if (!(parent.equals(this))) {
579             throw new YangParseException(name, line, "feature can be defined only in module or submodule");
580         }
581
582         final FeatureBuilder builder = new FeatureBuilder(name, line, qname);
583         builder.setParent(parent);
584
585         String featureName = qname.getLocalName();
586         for (FeatureBuilder addedFeature : addedFeatures) {
587             if (addedFeature.getQName().getLocalName().equals(featureName)) {
588                 throw new YangParseException(name, line, "feature with same name '" + featureName
589                         + "' already declared at line " + addedFeature.getLine());
590             }
591         }
592         addedFeatures.add(builder);
593         return builder;
594     }
595
596     public ChoiceBuilder addChoice(final int line, final QName qname) {
597         final ChoiceBuilder builder = new ChoiceBuilder(name, line, qname);
598
599         Builder parent = getActualNode();
600         builder.setParent(parent);
601         addChildToParent(parent, builder, qname.getLocalName());
602
603         return builder;
604     }
605
606     public ChoiceCaseBuilder addCase(final int line, final QName qname) {
607         Builder parent = getActualNode();
608         if (parent == null || parent.equals(this)) {
609             throw new YangParseException(name, line, "'case' parent not found");
610         }
611
612         final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(name, line, qname);
613         builder.setParent(parent);
614
615         if (parent instanceof ChoiceBuilder) {
616             ((ChoiceBuilder) parent).addCase(builder);
617         } else if (parent instanceof AugmentationSchemaBuilder) {
618             ((AugmentationSchemaBuilder) parent).addChildNode(builder);
619         } else {
620             throw new YangParseException(name, line, "Unresolved parent of 'case' " + qname.getLocalName());
621         }
622
623         return builder;
624     }
625
626     public AnyXmlBuilder addAnyXml(final int line, final QName qname, final SchemaPath schemaPath) {
627         final AnyXmlBuilder builder = new AnyXmlBuilder(name, line, qname, schemaPath);
628
629         Builder parent = getActualNode();
630         builder.setParent(parent);
631         addChildToParent(parent, builder, qname.getLocalName());
632
633         return builder;
634     }
635
636     @Override
637     public void addTypedef(TypeDefinitionBuilder typedefBuilder) {
638         String nodeName = typedefBuilder.getQName().getLocalName();
639         for (TypeDefinitionBuilder tdb : addedTypedefs) {
640             if (tdb.getQName().getLocalName().equals(nodeName)) {
641                 throw new YangParseException(name, typedefBuilder.getLine(), "typedef with same name '" + nodeName
642                         + "' already declared at line " + tdb.getLine());
643             }
644         }
645         addedTypedefs.add(typedefBuilder);
646     }
647
648     public TypeDefinitionBuilderImpl addTypedef(final int line, final QName qname) {
649         final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(name, line, qname);
650
651         Builder parent = getActualNode();
652         builder.setParent(parent);
653
654         String typedefName = qname.getLocalName();
655         if (parent.equals(this)) {
656             for (TypeDefinitionBuilder tdb : addedTypedefs) {
657                 if (tdb.getQName().getLocalName().equals(typedefName)) {
658                     throw new YangParseException(name, line, "typedef with same name '" + typedefName
659                             + "' already declared at line " + tdb.getLine());
660                 }
661             }
662             addedTypedefs.add(builder);
663         } else {
664             if (parent instanceof DataNodeContainerBuilder) {
665                 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
666                 for (TypeDefinitionBuilder child : parentNode.getTypeDefinitionBuilders()) {
667                     if (child.getQName().getLocalName().equals(typedefName)) {
668                         throw new YangParseException(name, line, "typedef with same name '" + typedefName
669                                 + "' already declared at line " + child.getLine());
670                     }
671                 }
672                 parentNode.addTypedef(builder);
673             } else if (parent instanceof RpcDefinitionBuilder) {
674                 RpcDefinitionBuilder rpcParent = (RpcDefinitionBuilder) parent;
675                 for (TypeDefinitionBuilder tdb : rpcParent.getTypeDefinitions()) {
676                     if (tdb.getQName().getLocalName().equals(builder.getQName().getLocalName())) {
677                         throw new YangParseException(name, line, "typedef with same name '" + typedefName
678                                 + "' already declared at line " + tdb.getLine());
679                     }
680                 }
681                 rpcParent.addTypedef(builder);
682             } else {
683                 throw new YangParseException(name, line, "Unresolved parent of typedef " + typedefName);
684             }
685         }
686
687         return builder;
688     }
689
690     public void setType(final TypeDefinition<?> type) {
691         Builder parent = getActualNode();
692         if (parent == null || !(parent instanceof TypeAwareBuilder)) {
693             throw new YangParseException("Failed to set type '" + type.getQName().getLocalName()
694                     + "'. Invalid parent node: " + parent);
695         }
696         ((TypeAwareBuilder) parent).setType(type);
697     }
698
699     public UnionTypeBuilder addUnionType(final int line, final URI namespace, final Date revision) {
700         final Builder parent = getActualNode();
701         if (parent == null) {
702             throw new YangParseException(name, line, "Unresolved parent of union type");
703         } else {
704             final UnionTypeBuilder union = new UnionTypeBuilder(name, line);
705             if (parent instanceof TypeAwareBuilder) {
706                 ((TypeAwareBuilder) parent).setTypedef(union);
707                 return union;
708             } else {
709                 throw new YangParseException(name, line, "Invalid parent of union type.");
710             }
711         }
712     }
713
714     public void addIdentityrefType(final int line, final SchemaPath schemaPath, final String baseString) {
715         final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(name, line, baseString, schemaPath);
716
717         final Builder parent = getActualNode();
718         if (parent == null) {
719             throw new YangParseException(name, line, "Unresolved parent of identityref type.");
720         } else {
721             if (parent instanceof TypeAwareBuilder) {
722                 final TypeAwareBuilder typeParent = (TypeAwareBuilder) parent;
723                 typeParent.setTypedef(identityref);
724                 dirtyNodes.add(typeParent);
725             } else {
726                 throw new YangParseException(name, line, "Invalid parent of identityref type.");
727             }
728         }
729     }
730
731     public DeviationBuilder addDeviation(final int line, final String targetPath) {
732         Builder parent = getActualNode();
733         if (!(parent.equals(this))) {
734             throw new YangParseException(name, line, "deviation can be defined only in module or submodule");
735         }
736
737         final DeviationBuilder builder = new DeviationBuilder(name, line, targetPath);
738         builder.setParent(parent);
739         addedDeviations.add(builder);
740         return builder;
741     }
742
743     public IdentitySchemaNodeBuilder addIdentity(final QName qname, final int line) {
744         Builder parent = getActualNode();
745         if (!(parent.equals(this))) {
746             throw new YangParseException(name, line, "identity can be defined only in module or submodule");
747         }
748         String identityName = qname.getLocalName();
749         for (IdentitySchemaNodeBuilder idBuilder : addedIdentities) {
750             if (idBuilder.getQName().equals(qname)) {
751                 throw new YangParseException(name, line, "identity with same name '" + identityName
752                         + "' already declared at line " + idBuilder.getLine());
753             }
754         }
755
756         final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(name, line, qname);
757         builder.setParent(parent);
758         addedIdentities.add(builder);
759         return builder;
760     }
761
762     @Override
763     public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder builder) {
764         addedUnknownNodes.add(builder);
765         allUnknownNodes.add(builder);
766     }
767
768     public UnknownSchemaNodeBuilder addUnknownSchemaNode(final int line, final QName qname) {
769         final Builder parent = getActualNode();
770         final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(name, line, qname);
771         builder.setParent(parent);
772         allUnknownNodes.add(builder);
773
774         if (parent.equals(this)) {
775             addedUnknownNodes.add(builder);
776         } else {
777             if (parent instanceof SchemaNodeBuilder) {
778                 ((SchemaNodeBuilder) parent).addUnknownNodeBuilder(builder);
779             } else if (parent instanceof DataNodeContainerBuilder) {
780                 ((DataNodeContainerBuilder) parent).addUnknownNodeBuilder(builder);
781             } else if (parent instanceof RefineHolder) {
782                 ((RefineHolder) parent).addUnknownNodeBuilder(builder);
783             } else {
784                 throw new YangParseException(name, line, "Unresolved parent of unknown node '" + qname.getLocalName()
785                         + "'");
786             }
787         }
788
789         return builder;
790     }
791
792     @Override
793     public String toString() {
794         return "module " + name;
795     }
796
797     private final class ModuleImpl implements Module {
798         private URI namespace;
799         private final String name;
800         private Date revision;
801         private String prefix;
802         private String yangVersion;
803         private String description;
804         private String reference;
805         private String organization;
806         private String contact;
807         private Set<ModuleImport> imports = Collections.emptySet();
808         private Set<FeatureDefinition> features = Collections.emptySet();
809         private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
810         private Set<NotificationDefinition> notifications = Collections.emptySet();
811         private Set<AugmentationSchema> augmentations = Collections.emptySet();
812         private Set<RpcDefinition> rpcs = Collections.emptySet();
813         private Set<Deviation> deviations = Collections.emptySet();
814         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
815         private Set<GroupingDefinition> groupings = Collections.emptySet();
816         private Set<UsesNode> uses = Collections.emptySet();
817         private List<ExtensionDefinition> extensionNodes = Collections.emptyList();
818         private Set<IdentitySchemaNode> identities = Collections.emptySet();
819         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
820
821         private ModuleImpl(String name) {
822             this.name = name;
823         }
824
825         @Override
826         public URI getNamespace() {
827             return namespace;
828         }
829
830         private void setNamespace(URI namespace) {
831             this.namespace = namespace;
832         }
833
834         @Override
835         public String getName() {
836             return name;
837         }
838
839         @Override
840         public Date getRevision() {
841             return revision;
842         }
843
844         private void setRevision(Date revision) {
845             this.revision = revision;
846         }
847
848         @Override
849         public String getPrefix() {
850             return prefix;
851         }
852
853         private void setPrefix(String prefix) {
854             this.prefix = prefix;
855         }
856
857         @Override
858         public String getYangVersion() {
859             return yangVersion;
860         }
861
862         private void setYangVersion(String yangVersion) {
863             this.yangVersion = yangVersion;
864         }
865
866         @Override
867         public String getDescription() {
868             return description;
869         }
870
871         private void setDescription(String description) {
872             this.description = description;
873         }
874
875         @Override
876         public String getReference() {
877             return reference;
878         }
879
880         private void setReference(String reference) {
881             this.reference = reference;
882         }
883
884         @Override
885         public String getOrganization() {
886             return organization;
887         }
888
889         private void setOrganization(String organization) {
890             this.organization = organization;
891         }
892
893         @Override
894         public String getContact() {
895             return contact;
896         }
897
898         private void setContact(String contact) {
899             this.contact = contact;
900         }
901
902         @Override
903         public Set<ModuleImport> getImports() {
904             return imports;
905         }
906
907         private void setImports(Set<ModuleImport> imports) {
908             if (imports != null) {
909                 this.imports = imports;
910             }
911         }
912
913         @Override
914         public Set<FeatureDefinition> getFeatures() {
915             return features;
916         }
917
918         private void setFeatures(Set<FeatureDefinition> features) {
919             if (features != null) {
920                 this.features = features;
921             }
922         }
923
924         @Override
925         public Set<TypeDefinition<?>> getTypeDefinitions() {
926             return typeDefinitions;
927         }
928
929         private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
930             if (typeDefinitions != null) {
931                 this.typeDefinitions = typeDefinitions;
932             }
933         }
934
935         @Override
936         public Set<NotificationDefinition> getNotifications() {
937             return notifications;
938         }
939
940         private void setNotifications(Set<NotificationDefinition> notifications) {
941             if (notifications != null) {
942                 this.notifications = notifications;
943             }
944         }
945
946         @Override
947         public Set<AugmentationSchema> getAugmentations() {
948             return augmentations;
949         }
950
951         private void setAugmentations(Set<AugmentationSchema> augmentations) {
952             if (augmentations != null) {
953                 this.augmentations = augmentations;
954             }
955         }
956
957         @Override
958         public Set<RpcDefinition> getRpcs() {
959             return rpcs;
960         }
961
962         private void setRpcs(Set<RpcDefinition> rpcs) {
963             if (rpcs != null) {
964                 this.rpcs = rpcs;
965             }
966         }
967
968         @Override
969         public Set<Deviation> getDeviations() {
970             return deviations;
971         }
972
973         private void setDeviations(Set<Deviation> deviations) {
974             if (deviations != null) {
975                 this.deviations = deviations;
976             }
977         }
978
979         @Override
980         public Set<DataSchemaNode> getChildNodes() {
981             return new LinkedHashSet<DataSchemaNode>(childNodes.values());
982         }
983
984         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
985             if (childNodes != null) {
986                 this.childNodes = childNodes;
987             }
988         }
989
990         @Override
991         public Set<GroupingDefinition> getGroupings() {
992             return groupings;
993         }
994
995         private void setGroupings(Set<GroupingDefinition> groupings) {
996             if (groupings != null) {
997                 this.groupings = groupings;
998             }
999         }
1000
1001         @Override
1002         public Set<UsesNode> getUses() {
1003             return uses;
1004         }
1005
1006         private void setUses(Set<UsesNode> uses) {
1007             if (uses != null) {
1008                 this.uses = uses;
1009             }
1010         }
1011
1012         @Override
1013         public List<ExtensionDefinition> getExtensionSchemaNodes() {
1014             return extensionNodes;
1015         }
1016
1017         private void setExtensionSchemaNodes(final List<ExtensionDefinition> extensionNodes) {
1018             if (extensionNodes != null) {
1019                 this.extensionNodes = extensionNodes;
1020             }
1021         }
1022
1023         @Override
1024         public Set<IdentitySchemaNode> getIdentities() {
1025             return identities;
1026         }
1027
1028         private void setIdentities(final Set<IdentitySchemaNode> identities) {
1029             if (identities != null) {
1030                 this.identities = identities;
1031             }
1032         }
1033
1034         @Override
1035         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
1036             return unknownNodes;
1037         }
1038
1039         private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
1040             if (unknownNodes != null) {
1041                 this.unknownNodes = unknownNodes;
1042             }
1043         }
1044
1045         @Override
1046         public DataSchemaNode getDataChildByName(QName name) {
1047             return childNodes.get(name);
1048         }
1049
1050         @Override
1051         public DataSchemaNode getDataChildByName(String name) {
1052             DataSchemaNode result = null;
1053             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
1054                 if (entry.getKey().getLocalName().equals(name)) {
1055                     result = entry.getValue();
1056                     break;
1057                 }
1058             }
1059             return result;
1060         }
1061
1062         @Override
1063         public int hashCode() {
1064             final int prime = 31;
1065             int result = 1;
1066             result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1067             result = prime * result + ((name == null) ? 0 : name.hashCode());
1068             result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1069             result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1070             result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());
1071             return result;
1072         }
1073
1074         @Override
1075         public boolean equals(Object obj) {
1076             if (this == obj) {
1077                 return true;
1078             }
1079             if (obj == null) {
1080                 return false;
1081             }
1082             if (getClass() != obj.getClass()) {
1083                 return false;
1084             }
1085             ModuleImpl other = (ModuleImpl) obj;
1086             if (namespace == null) {
1087                 if (other.namespace != null) {
1088                     return false;
1089                 }
1090             } else if (!namespace.equals(other.namespace)) {
1091                 return false;
1092             }
1093             if (name == null) {
1094                 if (other.name != null) {
1095                     return false;
1096                 }
1097             } else if (!name.equals(other.name)) {
1098                 return false;
1099             }
1100             if (revision == null) {
1101                 if (other.revision != null) {
1102                     return false;
1103                 }
1104             } else if (!revision.equals(other.revision)) {
1105                 return false;
1106             }
1107             if (prefix == null) {
1108                 if (other.prefix != null) {
1109                     return false;
1110                 }
1111             } else if (!prefix.equals(other.prefix)) {
1112                 return false;
1113             }
1114             if (yangVersion == null) {
1115                 if (other.yangVersion != null) {
1116                     return false;
1117                 }
1118             } else if (!yangVersion.equals(other.yangVersion)) {
1119                 return false;
1120             }
1121             return true;
1122         }
1123
1124         @Override
1125         public String toString() {
1126             StringBuilder sb = new StringBuilder(ModuleImpl.class.getSimpleName());
1127             sb.append("[");
1128             sb.append("name=" + name);
1129             sb.append(", namespace=" + namespace);
1130             sb.append(", revision=" + revision);
1131             sb.append(", prefix=" + prefix);
1132             sb.append(", yangVersion=" + yangVersion);
1133             sb.append("]");
1134             return sb.toString();
1135         }
1136     }
1137
1138     /**
1139      * Add child to parent. Method checks for duplicates and add given child
1140      * node to parent. If node with same name is found, throws exception. If
1141      * parent is null, child node will be added directly to module.
1142      *
1143      * @param parent
1144      * @param child
1145      * @param childName
1146      */
1147     private void addChildToParent(final Builder parent, final DataSchemaNodeBuilder child, final String childName) {
1148         final int line = child.getLine();
1149         if (parent.equals(this)) {
1150             // if parent == null => node is defined under module
1151             // All leafs, leaf-lists, lists, containers, choices, rpcs,
1152             // notifications, and anyxmls defined within a parent node or at the
1153             // top level of the module or its submodules share the same
1154             // identifier namespace.
1155             for (DataSchemaNodeBuilder childNode : addedChildNodes) {
1156                 if (childNode.getQName().getLocalName().equals(childName)) {
1157                     throw new YangParseException(name, line, "Can not add '" + child
1158                             + "': node with same name already declared at line " + childNode.getLine());
1159                 }
1160             }
1161             for (RpcDefinitionBuilder rpc : addedRpcs) {
1162                 if (rpc.getQName().getLocalName().equals(childName)) {
1163                     throw new YangParseException(name, line, "Can not add '" + child
1164                             + "': rpc with same name already declared at line " + rpc.getLine());
1165                 }
1166             }
1167             for (NotificationBuilder notification : addedNotifications) {
1168                 if (notification.getQName().getLocalName().equals(childName)) {
1169                     throw new YangParseException(name, line, "Can not add '" + child
1170                             + "': notification with same name already declared at line " + notification.getLine());
1171                 }
1172             }
1173             addedChildNodes.add(child);
1174         } else {
1175             if(parent instanceof AugmentationSchemaBuilder) {
1176                 child.setAugmenting(true);
1177             }
1178             // no need for checking rpc and notification because they can be
1179             // defined only under module or submodule
1180             if (parent instanceof DataNodeContainerBuilder) {
1181                 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
1182                 for (DataSchemaNodeBuilder childNode : parentNode.getChildNodeBuilders()) {
1183                     if (childNode.getQName().getLocalName().equals(childName)) {
1184                         throw new YangParseException(name, line, "Can not add '" + child + "': node with same name '"
1185                                 + childName + "' already declared at line " + childNode.getLine());
1186                     }
1187                 }
1188                 parentNode.addChildNode(child);
1189             } else if (parent instanceof ChoiceBuilder) {
1190                 ChoiceBuilder parentNode = (ChoiceBuilder) parent;
1191                 for (ChoiceCaseBuilder caseBuilder : parentNode.getCases()) {
1192                     if (caseBuilder.getQName().getLocalName().equals(childName)) {
1193                         throw new YangParseException(name, line, "Can not add '" + child + "': case with same name '"
1194                                 + childName + "' already declared at line " + caseBuilder.getLine());
1195                     }
1196                 }
1197                 parentNode.addCase(child);
1198             } else {
1199                 throw new YangParseException(name, line, "Unresolved parent of node '" + childName + "'.");
1200             }
1201         }
1202     }
1203
1204     private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) {
1205         final ModuleImport moduleImport = new ModuleImport() {
1206             @Override
1207             public String getModuleName() {
1208                 return moduleName;
1209             }
1210
1211             @Override
1212             public Date getRevision() {
1213                 return revision;
1214             }
1215
1216             @Override
1217             public String getPrefix() {
1218                 return prefix;
1219             }
1220
1221             @Override
1222             public int hashCode() {
1223                 final int prime = 31;
1224                 int result = 1;
1225                 result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
1226                 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1227                 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1228                 return result;
1229             }
1230
1231             @Override
1232             public boolean equals(Object obj) {
1233                 if (this == obj) {
1234                     return true;
1235                 }
1236                 if (obj == null) {
1237                     return false;
1238                 }
1239                 if (getClass() != obj.getClass()) {
1240                     return false;
1241                 }
1242                 ModuleImport other = (ModuleImport) obj;
1243                 if (getModuleName() == null) {
1244                     if (other.getModuleName() != null) {
1245                         return false;
1246                     }
1247                 } else if (!getModuleName().equals(other.getModuleName())) {
1248                     return false;
1249                 }
1250                 if (getRevision() == null) {
1251                     if (other.getRevision() != null) {
1252                         return false;
1253                     }
1254                 } else if (!getRevision().equals(other.getRevision())) {
1255                     return false;
1256                 }
1257                 if (getPrefix() == null) {
1258                     if (other.getPrefix() != null) {
1259                         return false;
1260                     }
1261                 } else if (!getPrefix().equals(other.getPrefix())) {
1262                     return false;
1263                 }
1264                 return true;
1265             }
1266
1267             @Override
1268             public String toString() {
1269                 return "ModuleImport[moduleName=" + moduleName + ", revision=" + revision + ", prefix=" + prefix + "]";
1270             }
1271         };
1272         return moduleImport;
1273     }
1274
1275 }