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.Deviation;
25 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
26 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
27 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
30 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
31 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
33 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
35 import org.opendaylight.yangtools.yang.model.util.ModuleImportImpl;
36 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
37 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
38 import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder;
39 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
40 import org.opendaylight.yangtools.yang.parser.builder.api.DocumentedNodeBuilder;
41 import org.opendaylight.yangtools.yang.parser.builder.api.ExtensionBuilder;
42 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
43 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
44 import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
45 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
46 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
47 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
48 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
49 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
50 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
53 * Builder of Module object. If this module is dependent on external
54 * module/modules, these dependencies must be resolved before module is built,
55 * otherwise result may not be valid.
57 public class ModuleBuilder extends AbstractDocumentedDataNodeContainerBuilder implements DocumentedNodeBuilder {
59 private ModuleImpl instance;
60 private final String name;
61 private final String sourcePath;
62 private static final SchemaPath SCHEMA_PATH = SchemaPath.create(Collections.<QName> emptyList(), true);
63 private URI namespace;
64 private String prefix;
65 private Date revision;
67 private final boolean submodule;
68 private String belongsTo;
69 private ModuleBuilder parent;
71 private final Deque<Builder> actualPath = new LinkedList<>();
72 private final Set<TypeAwareBuilder> dirtyNodes = new HashSet<>();
74 final Set<ModuleImport> imports = new HashSet<ModuleImport>();
76 private final Set<AugmentationSchema> augments = new LinkedHashSet<>();
77 private final List<AugmentationSchemaBuilder> augmentBuilders = new ArrayList<>();
78 private final List<AugmentationSchemaBuilder> allAugments = new ArrayList<>();
80 private final List<GroupingBuilder> allGroupings = new ArrayList<>();
82 private final List<UsesNodeBuilder> allUsesNodes = new ArrayList<>();
84 private final Set<RpcDefinition> rpcs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
85 private final Set<RpcDefinitionBuilder> addedRpcs = new HashSet<>();
87 private final Set<NotificationDefinition> notifications = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
88 private final Set<NotificationBuilder> addedNotifications = new HashSet<>();
90 private final Set<IdentitySchemaNode> identities = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
91 private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<>();
93 private final Set<FeatureDefinition> features = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
94 private final Set<FeatureBuilder> addedFeatures = new HashSet<>();
96 private final Set<Deviation> deviations = new HashSet<>();
97 private final Set<DeviationBuilder> deviationBuilders = new HashSet<>();
99 private final List<ExtensionDefinition> extensions = new ArrayList<>();
100 private final List<ExtensionBuilder> addedExtensions = new ArrayList<>();
102 private final List<UnknownSchemaNodeBuilder> allUnknownNodes = new ArrayList<>();
104 private final List<ListSchemaNodeBuilder> allLists = new ArrayList<>();
107 private String yangVersion;
108 private String organization;
109 private String contact;
111 public ModuleBuilder(final String name, final String sourcePath) {
112 this(name, false, sourcePath);
115 public ModuleBuilder(final String name, final boolean submodule, final String sourcePath) {
116 super(name, 0, null);
118 this.sourcePath = sourcePath;
119 this.submodule = submodule;
120 actualPath.push(this);
123 public ModuleBuilder(final Module base) {
124 super(base.getName(), 0, new QName(base.getNamespace(), base.getRevision(), base.getPrefix(), base.getName()),
126 this.name = base.getName();
127 this.sourcePath = base.getModuleSourcePath();
130 yangVersion = base.getYangVersion();
131 actualPath.push(this);
132 namespace = base.getNamespace();
133 prefix = base.getPrefix();
134 revision = base.getRevision();
136 augments.addAll(base.getAugmentations());
137 rpcs.addAll(base.getRpcs());
138 notifications.addAll(base.getNotifications());
140 for (IdentitySchemaNode identityNode : base.getIdentities()) {
141 addedIdentities.add(new IdentitySchemaNodeBuilder(name, identityNode));
144 features.addAll(base.getFeatures());
145 deviations.addAll(base.getDeviations());
146 extensions.addAll(base.getExtensionSchemaNodes());
147 unknownNodes.addAll(base.getUnknownSchemaNodes());
151 protected String getStatementName() {
156 * Build new Module object based on this builder.
159 public Module build() {
160 if(instance != null) {
167 for (FeatureBuilder fb : addedFeatures) {
168 features.add(fb.build());
172 for (NotificationBuilder entry : addedNotifications) {
173 notifications.add(entry.build());
177 for (AugmentationSchemaBuilder builder : augmentBuilders) {
178 augments.add(builder.build());
182 for (RpcDefinitionBuilder rpc : addedRpcs) {
183 rpcs.add(rpc.build());
187 for (DeviationBuilder entry : deviationBuilders) {
188 deviations.add(entry.build());
192 for (ExtensionBuilder eb : addedExtensions) {
193 extensions.add(eb.build());
195 Collections.sort(extensions, Comparators.SCHEMA_NODE_COMP);
199 for (IdentitySchemaNodeBuilder id : addedIdentities) {
200 identities.add(id.build());
204 for (UnknownSchemaNodeBuilder unb : addedUnknownNodes) {
205 unknownNodes.add(unb.build());
207 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
209 instance = new ModuleImpl(name, sourcePath, this);
213 public String getModuleSourcePath() {
218 public ModuleBuilder getParent() {
222 public void setParent(final ModuleBuilder parent) {
223 this.parent = parent;
227 public void setParent(final Builder parent) {
228 throw new YangParseException(name, 0, "Can not set parent to module");
232 public SchemaPath getPath() {
236 public void enterNode(final Builder node) {
237 actualPath.push(node);
240 public void exitNode() {
244 public Builder getActualNode() {
245 if (actualPath.isEmpty()) {
248 return actualPath.peekFirst();
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 List<ListSchemaNodeBuilder> getAllLists() {
316 public String getName() {
320 public URI getNamespace() {
324 public void setNamespace(final URI namespace) {
325 this.namespace = namespace;
328 public String getPrefix() {
332 public Date getRevision() {
336 protected Set<ModuleImport> getImports() {
340 protected String getSource() {
344 public boolean isSubmodule() {
348 public String getBelongsTo() {
352 public void setBelongsTo(final String belongsTo) {
353 this.belongsTo = belongsTo;
356 public void markActualNodeDirty() {
357 final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) getActualNode();
358 dirtyNodes.add(nodeBuilder);
361 public void setRevision(final Date revision) {
362 this.revision = revision;
365 public void setPrefix(final String prefix) {
366 this.prefix = prefix;
369 public void setYangVersion(final String yangVersion) {
370 this.yangVersion = yangVersion;
373 public void setOrganization(final String organization) {
374 this.organization = organization;
377 public void setContact(final String contact) {
378 this.contact = contact;
381 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) {
393 Builder parent = getActualNode();
394 if (!(parent.equals(this))) {
395 throw new YangParseException(name, line, "extension can be defined only in module or submodule");
398 final String extName = qname.getLocalName();
399 for (ExtensionBuilder addedExtension : addedExtensions) {
400 if (addedExtension.getQName().getLocalName().equals(extName)) {
401 raiseYangParserException("extension", "node", extName, line, addedExtension.getLine());
404 final ExtensionBuilder builder = new ExtensionBuilderImpl(name, line, qname, path);
405 builder.setParent(parent);
406 addedExtensions.add(builder);
410 public ContainerSchemaNodeBuilder addContainerNode(final int line, final QName qname, final SchemaPath schemaPath) {
412 final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
414 Builder parent = getActualNode();
415 builder.setParent(parent);
416 addChildToParent(parent, builder, qname.getLocalName());
421 public ListSchemaNodeBuilder addListNode(final int line, final QName qname, final SchemaPath schemaPath) {
423 final ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(name, line, qname, schemaPath);
425 Builder parent = getActualNode();
426 builder.setParent(parent);
427 addChildToParent(parent, builder, qname.getLocalName());
428 allLists.add(builder);
433 public LeafSchemaNodeBuilder addLeafNode(final int line, final QName qname, final SchemaPath schemaPath) {
435 final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(name, line, qname, schemaPath);
437 Builder parent = getActualNode();
438 builder.setParent(parent);
439 addChildToParent(parent, builder, qname.getLocalName());
444 public LeafListSchemaNodeBuilder addLeafListNode(final int line, final QName qname, final SchemaPath schemaPath) {
446 final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(name, line, qname, schemaPath);
448 Builder parent = getActualNode();
449 builder.setParent(parent);
450 addChildToParent(parent, builder, qname.getLocalName());
455 public GroupingBuilder addGrouping(final int line, final QName qname, final SchemaPath path) {
457 final GroupingBuilder builder = new GroupingBuilderImpl(name, line, qname, path);
459 Builder parent = getActualNode();
460 builder.setParent(parent);
462 String groupingName = qname.getLocalName();
463 if (parent.equals(this)) {
464 for (GroupingBuilder addedGrouping : getGroupingBuilders()) {
465 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
466 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
469 addGrouping(builder);
471 if (parent instanceof DataNodeContainerBuilder) {
472 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
473 for (GroupingBuilder addedGrouping : parentNode.getGroupingBuilders()) {
474 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
475 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
478 parentNode.addGrouping(builder);
479 } else if (parent instanceof RpcDefinitionBuilder) {
480 RpcDefinitionBuilder parentNode = (RpcDefinitionBuilder) parent;
481 for (GroupingBuilder child : parentNode.getGroupings()) {
482 if (child.getQName().getLocalName().equals(groupingName)) {
483 raiseYangParserException("", "Grouping", groupingName, line, child.getLine());
486 parentNode.addGrouping(builder);
488 throw new YangParseException(name, line, "Unresolved parent of grouping " + groupingName);
492 allGroupings.add(builder);
496 public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr, final int order) {
498 final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr, order);
500 Builder parent = getActualNode();
501 builder.setParent(parent);
503 if (parent.equals(this)) {
504 // augment can be declared only under 'module' ...
505 if (!(augmentTargetStr.startsWith("/"))) {
506 throw new YangParseException(
509 "If the 'augment' statement is on the top level in a module, the absolute form of a schema node identifier MUST be used.");
511 augmentBuilders.add(builder);
513 // ... or 'uses' statement
514 if (parent instanceof UsesNodeBuilder) {
515 if (augmentTargetStr.startsWith("/")) {
516 throw new YangParseException(name, line,
517 "If 'augment' statement is a substatement to the 'uses' statement, it cannot contain absolute path ("
518 + augmentTargetStr + ")");
520 ((UsesNodeBuilder) parent).addAugment(builder);
522 throw new YangParseException(name, line, "Augment can be declared only under module or uses statement.");
525 allAugments.add(builder);
530 public UsesNodeBuilder addUsesNode(final int line, final String groupingPathStr) {
532 final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(name, line, groupingPathStr);
534 Builder parent = getActualNode();
535 usesBuilder.setParent(parent);
537 if (parent.equals(this)) {
538 addUsesNode(usesBuilder);
540 if (!(parent instanceof DataNodeContainerBuilder)) {
541 throw new YangParseException(name, line, "Unresolved parent of uses '" + groupingPathStr + "'.");
543 ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder);
545 if (parent instanceof AugmentationSchemaBuilder) {
546 usesBuilder.setAugmenting(true);
549 allUsesNodes.add(usesBuilder);
553 public void addRefine(final RefineHolderImpl refine) {
555 final Builder parent = getActualNode();
556 if (!(parent instanceof UsesNodeBuilder)) {
557 throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
559 ((UsesNodeBuilder) parent).addRefine(refine);
560 refine.setParent(parent);
563 public RpcDefinitionBuilder addRpc(final int line, final QName qname, final SchemaPath path) {
565 Builder parent = getActualNode();
566 if (!(parent.equals(this))) {
567 throw new YangParseException(name, line, "rpc can be defined only in module or submodule");
570 final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(name, line, qname, path);
571 rpcBuilder.setParent(parent);
573 String rpcName = qname.getLocalName();
574 checkNotConflictingInDataNamespace(rpcName, line);
575 addedRpcs.add(rpcBuilder);
579 private void checkNotConflictingInDataNamespace(final String rpcName, final int line) {
580 for (DataSchemaNodeBuilder addedChild : getChildNodeBuilders()) {
581 if (addedChild.getQName().getLocalName().equals(rpcName)) {
582 raiseYangParserException("rpc", "node", rpcName, line, addedChild.getLine());
585 for (RpcDefinitionBuilder rpc : addedRpcs) {
586 if (rpc.getQName().getLocalName().equals(rpcName)) {
587 raiseYangParserException("", "rpc", rpcName, line, rpc.getLine());
590 for (NotificationBuilder addedNotification : addedNotifications) {
591 if (addedNotification.getQName().getLocalName().equals(rpcName)) {
592 raiseYangParserException("rpc", "notification", rpcName, line, addedNotification.getLine());
597 public ContainerSchemaNodeBuilder addRpcInput(final int line, final QName qname, final SchemaPath schemaPath) {
599 final Builder parent = getActualNode();
600 if (!(parent instanceof RpcDefinitionBuilder)) {
601 throw new YangParseException(name, line, "input can be defined only in rpc statement");
603 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
605 final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
606 inputBuilder.setParent(rpc);
608 rpc.setInput(inputBuilder);
612 public ContainerSchemaNodeBuilder addRpcOutput(final SchemaPath schemaPath, final QName qname, final int line) {
614 final Builder parent = getActualNode();
615 if (!(parent instanceof RpcDefinitionBuilder)) {
616 throw new YangParseException(name, line, "output can be defined only in rpc statement");
618 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
620 final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
621 outputBuilder.setParent(rpc);
623 rpc.setOutput(outputBuilder);
624 return outputBuilder;
627 public void addNotification(final NotificationDefinition notification) {
629 notifications.add(notification);
632 public NotificationBuilder addNotification(final int line, final QName qname, final SchemaPath path) {
634 final Builder parent = getActualNode();
635 if (!(parent.equals(this))) {
636 throw new YangParseException(name, line, "notification can be defined only in module or submodule");
639 String notificationName = qname.getLocalName();
640 checkNotConflictingInDataNamespace(notificationName, line);
642 final NotificationBuilder builder = new NotificationBuilder(name, line, qname, path);
643 builder.setParent(parent);
644 addedNotifications.add(builder);
649 public FeatureBuilder addFeature(final int line, final QName qname, final SchemaPath path) {
650 Builder parent = getActualNode();
651 if (!(parent.equals(this))) {
652 throw new YangParseException(name, line, "feature can be defined only in module or submodule");
655 final FeatureBuilder builder = new FeatureBuilder(name, line, qname, path);
656 builder.setParent(parent);
658 String featureName = qname.getLocalName();
659 for (FeatureBuilder addedFeature : addedFeatures) {
660 if (addedFeature.getQName().getLocalName().equals(featureName)) {
661 raiseYangParserException("", "feature", featureName, line, addedFeature.getLine());
664 addedFeatures.add(builder);
668 public ChoiceBuilder addChoice(final int line, final QName qname, final SchemaPath path) {
669 final ChoiceBuilder builder = new ChoiceBuilder(name, line, qname, path);
671 Builder parent = getActualNode();
672 builder.setParent(parent);
673 addChildToParent(parent, builder, qname.getLocalName());
678 public ChoiceCaseBuilder addCase(final int line, final QName qname, final SchemaPath path) {
679 Builder parent = getActualNode();
680 if (parent == null || parent.equals(this)) {
681 throw new YangParseException(name, line, "'case' parent not found");
684 final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(name, line, qname, path);
685 builder.setParent(parent);
687 if (parent instanceof ChoiceBuilder) {
688 ((ChoiceBuilder) parent).addCase(builder);
689 } else if (parent instanceof AugmentationSchemaBuilder) {
690 ((AugmentationSchemaBuilder) parent).addChildNode(builder);
692 throw new YangParseException(name, line, "Unresolved parent of 'case' " + qname.getLocalName());
698 public AnyXmlBuilder addAnyXml(final int line, final QName qname, final SchemaPath schemaPath) {
699 final AnyXmlBuilder builder = new AnyXmlBuilder(name, line, qname, schemaPath);
701 Builder parent = getActualNode();
702 builder.setParent(parent);
703 addChildToParent(parent, builder, qname.getLocalName());
709 public void addTypedef(final TypeDefinitionBuilder typedefBuilder) {
710 String nodeName = typedefBuilder.getQName().getLocalName();
711 for (TypeDefinitionBuilder tdb : getTypeDefinitionBuilders()) {
712 if (tdb.getQName().getLocalName().equals(nodeName)) {
713 raiseYangParserException("", "typedef", nodeName, typedefBuilder.getLine(), tdb.getLine());
716 super.addTypedef(typedefBuilder);
719 public TypeDefinitionBuilderImpl addTypedef(final int line, final QName qname, final SchemaPath path) {
720 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(name, line, qname, path);
722 Builder parent = getActualNode();
723 builder.setParent(parent);
725 String typedefName = qname.getLocalName();
726 if (parent.equals(this)) {
729 if (parent instanceof DataNodeContainerBuilder) {
730 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
731 for (TypeDefinitionBuilder child : parentNode.getTypeDefinitionBuilders()) {
732 if (child.getQName().getLocalName().equals(typedefName)) {
733 raiseYangParserException("", "typedef", typedefName, line, child.getLine());
736 parentNode.addTypedef(builder);
737 } else if (parent instanceof RpcDefinitionBuilder) {
738 RpcDefinitionBuilder rpcParent = (RpcDefinitionBuilder) parent;
739 for (TypeDefinitionBuilder tdb : rpcParent.getTypeDefinitions()) {
740 if (tdb.getQName().getLocalName().equals(builder.getQName().getLocalName())) {
741 raiseYangParserException("", "typedef", typedefName, line, tdb.getLine());
744 rpcParent.addTypedef(builder);
746 throw new YangParseException(name, line, "Unresolved parent of typedef " + typedefName);
753 public void setType(final TypeDefinition<?> type) {
754 Builder parent = getActualNode();
755 if (!(parent instanceof TypeAwareBuilder)) {
756 throw new YangParseException("Failed to set type '" + type.getQName().getLocalName()
757 + "'. Invalid parent node: " + parent);
759 ((TypeAwareBuilder) parent).setType(type);
762 public UnionTypeBuilder addUnionType(final int line, final URI namespace, final Date revision) {
763 final Builder parent = getActualNode();
764 if (parent == null) {
765 throw new YangParseException(name, line, "Unresolved parent of union type");
767 final UnionTypeBuilder union = new UnionTypeBuilder(name, line);
768 if (parent instanceof TypeAwareBuilder) {
769 ((TypeAwareBuilder) parent).setTypedef(union);
772 throw new YangParseException(name, line, "Invalid parent of union type.");
777 public void addIdentityrefType(final int line, final SchemaPath schemaPath, final String baseString) {
778 final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(name, line, baseString, schemaPath);
780 final Builder parent = getActualNode();
781 if (parent == null) {
782 throw new YangParseException(name, line, "Unresolved parent of identityref type.");
784 if (parent instanceof TypeAwareBuilder) {
785 final TypeAwareBuilder typeParent = (TypeAwareBuilder) parent;
786 typeParent.setTypedef(identityref);
787 dirtyNodes.add(typeParent);
789 throw new YangParseException(name, line, "Invalid parent of identityref type.");
794 public DeviationBuilder addDeviation(final int line, final String targetPath) {
795 Builder parent = getActualNode();
796 if (!(parent.equals(this))) {
797 throw new YangParseException(name, line, "deviation can be defined only in module or submodule");
800 final DeviationBuilder builder = new DeviationBuilder(name, line, targetPath);
801 builder.setParent(parent);
802 deviationBuilders.add(builder);
806 public IdentitySchemaNodeBuilder addIdentity(final QName qname, final int line, final SchemaPath path) {
807 Builder parent = getActualNode();
808 if (!(parent.equals(this))) {
809 throw new YangParseException(name, line, "identity can be defined only in module or submodule");
811 String identityName = qname.getLocalName();
812 for (IdentitySchemaNodeBuilder idBuilder : addedIdentities) {
813 if (idBuilder.getQName().equals(qname)) {
814 raiseYangParserException("", "identity", identityName, line, idBuilder.getLine());
818 final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(name, line, qname, path);
819 builder.setParent(parent);
820 addedIdentities.add(builder);
825 public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder builder) {
826 addedUnknownNodes.add(builder);
827 allUnknownNodes.add(builder);
830 public UnknownSchemaNodeBuilderImpl addUnknownSchemaNode(final int line, final QName qname, final SchemaPath path) {
831 final Builder parent = getActualNode();
832 final UnknownSchemaNodeBuilderImpl builder = new UnknownSchemaNodeBuilderImpl(name, line, qname, path);
833 builder.setParent(parent);
834 allUnknownNodes.add(builder);
836 if (parent.equals(this)) {
837 addedUnknownNodes.add(builder);
839 if (parent instanceof SchemaNodeBuilder) {
840 ((SchemaNodeBuilder) parent).addUnknownNodeBuilder(builder);
841 } else if (parent instanceof DataNodeContainerBuilder) {
842 ((DataNodeContainerBuilder) parent).addUnknownNodeBuilder(builder);
843 } else if (parent instanceof RefineHolderImpl) {
844 ((RefineHolderImpl) parent).addUnknownNodeBuilder(builder);
846 throw new YangParseException(name, line, "Unresolved parent of unknown node '" + qname.getLocalName()
854 public Set<RpcDefinition> getRpcs() {
858 public Set<RpcDefinitionBuilder> getAddedRpcs() {
862 public Set<NotificationDefinition> getNotifications() {
863 return notifications;
866 public Set<NotificationBuilder> getAddedNotifications() {
867 return addedNotifications;
871 public String toString() {
872 return "module " + name;
875 public void setSource(final String source) {
876 this.source = source;
880 * Add child to parent. Method checks for duplicates and add given child
881 * node to parent. If node with same name is found, throws exception. If
882 * parent is null, child node will be added directly to module.
888 private void addChildToParent(final Builder parent, final DataSchemaNodeBuilder child, final String childName) {
889 final int lineNum = child.getLine();
890 if (parent.equals(this)) {
891 addChildToModule(child, childName, lineNum);
893 addChildToSubnodeOfModule(parent, child, childName, lineNum);
897 public String getYangVersion() {
901 public String getContact() {
905 public String getOrganization() {
910 * Adds child node <code>child</code> to the set of nodes child nodes.
912 * The method reduces the complexity of the method
913 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
917 * data schema node builder for child node
919 * string with name of child node
921 * line number in YANG file where is the node with the name equal
922 * to <code>childName</code> is defined
924 private void addChildToModule(final DataSchemaNodeBuilder child, final String childName, final int lineNum) {
925 // if parent == null => node is defined under module
926 // All leafs, leaf-lists, lists, containers, choices, rpcs,
927 // notifications, and anyxmls defined within a parent node or at the
928 // top level of the module or its submodules share the same
929 // identifier namespace.
930 for (DataSchemaNodeBuilder childNode : getChildNodeBuilders()) {
931 if (childNode.getQName().getLocalName().equals(childName)) {
932 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
935 for (RpcDefinitionBuilder rpc : addedRpcs) {
936 if (rpc.getQName().getLocalName().equals(childName)) {
937 raiseYangParserException("'" + child + "'", "rpc", childName, lineNum, rpc.getLine());
940 for (NotificationBuilder notification : addedNotifications) {
941 if (notification.getQName().getLocalName().equals(childName)) {
942 raiseYangParserException("'" + child + "'", "notification", childName, lineNum, notification.getLine());
949 * Adds child node <code>child</code> to the group of child nodes of the
950 * <code>parent</code>
952 * The method reduces the complexity of the method
953 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
954 * addChildToParent}. *
957 * builder of node which is parent for <code>child</code>
959 * data schema node builder for child node
961 * string with name of child node
963 * line number in YANG file where is the node with the name equal
964 * to <code>childName</code> is defined
966 private void addChildToSubnodeOfModule(final Builder parent, final DataSchemaNodeBuilder child,
967 final String childName, final int lineNum) {
968 // no need for checking rpc and notification because they can be
969 // defined only under module or submodule
970 if (parent instanceof DataNodeContainerBuilder) {
971 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
972 for (DataSchemaNodeBuilder childNode : parentNode.getChildNodeBuilders()) {
973 if (childNode.getQName().getLocalName().equals(childName)) {
974 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
977 parentNode.addChildNode(child);
978 } else if (parent instanceof ChoiceBuilder) {
979 ChoiceBuilder parentNode = (ChoiceBuilder) parent;
980 for (ChoiceCaseBuilder caseBuilder : parentNode.getCases()) {
981 if (caseBuilder.getQName().getLocalName().equals(childName)) {
982 raiseYangParserException("'" + child + "'", "node", childName, lineNum, caseBuilder.getLine());
985 parentNode.addCase(child);
987 throw new YangParseException(name, lineNum, "Unresolved parent of node '" + childName + "'.");
991 private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) {
992 final ModuleImport moduleImport = new ModuleImportImpl(moduleName, revision, prefix);
996 private void raiseYangParserException(final String cantAddType, final String type, final String name,
997 final int currentLine, final int duplicateLine) {
999 StringBuilder msgPrefix = new StringBuilder("");
1000 if (cantAddType != null && !cantAddType.isEmpty()) {
1001 msgPrefix.append("Can not add ");
1002 msgPrefix.append(cantAddType);
1003 msgPrefix.append(": ");
1006 String msg = String.format("%s%s with same name '%s' already declared at line %d.", msgPrefix, type, name,
1008 throw new YangParseException(getModuleName(), currentLine, msg);
1012 public int hashCode() {
1013 final int prime = 31;
1015 result = prime * result + ((name == null) ? 0 : name.hashCode());
1016 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1017 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1018 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1024 public boolean equals(final Object obj) {
1031 if (getClass() != obj.getClass()) {
1034 ModuleBuilder other = (ModuleBuilder) obj;
1036 if (other.name != null) {
1039 } else if (!name.equals(other.name)) {
1042 if (namespace == null) {
1043 if (other.namespace != null) {
1046 } else if (!namespace.equals(other.namespace)) {
1049 if (prefix == null) {
1050 if (other.prefix != null) {
1053 } else if (!prefix.equals(other.prefix)) {
1056 if (revision == null) {
1057 if (other.revision != null) {
1060 } else if (!revision.equals(other.revision)) {
1066 public List<UnknownSchemaNode> getExtensionInstances() {
1067 return unknownNodes;