2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.yang.parser.builder.impl;
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.LinkedList;
17 import java.util.List;
21 import org.opendaylight.controller.yang.common.QName;
22 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
23 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
24 import org.opendaylight.controller.yang.model.api.Deviation;
25 import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
26 import org.opendaylight.controller.yang.model.api.FeatureDefinition;
27 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
28 import org.opendaylight.controller.yang.model.api.IdentitySchemaNode;
29 import org.opendaylight.controller.yang.model.api.Module;
30 import org.opendaylight.controller.yang.model.api.ModuleImport;
31 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
32 import org.opendaylight.controller.yang.model.api.RpcDefinition;
33 import org.opendaylight.controller.yang.model.api.SchemaPath;
34 import org.opendaylight.controller.yang.model.api.TypeDefinition;
35 import org.opendaylight.controller.yang.model.api.UsesNode;
36 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
37 import org.opendaylight.controller.yang.parser.builder.api.Builder;
38 import org.opendaylight.controller.yang.parser.builder.api.DataNodeContainerBuilder;
39 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
40 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
41 import org.opendaylight.controller.yang.parser.builder.api.SchemaNodeBuilder;
42 import org.opendaylight.controller.yang.parser.builder.api.TypeAwareBuilder;
43 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionAwareBuilder;
44 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
45 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
46 import org.opendaylight.controller.yang.parser.util.RefineHolder;
47 import org.opendaylight.controller.yang.parser.util.YangParseException;
50 * This builder builds Module object. If this module is dependent on external
51 * module/modules, these dependencies must be resolved before module is built,
52 * otherwise result may not be valid.
54 public class ModuleBuilder implements Builder {
55 private final ModuleImpl instance;
56 private final String name;
57 private URI namespace;
58 private String prefix;
59 private Date revision;
61 private int augmentsResolved;
63 private final Set<ModuleImport> imports = new HashSet<ModuleImport>();
66 * Holds all child (DataSchemaNode) nodes: anyxml, choice, case, container,
67 * list, leaf, leaf-list.
69 private final Map<List<String>, DataSchemaNodeBuilder> childNodes = new HashMap<List<String>, DataSchemaNodeBuilder>();
71 private final Map<List<String>, GroupingBuilder> addedGroupings = new HashMap<List<String>, GroupingBuilder>();
72 private final List<AugmentationSchemaBuilder> addedAugments = new ArrayList<AugmentationSchemaBuilder>();
73 private final Map<List<String>, UsesNodeBuilder> addedUsesNodes = new HashMap<List<String>, UsesNodeBuilder>();
74 //private final Map<List<String>, RefineHolder> addedRefines = new HashMap<List<String>, RefineHolder>();
75 private final Map<List<String>, RpcDefinitionBuilder> addedRpcs = new HashMap<List<String>, RpcDefinitionBuilder>();
76 private final Set<NotificationBuilder> addedNotifications = new HashSet<NotificationBuilder>();
77 private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<IdentitySchemaNodeBuilder>();
78 private final Map<List<String>, FeatureBuilder> addedFeatures = new HashMap<List<String>, FeatureBuilder>();
79 private final Map<List<String>, DeviationBuilder> addedDeviations = new HashMap<List<String>, DeviationBuilder>();
80 private final Map<List<String>, TypeDefinitionBuilder> addedTypedefs = new HashMap<List<String>, TypeDefinitionBuilder>();
81 private final Map<List<String>, UnionTypeBuilder> addedUnionTypes = new HashMap<List<String>, UnionTypeBuilder>();
82 private final List<ExtensionBuilder> addedExtensions = new ArrayList<ExtensionBuilder>();
83 private final Set<UnknownSchemaNodeBuilder> addedUnknownNodes = new HashSet<UnknownSchemaNodeBuilder>();
85 private final Map<List<String>, TypeAwareBuilder> dirtyNodes = new HashMap<List<String>, TypeAwareBuilder>();
87 private final LinkedList<Builder> actualPath = new LinkedList<Builder>();
89 public ModuleBuilder(final String name) {
91 instance = new ModuleImpl(name);
95 * Build new Module object based on this builder.
98 public Module build() {
99 instance.setPrefix(prefix);
100 instance.setRevision(revision);
101 instance.setImports(imports);
102 instance.setNamespace(namespace);
105 final Set<TypeDefinition<?>> typedefs = buildModuleTypedefs(addedTypedefs);
106 instance.setTypeDefinitions(typedefs);
109 final Map<QName, DataSchemaNode> children = buildModuleChildNodes(childNodes);
110 instance.setChildNodes(children);
113 final Set<GroupingDefinition> groupings = buildModuleGroupings(addedGroupings);
114 instance.setGroupings(groupings);
117 final Set<UsesNode> usesDefinitions = buildUsesNodes(addedUsesNodes);
118 instance.setUses(usesDefinitions);
121 final Set<FeatureDefinition> features = buildModuleFeatures(addedFeatures);
122 instance.setFeatures(features);
125 final Set<NotificationDefinition> notifications = new HashSet<NotificationDefinition>();
126 for (NotificationBuilder entry : addedNotifications) {
127 notifications.add((NotificationDefinition) entry.build());
129 instance.setNotifications(notifications);
132 final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
133 for (AugmentationSchemaBuilder builder : addedAugments) {
134 augmentations.add(builder.build());
136 instance.setAugmentations(augmentations);
139 final Set<RpcDefinition> rpcs = buildModuleRpcs(addedRpcs);
140 instance.setRpcs(rpcs);
143 final Set<Deviation> deviations = new HashSet<Deviation>();
144 for (Map.Entry<List<String>, DeviationBuilder> entry : addedDeviations
146 deviations.add(entry.getValue().build());
148 instance.setDeviations(deviations);
151 final List<ExtensionDefinition> extensions = new ArrayList<ExtensionDefinition>();
152 for (ExtensionBuilder b : addedExtensions) {
153 extensions.add(b.build());
155 instance.setExtensionSchemaNodes(extensions);
158 final Set<IdentitySchemaNode> identities = new HashSet<IdentitySchemaNode>();
159 for (IdentitySchemaNodeBuilder idBuilder : addedIdentities) {
160 identities.add(idBuilder.build());
162 instance.setIdentities(identities);
168 public int getLine() {
172 public void enterNode(final Builder node) {
173 actualPath.push(node);
176 public void exitNode() {
180 public Builder getModuleNode(final List<String> path) {
181 return childNodes.get(path);
184 public GroupingBuilder getGrouping(final List<String> path) {
185 return addedGroupings.get(path);
188 public Builder getModuleTypedef(final List<String> path) {
189 return addedTypedefs.get(path);
192 public Set<DataSchemaNodeBuilder> getChildNodes() {
193 final Set<DataSchemaNodeBuilder> children = new HashSet<DataSchemaNodeBuilder>();
194 for (Map.Entry<List<String>, DataSchemaNodeBuilder> entry : childNodes
196 final List<String> path = entry.getKey();
197 final DataSchemaNodeBuilder child = entry.getValue();
198 if (path.size() == 2) {
205 public Map<List<String>, TypeAwareBuilder> getDirtyNodes() {
209 public List<AugmentationSchemaBuilder> getAugments() {
210 return addedAugments;
213 public Set<IdentitySchemaNodeBuilder> getIdentities() {
214 return addedIdentities;
217 public Map<List<String>, UsesNodeBuilder> getUsesNodes() {
218 return addedUsesNodes;
221 public Set<UnknownSchemaNodeBuilder> getUnknownNodes() {
222 return addedUnknownNodes;
225 public Set<TypeDefinitionBuilder> getModuleTypedefs() {
226 final Set<TypeDefinitionBuilder> typedefs = new HashSet<TypeDefinitionBuilder>();
227 for (Map.Entry<List<String>, TypeDefinitionBuilder> entry : addedTypedefs
229 if (entry.getKey().size() == 2) {
230 typedefs.add(entry.getValue());
236 public String getName() {
240 public URI getNamespace() {
244 public void setNamespace(final URI namespace) {
245 this.namespace = namespace;
248 public String getPrefix() {
252 public Date getRevision() {
256 public int getAugmentsResolved() {
257 return augmentsResolved;
260 public void augmentResolved() {
264 public void addDirtyNode(final List<String> path) {
265 final List<String> dirtyNodePath = new ArrayList<String>(path);
266 final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) actualPath
268 dirtyNodes.put(dirtyNodePath, nodeBuilder);
271 public void setRevision(final Date revision) {
272 this.revision = revision;
275 public void setPrefix(final String prefix) {
276 this.prefix = prefix;
279 public void setYangVersion(final String yangVersion) {
280 instance.setYangVersion(yangVersion);
283 public void setDescription(final String description) {
284 instance.setDescription(description);
287 public void setReference(final String reference) {
288 instance.setReference(reference);
291 public void setOrganization(final String organization) {
292 instance.setOrganization(organization);
295 public void setContact(final String contact) {
296 instance.setContact(contact);
299 public boolean addModuleImport(final String moduleName,
300 final Date revision, final String prefix) {
301 final ModuleImport moduleImport = createModuleImport(moduleName,
303 return imports.add(moduleImport);
306 public Set<ModuleImport> getModuleImports() {
310 public ExtensionBuilder addExtension(final QName qname, final int line) {
311 final ExtensionBuilder builder = new ExtensionBuilder(qname, line);
312 addedExtensions.add(builder);
316 public ContainerSchemaNodeBuilder addContainerNode(
317 final QName containerName, final List<String> parentPath,
319 final List<String> pathToNode = new ArrayList<String>(parentPath);
320 final ContainerSchemaNodeBuilder containerBuilder = new ContainerSchemaNodeBuilder(
321 containerName, line);
322 updateParent(containerBuilder, line, "container");
324 pathToNode.add(containerName.getLocalName());
325 childNodes.put(pathToNode, containerBuilder);
327 return containerBuilder;
330 public ListSchemaNodeBuilder addListNode(final QName listName,
331 final List<String> parentPath, final int line) {
332 final List<String> pathToNode = new ArrayList<String>(parentPath);
333 final ListSchemaNodeBuilder listBuilder = new ListSchemaNodeBuilder(
335 updateParent(listBuilder, line, "list");
337 pathToNode.add(listName.getLocalName());
338 childNodes.put(pathToNode, listBuilder);
343 public LeafSchemaNodeBuilder addLeafNode(final QName leafName,
344 final List<String> parentPath, final int line) {
345 final List<String> pathToNode = new ArrayList<String>(parentPath);
346 final LeafSchemaNodeBuilder leafBuilder = new LeafSchemaNodeBuilder(
348 updateParent(leafBuilder, line, "leaf");
350 pathToNode.add(leafName.getLocalName());
351 childNodes.put(pathToNode, leafBuilder);
356 public LeafListSchemaNodeBuilder addLeafListNode(final QName qname,
357 final List<String> parentPath, final int line) {
358 final List<String> pathToNode = new ArrayList<String>(parentPath);
359 final LeafListSchemaNodeBuilder leafListBuilder = new LeafListSchemaNodeBuilder(
361 updateParent(leafListBuilder, line, "leaf-list");
363 pathToNode.add(qname.getLocalName());
364 childNodes.put(pathToNode, leafListBuilder);
366 return leafListBuilder;
369 public GroupingBuilder addGrouping(final QName qname,
370 final List<String> parentPath, final int line) {
371 final List<String> pathToGroup = new ArrayList<String>(parentPath);
372 final GroupingBuilder builder = new GroupingBuilderImpl(qname, line);
374 if (!(actualPath.isEmpty())) {
375 final Builder parent = actualPath.getFirst();
376 if (parent instanceof DataNodeContainerBuilder) {
377 ((DataNodeContainerBuilder) parent).addGrouping(builder);
379 throw new YangParseException(name, line,
380 "Unresolved parent of grouping " + qname.getLocalName());
384 pathToGroup.add(qname.getLocalName());
385 addedGroupings.put(pathToGroup, builder);
390 public AugmentationSchemaBuilder addAugment(final String name,
391 final List<String> parentPath, final int line) {
392 final List<String> pathToAugment = new ArrayList<String>(parentPath);
393 final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(
396 // augment can only be in 'module' or 'uses' statement
397 if (!(actualPath.isEmpty())) {
398 final Builder parent = actualPath.getFirst();
399 if (parent instanceof UsesNodeBuilder) {
400 ((UsesNodeBuilder) parent).addAugment(builder);
402 throw new YangParseException(this.name, line,
403 "Unresolved parent of augment " + name);
407 pathToAugment.add(name);
408 addedAugments.add(builder);
413 public UsesNodeBuilder addUsesNode(final String groupingPathStr,
414 final List<String> parentPath, final int line) {
415 final List<String> pathToUses = new ArrayList<String>(parentPath);
416 final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(
417 groupingPathStr, line);
419 if (!(actualPath.isEmpty())) {
420 final Builder parent = actualPath.getFirst();
421 if (parent instanceof DataNodeContainerBuilder) {
422 if (parent instanceof AugmentationSchemaBuilder) {
423 usesBuilder.setAugmenting(true);
425 ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder);
427 throw new YangParseException(name, line,
428 "Unresolved parent of uses " + groupingPathStr);
432 pathToUses.add(groupingPathStr);
433 addedUsesNodes.put(pathToUses, usesBuilder);
437 public void addRefine(final RefineHolder refine,
438 final List<String> parentPath) {
439 final List<String> path = new ArrayList<String>(parentPath);
441 if (actualPath.isEmpty()) {
442 throw new YangParseException(name, refine.getLine(),
443 "refine can be defined only in uses statement");
445 final Builder parent = actualPath.getFirst();
446 if (parent instanceof UsesNodeBuilder) {
447 ((UsesNodeBuilder) parent).addRefine(refine);
449 throw new YangParseException(name, refine.getLine(),
450 "refine can be defined only in uses statement");
454 path.add(refine.getName());
457 public RpcDefinitionBuilder addRpc(final QName qname,
458 final List<String> parentPath, final int line) {
460 if (!(actualPath.isEmpty())) {
461 throw new YangParseException(name, line,
462 "rpc can be defined only in module or submodule");
465 final List<String> pathToRpc = new ArrayList<String>(parentPath);
466 final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(qname,
469 pathToRpc.add(qname.getLocalName());
470 addedRpcs.put(pathToRpc, rpcBuilder);
475 public ContainerSchemaNodeBuilder addRpcInput(final QName inputQName,
477 final Builder parent = actualPath.getFirst();
478 if (!(parent instanceof RpcDefinitionBuilder)) {
479 throw new YangParseException(name, line,
480 "input can be defined only in rpc statement");
482 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
484 final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(
486 rpc.setInput(inputBuilder);
490 public ContainerSchemaNodeBuilder addRpcOutput(final QName outputQName,
492 final Builder parent = actualPath.getFirst();
493 if (!(parent instanceof RpcDefinitionBuilder)) {
494 throw new YangParseException(name, line,
495 "output can be defined only in rpc statement");
497 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
499 final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(
501 rpc.setOutput(outputBuilder);
502 return outputBuilder;
505 public NotificationBuilder addNotification(final QName notificationName,
506 final List<String> parentPath, final int line) {
507 if (!(actualPath.isEmpty())) {
508 throw new YangParseException(name, line,
509 "notification can be defined only in module or submodule");
512 final NotificationBuilder builder = new NotificationBuilder(
513 notificationName, line);
515 final List<String> notificationPath = new ArrayList<String>(parentPath);
516 notificationPath.add(notificationName.getLocalName());
517 addedNotifications.add(builder);
522 public FeatureBuilder addFeature(final QName featureName,
523 final List<String> parentPath, final int line) {
524 if (!(actualPath.isEmpty())) {
525 throw new YangParseException(name, line,
526 "feature can be defined only in module or submodule");
529 final List<String> pathToFeature = new ArrayList<String>(parentPath);
530 pathToFeature.add(featureName.getLocalName());
532 final FeatureBuilder builder = new FeatureBuilder(featureName, line);
533 addedFeatures.put(pathToFeature, builder);
537 public ChoiceBuilder addChoice(final QName choiceName,
538 final List<String> parentPath, final int line) {
539 final List<String> pathToChoice = new ArrayList<String>(parentPath);
540 final ChoiceBuilder builder = new ChoiceBuilder(choiceName, line);
542 if (!(actualPath.isEmpty())) {
543 Builder parent = actualPath.getFirst();
544 if (parent instanceof DataNodeContainerBuilder) {
545 if (parent instanceof AugmentationSchemaBuilder) {
546 builder.setAugmenting(true);
548 ((DataNodeContainerBuilder) parent).addChildNode(builder);
550 throw new YangParseException(name, line,
551 "Unresolved parent of choice "
552 + choiceName.getLocalName());
556 pathToChoice.add(choiceName.getLocalName());
557 childNodes.put(pathToChoice, builder);
562 public ChoiceCaseBuilder addCase(final QName caseName,
563 final List<String> parentPath, final int line) {
564 final List<String> pathToCase = new ArrayList<String>(parentPath);
565 final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(caseName, line);
567 if (actualPath.isEmpty()) {
568 throw new YangParseException(name, line, "'case' parent not found");
570 final Builder parent = actualPath.getFirst();
571 if (parent instanceof ChoiceBuilder) {
572 ((ChoiceBuilder) parent).addChildNode(builder);
573 } else if (parent instanceof AugmentationSchemaBuilder) {
574 builder.setAugmenting(true);
575 ((AugmentationSchemaBuilder) parent).addChildNode(builder);
577 throw new YangParseException(name, line,
578 "Unresolved parent of 'case' "
579 + caseName.getLocalName());
583 pathToCase.add(caseName.getLocalName());
584 childNodes.put(pathToCase, builder);
589 public AnyXmlBuilder addAnyXml(final QName anyXmlName,
590 final List<String> parentPath, final int line) {
591 final List<String> pathToAnyXml = new ArrayList<String>(parentPath);
592 final AnyXmlBuilder builder = new AnyXmlBuilder(anyXmlName, line);
593 updateParent(builder, line, "anyxml");
595 pathToAnyXml.add(anyXmlName.getLocalName());
596 childNodes.put(pathToAnyXml, builder);
601 public TypeDefinitionBuilderImpl addTypedef(final QName typeDefName,
602 final List<String> parentPath, final int line) {
603 final List<String> pathToType = new ArrayList<String>(parentPath);
604 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(
607 if (!(actualPath.isEmpty())) {
608 final Builder parent = actualPath.getFirst();
609 if (parent instanceof TypeDefinitionAwareBuilder) {
610 ((TypeDefinitionAwareBuilder) parent).addTypedef(builder);
612 throw new YangParseException(name, line,
613 "Unresolved parent of typedef "
614 + typeDefName.getLocalName());
618 pathToType.add(typeDefName.getLocalName());
619 addedTypedefs.put(pathToType, builder);
623 public void setType(final TypeDefinition<?> type,
624 final List<String> parentPath) {
626 if (!(actualPath.isEmpty())) {
627 final Builder parent = actualPath.getFirst();
628 if (parent instanceof TypeAwareBuilder) {
629 ((TypeAwareBuilder) parent).setType(type);
631 throw new YangParseException("Failed to set type '"
632 + type.getQName().getLocalName()
633 + "'. Unknown parent node: " + parent);
638 public UnionTypeBuilder addUnionType(final List<String> currentPath,
639 final URI namespace, final Date revision, final int line) {
640 final List<String> pathToUnion = new ArrayList<String>(currentPath);
641 final UnionTypeBuilder union = new UnionTypeBuilder(line);
643 if (actualPath.isEmpty()) {
644 throw new YangParseException(line, "union error");
646 final Builder parent = actualPath.getFirst();
647 if (parent instanceof TypeAwareBuilder) {
649 ((TypeAwareBuilder) parent).setTypedef(union);
651 final List<String> path = new ArrayList<String>(pathToUnion);
654 addedUnionTypes.put(path, union);
657 throw new YangParseException(name, line,
658 "Unresolved parent of union type.");
663 public void addIdentityrefType(final String baseString,
664 final List<String> parentPath, final SchemaPath schemaPath,
666 final List<String> pathToIdentityref = new ArrayList<String>(parentPath);
667 final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(
668 baseString, schemaPath, line);
670 if (actualPath.isEmpty()) {
671 throw new YangParseException(line, "identityref error");
673 final Builder parent = actualPath.getFirst();
674 if (parent instanceof TypeAwareBuilder) {
675 final TypeAwareBuilder typeParent = (TypeAwareBuilder) parent;
676 typeParent.setTypedef(identityref);
677 dirtyNodes.put(pathToIdentityref, typeParent);
679 throw new YangParseException(name, line,
680 "Unresolved parent of identityref type.");
685 public DeviationBuilder addDeviation(final String targetPath,
686 final List<String> parentPath, final int line) {
687 if (!(actualPath.isEmpty())) {
688 throw new YangParseException(name, line,
689 "deviation can be defined only in module or submodule");
692 final List<String> pathToDeviation = new ArrayList<String>(parentPath);
693 pathToDeviation.add(targetPath);
694 final DeviationBuilder builder = new DeviationBuilder(targetPath, line);
695 addedDeviations.put(pathToDeviation, builder);
699 public IdentitySchemaNodeBuilder addIdentity(final QName qname,
700 final List<String> parentPath, final int line) {
701 if (!(actualPath.isEmpty())) {
702 throw new YangParseException(name, line,
703 "identity can be defined only in module or submodule");
706 final List<String> pathToIdentity = new ArrayList<String>(parentPath);
707 final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(
709 pathToIdentity.add(qname.getLocalName());
710 addedIdentities.add(builder);
714 public void addConfiguration(final boolean configuration,
715 final List<String> parentPath, final int line) {
716 if (actualPath.isEmpty()) {
717 throw new YangParseException(name, line,
718 "Parent node of config statement not found.");
720 final Builder parent = actualPath.getFirst();
721 if (parent instanceof DataSchemaNodeBuilder) {
722 ((DataSchemaNodeBuilder) parent)
723 .setConfiguration(configuration);
724 } else if (parent instanceof RefineHolder) {
725 ((RefineHolder) parent).setConfig(configuration);
726 } else if (parent instanceof DeviationBuilder) {
727 // skip: set config to deviation (deviate stmt) not supported by
731 throw new YangParseException(name, line,
732 "Unresolved parent of config statement.");
737 public UnknownSchemaNodeBuilder addUnknownSchemaNode(final QName qname,
738 final List<String> parentPath, final int line) {
739 final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(
742 if (!(actualPath.isEmpty())) {
743 final Builder parent = actualPath.getFirst();
744 if (parent instanceof SchemaNodeBuilder) {
745 ((SchemaNodeBuilder) parent).addUnknownSchemaNode(builder);
746 } else if (parent instanceof RefineHolder) {
747 ((RefineHolder) parent).addUnknownSchemaNode(builder);
749 throw new YangParseException(name, line,
750 "Unresolved parent of unknown node '"
751 + qname.getLocalName() + "'");
755 addedUnknownNodes.add(builder);
759 private final class ModuleImpl implements Module {
760 private URI namespace;
761 private final String name;
762 private Date revision;
763 private String prefix;
764 private String yangVersion;
765 private String description;
766 private String reference;
767 private String organization;
768 private String contact;
769 private Set<ModuleImport> imports = Collections.emptySet();
770 private Set<FeatureDefinition> features = Collections.emptySet();
771 private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
772 private Set<NotificationDefinition> notifications = Collections
774 private Set<AugmentationSchema> augmentations = Collections.emptySet();
775 private Set<RpcDefinition> rpcs = Collections.emptySet();
776 private Set<Deviation> deviations = Collections.emptySet();
777 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
778 private Set<GroupingDefinition> groupings = Collections.emptySet();
779 private Set<UsesNode> uses = Collections.emptySet();
780 private List<ExtensionDefinition> extensionNodes = Collections
782 private Set<IdentitySchemaNode> identities = Collections.emptySet();
784 private ModuleImpl(String name) {
789 public URI getNamespace() {
793 private void setNamespace(URI namespace) {
794 this.namespace = namespace;
798 public String getName() {
803 public Date getRevision() {
807 private void setRevision(Date revision) {
808 this.revision = revision;
812 public String getPrefix() {
816 private void setPrefix(String prefix) {
817 this.prefix = prefix;
821 public String getYangVersion() {
825 private void setYangVersion(String yangVersion) {
826 this.yangVersion = yangVersion;
830 public String getDescription() {
834 private void setDescription(String description) {
835 this.description = description;
839 public String getReference() {
843 private void setReference(String reference) {
844 this.reference = reference;
848 public String getOrganization() {
852 private void setOrganization(String organization) {
853 this.organization = organization;
857 public String getContact() {
861 private void setContact(String contact) {
862 this.contact = contact;
866 public Set<ModuleImport> getImports() {
870 private void setImports(Set<ModuleImport> imports) {
871 if (imports != null) {
872 this.imports = imports;
877 public Set<FeatureDefinition> getFeatures() {
881 private void setFeatures(Set<FeatureDefinition> features) {
882 if (features != null) {
883 this.features = features;
888 public Set<TypeDefinition<?>> getTypeDefinitions() {
889 return typeDefinitions;
892 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
893 if (typeDefinitions != null) {
894 this.typeDefinitions = typeDefinitions;
899 public Set<NotificationDefinition> getNotifications() {
900 return notifications;
903 private void setNotifications(Set<NotificationDefinition> notifications) {
904 if (notifications != null) {
905 this.notifications = notifications;
910 public Set<AugmentationSchema> getAugmentations() {
911 return augmentations;
914 private void setAugmentations(Set<AugmentationSchema> augmentations) {
915 if (augmentations != null) {
916 this.augmentations = augmentations;
921 public Set<RpcDefinition> getRpcs() {
925 private void setRpcs(Set<RpcDefinition> rpcs) {
932 public Set<Deviation> getDeviations() {
936 private void setDeviations(Set<Deviation> deviations) {
937 if (deviations != null) {
938 this.deviations = deviations;
943 public Set<DataSchemaNode> getChildNodes() {
944 return new HashSet<DataSchemaNode>(childNodes.values());
947 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
948 if (childNodes != null) {
949 this.childNodes = childNodes;
954 public Set<GroupingDefinition> getGroupings() {
958 private void setGroupings(Set<GroupingDefinition> groupings) {
959 if (groupings != null) {
960 this.groupings = groupings;
965 public Set<UsesNode> getUses() {
969 private void setUses(Set<UsesNode> uses) {
976 public List<ExtensionDefinition> getExtensionSchemaNodes() {
977 return extensionNodes;
980 private void setExtensionSchemaNodes(
981 List<ExtensionDefinition> extensionNodes) {
982 if (extensionNodes != null) {
983 this.extensionNodes = extensionNodes;
988 public Set<IdentitySchemaNode> getIdentities() {
992 private void setIdentities(Set<IdentitySchemaNode> identities) {
993 if (identities != null) {
994 this.identities = identities;
999 public DataSchemaNode getDataChildByName(QName name) {
1000 return childNodes.get(name);
1004 public DataSchemaNode getDataChildByName(String name) {
1005 DataSchemaNode result = null;
1006 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
1007 if (entry.getKey().getLocalName().equals(name)) {
1008 result = entry.getValue();
1016 public int hashCode() {
1017 final int prime = 31;
1019 result = prime * result
1020 + ((namespace == null) ? 0 : namespace.hashCode());
1021 result = prime * result + ((name == null) ? 0 : name.hashCode());
1022 result = prime * result
1023 + ((revision == null) ? 0 : revision.hashCode());
1024 result = prime * result
1025 + ((prefix == null) ? 0 : prefix.hashCode());
1026 result = prime * result
1027 + ((yangVersion == null) ? 0 : yangVersion.hashCode());
1032 public boolean equals(Object obj) {
1039 if (getClass() != obj.getClass()) {
1042 ModuleImpl other = (ModuleImpl) obj;
1043 if (namespace == null) {
1044 if (other.namespace != null) {
1047 } else if (!namespace.equals(other.namespace)) {
1051 if (other.name != null) {
1054 } else if (!name.equals(other.name)) {
1057 if (revision == null) {
1058 if (other.revision != null) {
1061 } else if (!revision.equals(other.revision)) {
1064 if (prefix == null) {
1065 if (other.prefix != null) {
1068 } else if (!prefix.equals(other.prefix)) {
1071 if (yangVersion == null) {
1072 if (other.yangVersion != null) {
1075 } else if (!yangVersion.equals(other.yangVersion)) {
1082 public String toString() {
1083 StringBuilder sb = new StringBuilder(
1084 ModuleImpl.class.getSimpleName());
1086 sb.append("name=" + name);
1087 sb.append(", namespace=" + namespace);
1088 sb.append(", revision=" + revision);
1089 sb.append(", prefix=" + prefix);
1090 sb.append(", yangVersion=" + yangVersion);
1092 return sb.toString();
1096 private void updateParent(DataSchemaNodeBuilder nodeBuilder, int line,
1097 String nodeTypeName) {
1098 if (!(actualPath.isEmpty())) {
1099 final Builder parent = actualPath.getFirst();
1100 if (parent instanceof DataNodeContainerBuilder) {
1101 if (parent instanceof AugmentationSchemaBuilder) {
1102 nodeBuilder.setAugmenting(true);
1104 ((DataNodeContainerBuilder) parent)
1105 .addChildNode(nodeBuilder);
1106 } else if (parent instanceof ChoiceBuilder) {
1107 ((ChoiceBuilder) parent).addChildNode(nodeBuilder);
1109 throw new YangParseException(name, line,
1110 "Unresolved parent of " + nodeTypeName + " "
1111 + nodeBuilder.getQName().getLocalName());
1116 private ModuleImport createModuleImport(final String moduleName,
1117 final Date revision, final String prefix) {
1118 ModuleImport moduleImport = new ModuleImport() {
1120 public String getModuleName() {
1125 public Date getRevision() {
1130 public String getPrefix() {
1135 public int hashCode() {
1136 final int prime = 31;
1138 result = prime * result
1139 + ((moduleName == null) ? 0 : moduleName.hashCode());
1140 result = prime * result
1141 + ((revision == null) ? 0 : revision.hashCode());
1142 result = prime * result
1143 + ((prefix == null) ? 0 : prefix.hashCode());
1148 public boolean equals(Object obj) {
1155 if (getClass() != obj.getClass()) {
1158 ModuleImport other = (ModuleImport) obj;
1159 if (getModuleName() == null) {
1160 if (other.getModuleName() != null) {
1163 } else if (!getModuleName().equals(other.getModuleName())) {
1166 if (getRevision() == null) {
1167 if (other.getRevision() != null) {
1170 } else if (!getRevision().equals(other.getRevision())) {
1173 if (getPrefix() == null) {
1174 if (other.getPrefix() != null) {
1177 } else if (!getPrefix().equals(other.getPrefix())) {
1184 public String toString() {
1185 return "ModuleImport[moduleName=" + moduleName + ", revision="
1186 + revision + ", prefix=" + prefix + "]";
1189 return moduleImport;
1193 * Traverse through given addedChilds and add only direct module childs.
1194 * Direct module child path size is 2 (1. module name, 2. child name).
1196 * @param addedChilds
1197 * @return map of children, where key is child QName and value is child
1200 private Map<QName, DataSchemaNode> buildModuleChildNodes(
1201 Map<List<String>, DataSchemaNodeBuilder> addedChilds) {
1202 final Map<QName, DataSchemaNode> childNodes = new HashMap<QName, DataSchemaNode>();
1203 for (Map.Entry<List<String>, DataSchemaNodeBuilder> entry : addedChilds
1205 List<String> path = entry.getKey();
1206 DataSchemaNodeBuilder child = entry.getValue();
1207 if (path.size() == 2) {
1208 DataSchemaNode node = child.build();
1209 QName qname = node.getQName();
1210 childNodes.put(qname, node);
1217 * Traverse through given addedGroupings and add only direct module
1218 * groupings. Direct module grouping path size is 2 (1. module name, 2.
1221 * @param addedGroupings
1222 * @return set of built GroupingDefinition objects
1224 private Set<GroupingDefinition> buildModuleGroupings(
1225 Map<List<String>, GroupingBuilder> addedGroupings) {
1226 final Set<GroupingDefinition> groupings = new HashSet<GroupingDefinition>();
1227 for (Map.Entry<List<String>, GroupingBuilder> entry : addedGroupings
1229 if (entry.getKey().size() == 2) {
1230 groupings.add(entry.getValue().build());
1237 * Traverse through given addedRpcs and build RpcDefinition objects.
1240 * @return set of built RpcDefinition objects
1242 private Set<RpcDefinition> buildModuleRpcs(
1243 Map<List<String>, RpcDefinitionBuilder> addedRpcs) {
1244 final Set<RpcDefinition> rpcs = new HashSet<RpcDefinition>();
1245 RpcDefinitionBuilder builder;
1246 for (Map.Entry<List<String>, RpcDefinitionBuilder> entry : addedRpcs
1248 builder = entry.getValue();
1249 RpcDefinition rpc = builder.build();
1256 * Traverse through given addedTypedefs and add only direct module typedef
1257 * statements. Direct module typedef path size is 2 (1. module name, 2.
1260 * @param addedTypedefs
1261 * @return set of built module typedef statements
1263 private Set<TypeDefinition<?>> buildModuleTypedefs(
1264 Map<List<String>, TypeDefinitionBuilder> addedTypedefs) {
1265 Set<TypeDefinition<?>> typedefs = new HashSet<TypeDefinition<?>>();
1266 for (Map.Entry<List<String>, TypeDefinitionBuilder> entry : addedTypedefs
1268 List<String> key = entry.getKey();
1269 TypeDefinitionBuilder typedefBuilder = entry.getValue();
1270 if (key.size() == 2) {
1271 TypeDefinition<? extends TypeDefinition<?>> node = typedefBuilder
1280 * Traverse through given addedUsesNodes and add only direct module uses
1281 * nodes. Direct module uses node path size is 2 (1. module name, 2. uses
1284 * @param addedUsesNodes
1285 * @return set of built module uses nodes
1287 private Set<UsesNode> buildUsesNodes(
1288 Map<List<String>, UsesNodeBuilder> addedUsesNodes) {
1289 final Set<UsesNode> usesNodeDefs = new HashSet<UsesNode>();
1290 for (Map.Entry<List<String>, UsesNodeBuilder> entry : addedUsesNodes
1292 if (entry.getKey().size() == 2) {
1293 usesNodeDefs.add(entry.getValue().build());
1296 return usesNodeDefs;
1300 * Traverse through given addedFeatures and add only direct module features.
1301 * Direct module feature path size is 2 (1. module name, 2. feature name).
1303 * @param addedFeatures
1304 * @return set of built module features
1306 private Set<FeatureDefinition> buildModuleFeatures(
1307 Map<List<String>, FeatureBuilder> addedFeatures) {
1308 Set<FeatureDefinition> features = new HashSet<FeatureDefinition>();
1309 for (Map.Entry<List<String>, FeatureBuilder> entry : addedFeatures
1311 if (entry.getKey().size() == 2) {
1312 features.add(entry.getValue().build());