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;
9 import com.google.common.base.Charsets;
10 import com.google.common.base.Preconditions;
11 import com.google.common.io.ByteSource;
12 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.Date;
17 import java.util.Deque;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.LinkedHashSet;
21 import java.util.LinkedList;
22 import java.util.List;
25 import java.util.TreeSet;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.QNameModule;
28 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
29 import org.opendaylight.yangtools.yang.model.api.Deviation;
30 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
31 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
32 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
35 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
36 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
38 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
40 import org.opendaylight.yangtools.yang.model.util.ModuleImportImpl;
41 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
42 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
43 import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder;
44 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
45 import org.opendaylight.yangtools.yang.parser.builder.api.DocumentedNodeBuilder;
46 import org.opendaylight.yangtools.yang.parser.builder.api.ExtensionBuilder;
47 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
48 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
49 import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
50 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
51 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
52 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
53 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
54 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
55 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
58 * Builder of Module object. If this module is dependent on external
59 * module/modules, these dependencies must be resolved before module is built,
60 * otherwise result may not be valid.
62 public class ModuleBuilder extends AbstractDocumentedDataNodeContainerBuilder implements DocumentedNodeBuilder {
64 private ModuleImpl instance;
65 private final String name;
66 private final String sourcePath;
67 private static final SchemaPath SCHEMA_PATH = SchemaPath.create(Collections.<QName> emptyList(), true);
68 private String prefix;
69 private QNameModule qnameModule = QNameModule.create(null, null);
71 private final boolean submodule;
72 private String belongsTo;
73 private ModuleBuilder parent;
75 private final Deque<Builder> actualPath = new LinkedList<>();
76 private final Set<TypeAwareBuilder> dirtyNodes = new HashSet<>();
78 final Map<String, ModuleImport> imports = new HashMap<>();
79 final Map<String, ModuleBuilder> importedModules = new HashMap<>();
81 final Set<ModuleBuilder> addedSubmodules = new HashSet<>();
82 final Set<Module> submodules = new HashSet<>();
83 final Map<String, Date> includedModules = new HashMap<>();
85 private final Set<AugmentationSchema> augments = new LinkedHashSet<>();
86 private final List<AugmentationSchemaBuilder> augmentBuilders = new ArrayList<>();
87 private final List<AugmentationSchemaBuilder> allAugments = new ArrayList<>();
89 private final List<GroupingBuilder> allGroupings = new ArrayList<>();
91 private final List<UsesNodeBuilder> allUsesNodes = new ArrayList<>();
93 private final Set<RpcDefinition> rpcs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
94 private final Set<RpcDefinitionBuilder> addedRpcs = new HashSet<>();
96 private final Set<NotificationDefinition> notifications = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
97 private final Set<NotificationBuilder> addedNotifications = new HashSet<>();
99 private final Set<IdentitySchemaNode> identities = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
100 private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<>();
102 private final Set<FeatureDefinition> features = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
103 private final Set<FeatureBuilder> addedFeatures = new HashSet<>();
105 private final Set<Deviation> deviations = new HashSet<>();
106 private final Set<DeviationBuilder> deviationBuilders = new HashSet<>();
108 private final List<ExtensionDefinition> extensions = new ArrayList<>();
109 private final List<ExtensionBuilder> addedExtensions = new ArrayList<>();
111 private final List<UnknownSchemaNodeBuilder> allUnknownNodes = new ArrayList<>();
113 private final List<ListSchemaNodeBuilder> allLists = new ArrayList<>();
115 private String source;
116 private String yangVersion;
117 private String organization;
118 private String contact;
120 public ModuleBuilder(final String name, final String sourcePath) {
121 this(name, false, sourcePath);
124 public ModuleBuilder(final String name, final boolean submodule, final String sourcePath) {
125 super(name, 0, null);
127 this.sourcePath = sourcePath;
128 this.submodule = submodule;
129 actualPath.push(this);//FIXME: this escapes constructor
132 public ModuleBuilder(final Module base) {
133 super(base.getName(), 0, QName.create(base.getQNameModule(), base.getName()),
135 this.name = base.getName();
136 this.sourcePath = base.getModuleSourcePath();
139 yangVersion = base.getYangVersion();
140 actualPath.push(this);//FIXME: this escapes constructor
141 prefix = base.getPrefix();
142 qnameModule = base.getQNameModule();
144 augments.addAll(base.getAugmentations());
145 rpcs.addAll(base.getRpcs());
146 notifications.addAll(base.getNotifications());
148 for (IdentitySchemaNode identityNode : base.getIdentities()) {
149 addedIdentities.add(new IdentitySchemaNodeBuilder(name, identityNode));
152 features.addAll(base.getFeatures());
153 deviations.addAll(base.getDeviations());
154 extensions.addAll(base.getExtensionSchemaNodes());
155 unknownNodes.addAll(base.getUnknownSchemaNodes());
156 source = base.getSource();
160 protected String getStatementName() {
165 * Build new Module object based on this builder.
168 public Module build() {
169 if(instance != null) {
176 for (ModuleBuilder submodule : addedSubmodules) {
177 submodules.add(submodule.build());
181 for (FeatureBuilder fb : addedFeatures) {
182 features.add(fb.build());
186 for (NotificationBuilder entry : addedNotifications) {
187 notifications.add(entry.build());
191 for (AugmentationSchemaBuilder builder : augmentBuilders) {
192 augments.add(builder.build());
196 for (RpcDefinitionBuilder rpc : addedRpcs) {
197 rpcs.add(rpc.build());
201 for (DeviationBuilder entry : deviationBuilders) {
202 deviations.add(entry.build());
206 for (ExtensionBuilder eb : addedExtensions) {
207 extensions.add(eb.build());
209 Collections.sort(extensions, Comparators.SCHEMA_NODE_COMP);
213 for (IdentitySchemaNodeBuilder id : addedIdentities) {
214 identities.add(id.build());
218 for (UnknownSchemaNodeBuilder unb : addedUnknownNodes) {
219 unknownNodes.add(unb.build());
221 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
223 instance = new ModuleImpl(name, sourcePath, this);
227 public String getModuleSourcePath() {
232 public ModuleBuilder getParent() {
236 public void setParent(final ModuleBuilder parent) {
237 this.parent = parent;
241 public void setParent(final Builder parent) {
242 throw new YangParseException(name, 0, "Can not set parent to module");
246 public SchemaPath getPath() {
250 public void enterNode(final Builder node) {
251 actualPath.push(node);
254 public void exitNode() {
258 public Builder getActualNode() {
259 if (actualPath.isEmpty()) {
262 return actualPath.peekFirst();
266 public Set<TypeAwareBuilder> getDirtyNodes() {
270 public Set<AugmentationSchema> getAugments() {
274 public List<AugmentationSchemaBuilder> getAugmentBuilders() {
275 return augmentBuilders;
278 public List<AugmentationSchemaBuilder> getAllAugments() {
282 public Set<IdentitySchemaNode> getIdentities() {
286 public Set<IdentitySchemaNodeBuilder> getAddedIdentities() {
287 return addedIdentities;
290 public Set<FeatureDefinition> getFeatures() {
294 public Set<FeatureBuilder> getAddedFeatures() {
295 return addedFeatures;
298 public List<GroupingBuilder> getAllGroupings() {
302 public List<UsesNodeBuilder> getAllUsesNodes() {
306 public Set<Deviation> getDeviations() {
310 public Set<DeviationBuilder> getDeviationBuilders() {
311 return deviationBuilders;
314 public List<ExtensionDefinition> getExtensions() {
318 public List<ExtensionBuilder> getAddedExtensions() {
319 return addedExtensions;
322 public List<UnknownSchemaNodeBuilder> getAllUnknownNodes() {
323 return allUnknownNodes;
326 public List<ListSchemaNodeBuilder> getAllLists() {
330 public String getName() {
334 public URI getNamespace() {
335 return qnameModule.getNamespace();
338 public QNameModule getQNameModule() {
342 public void setQNameModule(final QNameModule qnameModule) {
343 this.qnameModule = Preconditions.checkNotNull(qnameModule);
346 public void setNamespace(final URI namespace) {
347 this.qnameModule = QNameModule.create(namespace, qnameModule.getRevision());
350 public String getPrefix() {
354 public Date getRevision() {
355 return qnameModule.getRevision();
358 public ModuleImport getImport(final String prefix) {
359 return imports.get(prefix);
362 public Map<String, ModuleImport> getImports() {
366 public ModuleBuilder getImportedModule(final String prefix) {
367 return importedModules.get(prefix);
370 public void addImportedModule(final String prefix, final ModuleBuilder module) {
372 importedModules.put(prefix, module);
375 public Map<String, Date> getIncludedModules() {
376 return includedModules;
379 public void addInclude(final String name, final Date revision) {
380 includedModules.put(name, revision);
383 public void addSubmodule(final ModuleBuilder submodule) {
384 addedSubmodules.add(submodule);
387 protected String getSource() {
391 public boolean isSubmodule() {
395 public String getBelongsTo() {
399 public void setBelongsTo(final String belongsTo) {
400 this.belongsTo = belongsTo;
403 public void markActualNodeDirty() {
404 final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) getActualNode();
405 dirtyNodes.add(nodeBuilder);
408 public void setRevision(final Date revision) {
409 this.qnameModule = QNameModule.create(qnameModule.getNamespace(), revision);
412 public void setPrefix(final String prefix) {
413 this.prefix = prefix;
416 public void setYangVersion(final String yangVersion) {
417 this.yangVersion = yangVersion;
420 public void setOrganization(final String organization) {
421 this.organization = organization;
424 public void setContact(final String contact) {
425 this.contact = contact;
428 public void addModuleImport(final String moduleName, final Date revision, final String prefix) {
431 final ModuleImport moduleImport = createModuleImport(moduleName, revision, prefix);
432 imports.put(prefix, moduleImport);
435 private void checkPrefix(final String prefix) {
436 if (prefix == null || prefix.isEmpty()) {
437 throw new IllegalArgumentException("Cannot add imported module with undefined prefix");
439 if (prefix.equals(this.prefix)) {
440 throw new IllegalArgumentException("Cannot add imported module with prefix equals to module prefix");
444 public ExtensionBuilder addExtension(final QName qname, final int line, final SchemaPath path) {
446 Builder parent = getActualNode();
447 if (!(parent.equals(this))) {
448 throw new YangParseException(name, line, "extension can be defined only in module or submodule");
451 final String extName = qname.getLocalName();
452 for (ExtensionBuilder addedExtension : addedExtensions) {
453 if (addedExtension.getQName().getLocalName().equals(extName)) {
454 raiseYangParserException("extension", "node", extName, line, addedExtension.getLine());
457 final ExtensionBuilder builder = new ExtensionBuilderImpl(name, line, qname, path);
458 builder.setParent(parent);
459 addedExtensions.add(builder);
463 public ContainerSchemaNodeBuilder addContainerNode(final int line, final QName qname, final SchemaPath schemaPath) {
465 final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
467 Builder parent = getActualNode();
468 builder.setParent(parent);
469 addChildToParent(parent, builder, qname.getLocalName());
474 public ListSchemaNodeBuilder addListNode(final int line, final QName qname, final SchemaPath schemaPath) {
476 final ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(name, line, qname, schemaPath);
478 Builder parent = getActualNode();
479 builder.setParent(parent);
480 addChildToParent(parent, builder, qname.getLocalName());
481 allLists.add(builder);
486 public LeafSchemaNodeBuilder addLeafNode(final int line, final QName qname, final SchemaPath schemaPath) {
488 final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(name, line, qname, schemaPath);
490 Builder parent = getActualNode();
491 builder.setParent(parent);
492 addChildToParent(parent, builder, qname.getLocalName());
497 public LeafListSchemaNodeBuilder addLeafListNode(final int line, final QName qname, final SchemaPath schemaPath) {
499 final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(name, line, qname, schemaPath);
501 Builder parent = getActualNode();
502 builder.setParent(parent);
503 addChildToParent(parent, builder, qname.getLocalName());
508 public GroupingBuilder addGrouping(final int line, final QName qname, final SchemaPath path) {
510 final GroupingBuilder builder = new GroupingBuilderImpl(name, line, qname, path);
512 Builder parent = getActualNode();
513 builder.setParent(parent);
515 String groupingName = qname.getLocalName();
516 if (parent.equals(this)) {
517 for (GroupingBuilder addedGrouping : getGroupingBuilders()) {
518 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
519 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
522 addGrouping(builder);
524 if (parent instanceof DataNodeContainerBuilder) {
525 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
526 for (GroupingBuilder addedGrouping : parentNode.getGroupingBuilders()) {
527 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
528 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
531 parentNode.addGrouping(builder);
532 } else if (parent instanceof RpcDefinitionBuilder) {
533 RpcDefinitionBuilder parentNode = (RpcDefinitionBuilder) parent;
534 for (GroupingBuilder child : parentNode.getGroupings()) {
535 if (child.getQName().getLocalName().equals(groupingName)) {
536 raiseYangParserException("", "Grouping", groupingName, line, child.getLine());
539 parentNode.addGrouping(builder);
541 throw new YangParseException(name, line, "Unresolved parent of grouping " + groupingName);
545 allGroupings.add(builder);
549 public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr,
550 final SchemaPath targetPath, final int order) {
552 final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr,
555 Builder parent = getActualNode();
556 builder.setParent(parent);
558 if (parent.equals(this)) {
559 // augment can be declared only under 'module' ...
560 if (!(augmentTargetStr.startsWith("/"))) {
561 throw new YangParseException(
564 "If the 'augment' statement is on the top level in a module, the absolute form of a schema node identifier MUST be used.");
566 augmentBuilders.add(builder);
568 // ... or 'uses' statement
569 if (parent instanceof UsesNodeBuilder) {
570 if (augmentTargetStr.startsWith("/")) {
571 throw new YangParseException(name, line,
572 "If 'augment' statement is a substatement to the 'uses' statement, it cannot contain absolute path ("
573 + augmentTargetStr + ")");
575 ((UsesNodeBuilder) parent).addAugment(builder);
577 throw new YangParseException(name, line, "Augment can be declared only under module or uses statement.");
580 allAugments.add(builder);
585 public UsesNodeBuilder addUsesNode(final int line, final SchemaPath grouping) {
587 final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(name, line, grouping);
589 Builder parent = getActualNode();
590 usesBuilder.setParent(parent);
592 if (parent.equals(this)) {
593 addUsesNode(usesBuilder);
595 if (!(parent instanceof DataNodeContainerBuilder)) {
596 throw new YangParseException(name, line, "Unresolved parent of uses '" + grouping + "'.");
598 ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder);
600 if (parent instanceof AugmentationSchemaBuilder) {
601 usesBuilder.setAugmenting(true);
604 allUsesNodes.add(usesBuilder);
608 public void addRefine(final RefineHolderImpl refine) {
610 final Builder parent = getActualNode();
611 if (!(parent instanceof UsesNodeBuilder)) {
612 throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
614 ((UsesNodeBuilder) parent).addRefine(refine);
615 refine.setParent(parent);
618 public RpcDefinitionBuilder addRpc(final int line, final QName qname, final SchemaPath path) {
620 Builder parent = getActualNode();
621 if (!(parent.equals(this))) {
622 throw new YangParseException(name, line, "rpc can be defined only in module or submodule");
625 final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(name, line, qname, path);
626 rpcBuilder.setParent(parent);
628 String rpcName = qname.getLocalName();
629 checkNotConflictingInDataNamespace(rpcName, line);
630 addedRpcs.add(rpcBuilder);
634 private void checkNotConflictingInDataNamespace(final String rpcName, final int line) {
635 for (DataSchemaNodeBuilder addedChild : getChildNodeBuilders()) {
636 if (addedChild.getQName().getLocalName().equals(rpcName)) {
637 raiseYangParserException("rpc", "node", rpcName, line, addedChild.getLine());
640 for (RpcDefinitionBuilder rpc : addedRpcs) {
641 if (rpc.getQName().getLocalName().equals(rpcName)) {
642 raiseYangParserException("", "rpc", rpcName, line, rpc.getLine());
645 for (NotificationBuilder addedNotification : addedNotifications) {
646 if (addedNotification.getQName().getLocalName().equals(rpcName)) {
647 raiseYangParserException("rpc", "notification", rpcName, line, addedNotification.getLine());
652 public ContainerSchemaNodeBuilder addRpcInput(final int line, final QName qname, final SchemaPath schemaPath) {
654 final Builder parent = getActualNode();
655 if (!(parent instanceof RpcDefinitionBuilder)) {
656 throw new YangParseException(name, line, "input can be defined only in rpc statement");
658 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
660 final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
661 inputBuilder.setParent(rpc);
663 rpc.setInput(inputBuilder);
667 public ContainerSchemaNodeBuilder addRpcOutput(final SchemaPath schemaPath, final QName qname, final int line) {
669 final Builder parent = getActualNode();
670 if (!(parent instanceof RpcDefinitionBuilder)) {
671 throw new YangParseException(name, line, "output can be defined only in rpc statement");
673 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
675 final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
676 outputBuilder.setParent(rpc);
678 rpc.setOutput(outputBuilder);
679 return outputBuilder;
682 public void addNotification(final NotificationDefinition notification) {
684 notifications.add(notification);
687 public NotificationBuilder addNotification(final int line, final QName qname, final SchemaPath path) {
689 final Builder parent = getActualNode();
690 if (!(parent.equals(this))) {
691 throw new YangParseException(name, line, "notification can be defined only in module or submodule");
694 String notificationName = qname.getLocalName();
695 checkNotConflictingInDataNamespace(notificationName, line);
697 final NotificationBuilder builder = new NotificationBuilder(name, line, qname, path);
698 builder.setParent(parent);
699 addedNotifications.add(builder);
704 public FeatureBuilder addFeature(final int line, final QName qname, final SchemaPath path) {
705 Builder parent = getActualNode();
706 if (!(parent.equals(this))) {
707 throw new YangParseException(name, line, "feature can be defined only in module or submodule");
710 final FeatureBuilder builder = new FeatureBuilder(name, line, qname, path);
711 builder.setParent(parent);
713 String featureName = qname.getLocalName();
714 for (FeatureBuilder addedFeature : addedFeatures) {
715 if (addedFeature.getQName().getLocalName().equals(featureName)) {
716 raiseYangParserException("", "feature", featureName, line, addedFeature.getLine());
719 addedFeatures.add(builder);
723 public ChoiceBuilder addChoice(final int line, final QName qname, final SchemaPath path) {
724 final ChoiceBuilder builder = new ChoiceBuilder(name, line, qname, path);
726 Builder parent = getActualNode();
727 builder.setParent(parent);
728 addChildToParent(parent, builder, qname.getLocalName());
733 public ChoiceCaseBuilder addCase(final int line, final QName qname, final SchemaPath path) {
734 Builder parent = getActualNode();
735 if (parent == null || parent.equals(this)) {
736 throw new YangParseException(name, line, "'case' parent not found");
739 final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(name, line, qname, path);
740 builder.setParent(parent);
742 if (parent instanceof ChoiceBuilder) {
743 ((ChoiceBuilder) parent).addCase(builder);
744 } else if (parent instanceof AugmentationSchemaBuilder) {
745 ((AugmentationSchemaBuilder) parent).addChildNode(builder);
747 throw new YangParseException(name, line, "Unresolved parent of 'case' " + qname.getLocalName());
753 public AnyXmlBuilder addAnyXml(final int line, final QName qname, final SchemaPath schemaPath) {
754 final AnyXmlBuilder builder = new AnyXmlBuilder(name, line, qname, schemaPath);
756 Builder parent = getActualNode();
757 builder.setParent(parent);
758 addChildToParent(parent, builder, qname.getLocalName());
764 public void addTypedef(final TypeDefinitionBuilder typedefBuilder) {
765 String nodeName = typedefBuilder.getQName().getLocalName();
766 for (TypeDefinitionBuilder tdb : getTypeDefinitionBuilders()) {
767 if (tdb.getQName().getLocalName().equals(nodeName)) {
768 raiseYangParserException("", "typedef", nodeName, typedefBuilder.getLine(), tdb.getLine());
771 super.addTypedef(typedefBuilder);
774 public TypeDefinitionBuilderImpl addTypedef(final int line, final QName qname, final SchemaPath path) {
775 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(name, line, qname, path);
777 Builder parent = getActualNode();
778 builder.setParent(parent);
780 String typedefName = qname.getLocalName();
781 if (parent.equals(this)) {
784 if (parent instanceof DataNodeContainerBuilder) {
785 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
786 for (TypeDefinitionBuilder child : parentNode.getTypeDefinitionBuilders()) {
787 if (child.getQName().getLocalName().equals(typedefName)) {
788 raiseYangParserException("", "typedef", typedefName, line, child.getLine());
791 parentNode.addTypedef(builder);
792 } else if (parent instanceof RpcDefinitionBuilder) {
793 RpcDefinitionBuilder rpcParent = (RpcDefinitionBuilder) parent;
794 for (TypeDefinitionBuilder tdb : rpcParent.getTypeDefinitions()) {
795 if (tdb.getQName().getLocalName().equals(builder.getQName().getLocalName())) {
796 raiseYangParserException("", "typedef", typedefName, line, tdb.getLine());
799 rpcParent.addTypedef(builder);
801 throw new YangParseException(name, line, "Unresolved parent of typedef " + typedefName);
808 public void setType(final TypeDefinition<?> type) {
809 Builder parent = getActualNode();
810 if (!(parent instanceof TypeAwareBuilder)) {
811 throw new YangParseException("Failed to set type '" + type.getQName().getLocalName()
812 + "'. Invalid parent node: " + parent);
814 ((TypeAwareBuilder) parent).setType(type);
817 public UnionTypeBuilder addUnionType(final int line, final QNameModule module) {
818 final Builder parent = getActualNode();
819 if (parent == null) {
820 throw new YangParseException(name, line, "Unresolved parent of union type");
822 final UnionTypeBuilder union = new UnionTypeBuilder(name, line);
823 if (parent instanceof TypeAwareBuilder) {
824 ((TypeAwareBuilder) parent).setTypedef(union);
827 throw new YangParseException(name, line, "Invalid parent of union type.");
832 public void addIdentityrefType(final int line, final SchemaPath schemaPath, final String baseString) {
833 final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(name, line, baseString, schemaPath);
835 final Builder parent = getActualNode();
836 if (parent == null) {
837 throw new YangParseException(name, line, "Unresolved parent of identityref type.");
839 if (parent instanceof TypeAwareBuilder) {
840 final TypeAwareBuilder typeParent = (TypeAwareBuilder) parent;
841 typeParent.setTypedef(identityref);
842 dirtyNodes.add(typeParent);
844 throw new YangParseException(name, line, "Invalid parent of identityref type.");
849 public DeviationBuilder addDeviation(final int line, final SchemaPath targetPath) {
850 Builder parent = getActualNode();
851 if (!(parent.equals(this))) {
852 throw new YangParseException(name, line, "deviation can be defined only in module or submodule");
855 final DeviationBuilder builder = new DeviationBuilder(name, line, targetPath);
856 builder.setParent(parent);
857 deviationBuilders.add(builder);
861 public IdentitySchemaNodeBuilder addIdentity(final QName qname, final int line, final SchemaPath path) {
862 Builder parent = getActualNode();
863 if (!(parent.equals(this))) {
864 throw new YangParseException(name, line, "identity can be defined only in module or submodule");
866 String identityName = qname.getLocalName();
867 for (IdentitySchemaNodeBuilder idBuilder : addedIdentities) {
868 if (idBuilder.getQName().equals(qname)) {
869 raiseYangParserException("", "identity", identityName, line, idBuilder.getLine());
873 final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(name, line, qname, path);
874 builder.setParent(parent);
875 addedIdentities.add(builder);
880 public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder builder) {
881 addedUnknownNodes.add(builder);
882 allUnknownNodes.add(builder);
885 public UnknownSchemaNodeBuilderImpl addUnknownSchemaNode(final int line, final QName qname, final SchemaPath path) {
886 final Builder parent = getActualNode();
887 final UnknownSchemaNodeBuilderImpl builder = new UnknownSchemaNodeBuilderImpl(name, line, qname, path);
888 builder.setParent(parent);
889 allUnknownNodes.add(builder);
891 if (parent.equals(this)) {
892 addedUnknownNodes.add(builder);
894 if (parent instanceof SchemaNodeBuilder) {
895 parent.addUnknownNodeBuilder(builder);
896 } else if (parent instanceof DataNodeContainerBuilder) {
897 parent.addUnknownNodeBuilder(builder);
898 } else if (parent instanceof RefineHolderImpl) {
899 parent.addUnknownNodeBuilder(builder);
901 throw new YangParseException(name, line, "Unresolved parent of unknown node '" + qname.getLocalName()
909 public Set<RpcDefinition> getRpcs() {
913 public Set<RpcDefinitionBuilder> getAddedRpcs() {
917 public Set<NotificationDefinition> getNotifications() {
918 return notifications;
921 public Set<NotificationBuilder> getAddedNotifications() {
922 return addedNotifications;
926 public String toString() {
927 return "module " + name;
930 public void setSource(final ByteSource byteSource) throws IOException {
931 setSource(byteSource.asCharSource(Charsets.UTF_8).read());
934 public void setSource(final String source) {
935 this.source = source;
939 * Add child to parent. Method checks for duplicates and add given child
940 * node to parent. If node with same name is found, throws exception. If
941 * parent is null, child node will be added directly to module.
947 private void addChildToParent(final Builder parent, final DataSchemaNodeBuilder child, final String childName) {
948 final int lineNum = child.getLine();
949 if (parent.equals(this)) {
950 addChildToModule(child, childName, lineNum);
952 addChildToSubnodeOfModule(parent, child, childName, lineNum);
956 public String getYangVersion() {
960 public String getContact() {
964 public String getOrganization() {
969 * Adds child node <code>child</code> to the set of nodes child nodes.
971 * The method reduces the complexity of the method
972 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
976 * data schema node builder for child node
978 * string with name of child node
980 * line number in YANG file where is the node with the name equal
981 * to <code>childName</code> is defined
983 private void addChildToModule(final DataSchemaNodeBuilder child, final String childName, final int lineNum) {
984 // if parent == null => node is defined under module
985 // All leafs, leaf-lists, lists, containers, choices, rpcs,
986 // notifications, and anyxmls defined within a parent node or at the
987 // top level of the module or its submodules share the same
988 // identifier namespace.
989 for (DataSchemaNodeBuilder childNode : getChildNodeBuilders()) {
990 if (childNode.getQName().getLocalName().equals(childName)) {
991 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
994 for (RpcDefinitionBuilder rpc : addedRpcs) {
995 if (rpc.getQName().getLocalName().equals(childName)) {
996 raiseYangParserException("'" + child + "'", "rpc", childName, lineNum, rpc.getLine());
999 for (NotificationBuilder notification : addedNotifications) {
1000 if (notification.getQName().getLocalName().equals(childName)) {
1001 raiseYangParserException("'" + child + "'", "notification", childName, lineNum, notification.getLine());
1004 addChildNode(child);
1008 * Adds child node <code>child</code> to the group of child nodes of the
1009 * <code>parent</code>
1011 * The method reduces the complexity of the method
1012 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1013 * addChildToParent}. *
1016 * builder of node which is parent for <code>child</code>
1018 * data schema node builder for child node
1020 * string with name of child node
1022 * line number in YANG file where is the node with the name equal
1023 * to <code>childName</code> is defined
1025 private void addChildToSubnodeOfModule(final Builder parent, final DataSchemaNodeBuilder child,
1026 final String childName, final int lineNum) {
1027 // no need for checking rpc and notification because they can be
1028 // defined only under module or submodule
1029 if (parent instanceof DataNodeContainerBuilder) {
1030 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
1031 for (DataSchemaNodeBuilder childNode : parentNode.getChildNodeBuilders()) {
1032 if (childNode.getQName().getLocalName().equals(childName)) {
1033 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
1036 parentNode.addChildNode(child);
1037 } else if (parent instanceof ChoiceBuilder) {
1038 ChoiceBuilder parentNode = (ChoiceBuilder) parent;
1039 for (ChoiceCaseBuilder caseBuilder : parentNode.getCases()) {
1040 if (caseBuilder.getQName().getLocalName().equals(childName)) {
1041 raiseYangParserException("'" + child + "'", "node", childName, lineNum, caseBuilder.getLine());
1044 parentNode.addCase(child);
1046 throw new YangParseException(name, lineNum, "Unresolved parent of node '" + childName + "'.");
1050 private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) {
1051 return new ModuleImportImpl(moduleName, revision, prefix);
1054 private void raiseYangParserException(final String cantAddType, final String type, final String name,
1055 final int currentLine, final int duplicateLine) {
1057 StringBuilder msgPrefix = new StringBuilder("");
1058 if (cantAddType != null && !cantAddType.isEmpty()) {
1059 msgPrefix.append("Can not add ");
1060 msgPrefix.append(cantAddType);
1061 msgPrefix.append(": ");
1064 String msg = String.format("%s%s with same name '%s' already declared at line %d.", msgPrefix, type, name,
1066 throw new YangParseException(getModuleName(), currentLine, msg);
1070 public int hashCode() {
1071 final int prime = 31;
1073 result = prime * result + ((name == null) ? 0 : name.hashCode());
1074 result = prime * result + qnameModule.hashCode();
1075 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1081 public boolean equals(final Object obj) {
1088 if (getClass() != obj.getClass()) {
1091 ModuleBuilder other = (ModuleBuilder) obj;
1093 if (other.name != null) {
1096 } else if (!name.equals(other.name)) {
1099 if (!qnameModule.equals(other.qnameModule)) {
1102 if (prefix == null) {
1103 if (other.prefix != null) {
1106 } else if (!prefix.equals(other.prefix)) {
1112 public List<UnknownSchemaNode> getExtensionInstances() {
1113 return unknownNodes;