2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 package org.opendaylight.yangtools.yang.parser.builder.impl;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.Deque;
15 import java.util.HashSet;
16 import java.util.LinkedHashSet;
17 import java.util.LinkedList;
18 import java.util.List;
20 import java.util.TreeSet;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Deviation;
26 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
27 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
28 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
29 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
32 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
33 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.UsesNode;
38 import org.opendaylight.yangtools.yang.model.util.ModuleImportImpl;
39 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
40 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
41 import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder;
42 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
43 import org.opendaylight.yangtools.yang.parser.builder.api.ExtensionBuilder;
44 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
45 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
46 import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
47 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
48 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
49 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
50 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDataNodeContainerBuilder;
51 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
52 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
55 * Builder of Module object. If this module is dependent on external
56 * module/modules, these dependencies must be resolved before module is built,
57 * otherwise result may not be valid.
59 public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
61 private final ModuleImpl instance;
62 private final String name;
63 private final String sourcePath;
64 private final SchemaPath schemaPath;
65 private URI namespace;
66 private String prefix;
67 private Date revision;
69 private final boolean submodule;
70 private String belongsTo;
71 private ModuleBuilder parent;
74 public ModuleBuilder getParent() {
78 public void setParent(final ModuleBuilder parent) {
82 private final Deque<Builder> actualPath = new LinkedList<>();
83 private final Set<TypeAwareBuilder> dirtyNodes = new HashSet<>();
85 private final Set<ModuleImport> imports = new HashSet<ModuleImport>();
87 private final Set<AugmentationSchema> augments = new LinkedHashSet<>();
88 private final List<AugmentationSchemaBuilder> augmentBuilders = new ArrayList<>();
89 private final List<AugmentationSchemaBuilder> allAugments = new ArrayList<>();
91 private final List<GroupingBuilder> allGroupings = new ArrayList<>();
93 private final List<UsesNodeBuilder> allUsesNodes = new ArrayList<>();
95 private final Set<RpcDefinition> rpcs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
96 private final Set<RpcDefinitionBuilder> addedRpcs = new HashSet<>();
98 private final Set<NotificationDefinition> notifications = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
99 private final Set<NotificationBuilder> addedNotifications = new HashSet<>();
101 private final Set<IdentitySchemaNode> identities = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
102 private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<>();
104 private final Set<FeatureDefinition> features = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
105 private final Set<FeatureBuilder> addedFeatures = new HashSet<>();
107 private final Set<Deviation> deviations = new HashSet<>();
108 private final Set<DeviationBuilder> deviationBuilders = new HashSet<>();
110 private final List<ExtensionDefinition> extensions = new ArrayList<>();
111 private final List<ExtensionBuilder> addedExtensions = new ArrayList<>();
113 private final List<UnknownSchemaNodeBuilder> allUnknownNodes = new ArrayList<>();
115 private final List<ListSchemaNodeBuilder> allLists = new ArrayList<>();
117 private String source;
119 public ModuleBuilder(final String name, final String sourcePath) {
120 this(name, false, sourcePath);
123 public ModuleBuilder(final String name, final boolean submodule, final String sourcePath) {
124 super(name, 0, null);
126 this.sourcePath = sourcePath;
127 schemaPath = SchemaPath.create(Collections.<QName> emptyList(), true);
128 this.submodule = submodule;
129 instance = new ModuleImpl(name, sourcePath);
130 actualPath.push(this);
133 public ModuleBuilder(final Module base) {
134 super(base.getName(), 0, null);
135 this.name = base.getName();
136 this.sourcePath = base.getModuleSourcePath();
137 schemaPath = SchemaPath.create(Collections.<QName> emptyList(), true);
139 instance = new ModuleImpl(base.getName(), base.getModuleSourcePath());
140 instance.setYangVersion(base.getYangVersion());
141 actualPath.push(this);
142 namespace = base.getNamespace();
143 prefix = base.getPrefix();
144 revision = base.getRevision();
146 for (DataSchemaNode childNode : base.getChildNodes()) {
147 childNodes.put(childNode.getQName(), childNode);
150 typedefs.addAll(base.getTypeDefinitions());
151 groupings.addAll(base.getGroupings());
152 usesNodes.addAll(base.getUses());
153 augments.addAll(base.getAugmentations());
154 rpcs.addAll(base.getRpcs());
155 notifications.addAll(base.getNotifications());
157 for (IdentitySchemaNode identityNode : base.getIdentities()) {
158 addedIdentities.add(new IdentitySchemaNodeBuilder(name, identityNode));
161 features.addAll(base.getFeatures());
162 deviations.addAll(base.getDeviations());
163 extensions.addAll(base.getExtensionSchemaNodes());
164 unknownNodes.addAll(base.getUnknownSchemaNodes());
168 * Build new Module object based on this builder.
171 public Module build() {
172 instance.setPrefix(prefix);
173 instance.setRevision(revision);
174 instance.setImports(imports);
175 instance.setNamespace(namespace);
178 for (TypeDefinitionBuilder tdb : addedTypedefs) {
179 typedefs.add(tdb.build());
181 instance.setTypeDefinitions(typedefs);
184 for (DataSchemaNodeBuilder child : addedChildNodes) {
185 childNodes.put(child.getQName(), child.build());
187 instance.addChildNodes(childNodes.values());
190 for (GroupingBuilder gb : addedGroupings) {
191 groupings.add(gb.build());
193 instance.setGroupings(groupings);
196 for (UsesNodeBuilder unb : addedUsesNodes) {
197 usesNodes.add(unb.build());
199 instance.setUses(usesNodes);
202 for (FeatureBuilder fb : addedFeatures) {
203 features.add(fb.build());
205 instance.setFeatures(features);
208 for (NotificationBuilder entry : addedNotifications) {
209 notifications.add(entry.build());
211 instance.setNotifications(notifications);
214 for (AugmentationSchemaBuilder builder : augmentBuilders) {
215 augments.add(builder.build());
217 instance.setAugmentations(augments);
220 for (RpcDefinitionBuilder rpc : addedRpcs) {
221 rpcs.add(rpc.build());
223 instance.setRpcs(rpcs);
226 for (DeviationBuilder entry : deviationBuilders) {
227 deviations.add(entry.build());
229 instance.setDeviations(deviations);
232 for (ExtensionBuilder eb : addedExtensions) {
233 extensions.add(eb.build());
235 Collections.sort(extensions, Comparators.SCHEMA_NODE_COMP);
236 instance.setExtensionSchemaNodes(extensions);
239 for (IdentitySchemaNodeBuilder id : addedIdentities) {
240 identities.add(id.build());
242 instance.setIdentities(identities);
245 for (UnknownSchemaNodeBuilder unb : addedUnknownNodes) {
246 unknownNodes.add(unb.build());
248 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
249 instance.setUnknownSchemaNodes(unknownNodes);
251 instance.setSource(source);
256 public String getModuleSourcePath() {
261 public void setParent(final Builder parent) {
262 throw new YangParseException(name, 0, "Can not set parent to module");
266 public SchemaPath getPath() {
271 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
272 return addedTypedefs;
275 public void enterNode(final Builder node) {
276 actualPath.push(node);
279 public void exitNode() {
283 public Builder getActualNode() {
284 if (actualPath.isEmpty()) {
287 return actualPath.peekFirst();
291 public Set<TypeAwareBuilder> getDirtyNodes() {
295 public Set<AugmentationSchema> getAugments() {
299 public List<AugmentationSchemaBuilder> getAugmentBuilders() {
300 return augmentBuilders;
303 public List<AugmentationSchemaBuilder> getAllAugments() {
307 public Set<IdentitySchemaNode> getIdentities() {
311 public Set<IdentitySchemaNodeBuilder> getAddedIdentities() {
312 return addedIdentities;
315 public Set<FeatureDefinition> getFeatures() {
319 public Set<FeatureBuilder> getAddedFeatures() {
320 return addedFeatures;
323 public List<GroupingBuilder> getAllGroupings() {
327 public List<UsesNodeBuilder> getAllUsesNodes() {
331 public Set<Deviation> getDeviations() {
335 public Set<DeviationBuilder> getDeviationBuilders() {
336 return deviationBuilders;
339 public List<ExtensionDefinition> getExtensions() {
343 public List<ExtensionBuilder> getAddedExtensions() {
344 return addedExtensions;
347 public List<UnknownSchemaNodeBuilder> getAllUnknownNodes() {
348 return allUnknownNodes;
351 public List<ListSchemaNodeBuilder> getAllLists() {
355 public String getName() {
359 public URI getNamespace() {
363 public void setNamespace(final URI namespace) {
364 this.namespace = namespace;
367 public String getPrefix() {
371 public Date getRevision() {
375 public boolean isSubmodule() {
379 public String getBelongsTo() {
383 public void setBelongsTo(final String belongsTo) {
384 this.belongsTo = belongsTo;
387 public void markActualNodeDirty() {
388 final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) getActualNode();
389 dirtyNodes.add(nodeBuilder);
392 public void setRevision(final Date revision) {
393 this.revision = revision;
396 public void setPrefix(final String prefix) {
397 this.prefix = prefix;
400 public void setYangVersion(final String yangVersion) {
401 instance.setYangVersion(yangVersion);
404 public void setDescription(final String description) {
405 instance.setDescription(description);
408 public void setReference(final String reference) {
409 instance.setReference(reference);
412 public void setOrganization(final String organization) {
413 instance.setOrganization(organization);
416 public void setContact(final String contact) {
417 instance.setContact(contact);
420 public boolean addModuleImport(final String moduleName, final Date revision, final String prefix) {
421 final ModuleImport moduleImport = createModuleImport(moduleName, revision, prefix);
422 return imports.add(moduleImport);
425 public Set<ModuleImport> getModuleImports() {
429 public ExtensionBuilder addExtension(final QName qname, final int line, final SchemaPath path) {
430 Builder parent = getActualNode();
431 if (!(parent.equals(this))) {
432 throw new YangParseException(name, line, "extension can be defined only in module or submodule");
435 final String extName = qname.getLocalName();
436 for (ExtensionBuilder addedExtension : addedExtensions) {
437 if (addedExtension.getQName().getLocalName().equals(extName)) {
438 raiseYangParserException("extension", "node", extName, line, addedExtension.getLine());
441 final ExtensionBuilder builder = new ExtensionBuilderImpl(name, line, qname, path);
442 builder.setParent(parent);
443 addedExtensions.add(builder);
447 public ContainerSchemaNodeBuilder addContainerNode(final int line, final QName qname, final SchemaPath schemaPath) {
448 final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
450 Builder parent = getActualNode();
451 builder.setParent(parent);
452 addChildToParent(parent, builder, qname.getLocalName());
457 public ListSchemaNodeBuilder addListNode(final int line, final QName qname, final SchemaPath schemaPath) {
458 final ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(name, line, qname, schemaPath);
460 Builder parent = getActualNode();
461 builder.setParent(parent);
462 addChildToParent(parent, builder, qname.getLocalName());
463 allLists.add(builder);
468 public LeafSchemaNodeBuilder addLeafNode(final int line, final QName qname, final SchemaPath schemaPath) {
469 final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(name, line, qname, schemaPath);
471 Builder parent = getActualNode();
472 builder.setParent(parent);
473 addChildToParent(parent, builder, qname.getLocalName());
478 public LeafListSchemaNodeBuilder addLeafListNode(final int line, final QName qname, final SchemaPath schemaPath) {
479 final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(name, line, qname, schemaPath);
481 Builder parent = getActualNode();
482 builder.setParent(parent);
483 addChildToParent(parent, builder, qname.getLocalName());
488 public GroupingBuilder addGrouping(final int line, final QName qname, final SchemaPath path) {
489 final GroupingBuilder builder = new GroupingBuilderImpl(name, line, qname, path);
491 Builder parent = getActualNode();
492 builder.setParent(parent);
494 String groupingName = qname.getLocalName();
495 if (parent.equals(this)) {
496 for (GroupingBuilder addedGrouping : addedGroupings) {
497 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
498 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
501 addedGroupings.add(builder);
503 if (parent instanceof DataNodeContainerBuilder) {
504 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
505 for (GroupingBuilder addedGrouping : parentNode.getGroupingBuilders()) {
506 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
507 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
510 parentNode.addGrouping(builder);
511 } else if (parent instanceof RpcDefinitionBuilder) {
512 RpcDefinitionBuilder parentNode = (RpcDefinitionBuilder) parent;
513 for (GroupingBuilder child : parentNode.getGroupings()) {
514 if (child.getQName().getLocalName().equals(groupingName)) {
515 raiseYangParserException("", "Grouping", groupingName, line, child.getLine());
518 parentNode.addGrouping(builder);
520 throw new YangParseException(name, line, "Unresolved parent of grouping " + groupingName);
524 allGroupings.add(builder);
528 public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr, final int order) {
529 final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr, order);
531 Builder parent = getActualNode();
532 builder.setParent(parent);
534 if (parent.equals(this)) {
535 // augment can be declared only under 'module' ...
536 if (!(augmentTargetStr.startsWith("/"))) {
537 throw new YangParseException(
540 "If the 'augment' statement is on the top level in a module, the absolute form of a schema node identifier MUST be used.");
542 augmentBuilders.add(builder);
544 // ... or 'uses' statement
545 if (parent instanceof UsesNodeBuilder) {
546 if (augmentTargetStr.startsWith("/")) {
547 throw new YangParseException(name, line,
548 "If 'augment' statement is a substatement to the 'uses' statement, it cannot contain absolute path ("
549 + augmentTargetStr + ")");
551 ((UsesNodeBuilder) parent).addAugment(builder);
553 throw new YangParseException(name, line, "Augment can be declared only under module or uses statement.");
556 allAugments.add(builder);
562 public void addUsesNode(final UsesNodeBuilder usesBuilder) {
563 addedUsesNodes.add(usesBuilder);
564 allUsesNodes.add(usesBuilder);
567 public UsesNodeBuilder addUsesNode(final int line, final String groupingPathStr) {
568 final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(name, line, groupingPathStr);
570 Builder parent = getActualNode();
571 usesBuilder.setParent(parent);
573 if (parent.equals(this)) {
574 addedUsesNodes.add(usesBuilder);
576 if (!(parent instanceof DataNodeContainerBuilder)) {
577 throw new YangParseException(name, line, "Unresolved parent of uses '" + groupingPathStr + "'.");
579 ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder);
581 if (parent instanceof AugmentationSchemaBuilder) {
582 usesBuilder.setAugmenting(true);
585 allUsesNodes.add(usesBuilder);
589 public void addRefine(final RefineHolderImpl refine) {
590 final Builder parent = getActualNode();
591 if (!(parent instanceof UsesNodeBuilder)) {
592 throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
594 ((UsesNodeBuilder) parent).addRefine(refine);
595 refine.setParent(parent);
598 public RpcDefinitionBuilder addRpc(final int line, final QName qname, final SchemaPath path) {
599 Builder parent = getActualNode();
600 if (!(parent.equals(this))) {
601 throw new YangParseException(name, line, "rpc can be defined only in module or submodule");
604 final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(name, line, qname, path);
605 rpcBuilder.setParent(parent);
607 String rpcName = qname.getLocalName();
608 for (RpcDefinitionBuilder rpc : addedRpcs) {
609 if (rpc.getQName().getLocalName().equals(rpcName)) {
610 raiseYangParserException("", "rpc", rpcName, line, rpc.getLine());
613 for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
614 if (addedChild.getQName().getLocalName().equals(rpcName)) {
615 raiseYangParserException("rpc", "node", rpcName, line, addedChild.getLine());
618 for (NotificationBuilder addedNotification : addedNotifications) {
619 if (addedNotification.getQName().getLocalName().equals(rpcName)) {
620 raiseYangParserException("rpc", "notification", rpcName, line, addedNotification.getLine());
623 addedRpcs.add(rpcBuilder);
627 public ContainerSchemaNodeBuilder addRpcInput(final int line, final QName qname, final SchemaPath schemaPath) {
628 final Builder parent = getActualNode();
629 if (!(parent instanceof RpcDefinitionBuilder)) {
630 throw new YangParseException(name, line, "input can be defined only in rpc statement");
632 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
634 final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
635 inputBuilder.setParent(rpc);
637 rpc.setInput(inputBuilder);
641 public ContainerSchemaNodeBuilder addRpcOutput(final SchemaPath schemaPath, final QName qname, final int line) {
642 final Builder parent = getActualNode();
643 if (!(parent instanceof RpcDefinitionBuilder)) {
644 throw new YangParseException(name, line, "output can be defined only in rpc statement");
646 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
648 final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
649 outputBuilder.setParent(rpc);
651 rpc.setOutput(outputBuilder);
652 return outputBuilder;
655 public void addNotification(final NotificationDefinition notification) {
656 notifications.add(notification);
659 public NotificationBuilder addNotification(final int line, final QName qname, final SchemaPath path) {
660 final Builder parent = getActualNode();
661 if (!(parent.equals(this))) {
662 throw new YangParseException(name, line, "notification can be defined only in module or submodule");
665 String notificationName = qname.getLocalName();
666 for (NotificationBuilder nb : addedNotifications) {
667 if (nb.getQName().equals(qname)) {
668 raiseYangParserException("", "notification", notificationName, line, nb.getLine());
671 for (RpcDefinitionBuilder rpc : addedRpcs) {
672 if (rpc.getQName().getLocalName().equals(notificationName)) {
673 raiseYangParserException("notification", "rpc", notificationName, line, rpc.getLine());
676 for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
677 if (addedChild.getQName().getLocalName().equals(notificationName)) {
678 raiseYangParserException("notification", "node", notificationName, line, addedChild.getLine());
682 final NotificationBuilder builder = new NotificationBuilder(name, line, qname, path);
683 builder.setParent(parent);
684 addedNotifications.add(builder);
689 public FeatureBuilder addFeature(final int line, final QName qname, final SchemaPath path) {
690 Builder parent = getActualNode();
691 if (!(parent.equals(this))) {
692 throw new YangParseException(name, line, "feature can be defined only in module or submodule");
695 final FeatureBuilder builder = new FeatureBuilder(name, line, qname, path);
696 builder.setParent(parent);
698 String featureName = qname.getLocalName();
699 for (FeatureBuilder addedFeature : addedFeatures) {
700 if (addedFeature.getQName().getLocalName().equals(featureName)) {
701 raiseYangParserException("", "feature", featureName, line, addedFeature.getLine());
704 addedFeatures.add(builder);
708 public ChoiceBuilder addChoice(final int line, final QName qname, final SchemaPath path) {
709 final ChoiceBuilder builder = new ChoiceBuilder(name, line, qname, path);
711 Builder parent = getActualNode();
712 builder.setParent(parent);
713 addChildToParent(parent, builder, qname.getLocalName());
718 public ChoiceCaseBuilder addCase(final int line, final QName qname, final SchemaPath path) {
719 Builder parent = getActualNode();
720 if (parent == null || parent.equals(this)) {
721 throw new YangParseException(name, line, "'case' parent not found");
724 final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(name, line, qname, path);
725 builder.setParent(parent);
727 if (parent instanceof ChoiceBuilder) {
728 ((ChoiceBuilder) parent).addCase(builder);
729 } else if (parent instanceof AugmentationSchemaBuilder) {
730 ((AugmentationSchemaBuilder) parent).addChildNode(builder);
732 throw new YangParseException(name, line, "Unresolved parent of 'case' " + qname.getLocalName());
738 public AnyXmlBuilder addAnyXml(final int line, final QName qname, final SchemaPath schemaPath) {
739 final AnyXmlBuilder builder = new AnyXmlBuilder(name, line, qname, schemaPath);
741 Builder parent = getActualNode();
742 builder.setParent(parent);
743 addChildToParent(parent, builder, qname.getLocalName());
749 public void addTypedef(final TypeDefinitionBuilder typedefBuilder) {
750 String nodeName = typedefBuilder.getQName().getLocalName();
751 for (TypeDefinitionBuilder tdb : addedTypedefs) {
752 if (tdb.getQName().getLocalName().equals(nodeName)) {
753 raiseYangParserException("", "typedef", nodeName, typedefBuilder.getLine(), tdb.getLine());
756 addedTypedefs.add(typedefBuilder);
759 public TypeDefinitionBuilderImpl addTypedef(final int line, final QName qname, final SchemaPath path) {
760 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(name, line, qname, path);
762 Builder parent = getActualNode();
763 builder.setParent(parent);
765 String typedefName = qname.getLocalName();
766 if (parent.equals(this)) {
767 for (TypeDefinitionBuilder tdb : addedTypedefs) {
768 if (tdb.getQName().getLocalName().equals(typedefName)) {
769 raiseYangParserException("", "typedef", typedefName, line, tdb.getLine());
772 addedTypedefs.add(builder);
774 if (parent instanceof DataNodeContainerBuilder) {
775 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
776 for (TypeDefinitionBuilder child : parentNode.getTypeDefinitionBuilders()) {
777 if (child.getQName().getLocalName().equals(typedefName)) {
778 raiseYangParserException("", "typedef", typedefName, line, child.getLine());
781 parentNode.addTypedef(builder);
782 } else if (parent instanceof RpcDefinitionBuilder) {
783 RpcDefinitionBuilder rpcParent = (RpcDefinitionBuilder) parent;
784 for (TypeDefinitionBuilder tdb : rpcParent.getTypeDefinitions()) {
785 if (tdb.getQName().getLocalName().equals(builder.getQName().getLocalName())) {
786 raiseYangParserException("", "typedef", typedefName, line, tdb.getLine());
789 rpcParent.addTypedef(builder);
791 throw new YangParseException(name, line, "Unresolved parent of typedef " + typedefName);
798 public void setType(final TypeDefinition<?> type) {
799 Builder parent = getActualNode();
800 if (!(parent instanceof TypeAwareBuilder)) {
801 throw new YangParseException("Failed to set type '" + type.getQName().getLocalName()
802 + "'. Invalid parent node: " + parent);
804 ((TypeAwareBuilder) parent).setType(type);
807 public UnionTypeBuilder addUnionType(final int line, final URI namespace, final Date revision) {
808 final Builder parent = getActualNode();
809 if (parent == null) {
810 throw new YangParseException(name, line, "Unresolved parent of union type");
812 final UnionTypeBuilder union = new UnionTypeBuilder(name, line);
813 if (parent instanceof TypeAwareBuilder) {
814 ((TypeAwareBuilder) parent).setTypedef(union);
817 throw new YangParseException(name, line, "Invalid parent of union type.");
822 public void addIdentityrefType(final int line, final SchemaPath schemaPath, final String baseString) {
823 final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(name, line, baseString, schemaPath);
825 final Builder parent = getActualNode();
826 if (parent == null) {
827 throw new YangParseException(name, line, "Unresolved parent of identityref type.");
829 if (parent instanceof TypeAwareBuilder) {
830 final TypeAwareBuilder typeParent = (TypeAwareBuilder) parent;
831 typeParent.setTypedef(identityref);
832 dirtyNodes.add(typeParent);
834 throw new YangParseException(name, line, "Invalid parent of identityref type.");
839 public DeviationBuilder addDeviation(final int line, final String targetPath) {
840 Builder parent = getActualNode();
841 if (!(parent.equals(this))) {
842 throw new YangParseException(name, line, "deviation can be defined only in module or submodule");
845 final DeviationBuilder builder = new DeviationBuilder(name, line, targetPath);
846 builder.setParent(parent);
847 deviationBuilders.add(builder);
851 public IdentitySchemaNodeBuilder addIdentity(final QName qname, final int line, final SchemaPath path) {
852 Builder parent = getActualNode();
853 if (!(parent.equals(this))) {
854 throw new YangParseException(name, line, "identity can be defined only in module or submodule");
856 String identityName = qname.getLocalName();
857 for (IdentitySchemaNodeBuilder idBuilder : addedIdentities) {
858 if (idBuilder.getQName().equals(qname)) {
859 raiseYangParserException("", "identity", identityName, line, idBuilder.getLine());
863 final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(name, line, qname, path);
864 builder.setParent(parent);
865 addedIdentities.add(builder);
870 public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder builder) {
871 addedUnknownNodes.add(builder);
872 allUnknownNodes.add(builder);
875 public UnknownSchemaNodeBuilderImpl addUnknownSchemaNode(final int line, final QName qname, final SchemaPath path) {
876 final Builder parent = getActualNode();
877 final UnknownSchemaNodeBuilderImpl builder = new UnknownSchemaNodeBuilderImpl(name, line, qname, path);
878 builder.setParent(parent);
879 allUnknownNodes.add(builder);
881 if (parent.equals(this)) {
882 addedUnknownNodes.add(builder);
884 if (parent instanceof SchemaNodeBuilder) {
885 ((SchemaNodeBuilder) parent).addUnknownNodeBuilder(builder);
886 } else if (parent instanceof DataNodeContainerBuilder) {
887 ((DataNodeContainerBuilder) parent).addUnknownNodeBuilder(builder);
888 } else if (parent instanceof RefineHolderImpl) {
889 ((RefineHolderImpl) parent).addUnknownNodeBuilder(builder);
891 throw new YangParseException(name, line, "Unresolved parent of unknown node '" + qname.getLocalName()
899 public Set<RpcDefinition> getRpcs() {
903 public Set<RpcDefinitionBuilder> getAddedRpcs() {
907 public Set<NotificationDefinition> getNotifications() {
908 return notifications;
911 public Set<NotificationBuilder> getAddedNotifications() {
912 return addedNotifications;
916 public String toString() {
917 return "module " + name;
920 public void setSource(final String source) {
921 this.source = source;
924 public static final class ModuleImpl implements Module {
925 private URI namespace;
926 private final String name;
927 private final String sourcePath;
928 private Date revision;
929 private String prefix;
930 private String yangVersion;
931 private String description;
932 private String reference;
933 private String organization;
934 private String contact;
935 private final Set<ModuleImport> imports = new HashSet<>();
936 private final Set<FeatureDefinition> features = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
937 private final Set<TypeDefinition<?>> typeDefinitions = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
938 private final Set<NotificationDefinition> notifications = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
939 private final Set<AugmentationSchema> augmentations = new HashSet<>();
940 private final Set<RpcDefinition> rpcs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
941 private final Set<Deviation> deviations = new HashSet<>();
942 private final Set<DataSchemaNode> childNodes = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
943 private final Set<GroupingDefinition> groupings = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
944 private final Set<UsesNode> uses = new HashSet<>();
945 private final List<ExtensionDefinition> extensionNodes = new ArrayList<>();
946 private final Set<IdentitySchemaNode> identities = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
947 private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
948 private String source;
950 private ModuleImpl(final String name, final String sourcePath) {
952 this.sourcePath = sourcePath;
956 public String getModuleSourcePath() {
961 public URI getNamespace() {
965 private void setNamespace(final URI namespace) {
966 this.namespace = namespace;
970 public String getName() {
975 public Date getRevision() {
979 private void setRevision(final Date revision) {
980 this.revision = revision;
984 public String getPrefix() {
988 private void setPrefix(final String prefix) {
989 this.prefix = prefix;
993 public String getYangVersion() {
997 private void setYangVersion(final String yangVersion) {
998 this.yangVersion = yangVersion;
1002 public String getDescription() {
1006 private void setDescription(final String description) {
1007 this.description = description;
1011 public String getReference() {
1015 private void setReference(final String reference) {
1016 this.reference = reference;
1020 public String getOrganization() {
1021 return organization;
1024 private void setOrganization(final String organization) {
1025 this.organization = organization;
1029 public String getContact() {
1033 private void setContact(final String contact) {
1034 this.contact = contact;
1038 public Set<ModuleImport> getImports() {
1042 private void setImports(final Set<ModuleImport> imports) {
1043 if (imports != null) {
1044 this.imports.addAll(imports);
1049 public Set<FeatureDefinition> getFeatures() {
1053 private void setFeatures(final Set<FeatureDefinition> features) {
1054 if (features != null) {
1055 this.features.addAll(features);
1060 public Set<TypeDefinition<?>> getTypeDefinitions() {
1061 return typeDefinitions;
1064 private void setTypeDefinitions(final Set<TypeDefinition<?>> typeDefinitions) {
1065 if (typeDefinitions != null) {
1066 this.typeDefinitions.addAll(typeDefinitions);
1071 public Set<NotificationDefinition> getNotifications() {
1072 return notifications;
1075 private void setNotifications(final Set<NotificationDefinition> notifications) {
1076 if (notifications != null) {
1077 this.notifications.addAll(notifications);
1082 public Set<AugmentationSchema> getAugmentations() {
1083 return augmentations;
1086 private void setAugmentations(final Set<AugmentationSchema> augmentations) {
1087 if (augmentations != null) {
1088 this.augmentations.addAll(augmentations);
1093 public Set<RpcDefinition> getRpcs() {
1097 private void setRpcs(final Set<RpcDefinition> rpcs) {
1099 this.rpcs.addAll(rpcs);
1104 public Set<Deviation> getDeviations() {
1108 private void setDeviations(final Set<Deviation> deviations) {
1109 if (deviations != null) {
1110 this.deviations.addAll(deviations);
1115 public Set<DataSchemaNode> getChildNodes() {
1116 return Collections.unmodifiableSet(childNodes);
1119 private void addChildNodes(final Collection<DataSchemaNode> childNodes) {
1120 if (childNodes != null) {
1121 this.childNodes.addAll(childNodes);
1126 public Set<GroupingDefinition> getGroupings() {
1130 private void setGroupings(final Set<GroupingDefinition> groupings) {
1131 if (groupings != null) {
1132 this.groupings.addAll(groupings);
1137 public Set<UsesNode> getUses() {
1141 private void setUses(final Set<UsesNode> uses) {
1143 this.uses.addAll(uses);
1148 public List<ExtensionDefinition> getExtensionSchemaNodes() {
1149 Collections.sort(extensionNodes, Comparators.SCHEMA_NODE_COMP);
1150 return extensionNodes;
1153 private void setExtensionSchemaNodes(final List<ExtensionDefinition> extensionNodes) {
1154 if (extensionNodes != null) {
1155 this.extensionNodes.addAll(extensionNodes);
1160 public Set<IdentitySchemaNode> getIdentities() {
1164 private void setIdentities(final Set<IdentitySchemaNode> identities) {
1165 if (identities != null) {
1166 this.identities.addAll(identities);
1171 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
1172 return unknownNodes;
1175 private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
1176 if (unknownNodes != null) {
1177 this.unknownNodes.addAll(unknownNodes);
1182 public DataSchemaNode getDataChildByName(final QName name) {
1183 return getChildNode(childNodes, name);
1187 public DataSchemaNode getDataChildByName(final String name) {
1188 return getChildNode(childNodes, name);
1191 void setSource(final String source) {
1192 this.source = source;
1195 public String getSource() {
1200 public int hashCode() {
1201 final int prime = 31;
1203 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1204 result = prime * result + ((name == null) ? 0 : name.hashCode());
1205 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1206 result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());
1211 public boolean equals(final Object obj) {
1218 if (getClass() != obj.getClass()) {
1221 ModuleImpl other = (ModuleImpl) obj;
1222 if (namespace == null) {
1223 if (other.namespace != null) {
1226 } else if (!namespace.equals(other.namespace)) {
1230 if (other.name != null) {
1233 } else if (!name.equals(other.name)) {
1236 if (revision == null) {
1237 if (other.revision != null) {
1240 } else if (!revision.equals(other.revision)) {
1243 if (yangVersion == null) {
1244 if (other.yangVersion != null) {
1247 } else if (!yangVersion.equals(other.yangVersion)) {
1254 public String toString() {
1255 StringBuilder sb = new StringBuilder(ModuleImpl.class.getSimpleName());
1257 sb.append("name=" + name);
1258 sb.append(", namespace=" + namespace);
1259 sb.append(", revision=" + revision);
1260 sb.append(", prefix=" + prefix);
1261 sb.append(", yangVersion=" + yangVersion);
1263 return sb.toString();
1268 * Add child to parent. Method checks for duplicates and add given child
1269 * node to parent. If node with same name is found, throws exception. If
1270 * parent is null, child node will be added directly to module.
1276 private void addChildToParent(final Builder parent, final DataSchemaNodeBuilder child, final String childName) {
1277 final int lineNum = child.getLine();
1278 if (parent.equals(this)) {
1279 addChildToModule(child, childName, lineNum);
1281 addChildToSubnodeOfModule(parent, child, childName, lineNum);
1286 * Adds child node <code>child</code> to the set of nodes child nodes.
1288 * The method reduces the complexity of the method
1289 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1290 * addChildToParent}.
1293 * data schema node builder for child node
1295 * string with name of child node
1297 * line number in YANG file where is the node with the name equal
1298 * to <code>childName</code> is defined
1300 private void addChildToModule(final DataSchemaNodeBuilder child, final String childName, final int lineNum) {
1301 // if parent == null => node is defined under module
1302 // All leafs, leaf-lists, lists, containers, choices, rpcs,
1303 // notifications, and anyxmls defined within a parent node or at the
1304 // top level of the module or its submodules share the same
1305 // identifier namespace.
1306 for (DataSchemaNodeBuilder childNode : addedChildNodes) {
1307 if (childNode.getQName().getLocalName().equals(childName)) {
1308 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
1311 for (RpcDefinitionBuilder rpc : addedRpcs) {
1312 if (rpc.getQName().getLocalName().equals(childName)) {
1313 raiseYangParserException("'" + child + "'", "rpc", childName, lineNum, rpc.getLine());
1316 for (NotificationBuilder notification : addedNotifications) {
1317 if (notification.getQName().getLocalName().equals(childName)) {
1318 raiseYangParserException("'" + child + "'", "notification", childName, lineNum, notification.getLine());
1321 addedChildNodes.add(child);
1325 * Adds child node <code>child</code> to the group of child nodes of the
1326 * <code>parent</code>
1328 * The method reduces the complexity of the method
1329 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1330 * addChildToParent}. *
1333 * builder of node which is parent for <code>child</code>
1335 * data schema node builder for child node
1337 * string with name of child node
1339 * line number in YANG file where is the node with the name equal
1340 * to <code>childName</code> is defined
1342 private void addChildToSubnodeOfModule(final Builder parent, final DataSchemaNodeBuilder child,
1343 final String childName, final int lineNum) {
1344 // no need for checking rpc and notification because they can be
1345 // defined only under module or submodule
1346 if (parent instanceof DataNodeContainerBuilder) {
1347 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
1348 for (DataSchemaNodeBuilder childNode : parentNode.getChildNodeBuilders()) {
1349 if (childNode.getQName().getLocalName().equals(childName)) {
1350 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
1353 parentNode.addChildNode(child);
1354 } else if (parent instanceof ChoiceBuilder) {
1355 ChoiceBuilder parentNode = (ChoiceBuilder) parent;
1356 for (ChoiceCaseBuilder caseBuilder : parentNode.getCases()) {
1357 if (caseBuilder.getQName().getLocalName().equals(childName)) {
1358 raiseYangParserException("'" + child + "'", "node", childName, lineNum, caseBuilder.getLine());
1361 parentNode.addCase(child);
1363 throw new YangParseException(name, lineNum, "Unresolved parent of node '" + childName + "'.");
1367 private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) {
1368 final ModuleImport moduleImport = new ModuleImportImpl(moduleName, revision, prefix);
1369 return moduleImport;
1372 private void raiseYangParserException(final String cantAddType, final String type, final String name,
1373 final int currentLine, final int duplicateLine) {
1375 StringBuilder msgPrefix = new StringBuilder("");
1376 if (cantAddType != null && !cantAddType.isEmpty()) {
1377 msgPrefix.append("Can not add ");
1378 msgPrefix.append(cantAddType);
1379 msgPrefix.append(": ");
1382 String msg = String.format("%s%s with same name '%s' already declared at line %d.", msgPrefix, type, name,
1384 throw new YangParseException(getModuleName(), currentLine, msg);
1388 public int hashCode() {
1389 final int prime = 31;
1391 result = prime * result + ((name == null) ? 0 : name.hashCode());
1392 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1393 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1394 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1400 public boolean equals(final Object obj) {
1407 if (getClass() != obj.getClass()) {
1410 ModuleBuilder other = (ModuleBuilder) obj;
1412 if (other.name != null) {
1415 } else if (!name.equals(other.name)) {
1418 if (namespace == null) {
1419 if (other.namespace != null) {
1422 } else if (!namespace.equals(other.namespace)) {
1425 if (prefix == null) {
1426 if (other.prefix != null) {
1429 } else if (!prefix.equals(other.prefix)) {
1432 if (revision == null) {
1433 if (other.revision != null) {
1436 } else if (!revision.equals(other.revision)) {