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