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 public ModuleBuilder(final String name) {
84 public ModuleBuilder(final String name, final boolean submodule) {
87 schemaPath = new SchemaPath(Collections.<QName> emptyList(), true);
88 instance = new ModuleImpl(name);
89 this.submodule = submodule;
90 actualPath.push(this);
93 public ModuleBuilder(Module base) {
94 super(base.getName(), 0, null);
95 this.name = base.getName();
96 schemaPath = new SchemaPath(Collections.<QName> emptyList(), true);
97 instance = new ModuleImpl(base.getName());
99 actualPath.push(this);
101 namespace = base.getNamespace();
102 prefix = base.getPrefix();
103 revision = base.getRevision();
105 for (DataSchemaNode childNode : base.getChildNodes()) {
106 childNodes.put(childNode.getQName(), childNode);
109 typedefs.addAll(base.getTypeDefinitions());
110 groupings.addAll(base.getGroupings());
111 usesNodes.addAll(base.getUses());
112 augments.addAll(base.getAugmentations());
113 rpcs.addAll(base.getRpcs());
114 notifications.addAll(base.getNotifications());
115 identities.addAll(base.getIdentities());
116 features.addAll(base.getFeatures());
117 deviations.addAll(base.getDeviations());
118 extensions.addAll(base.getExtensionSchemaNodes());
119 unknownNodes.addAll(base.getUnknownSchemaNodes());
123 * Build new Module object based on this builder.
126 public Module build() {
127 instance.setPrefix(prefix);
128 instance.setRevision(revision);
129 instance.setImports(imports);
130 instance.setNamespace(namespace);
133 for (TypeDefinitionBuilder tdb : addedTypedefs) {
134 typedefs.add(tdb.build());
136 instance.setTypeDefinitions(typedefs);
139 for (DataSchemaNodeBuilder child : addedChildNodes) {
140 DataSchemaNode childNode = child.build();
141 childNodes.put(childNode.getQName(), childNode);
143 instance.addChildNodes(childNodes);
146 for (GroupingBuilder gb : addedGroupings) {
147 groupings.add(gb.build());
149 instance.setGroupings(groupings);
152 for (UsesNodeBuilder unb : addedUsesNodes) {
153 usesNodes.add(unb.build());
155 instance.setUses(usesNodes);
158 for (FeatureBuilder fb : addedFeatures) {
159 features.add(fb.build());
161 instance.setFeatures(features);
164 for (NotificationBuilder entry : addedNotifications) {
165 notifications.add(entry.build());
167 instance.setNotifications(notifications);
170 for (AugmentationSchemaBuilder builder : augmentBuilders) {
171 augments.add(builder.build());
173 instance.setAugmentations(augments);
176 for (RpcDefinitionBuilder rpc : addedRpcs) {
177 rpcs.add(rpc.build());
179 instance.setRpcs(rpcs);
182 for (DeviationBuilder entry : deviationBuilders) {
183 deviations.add(entry.build());
185 instance.setDeviations(deviations);
188 for (ExtensionBuilder eb : addedExtensions) {
189 extensions.add(eb.build());
191 Collections.sort(extensions, Comparators.SCHEMA_NODE_COMP);
192 instance.setExtensionSchemaNodes(extensions);
195 for (IdentitySchemaNodeBuilder id : addedIdentities) {
196 identities.add(id.build());
198 instance.setIdentities(identities);
201 for (UnknownSchemaNodeBuilder unb : addedUnknownNodes) {
202 unknownNodes.add(unb.build());
204 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
205 instance.setUnknownSchemaNodes(unknownNodes);
211 public void setParent(Builder parent) {
212 throw new YangParseException(name, 0, "Can not set parent to module");
216 public SchemaPath getPath() {
221 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
222 return addedTypedefs;
225 public void enterNode(final Builder node) {
226 actualPath.push(node);
229 public void exitNode() {
233 public Builder getActualNode() {
234 if (actualPath.isEmpty()) {
237 return actualPath.peekFirst();
241 public Builder getActualParent() {
242 if (actualPath.size() < 2) {
245 Builder builderChild = actualPath.removeFirst();
246 Builder builderParent = actualPath.peekFirst();
247 actualPath.addFirst(builderChild);
248 return builderParent;
252 public Set<TypeAwareBuilder> getDirtyNodes() {
256 public Set<AugmentationSchema> getAugments() {
260 public List<AugmentationSchemaBuilder> getAugmentBuilders() {
261 return augmentBuilders;
264 public List<AugmentationSchemaBuilder> getAllAugments() {
268 public Set<IdentitySchemaNode> getIdentities() {
272 public Set<IdentitySchemaNodeBuilder> getAddedIdentities() {
273 return addedIdentities;
276 public Set<FeatureDefinition> getFeatures() {
280 public Set<FeatureBuilder> getAddedFeatures() {
281 return addedFeatures;
284 public List<GroupingBuilder> getAllGroupings() {
288 public List<UsesNodeBuilder> getAllUsesNodes() {
292 public Set<Deviation> getDeviations() {
296 public Set<DeviationBuilder> getDeviationBuilders() {
297 return deviationBuilders;
300 public List<ExtensionDefinition> getExtensions() {
304 public List<ExtensionBuilder> getAddedExtensions() {
305 return addedExtensions;
308 public List<UnknownSchemaNodeBuilder> getAllUnknownNodes() {
309 return allUnknownNodes;
312 public String getName() {
316 public URI getNamespace() {
320 public void setNamespace(final URI namespace) {
321 this.namespace = namespace;
324 public String getPrefix() {
328 public Date getRevision() {
332 public boolean isSubmodule() {
336 public String getBelongsTo() {
340 public void setBelongsTo(String belongsTo) {
341 this.belongsTo = belongsTo;
344 public void markActualNodeDirty() {
345 final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) getActualNode();
346 dirtyNodes.add(nodeBuilder);
349 public void setRevision(final Date revision) {
350 this.revision = revision;
353 public void setPrefix(final String prefix) {
354 this.prefix = prefix;
357 public void setYangVersion(final String yangVersion) {
358 instance.setYangVersion(yangVersion);
361 public void setDescription(final String description) {
362 instance.setDescription(description);
365 public void setReference(final String reference) {
366 instance.setReference(reference);
369 public void setOrganization(final String organization) {
370 instance.setOrganization(organization);
373 public void setContact(final String contact) {
374 instance.setContact(contact);
377 public boolean addModuleImport(final String moduleName, final Date revision, final String prefix) {
378 final ModuleImport moduleImport = createModuleImport(moduleName, revision, prefix);
379 return imports.add(moduleImport);
382 public Set<ModuleImport> getModuleImports() {
386 public ExtensionBuilder addExtension(final QName qname, final int line, final SchemaPath path) {
387 Builder parent = getActualNode();
388 if (!(parent.equals(this))) {
389 throw new YangParseException(name, line, "extension can be defined only in module or submodule");
392 final String extName = qname.getLocalName();
393 for (ExtensionBuilder addedExtension : addedExtensions) {
394 if (addedExtension.getQName().getLocalName().equals(extName)) {
395 raiseYangParserException("extension", "node", extName, line, addedExtension.getLine());
398 final ExtensionBuilder builder = new ExtensionBuilder(name, line, qname, path);
399 builder.setParent(parent);
400 addedExtensions.add(builder);
404 public ContainerSchemaNodeBuilder addContainerNode(final int line, final QName qname, final SchemaPath schemaPath) {
405 final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
407 Builder parent = getActualNode();
408 builder.setParent(parent);
409 addChildToParent(parent, builder, qname.getLocalName());
414 public ListSchemaNodeBuilder addListNode(final int line, final QName qname, final SchemaPath schemaPath) {
415 final ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(name, line, qname, schemaPath);
417 Builder parent = getActualNode();
418 builder.setParent(parent);
419 addChildToParent(parent, builder, qname.getLocalName());
424 public LeafSchemaNodeBuilder addLeafNode(final int line, final QName qname, final SchemaPath schemaPath) {
425 final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(name, line, qname, schemaPath);
427 Builder parent = getActualNode();
428 builder.setParent(parent);
429 addChildToParent(parent, builder, qname.getLocalName());
434 public LeafListSchemaNodeBuilder addLeafListNode(final int line, final QName qname, final SchemaPath schemaPath) {
435 final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(name, line, qname, schemaPath);
437 Builder parent = getActualNode();
438 builder.setParent(parent);
439 addChildToParent(parent, builder, qname.getLocalName());
444 public GroupingBuilder addGrouping(final int line, final QName qname, final SchemaPath path) {
445 final GroupingBuilder builder = new GroupingBuilderImpl(name, line, qname, path);
447 Builder parent = getActualNode();
448 builder.setParent(parent);
450 String groupingName = qname.getLocalName();
451 if (parent.equals(this)) {
452 for (GroupingBuilder addedGrouping : addedGroupings) {
453 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
454 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
457 addedGroupings.add(builder);
459 if (parent instanceof DataNodeContainerBuilder) {
460 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
461 for (GroupingBuilder addedGrouping : parentNode.getGroupingBuilders()) {
462 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
463 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
466 parentNode.addGrouping(builder);
467 } else if (parent instanceof RpcDefinitionBuilder) {
468 RpcDefinitionBuilder parentNode = (RpcDefinitionBuilder) parent;
469 for (GroupingBuilder child : parentNode.getGroupings()) {
470 if (child.getQName().getLocalName().equals(groupingName)) {
471 raiseYangParserException("", "Grouping", groupingName, line, child.getLine());
474 parentNode.addGrouping(builder);
476 throw new YangParseException(name, line, "Unresolved parent of grouping " + groupingName);
480 allGroupings.add(builder);
484 public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr) {
485 final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr);
487 Builder parent = getActualNode();
488 builder.setParent(parent);
490 if (parent.equals(this)) {
491 // augment can be declared only under 'module' ...
492 if (!(augmentTargetStr.startsWith("/"))) {
493 throw new YangParseException(
496 "If the 'augment' statement is on the top level in a module, the absolute form of a schema node identifier MUST be used.");
498 augmentBuilders.add(builder);
500 // ... or 'uses' statement
501 if (parent instanceof UsesNodeBuilder) {
502 if (augmentTargetStr.startsWith("/")) {
503 throw new YangParseException(name, line,
504 "If 'augment' statement is a substatement to the 'uses' statement, it cannot contain absolute path ("
505 + augmentTargetStr + ")");
507 ((UsesNodeBuilder) parent).addAugment(builder);
509 throw new YangParseException(name, line, "Augment can be declared only under module or uses statement.");
512 allAugments.add(builder);
518 public void addUsesNode(UsesNodeBuilder usesBuilder) {
519 addedUsesNodes.add(usesBuilder);
520 allUsesNodes.add(usesBuilder);
523 public UsesNodeBuilder addUsesNode(final int line, final String groupingPathStr) {
524 final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(name, line, groupingPathStr);
526 Builder parent = getActualNode();
527 usesBuilder.setParent(parent);
529 if (parent.equals(this)) {
530 addedUsesNodes.add(usesBuilder);
532 if (!(parent instanceof DataNodeContainerBuilder)) {
533 throw new YangParseException(name, line, "Unresolved parent of uses '" + groupingPathStr + "'.");
535 ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder);
537 if (parent instanceof AugmentationSchemaBuilder) {
538 usesBuilder.setAugmenting(true);
541 allUsesNodes.add(usesBuilder);
545 public void addRefine(final RefineHolder refine) {
546 final Builder parent = getActualNode();
547 if (!(parent instanceof UsesNodeBuilder)) {
548 throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
550 ((UsesNodeBuilder) parent).addRefine(refine);
551 refine.setParent(parent);
554 public RpcDefinitionBuilder addRpc(final int line, final QName qname, final SchemaPath path) {
555 Builder parent = getActualNode();
556 if (!(parent.equals(this))) {
557 throw new YangParseException(name, line, "rpc can be defined only in module or submodule");
560 final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(name, line, qname, path);
561 rpcBuilder.setParent(parent);
563 String rpcName = qname.getLocalName();
564 for (RpcDefinitionBuilder rpc : addedRpcs) {
565 if (rpc.getQName().getLocalName().equals(rpcName)) {
566 raiseYangParserException("", "rpc", rpcName, line, rpc.getLine());
569 for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
570 if (addedChild.getQName().getLocalName().equals(rpcName)) {
571 raiseYangParserException("rpc", "node", rpcName, line, addedChild.getLine());
574 for (NotificationBuilder addedNotification : addedNotifications) {
575 if (addedNotification.getQName().getLocalName().equals(rpcName)) {
576 raiseYangParserException("rpc", "notification", rpcName, line, addedNotification.getLine());
579 addedRpcs.add(rpcBuilder);
583 public ContainerSchemaNodeBuilder addRpcInput(final int line, final QName qname, final SchemaPath schemaPath) {
584 final Builder parent = getActualNode();
585 if (!(parent instanceof RpcDefinitionBuilder)) {
586 throw new YangParseException(name, line, "input can be defined only in rpc statement");
588 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
590 final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
591 inputBuilder.setParent(rpc);
593 rpc.setInput(inputBuilder);
597 public ContainerSchemaNodeBuilder addRpcOutput(final SchemaPath schemaPath, final QName qname, final int line) {
598 final Builder parent = getActualNode();
599 if (!(parent instanceof RpcDefinitionBuilder)) {
600 throw new YangParseException(name, line, "output can be defined only in rpc statement");
602 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
604 final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
605 outputBuilder.setParent(rpc);
607 rpc.setOutput(outputBuilder);
608 return outputBuilder;
611 public void addNotification(NotificationDefinition notification) {
612 notifications.add(notification);
615 public NotificationBuilder addNotification(final int line, final QName qname, final SchemaPath path) {
616 final Builder parent = getActualNode();
617 if (!(parent.equals(this))) {
618 throw new YangParseException(name, line, "notification can be defined only in module or submodule");
621 String notificationName = qname.getLocalName();
622 for (NotificationBuilder nb : addedNotifications) {
623 if (nb.getQName().equals(qname)) {
624 raiseYangParserException("", "notification", notificationName, line, nb.getLine());
627 for (RpcDefinitionBuilder rpc : addedRpcs) {
628 if (rpc.getQName().getLocalName().equals(notificationName)) {
629 raiseYangParserException("notification", "rpc", notificationName, line, rpc.getLine());
632 for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
633 if (addedChild.getQName().getLocalName().equals(notificationName)) {
634 raiseYangParserException("notification", "node", notificationName, line, addedChild.getLine());
638 final NotificationBuilder builder = new NotificationBuilder(name, line, qname, path);
639 builder.setParent(parent);
640 addedNotifications.add(builder);
645 public FeatureBuilder addFeature(final int line, final QName qname, final SchemaPath path) {
646 Builder parent = getActualNode();
647 if (!(parent.equals(this))) {
648 throw new YangParseException(name, line, "feature can be defined only in module or submodule");
651 final FeatureBuilder builder = new FeatureBuilder(name, line, qname, path);
652 builder.setParent(parent);
654 String featureName = qname.getLocalName();
655 for (FeatureBuilder addedFeature : addedFeatures) {
656 if (addedFeature.getQName().getLocalName().equals(featureName)) {
657 raiseYangParserException("", "feature", featureName, line, addedFeature.getLine());
660 addedFeatures.add(builder);
664 public ChoiceBuilder addChoice(final int line, final QName qname, final SchemaPath path) {
665 final ChoiceBuilder builder = new ChoiceBuilder(name, line, qname, path);
667 Builder parent = getActualNode();
668 builder.setParent(parent);
669 addChildToParent(parent, builder, qname.getLocalName());
674 public ChoiceCaseBuilder addCase(final int line, final QName qname, final SchemaPath path) {
675 Builder parent = getActualNode();
676 if (parent == null || parent.equals(this)) {
677 throw new YangParseException(name, line, "'case' parent not found");
680 final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(name, line, qname, path);
681 builder.setParent(parent);
683 if (parent instanceof ChoiceBuilder) {
684 ((ChoiceBuilder) parent).addCase(builder);
685 } else if (parent instanceof AugmentationSchemaBuilder) {
686 ((AugmentationSchemaBuilder) parent).addChildNode(builder);
688 throw new YangParseException(name, line, "Unresolved parent of 'case' " + qname.getLocalName());
694 public AnyXmlBuilder addAnyXml(final int line, final QName qname, final SchemaPath schemaPath) {
695 final AnyXmlBuilder builder = new AnyXmlBuilder(name, line, qname, schemaPath);
697 Builder parent = getActualNode();
698 builder.setParent(parent);
699 addChildToParent(parent, builder, qname.getLocalName());
705 public void addTypedef(TypeDefinitionBuilder typedefBuilder) {
706 String nodeName = typedefBuilder.getQName().getLocalName();
707 for (TypeDefinitionBuilder tdb : addedTypedefs) {
708 if (tdb.getQName().getLocalName().equals(nodeName)) {
709 raiseYangParserException("", "typedef", nodeName, typedefBuilder.getLine(), tdb.getLine());
712 addedTypedefs.add(typedefBuilder);
715 public TypeDefinitionBuilderImpl addTypedef(final int line, final QName qname, final SchemaPath path) {
716 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(name, line, qname, path);
718 Builder parent = getActualNode();
719 builder.setParent(parent);
721 String typedefName = qname.getLocalName();
722 if (parent.equals(this)) {
723 for (TypeDefinitionBuilder tdb : addedTypedefs) {
724 if (tdb.getQName().getLocalName().equals(typedefName)) {
725 raiseYangParserException("", "typedef", typedefName, line, tdb.getLine());
728 addedTypedefs.add(builder);
730 if (parent instanceof DataNodeContainerBuilder) {
731 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
732 for (TypeDefinitionBuilder child : parentNode.getTypeDefinitionBuilders()) {
733 if (child.getQName().getLocalName().equals(typedefName)) {
734 raiseYangParserException("", "typedef", typedefName, line, child.getLine());
737 parentNode.addTypedef(builder);
738 } else if (parent instanceof RpcDefinitionBuilder) {
739 RpcDefinitionBuilder rpcParent = (RpcDefinitionBuilder) parent;
740 for (TypeDefinitionBuilder tdb : rpcParent.getTypeDefinitions()) {
741 if (tdb.getQName().getLocalName().equals(builder.getQName().getLocalName())) {
742 raiseYangParserException("", "typedef", typedefName, line, tdb.getLine());
745 rpcParent.addTypedef(builder);
747 throw new YangParseException(name, line, "Unresolved parent of typedef " + typedefName);
754 public void setType(final TypeDefinition<?> type) {
755 Builder parent = getActualNode();
756 if (!(parent instanceof TypeAwareBuilder)) {
757 throw new YangParseException("Failed to set type '" + type.getQName().getLocalName()
758 + "'. Invalid parent node: " + parent);
760 ((TypeAwareBuilder) parent).setType(type);
763 public UnionTypeBuilder addUnionType(final int line, final URI namespace, final Date revision) {
764 final Builder parent = getActualNode();
765 if (parent == null) {
766 throw new YangParseException(name, line, "Unresolved parent of union type");
768 final UnionTypeBuilder union = new UnionTypeBuilder(name, line);
769 if (parent instanceof TypeAwareBuilder) {
770 ((TypeAwareBuilder) parent).setTypedef(union);
773 throw new YangParseException(name, line, "Invalid parent of union type.");
778 public void addIdentityrefType(final int line, final SchemaPath schemaPath, final String baseString) {
779 final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(name, line, baseString, schemaPath);
781 final Builder parent = getActualNode();
782 if (parent == null) {
783 throw new YangParseException(name, line, "Unresolved parent of identityref type.");
785 if (parent instanceof TypeAwareBuilder) {
786 final TypeAwareBuilder typeParent = (TypeAwareBuilder) parent;
787 typeParent.setTypedef(identityref);
788 dirtyNodes.add(typeParent);
790 throw new YangParseException(name, line, "Invalid parent of identityref type.");
795 public DeviationBuilder addDeviation(final int line, final String targetPath) {
796 Builder parent = getActualNode();
797 if (!(parent.equals(this))) {
798 throw new YangParseException(name, line, "deviation can be defined only in module or submodule");
801 final DeviationBuilder builder = new DeviationBuilder(name, line, targetPath);
802 builder.setParent(parent);
803 deviationBuilders.add(builder);
807 public IdentitySchemaNodeBuilder addIdentity(final QName qname, final int line, final SchemaPath path) {
808 Builder parent = getActualNode();
809 if (!(parent.equals(this))) {
810 throw new YangParseException(name, line, "identity can be defined only in module or submodule");
812 String identityName = qname.getLocalName();
813 for (IdentitySchemaNodeBuilder idBuilder : addedIdentities) {
814 if (idBuilder.getQName().equals(qname)) {
815 raiseYangParserException("", "identity", identityName, line, idBuilder.getLine());
819 final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(name, line, qname, path);
820 builder.setParent(parent);
821 addedIdentities.add(builder);
826 public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder builder) {
827 addedUnknownNodes.add(builder);
828 allUnknownNodes.add(builder);
831 public UnknownSchemaNodeBuilder addUnknownSchemaNode(final int line, final QName qname, final SchemaPath path) {
832 final Builder parent = getActualNode();
833 final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(name, line, qname, path);
834 builder.setParent(parent);
835 allUnknownNodes.add(builder);
837 if (parent.equals(this)) {
838 addedUnknownNodes.add(builder);
840 if (parent instanceof SchemaNodeBuilder) {
841 ((SchemaNodeBuilder) parent).addUnknownNodeBuilder(builder);
842 } else if (parent instanceof DataNodeContainerBuilder) {
843 ((DataNodeContainerBuilder) parent).addUnknownNodeBuilder(builder);
844 } else if (parent instanceof RefineHolder) {
845 ((RefineHolder) parent).addUnknownNodeBuilder(builder);
847 throw new YangParseException(name, line, "Unresolved parent of unknown node '" + qname.getLocalName()
855 public Set<RpcDefinition> getRpcs() {
859 public Set<RpcDefinitionBuilder> getAddedRpcs() {
863 public Set<NotificationDefinition> getNotifications() {
864 return notifications;
867 public Set<NotificationBuilder> getAddedNotifications() {
868 return addedNotifications;
872 public String toString() {
873 return "module " + name;
876 private final class ModuleImpl implements Module {
877 private URI namespace;
878 private final String name;
879 private Date revision;
880 private String prefix;
881 private String yangVersion;
882 private String description;
883 private String reference;
884 private String organization;
885 private String contact;
886 private final Set<ModuleImport> imports = new HashSet<>();
887 private final Set<FeatureDefinition> features = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
888 private final Set<TypeDefinition<?>> typeDefinitions = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
889 private final Set<NotificationDefinition> notifications = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
890 private final Set<AugmentationSchema> augmentations = new HashSet<>();
891 private final Set<RpcDefinition> rpcs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
892 private final Set<Deviation> deviations = new HashSet<>();
893 private final Map<QName, DataSchemaNode> childNodes = new TreeMap<>(Comparators.QNAME_COMP);
894 private final Set<GroupingDefinition> groupings = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
895 private final Set<UsesNode> uses = new HashSet<>();
896 private final List<ExtensionDefinition> extensionNodes = new ArrayList<>();
897 private final Set<IdentitySchemaNode> identities = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
898 private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
900 private ModuleImpl(String name) {
905 public URI getNamespace() {
909 private void setNamespace(URI namespace) {
910 this.namespace = namespace;
914 public String getName() {
919 public Date getRevision() {
923 private void setRevision(Date revision) {
924 this.revision = revision;
928 public String getPrefix() {
932 private void setPrefix(String prefix) {
933 this.prefix = prefix;
937 public String getYangVersion() {
941 private void setYangVersion(String yangVersion) {
942 this.yangVersion = yangVersion;
946 public String getDescription() {
950 private void setDescription(String description) {
951 this.description = description;
955 public String getReference() {
959 private void setReference(String reference) {
960 this.reference = reference;
964 public String getOrganization() {
968 private void setOrganization(String organization) {
969 this.organization = organization;
973 public String getContact() {
977 private void setContact(String contact) {
978 this.contact = contact;
982 public Set<ModuleImport> getImports() {
986 private void setImports(Set<ModuleImport> imports) {
987 if (imports != null) {
988 this.imports.addAll(imports);
993 public Set<FeatureDefinition> getFeatures() {
997 private void setFeatures(Set<FeatureDefinition> features) {
998 if (features != null) {
999 this.features.addAll(features);
1004 public Set<TypeDefinition<?>> getTypeDefinitions() {
1005 return typeDefinitions;
1008 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
1009 if (typeDefinitions != null) {
1010 this.typeDefinitions.addAll(typeDefinitions);
1015 public Set<NotificationDefinition> getNotifications() {
1016 return notifications;
1019 private void setNotifications(Set<NotificationDefinition> notifications) {
1020 if (notifications != null) {
1021 this.notifications.addAll(notifications);
1026 public Set<AugmentationSchema> getAugmentations() {
1027 return augmentations;
1030 private void setAugmentations(Set<AugmentationSchema> augmentations) {
1031 if (augmentations != null) {
1032 this.augmentations.addAll(augmentations);
1037 public Set<RpcDefinition> getRpcs() {
1041 private void setRpcs(Set<RpcDefinition> rpcs) {
1043 this.rpcs.addAll(rpcs);
1048 public Set<Deviation> getDeviations() {
1052 private void setDeviations(Set<Deviation> deviations) {
1053 if (deviations != null) {
1054 this.deviations.addAll(deviations);
1059 public Set<DataSchemaNode> getChildNodes() {
1060 final Set<DataSchemaNode> result = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
1061 result.addAll(childNodes.values());
1062 return Collections.unmodifiableSet(result);
1065 private void addChildNodes(Map<QName, DataSchemaNode> childNodes) {
1066 if (childNodes != null) {
1067 this.childNodes.putAll(childNodes);
1072 public Set<GroupingDefinition> getGroupings() {
1076 private void setGroupings(Set<GroupingDefinition> groupings) {
1077 if (groupings != null) {
1078 this.groupings.addAll(groupings);
1083 public Set<UsesNode> getUses() {
1087 private void setUses(Set<UsesNode> uses) {
1089 this.uses.addAll(uses);
1094 public List<ExtensionDefinition> getExtensionSchemaNodes() {
1095 Collections.sort(extensionNodes, Comparators.SCHEMA_NODE_COMP);
1096 return extensionNodes;
1099 private void setExtensionSchemaNodes(final List<ExtensionDefinition> extensionNodes) {
1100 if (extensionNodes != null) {
1101 this.extensionNodes.addAll(extensionNodes);
1106 public Set<IdentitySchemaNode> getIdentities() {
1110 private void setIdentities(final Set<IdentitySchemaNode> identities) {
1111 if (identities != null) {
1112 this.identities.addAll(identities);
1117 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
1118 return unknownNodes;
1121 private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
1122 if (unknownNodes != null) {
1123 this.unknownNodes.addAll(unknownNodes);
1128 public DataSchemaNode getDataChildByName(QName name) {
1129 return childNodes.get(name);
1133 public DataSchemaNode getDataChildByName(String name) {
1134 DataSchemaNode result = null;
1135 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
1136 if (entry.getKey().getLocalName().equals(name)) {
1137 result = entry.getValue();
1145 public int hashCode() {
1146 final int prime = 31;
1148 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1149 result = prime * result + ((name == null) ? 0 : name.hashCode());
1150 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1151 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1152 result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());
1157 public boolean equals(Object obj) {
1164 if (getClass() != obj.getClass()) {
1167 ModuleImpl other = (ModuleImpl) obj;
1168 if (namespace == null) {
1169 if (other.namespace != null) {
1172 } else if (!namespace.equals(other.namespace)) {
1176 if (other.name != null) {
1179 } else if (!name.equals(other.name)) {
1182 if (revision == null) {
1183 if (other.revision != null) {
1186 } else if (!revision.equals(other.revision)) {
1189 if (prefix == null) {
1190 if (other.prefix != null) {
1193 } else if (!prefix.equals(other.prefix)) {
1196 if (yangVersion == null) {
1197 if (other.yangVersion != null) {
1200 } else if (!yangVersion.equals(other.yangVersion)) {
1207 public String toString() {
1208 StringBuilder sb = new StringBuilder(ModuleImpl.class.getSimpleName());
1210 sb.append("name=" + name);
1211 sb.append(", namespace=" + namespace);
1212 sb.append(", revision=" + revision);
1213 sb.append(", prefix=" + prefix);
1214 sb.append(", yangVersion=" + yangVersion);
1216 return sb.toString();
1221 * Add child to parent. Method checks for duplicates and add given child
1222 * node to parent. If node with same name is found, throws exception. If
1223 * parent is null, child node will be added directly to module.
1229 private void addChildToParent(final Builder parent, final DataSchemaNodeBuilder child, final String childName) {
1230 final int lineNum = child.getLine();
1231 if (parent.equals(this)) {
1232 addChildToModule(child, childName, lineNum);
1234 addChildToSubnodeOfModule(parent, child, childName, lineNum);
1239 * Adds child node <code>child</code> to the set of nodes child nodes.
1241 * The method reduces the complexity of the method
1242 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1243 * addChildToParent}.
1246 * data schema node builder for child node
1248 * string with name of child node
1250 * line number in YANG file where is the node with the name equal
1251 * to <code>childName</code> is defined
1253 private void addChildToModule(final DataSchemaNodeBuilder child, final String childName, final int lineNum) {
1254 // if parent == null => node is defined under module
1255 // All leafs, leaf-lists, lists, containers, choices, rpcs,
1256 // notifications, and anyxmls defined within a parent node or at the
1257 // top level of the module or its submodules share the same
1258 // identifier namespace.
1259 for (DataSchemaNodeBuilder childNode : addedChildNodes) {
1260 if (childNode.getQName().getLocalName().equals(childName)) {
1261 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
1264 for (RpcDefinitionBuilder rpc : addedRpcs) {
1265 if (rpc.getQName().getLocalName().equals(childName)) {
1266 raiseYangParserException("'" + child + "'", "rpc", childName, lineNum, rpc.getLine());
1269 for (NotificationBuilder notification : addedNotifications) {
1270 if (notification.getQName().getLocalName().equals(childName)) {
1271 raiseYangParserException("'" + child + "'", "notification", childName, lineNum, notification.getLine());
1274 addedChildNodes.add(child);
1278 * Adds child node <code>child</code> to the group of child nodes of the
1279 * <code>parent</code>
1281 * The method reduces the complexity of the method
1282 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1283 * addChildToParent}. *
1286 * builder of node which is parent for <code>child</code>
1288 * data schema node builder for child node
1290 * string with name of child node
1292 * line number in YANG file where is the node with the name equal
1293 * to <code>childName</code> is defined
1295 private void addChildToSubnodeOfModule(final Builder parent, final DataSchemaNodeBuilder child,
1296 final String childName, final int lineNum) {
1297 // no need for checking rpc and notification because they can be
1298 // defined only under module or submodule
1299 if (parent instanceof DataNodeContainerBuilder) {
1300 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
1301 for (DataSchemaNodeBuilder childNode : parentNode.getChildNodeBuilders()) {
1302 if (childNode.getQName().getLocalName().equals(childName)) {
1303 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
1306 parentNode.addChildNode(child);
1307 } else if (parent instanceof ChoiceBuilder) {
1308 ChoiceBuilder parentNode = (ChoiceBuilder) parent;
1309 for (ChoiceCaseBuilder caseBuilder : parentNode.getCases()) {
1310 if (caseBuilder.getQName().getLocalName().equals(childName)) {
1311 raiseYangParserException("'" + child + "'", "node", childName, lineNum, caseBuilder.getLine());
1314 parentNode.addCase(child);
1316 throw new YangParseException(name, lineNum, "Unresolved parent of node '" + childName + "'.");
1320 private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) {
1321 final ModuleImport moduleImport = new ModuleImportImpl(moduleName, revision, prefix);
1322 return moduleImport;
1325 private void raiseYangParserException(final String cantAddType, final String type, final String name,
1326 final int currentLine, final int duplicateLine) {
1328 StringBuilder msgPrefix = new StringBuilder("");
1329 if (cantAddType != null && !cantAddType.isEmpty()) {
1330 msgPrefix.append("Can not add ");
1331 msgPrefix.append(cantAddType);
1332 msgPrefix.append(": ");
1335 String msg = String.format("%s%s with same name '%s' already declared at line %d.", msgPrefix, type, name,
1337 throw new YangParseException(moduleName, currentLine, msg);
1341 public int hashCode() {
1342 final int prime = 31;
1344 result = prime * result + ((name == null) ? 0 : name.hashCode());
1345 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1346 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1347 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1353 public boolean equals(Object obj) {
1360 if (getClass() != obj.getClass()) {
1363 ModuleBuilder other = (ModuleBuilder) obj;
1365 if (other.name != null) {
1368 } else if (!name.equals(other.name)) {
1371 if (namespace == null) {
1372 if (other.namespace != null) {
1375 } else if (!namespace.equals(other.namespace)) {
1378 if (prefix == null) {
1379 if (other.prefix != null) {
1382 } else if (!prefix.equals(other.prefix)) {
1385 if (revision == null) {
1386 if (other.revision != null) {
1389 } else if (!revision.equals(other.revision)) {