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.yangtools.yang.parser.builder.impl;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.*;
15 import org.opendaylight.yangtools.yang.parser.builder.api.*;
16 import org.opendaylight.yangtools.yang.parser.util.Comparators;
17 import org.opendaylight.yangtools.yang.parser.util.ModuleImportImpl;
18 import org.opendaylight.yangtools.yang.parser.util.RefineHolder;
19 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
22 * Builder of Module object. If this module is dependent on external
23 * module/modules, these dependencies must be resolved before module is built,
24 * otherwise result may not be valid.
26 public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
28 private final ModuleImpl instance;
29 private final String name;
30 private final SchemaPath schemaPath;
31 private URI namespace;
32 private String prefix;
33 private Date revision;
35 private final boolean submodule;
36 private String belongsTo;
37 private ModuleBuilder parent;
39 public ModuleBuilder getParent() {
43 public void setParent(ModuleBuilder parent) {
47 private final Deque<Builder> actualPath = new LinkedList<>();
48 private final Set<TypeAwareBuilder> dirtyNodes = new HashSet<>();
50 private final Set<ModuleImport> imports = new HashSet<ModuleImport>();
52 private final Set<AugmentationSchema> augments = new HashSet<>();
53 private final List<AugmentationSchemaBuilder> augmentBuilders = new ArrayList<>();
54 private final List<AugmentationSchemaBuilder> allAugments = new ArrayList<>();
56 private final List<GroupingBuilder> allGroupings = new ArrayList<>();
58 private final List<UsesNodeBuilder> allUsesNodes = new ArrayList<>();
60 private final Set<RpcDefinition> rpcs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
61 private final Set<RpcDefinitionBuilder> addedRpcs = new HashSet<>();
63 private final Set<NotificationDefinition> notifications = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
64 private final Set<NotificationBuilder> addedNotifications = new HashSet<>();
66 private final Set<IdentitySchemaNode> identities = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
67 private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<>();
69 private final Set<FeatureDefinition> features = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
70 private final Set<FeatureBuilder> addedFeatures = new HashSet<>();
72 private final Set<Deviation> deviations = new HashSet<>();
73 private final Set<DeviationBuilder> deviationBuilders = new HashSet<>();
75 private final List<ExtensionDefinition> extensions = new ArrayList<>();
76 private final List<ExtensionBuilder> addedExtensions = new ArrayList<>();
78 private final List<UnknownSchemaNodeBuilder> allUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
80 private final List<ListSchemaNodeBuilder> allLists = new ArrayList<ListSchemaNodeBuilder>();
82 public ModuleBuilder(final String name) {
86 public ModuleBuilder(final String name, final boolean submodule) {
89 schemaPath = new SchemaPath(Collections.<QName> emptyList(), true);
90 instance = new ModuleImpl(name);
91 this.submodule = submodule;
92 actualPath.push(this);
95 public ModuleBuilder(Module base) {
96 super(base.getName(), 0, null);
97 this.name = base.getName();
98 schemaPath = new SchemaPath(Collections.<QName> emptyList(), true);
99 instance = new ModuleImpl(base.getName());
101 actualPath.push(this);
103 namespace = base.getNamespace();
104 prefix = base.getPrefix();
105 revision = base.getRevision();
107 for (DataSchemaNode childNode : base.getChildNodes()) {
108 childNodes.add(childNode);
111 typedefs.addAll(base.getTypeDefinitions());
112 groupings.addAll(base.getGroupings());
113 usesNodes.addAll(base.getUses());
114 augments.addAll(base.getAugmentations());
115 rpcs.addAll(base.getRpcs());
116 notifications.addAll(base.getNotifications());
117 identities.addAll(base.getIdentities());
118 features.addAll(base.getFeatures());
119 deviations.addAll(base.getDeviations());
120 extensions.addAll(base.getExtensionSchemaNodes());
121 unknownNodes.addAll(base.getUnknownSchemaNodes());
125 * Build new Module object based on this builder.
128 public Module build() {
129 instance.setPrefix(prefix);
130 instance.setRevision(revision);
131 instance.setImports(imports);
132 instance.setNamespace(namespace);
135 for (TypeDefinitionBuilder tdb : addedTypedefs) {
136 typedefs.add(tdb.build());
138 instance.setTypeDefinitions(typedefs);
141 for (DataSchemaNodeBuilder child : addedChildNodes) {
142 childNodes.add(child.build());
144 instance.addChildNodes(childNodes);
147 for (GroupingBuilder gb : addedGroupings) {
148 groupings.add(gb.build());
150 instance.setGroupings(groupings);
153 for (UsesNodeBuilder unb : addedUsesNodes) {
154 usesNodes.add(unb.build());
156 instance.setUses(usesNodes);
159 for (FeatureBuilder fb : addedFeatures) {
160 features.add(fb.build());
162 instance.setFeatures(features);
165 for (NotificationBuilder entry : addedNotifications) {
166 notifications.add(entry.build());
168 instance.setNotifications(notifications);
171 for (AugmentationSchemaBuilder builder : augmentBuilders) {
172 augments.add(builder.build());
174 instance.setAugmentations(augments);
177 for (RpcDefinitionBuilder rpc : addedRpcs) {
178 rpcs.add(rpc.build());
180 instance.setRpcs(rpcs);
183 for (DeviationBuilder entry : deviationBuilders) {
184 deviations.add(entry.build());
186 instance.setDeviations(deviations);
189 for (ExtensionBuilder eb : addedExtensions) {
190 extensions.add(eb.build());
192 Collections.sort(extensions, Comparators.SCHEMA_NODE_COMP);
193 instance.setExtensionSchemaNodes(extensions);
196 for (IdentitySchemaNodeBuilder id : addedIdentities) {
197 identities.add(id.build());
199 instance.setIdentities(identities);
202 for (UnknownSchemaNodeBuilder unb : addedUnknownNodes) {
203 unknownNodes.add(unb.build());
205 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
206 instance.setUnknownSchemaNodes(unknownNodes);
212 public void setParent(Builder parent) {
213 throw new YangParseException(name, 0, "Can not set parent to module");
217 public SchemaPath getPath() {
222 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
223 return addedTypedefs;
226 public void enterNode(final Builder node) {
227 actualPath.push(node);
230 public void exitNode() {
234 public Builder getActualNode() {
235 if (actualPath.isEmpty()) {
238 return actualPath.peekFirst();
242 public Builder getActualParent() {
243 if (actualPath.size() < 2) {
246 Builder builderChild = actualPath.removeFirst();
247 Builder builderParent = actualPath.peekFirst();
248 actualPath.addFirst(builderChild);
249 return builderParent;
253 public Set<TypeAwareBuilder> getDirtyNodes() {
257 public Set<AugmentationSchema> getAugments() {
261 public List<AugmentationSchemaBuilder> getAugmentBuilders() {
262 return augmentBuilders;
265 public List<AugmentationSchemaBuilder> getAllAugments() {
269 public Set<IdentitySchemaNode> getIdentities() {
273 public Set<IdentitySchemaNodeBuilder> getAddedIdentities() {
274 return addedIdentities;
277 public Set<FeatureDefinition> getFeatures() {
281 public Set<FeatureBuilder> getAddedFeatures() {
282 return addedFeatures;
285 public List<GroupingBuilder> getAllGroupings() {
289 public List<UsesNodeBuilder> getAllUsesNodes() {
293 public Set<Deviation> getDeviations() {
297 public Set<DeviationBuilder> getDeviationBuilders() {
298 return deviationBuilders;
301 public List<ExtensionDefinition> getExtensions() {
305 public List<ExtensionBuilder> getAddedExtensions() {
306 return addedExtensions;
309 public List<UnknownSchemaNodeBuilder> getAllUnknownNodes() {
310 return allUnknownNodes;
313 public List<ListSchemaNodeBuilder> getAllLists() {
317 public String getName() {
321 public URI getNamespace() {
325 public void setNamespace(final URI namespace) {
326 this.namespace = namespace;
329 public String getPrefix() {
333 public Date getRevision() {
337 public boolean isSubmodule() {
341 public String getBelongsTo() {
345 public void setBelongsTo(String belongsTo) {
346 this.belongsTo = belongsTo;
349 public void markActualNodeDirty() {
350 final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) getActualNode();
351 dirtyNodes.add(nodeBuilder);
354 public void setRevision(final Date revision) {
355 this.revision = revision;
358 public void setPrefix(final String prefix) {
359 this.prefix = prefix;
362 public void setYangVersion(final String yangVersion) {
363 instance.setYangVersion(yangVersion);
366 public void setDescription(final String description) {
367 instance.setDescription(description);
370 public void setReference(final String reference) {
371 instance.setReference(reference);
374 public void setOrganization(final String organization) {
375 instance.setOrganization(organization);
378 public void setContact(final String contact) {
379 instance.setContact(contact);
382 public boolean addModuleImport(final String moduleName, final Date revision, final String prefix) {
383 final ModuleImport moduleImport = createModuleImport(moduleName, revision, prefix);
384 return imports.add(moduleImport);
387 public Set<ModuleImport> getModuleImports() {
391 public ExtensionBuilder addExtension(final QName qname, final int line, final SchemaPath path) {
392 Builder parent = getActualNode();
393 if (!(parent.equals(this))) {
394 throw new YangParseException(name, line, "extension can be defined only in module or submodule");
397 final String extName = qname.getLocalName();
398 for (ExtensionBuilder addedExtension : addedExtensions) {
399 if (addedExtension.getQName().getLocalName().equals(extName)) {
400 raiseYangParserException("extension", "node", extName, line, addedExtension.getLine());
403 final ExtensionBuilder builder = new ExtensionBuilder(name, line, qname, path);
404 builder.setParent(parent);
405 addedExtensions.add(builder);
409 public ContainerSchemaNodeBuilder addContainerNode(final int line, final QName qname, final SchemaPath schemaPath) {
410 final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
412 Builder parent = getActualNode();
413 builder.setParent(parent);
414 addChildToParent(parent, builder, qname.getLocalName());
419 public ListSchemaNodeBuilder addListNode(final int line, final QName qname, final SchemaPath schemaPath) {
420 final ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(name, line, qname, schemaPath);
422 Builder parent = getActualNode();
423 builder.setParent(parent);
424 addChildToParent(parent, builder, qname.getLocalName());
425 allLists.add(builder);
430 public LeafSchemaNodeBuilder addLeafNode(final int line, final QName qname, final SchemaPath schemaPath) {
431 final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(name, line, qname, schemaPath);
433 Builder parent = getActualNode();
434 builder.setParent(parent);
435 addChildToParent(parent, builder, qname.getLocalName());
440 public LeafListSchemaNodeBuilder addLeafListNode(final int line, final QName qname, final SchemaPath schemaPath) {
441 final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(name, line, qname, schemaPath);
443 Builder parent = getActualNode();
444 builder.setParent(parent);
445 addChildToParent(parent, builder, qname.getLocalName());
450 public GroupingBuilder addGrouping(final int line, final QName qname, final SchemaPath path) {
451 final GroupingBuilder builder = new GroupingBuilderImpl(name, line, qname, path);
453 Builder parent = getActualNode();
454 builder.setParent(parent);
456 String groupingName = qname.getLocalName();
457 if (parent.equals(this)) {
458 for (GroupingBuilder addedGrouping : addedGroupings) {
459 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
460 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
463 addedGroupings.add(builder);
465 if (parent instanceof DataNodeContainerBuilder) {
466 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
467 for (GroupingBuilder addedGrouping : parentNode.getGroupingBuilders()) {
468 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
469 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
472 parentNode.addGrouping(builder);
473 } else if (parent instanceof RpcDefinitionBuilder) {
474 RpcDefinitionBuilder parentNode = (RpcDefinitionBuilder) parent;
475 for (GroupingBuilder child : parentNode.getGroupings()) {
476 if (child.getQName().getLocalName().equals(groupingName)) {
477 raiseYangParserException("", "Grouping", groupingName, line, child.getLine());
480 parentNode.addGrouping(builder);
482 throw new YangParseException(name, line, "Unresolved parent of grouping " + groupingName);
486 allGroupings.add(builder);
490 public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr) {
491 final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr);
493 Builder parent = getActualNode();
494 builder.setParent(parent);
496 if (parent.equals(this)) {
497 // augment can be declared only under 'module' ...
498 if (!(augmentTargetStr.startsWith("/"))) {
499 throw new YangParseException(
502 "If the 'augment' statement is on the top level in a module, the absolute form of a schema node identifier MUST be used.");
504 augmentBuilders.add(builder);
506 // ... or 'uses' statement
507 if (parent instanceof UsesNodeBuilder) {
508 if (augmentTargetStr.startsWith("/")) {
509 throw new YangParseException(name, line,
510 "If 'augment' statement is a substatement to the 'uses' statement, it cannot contain absolute path ("
511 + augmentTargetStr + ")");
513 ((UsesNodeBuilder) parent).addAugment(builder);
515 throw new YangParseException(name, line, "Augment can be declared only under module or uses statement.");
518 allAugments.add(builder);
524 public void addUsesNode(UsesNodeBuilder usesBuilder) {
525 addedUsesNodes.add(usesBuilder);
526 allUsesNodes.add(usesBuilder);
529 public UsesNodeBuilder addUsesNode(final int line, final String groupingPathStr) {
530 final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(name, line, groupingPathStr);
532 Builder parent = getActualNode();
533 usesBuilder.setParent(parent);
535 if (parent.equals(this)) {
536 addedUsesNodes.add(usesBuilder);
538 if (!(parent instanceof DataNodeContainerBuilder)) {
539 throw new YangParseException(name, line, "Unresolved parent of uses '" + groupingPathStr + "'.");
541 ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder);
543 if (parent instanceof AugmentationSchemaBuilder) {
544 usesBuilder.setAugmenting(true);
547 allUsesNodes.add(usesBuilder);
551 public void addRefine(final RefineHolder refine) {
552 final Builder parent = getActualNode();
553 if (!(parent instanceof UsesNodeBuilder)) {
554 throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
556 ((UsesNodeBuilder) parent).addRefine(refine);
557 refine.setParent(parent);
560 public RpcDefinitionBuilder addRpc(final int line, final QName qname, final SchemaPath path) {
561 Builder parent = getActualNode();
562 if (!(parent.equals(this))) {
563 throw new YangParseException(name, line, "rpc can be defined only in module or submodule");
566 final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(name, line, qname, path);
567 rpcBuilder.setParent(parent);
569 String rpcName = qname.getLocalName();
570 for (RpcDefinitionBuilder rpc : addedRpcs) {
571 if (rpc.getQName().getLocalName().equals(rpcName)) {
572 raiseYangParserException("", "rpc", rpcName, line, rpc.getLine());
575 for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
576 if (addedChild.getQName().getLocalName().equals(rpcName)) {
577 raiseYangParserException("rpc", "node", rpcName, line, addedChild.getLine());
580 for (NotificationBuilder addedNotification : addedNotifications) {
581 if (addedNotification.getQName().getLocalName().equals(rpcName)) {
582 raiseYangParserException("rpc", "notification", rpcName, line, addedNotification.getLine());
585 addedRpcs.add(rpcBuilder);
589 public ContainerSchemaNodeBuilder addRpcInput(final int line, final QName qname, final SchemaPath schemaPath) {
590 final Builder parent = getActualNode();
591 if (!(parent instanceof RpcDefinitionBuilder)) {
592 throw new YangParseException(name, line, "input can be defined only in rpc statement");
594 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
596 final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
597 inputBuilder.setParent(rpc);
599 rpc.setInput(inputBuilder);
603 public ContainerSchemaNodeBuilder addRpcOutput(final SchemaPath schemaPath, final QName qname, final int line) {
604 final Builder parent = getActualNode();
605 if (!(parent instanceof RpcDefinitionBuilder)) {
606 throw new YangParseException(name, line, "output can be defined only in rpc statement");
608 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
610 final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
611 outputBuilder.setParent(rpc);
613 rpc.setOutput(outputBuilder);
614 return outputBuilder;
617 public void addNotification(NotificationDefinition notification) {
618 notifications.add(notification);
621 public NotificationBuilder addNotification(final int line, final QName qname, final SchemaPath path) {
622 final Builder parent = getActualNode();
623 if (!(parent.equals(this))) {
624 throw new YangParseException(name, line, "notification can be defined only in module or submodule");
627 String notificationName = qname.getLocalName();
628 for (NotificationBuilder nb : addedNotifications) {
629 if (nb.getQName().equals(qname)) {
630 raiseYangParserException("", "notification", notificationName, line, nb.getLine());
633 for (RpcDefinitionBuilder rpc : addedRpcs) {
634 if (rpc.getQName().getLocalName().equals(notificationName)) {
635 raiseYangParserException("notification", "rpc", notificationName, line, rpc.getLine());
638 for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
639 if (addedChild.getQName().getLocalName().equals(notificationName)) {
640 raiseYangParserException("notification", "node", notificationName, line, addedChild.getLine());
644 final NotificationBuilder builder = new NotificationBuilder(name, line, qname, path);
645 builder.setParent(parent);
646 addedNotifications.add(builder);
651 public FeatureBuilder addFeature(final int line, final QName qname, final SchemaPath path) {
652 Builder parent = getActualNode();
653 if (!(parent.equals(this))) {
654 throw new YangParseException(name, line, "feature can be defined only in module or submodule");
657 final FeatureBuilder builder = new FeatureBuilder(name, line, qname, path);
658 builder.setParent(parent);
660 String featureName = qname.getLocalName();
661 for (FeatureBuilder addedFeature : addedFeatures) {
662 if (addedFeature.getQName().getLocalName().equals(featureName)) {
663 raiseYangParserException("", "feature", featureName, line, addedFeature.getLine());
666 addedFeatures.add(builder);
670 public ChoiceBuilder addChoice(final int line, final QName qname, final SchemaPath path) {
671 final ChoiceBuilder builder = new ChoiceBuilder(name, line, qname, path);
673 Builder parent = getActualNode();
674 builder.setParent(parent);
675 addChildToParent(parent, builder, qname.getLocalName());
680 public ChoiceCaseBuilder addCase(final int line, final QName qname, final SchemaPath path) {
681 Builder parent = getActualNode();
682 if (parent == null || parent.equals(this)) {
683 throw new YangParseException(name, line, "'case' parent not found");
686 final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(name, line, qname, path);
687 builder.setParent(parent);
689 if (parent instanceof ChoiceBuilder) {
690 ((ChoiceBuilder) parent).addCase(builder);
691 } else if (parent instanceof AugmentationSchemaBuilder) {
692 ((AugmentationSchemaBuilder) parent).addChildNode(builder);
694 throw new YangParseException(name, line, "Unresolved parent of 'case' " + qname.getLocalName());
700 public AnyXmlBuilder addAnyXml(final int line, final QName qname, final SchemaPath schemaPath) {
701 final AnyXmlBuilder builder = new AnyXmlBuilder(name, line, qname, schemaPath);
703 Builder parent = getActualNode();
704 builder.setParent(parent);
705 addChildToParent(parent, builder, qname.getLocalName());
711 public void addTypedef(TypeDefinitionBuilder typedefBuilder) {
712 String nodeName = typedefBuilder.getQName().getLocalName();
713 for (TypeDefinitionBuilder tdb : addedTypedefs) {
714 if (tdb.getQName().getLocalName().equals(nodeName)) {
715 raiseYangParserException("", "typedef", nodeName, typedefBuilder.getLine(), tdb.getLine());
718 addedTypedefs.add(typedefBuilder);
721 public TypeDefinitionBuilderImpl addTypedef(final int line, final QName qname, final SchemaPath path) {
722 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(name, line, qname, path);
724 Builder parent = getActualNode();
725 builder.setParent(parent);
727 String typedefName = qname.getLocalName();
728 if (parent.equals(this)) {
729 for (TypeDefinitionBuilder tdb : addedTypedefs) {
730 if (tdb.getQName().getLocalName().equals(typedefName)) {
731 raiseYangParserException("", "typedef", typedefName, line, tdb.getLine());
734 addedTypedefs.add(builder);
736 if (parent instanceof DataNodeContainerBuilder) {
737 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
738 for (TypeDefinitionBuilder child : parentNode.getTypeDefinitionBuilders()) {
739 if (child.getQName().getLocalName().equals(typedefName)) {
740 raiseYangParserException("", "typedef", typedefName, line, child.getLine());
743 parentNode.addTypedef(builder);
744 } else if (parent instanceof RpcDefinitionBuilder) {
745 RpcDefinitionBuilder rpcParent = (RpcDefinitionBuilder) parent;
746 for (TypeDefinitionBuilder tdb : rpcParent.getTypeDefinitions()) {
747 if (tdb.getQName().getLocalName().equals(builder.getQName().getLocalName())) {
748 raiseYangParserException("", "typedef", typedefName, line, tdb.getLine());
751 rpcParent.addTypedef(builder);
753 throw new YangParseException(name, line, "Unresolved parent of typedef " + typedefName);
760 public void setType(final TypeDefinition<?> type) {
761 Builder parent = getActualNode();
762 if (!(parent instanceof TypeAwareBuilder)) {
763 throw new YangParseException("Failed to set type '" + type.getQName().getLocalName()
764 + "'. Invalid parent node: " + parent);
766 ((TypeAwareBuilder) parent).setType(type);
769 public UnionTypeBuilder addUnionType(final int line, final URI namespace, final Date revision) {
770 final Builder parent = getActualNode();
771 if (parent == null) {
772 throw new YangParseException(name, line, "Unresolved parent of union type");
774 final UnionTypeBuilder union = new UnionTypeBuilder(name, line);
775 if (parent instanceof TypeAwareBuilder) {
776 ((TypeAwareBuilder) parent).setTypedef(union);
779 throw new YangParseException(name, line, "Invalid parent of union type.");
784 public void addIdentityrefType(final int line, final SchemaPath schemaPath, final String baseString) {
785 final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(name, line, baseString, schemaPath);
787 final Builder parent = getActualNode();
788 if (parent == null) {
789 throw new YangParseException(name, line, "Unresolved parent of identityref type.");
791 if (parent instanceof TypeAwareBuilder) {
792 final TypeAwareBuilder typeParent = (TypeAwareBuilder) parent;
793 typeParent.setTypedef(identityref);
794 dirtyNodes.add(typeParent);
796 throw new YangParseException(name, line, "Invalid parent of identityref type.");
801 public DeviationBuilder addDeviation(final int line, final String targetPath) {
802 Builder parent = getActualNode();
803 if (!(parent.equals(this))) {
804 throw new YangParseException(name, line, "deviation can be defined only in module or submodule");
807 final DeviationBuilder builder = new DeviationBuilder(name, line, targetPath);
808 builder.setParent(parent);
809 deviationBuilders.add(builder);
813 public IdentitySchemaNodeBuilder addIdentity(final QName qname, final int line, final SchemaPath path) {
814 Builder parent = getActualNode();
815 if (!(parent.equals(this))) {
816 throw new YangParseException(name, line, "identity can be defined only in module or submodule");
818 String identityName = qname.getLocalName();
819 for (IdentitySchemaNodeBuilder idBuilder : addedIdentities) {
820 if (idBuilder.getQName().equals(qname)) {
821 raiseYangParserException("", "identity", identityName, line, idBuilder.getLine());
825 final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(name, line, qname, path);
826 builder.setParent(parent);
827 addedIdentities.add(builder);
832 public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder builder) {
833 addedUnknownNodes.add(builder);
834 allUnknownNodes.add(builder);
837 public UnknownSchemaNodeBuilder addUnknownSchemaNode(final int line, final QName qname, final SchemaPath path) {
838 final Builder parent = getActualNode();
839 final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(name, line, qname, path);
840 builder.setParent(parent);
841 allUnknownNodes.add(builder);
843 if (parent.equals(this)) {
844 addedUnknownNodes.add(builder);
846 if (parent instanceof SchemaNodeBuilder) {
847 ((SchemaNodeBuilder) parent).addUnknownNodeBuilder(builder);
848 } else if (parent instanceof DataNodeContainerBuilder) {
849 ((DataNodeContainerBuilder) parent).addUnknownNodeBuilder(builder);
850 } else if (parent instanceof RefineHolder) {
851 ((RefineHolder) parent).addUnknownNodeBuilder(builder);
853 throw new YangParseException(name, line, "Unresolved parent of unknown node '" + qname.getLocalName()
861 public Set<RpcDefinition> getRpcs() {
865 public Set<RpcDefinitionBuilder> getAddedRpcs() {
869 public Set<NotificationDefinition> getNotifications() {
870 return notifications;
873 public Set<NotificationBuilder> getAddedNotifications() {
874 return addedNotifications;
878 public String toString() {
879 return "module " + name;
882 private final class ModuleImpl implements Module {
883 private URI namespace;
884 private final String name;
885 private Date revision;
886 private String prefix;
887 private String yangVersion;
888 private String description;
889 private String reference;
890 private String organization;
891 private String contact;
892 private final Set<ModuleImport> imports = new HashSet<>();
893 private final Set<FeatureDefinition> features = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
894 private final Set<TypeDefinition<?>> typeDefinitions = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
895 private final Set<NotificationDefinition> notifications = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
896 private final Set<AugmentationSchema> augmentations = new HashSet<>();
897 private final Set<RpcDefinition> rpcs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
898 private final Set<Deviation> deviations = new HashSet<>();
899 private final Set<DataSchemaNode> childNodes = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
900 private final Set<GroupingDefinition> groupings = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
901 private final Set<UsesNode> uses = new HashSet<>();
902 private final List<ExtensionDefinition> extensionNodes = new ArrayList<>();
903 private final Set<IdentitySchemaNode> identities = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
904 private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
906 private ModuleImpl(String name) {
911 public URI getNamespace() {
915 private void setNamespace(URI namespace) {
916 this.namespace = namespace;
920 public String getName() {
925 public Date getRevision() {
929 private void setRevision(Date revision) {
930 this.revision = revision;
934 public String getPrefix() {
938 private void setPrefix(String prefix) {
939 this.prefix = prefix;
943 public String getYangVersion() {
947 private void setYangVersion(String yangVersion) {
948 this.yangVersion = yangVersion;
952 public String getDescription() {
956 private void setDescription(String description) {
957 this.description = description;
961 public String getReference() {
965 private void setReference(String reference) {
966 this.reference = reference;
970 public String getOrganization() {
974 private void setOrganization(String organization) {
975 this.organization = organization;
979 public String getContact() {
983 private void setContact(String contact) {
984 this.contact = contact;
988 public Set<ModuleImport> getImports() {
992 private void setImports(Set<ModuleImport> imports) {
993 if (imports != null) {
994 this.imports.addAll(imports);
999 public Set<FeatureDefinition> getFeatures() {
1003 private void setFeatures(Set<FeatureDefinition> features) {
1004 if (features != null) {
1005 this.features.addAll(features);
1010 public Set<TypeDefinition<?>> getTypeDefinitions() {
1011 return typeDefinitions;
1014 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
1015 if (typeDefinitions != null) {
1016 this.typeDefinitions.addAll(typeDefinitions);
1021 public Set<NotificationDefinition> getNotifications() {
1022 return notifications;
1025 private void setNotifications(Set<NotificationDefinition> notifications) {
1026 if (notifications != null) {
1027 this.notifications.addAll(notifications);
1032 public Set<AugmentationSchema> getAugmentations() {
1033 return augmentations;
1036 private void setAugmentations(Set<AugmentationSchema> augmentations) {
1037 if (augmentations != null) {
1038 this.augmentations.addAll(augmentations);
1043 public Set<RpcDefinition> getRpcs() {
1047 private void setRpcs(Set<RpcDefinition> rpcs) {
1049 this.rpcs.addAll(rpcs);
1054 public Set<Deviation> getDeviations() {
1058 private void setDeviations(Set<Deviation> deviations) {
1059 if (deviations != null) {
1060 this.deviations.addAll(deviations);
1065 public Set<DataSchemaNode> getChildNodes() {
1066 return Collections.unmodifiableSet(childNodes);
1069 private void addChildNodes(Set<DataSchemaNode> childNodes) {
1070 if (childNodes != null) {
1071 this.childNodes.addAll(childNodes);
1076 public Set<GroupingDefinition> getGroupings() {
1080 private void setGroupings(Set<GroupingDefinition> groupings) {
1081 if (groupings != null) {
1082 this.groupings.addAll(groupings);
1087 public Set<UsesNode> getUses() {
1091 private void setUses(Set<UsesNode> uses) {
1093 this.uses.addAll(uses);
1098 public List<ExtensionDefinition> getExtensionSchemaNodes() {
1099 Collections.sort(extensionNodes, Comparators.SCHEMA_NODE_COMP);
1100 return extensionNodes;
1103 private void setExtensionSchemaNodes(final List<ExtensionDefinition> extensionNodes) {
1104 if (extensionNodes != null) {
1105 this.extensionNodes.addAll(extensionNodes);
1110 public Set<IdentitySchemaNode> getIdentities() {
1114 private void setIdentities(final Set<IdentitySchemaNode> identities) {
1115 if (identities != null) {
1116 this.identities.addAll(identities);
1121 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
1122 return unknownNodes;
1125 private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
1126 if (unknownNodes != null) {
1127 this.unknownNodes.addAll(unknownNodes);
1132 public DataSchemaNode getDataChildByName(QName name) {
1133 return getChildNode(childNodes, name);
1137 public DataSchemaNode getDataChildByName(String name) {
1138 return getChildNode(childNodes, name);
1142 public int hashCode() {
1143 final int prime = 31;
1145 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1146 result = prime * result + ((name == null) ? 0 : name.hashCode());
1147 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1148 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1149 result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());
1154 public boolean equals(Object obj) {
1161 if (getClass() != obj.getClass()) {
1164 ModuleImpl other = (ModuleImpl) obj;
1165 if (namespace == null) {
1166 if (other.namespace != null) {
1169 } else if (!namespace.equals(other.namespace)) {
1173 if (other.name != null) {
1176 } else if (!name.equals(other.name)) {
1179 if (revision == null) {
1180 if (other.revision != null) {
1183 } else if (!revision.equals(other.revision)) {
1186 if (prefix == null) {
1187 if (other.prefix != null) {
1190 } else if (!prefix.equals(other.prefix)) {
1193 if (yangVersion == null) {
1194 if (other.yangVersion != null) {
1197 } else if (!yangVersion.equals(other.yangVersion)) {
1204 public String toString() {
1205 StringBuilder sb = new StringBuilder(ModuleImpl.class.getSimpleName());
1207 sb.append("name=" + name);
1208 sb.append(", namespace=" + namespace);
1209 sb.append(", revision=" + revision);
1210 sb.append(", prefix=" + prefix);
1211 sb.append(", yangVersion=" + yangVersion);
1213 return sb.toString();
1218 * Add child to parent. Method checks for duplicates and add given child
1219 * node to parent. If node with same name is found, throws exception. If
1220 * parent is null, child node will be added directly to module.
1226 private void addChildToParent(final Builder parent, final DataSchemaNodeBuilder child, final String childName) {
1227 final int lineNum = child.getLine();
1228 if (parent.equals(this)) {
1229 addChildToModule(child, childName, lineNum);
1231 addChildToSubnodeOfModule(parent, child, childName, lineNum);
1236 * Adds child node <code>child</code> to the set of nodes child nodes.
1238 * The method reduces the complexity of the method
1239 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1240 * addChildToParent}.
1243 * data schema node builder for child node
1245 * string with name of child node
1247 * line number in YANG file where is the node with the name equal
1248 * to <code>childName</code> is defined
1250 private void addChildToModule(final DataSchemaNodeBuilder child, final String childName, final int lineNum) {
1251 // if parent == null => node is defined under module
1252 // All leafs, leaf-lists, lists, containers, choices, rpcs,
1253 // notifications, and anyxmls defined within a parent node or at the
1254 // top level of the module or its submodules share the same
1255 // identifier namespace.
1256 for (DataSchemaNodeBuilder childNode : addedChildNodes) {
1257 if (childNode.getQName().getLocalName().equals(childName)) {
1258 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
1261 for (RpcDefinitionBuilder rpc : addedRpcs) {
1262 if (rpc.getQName().getLocalName().equals(childName)) {
1263 raiseYangParserException("'" + child + "'", "rpc", childName, lineNum, rpc.getLine());
1266 for (NotificationBuilder notification : addedNotifications) {
1267 if (notification.getQName().getLocalName().equals(childName)) {
1268 raiseYangParserException("'" + child + "'", "notification", childName, lineNum, notification.getLine());
1271 addedChildNodes.add(child);
1275 * Adds child node <code>child</code> to the group of child nodes of the
1276 * <code>parent</code>
1278 * The method reduces the complexity of the method
1279 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1280 * addChildToParent}. *
1283 * builder of node which is parent for <code>child</code>
1285 * data schema node builder for child node
1287 * string with name of child node
1289 * line number in YANG file where is the node with the name equal
1290 * to <code>childName</code> is defined
1292 private void addChildToSubnodeOfModule(final Builder parent, final DataSchemaNodeBuilder child,
1293 final String childName, final int lineNum) {
1294 // no need for checking rpc and notification because they can be
1295 // defined only under module or submodule
1296 if (parent instanceof DataNodeContainerBuilder) {
1297 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
1298 for (DataSchemaNodeBuilder childNode : parentNode.getChildNodeBuilders()) {
1299 if (childNode.getQName().getLocalName().equals(childName)) {
1300 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
1303 parentNode.addChildNode(child);
1304 } else if (parent instanceof ChoiceBuilder) {
1305 ChoiceBuilder parentNode = (ChoiceBuilder) parent;
1306 for (ChoiceCaseBuilder caseBuilder : parentNode.getCases()) {
1307 if (caseBuilder.getQName().getLocalName().equals(childName)) {
1308 raiseYangParserException("'" + child + "'", "node", childName, lineNum, caseBuilder.getLine());
1311 parentNode.addCase(child);
1313 throw new YangParseException(name, lineNum, "Unresolved parent of node '" + childName + "'.");
1317 private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) {
1318 final ModuleImport moduleImport = new ModuleImportImpl(moduleName, revision, prefix);
1319 return moduleImport;
1322 private void raiseYangParserException(final String cantAddType, final String type, final String name,
1323 final int currentLine, final int duplicateLine) {
1325 StringBuilder msgPrefix = new StringBuilder("");
1326 if (cantAddType != null && !cantAddType.isEmpty()) {
1327 msgPrefix.append("Can not add ");
1328 msgPrefix.append(cantAddType);
1329 msgPrefix.append(": ");
1332 String msg = String.format("%s%s with same name '%s' already declared at line %d.", msgPrefix, type, name,
1334 throw new YangParseException(moduleName, currentLine, msg);
1338 public int hashCode() {
1339 final int prime = 31;
1341 result = prime * result + ((name == null) ? 0 : name.hashCode());
1342 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1343 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1344 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1350 public boolean equals(Object obj) {
1357 if (getClass() != obj.getClass()) {
1360 ModuleBuilder other = (ModuleBuilder) obj;
1362 if (other.name != null) {
1365 } else if (!name.equals(other.name)) {
1368 if (namespace == null) {
1369 if (other.namespace != null) {
1372 } else if (!namespace.equals(other.namespace)) {
1375 if (prefix == null) {
1376 if (other.prefix != null) {
1379 } else if (!prefix.equals(other.prefix)) {
1382 if (revision == null) {
1383 if (other.revision != null) {
1386 } else if (!revision.equals(other.revision)) {