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.Preconditions;
10 import com.google.common.io.ByteSource;
11 import java.io.IOException;
12 import java.io.InputStream;
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.apache.commons.io.IOUtils;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.QNameModule;
29 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
30 import org.opendaylight.yangtools.yang.model.api.Deviation;
31 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
32 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
33 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.Module;
35 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
36 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
37 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
38 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
39 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
41 import org.opendaylight.yangtools.yang.model.util.ModuleImportImpl;
42 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
43 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
44 import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder;
45 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
46 import org.opendaylight.yangtools.yang.parser.builder.api.DocumentedNodeBuilder;
47 import org.opendaylight.yangtools.yang.parser.builder.api.ExtensionBuilder;
48 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
49 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
50 import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
51 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
52 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
53 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
54 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
55 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
56 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
59 * Builder of Module object. If this module is dependent on external
60 * module/modules, these dependencies must be resolved before module is built,
61 * otherwise result may not be valid.
63 public class ModuleBuilder extends AbstractDocumentedDataNodeContainerBuilder implements DocumentedNodeBuilder {
65 private ModuleImpl instance;
66 private final String name;
67 private final String sourcePath;
68 private static final SchemaPath SCHEMA_PATH = SchemaPath.create(Collections.<QName> emptyList(), true);
69 private String prefix;
70 private QNameModule qnameModule = QNameModule.create(null, null);
72 private final boolean submodule;
73 private String belongsTo;
74 private ModuleBuilder parent;
76 private final Deque<Builder> actualPath = new LinkedList<>();
77 private final Set<TypeAwareBuilder> dirtyNodes = new HashSet<>();
79 final Map<String, ModuleImport> imports = new HashMap<>();
80 final Map<String, ModuleBuilder> importedModules = new HashMap<>();
82 private final Map<String, Date> includedModules = new HashMap<>();
84 private final Set<AugmentationSchema> augments = new LinkedHashSet<>();
85 private final List<AugmentationSchemaBuilder> augmentBuilders = new ArrayList<>();
86 private final List<AugmentationSchemaBuilder> allAugments = new ArrayList<>();
88 private final List<GroupingBuilder> allGroupings = new ArrayList<>();
90 private final List<UsesNodeBuilder> allUsesNodes = new ArrayList<>();
92 private final Set<RpcDefinition> rpcs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
93 private final Set<RpcDefinitionBuilder> addedRpcs = new HashSet<>();
95 private final Set<NotificationDefinition> notifications = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
96 private final Set<NotificationBuilder> addedNotifications = new HashSet<>();
98 private final Set<IdentitySchemaNode> identities = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
99 private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<>();
101 private final Set<FeatureDefinition> features = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
102 private final Set<FeatureBuilder> addedFeatures = new HashSet<>();
104 private final Set<Deviation> deviations = new HashSet<>();
105 private final Set<DeviationBuilder> deviationBuilders = new HashSet<>();
107 private final List<ExtensionDefinition> extensions = new ArrayList<>();
108 private final List<ExtensionBuilder> addedExtensions = new ArrayList<>();
110 private final List<UnknownSchemaNodeBuilder> allUnknownNodes = new ArrayList<>();
112 private final List<ListSchemaNodeBuilder> allLists = new ArrayList<>();
114 private String source;
115 private String yangVersion;
116 private String organization;
117 private String contact;
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 this.submodule = submodule;
128 actualPath.push(this);//FIXME: this escapes constructor
131 public ModuleBuilder(final Module base) {
132 super(base.getName(), 0, QName.create(base.getQNameModule(), base.getPrefix(), base.getName()),
134 this.name = base.getName();
135 this.sourcePath = base.getModuleSourcePath();
138 yangVersion = base.getYangVersion();
139 actualPath.push(this);//FIXME: this escapes constructor
140 prefix = base.getPrefix();
141 qnameModule = base.getQNameModule();
143 augments.addAll(base.getAugmentations());
144 rpcs.addAll(base.getRpcs());
145 notifications.addAll(base.getNotifications());
147 for (IdentitySchemaNode identityNode : base.getIdentities()) {
148 addedIdentities.add(new IdentitySchemaNodeBuilder(name, identityNode));
151 features.addAll(base.getFeatures());
152 deviations.addAll(base.getDeviations());
153 extensions.addAll(base.getExtensionSchemaNodes());
154 unknownNodes.addAll(base.getUnknownSchemaNodes());
155 source = base.getSource();
159 protected String getStatementName() {
164 * Build new Module object based on this builder.
167 public Module build() {
168 if(instance != null) {
175 for (FeatureBuilder fb : addedFeatures) {
176 features.add(fb.build());
180 for (NotificationBuilder entry : addedNotifications) {
181 notifications.add(entry.build());
185 for (AugmentationSchemaBuilder builder : augmentBuilders) {
186 augments.add(builder.build());
190 for (RpcDefinitionBuilder rpc : addedRpcs) {
191 rpcs.add(rpc.build());
195 for (DeviationBuilder entry : deviationBuilders) {
196 deviations.add(entry.build());
200 for (ExtensionBuilder eb : addedExtensions) {
201 extensions.add(eb.build());
203 Collections.sort(extensions, Comparators.SCHEMA_NODE_COMP);
207 for (IdentitySchemaNodeBuilder id : addedIdentities) {
208 identities.add(id.build());
212 for (UnknownSchemaNodeBuilder unb : addedUnknownNodes) {
213 unknownNodes.add(unb.build());
215 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
217 instance = new ModuleImpl(name, sourcePath, this);
221 public String getModuleSourcePath() {
226 public ModuleBuilder getParent() {
230 public void setParent(final ModuleBuilder parent) {
231 this.parent = parent;
235 public void setParent(final Builder parent) {
236 throw new YangParseException(name, 0, "Can not set parent to module");
240 public SchemaPath getPath() {
244 public void enterNode(final Builder node) {
245 actualPath.push(node);
248 public void exitNode() {
252 public Builder getActualNode() {
253 if (actualPath.isEmpty()) {
256 return actualPath.peekFirst();
260 public Set<TypeAwareBuilder> getDirtyNodes() {
264 public Set<AugmentationSchema> getAugments() {
268 public List<AugmentationSchemaBuilder> getAugmentBuilders() {
269 return augmentBuilders;
272 public List<AugmentationSchemaBuilder> getAllAugments() {
276 public Set<IdentitySchemaNode> getIdentities() {
280 public Set<IdentitySchemaNodeBuilder> getAddedIdentities() {
281 return addedIdentities;
284 public Set<FeatureDefinition> getFeatures() {
288 public Set<FeatureBuilder> getAddedFeatures() {
289 return addedFeatures;
292 public List<GroupingBuilder> getAllGroupings() {
296 public List<UsesNodeBuilder> getAllUsesNodes() {
300 public Set<Deviation> getDeviations() {
304 public Set<DeviationBuilder> getDeviationBuilders() {
305 return deviationBuilders;
308 public List<ExtensionDefinition> getExtensions() {
312 public List<ExtensionBuilder> getAddedExtensions() {
313 return addedExtensions;
316 public List<UnknownSchemaNodeBuilder> getAllUnknownNodes() {
317 return allUnknownNodes;
320 public List<ListSchemaNodeBuilder> getAllLists() {
324 public String getName() {
328 public URI getNamespace() {
329 return qnameModule.getNamespace();
332 public QNameModule getQNameModule() {
336 public void setQNameModule(final QNameModule qnameModule) {
337 this.qnameModule = Preconditions.checkNotNull(qnameModule);
340 public void setNamespace(final URI namespace) {
341 this.qnameModule = QNameModule.create(namespace, qnameModule.getRevision());
344 public String getPrefix() {
348 public Date getRevision() {
349 return qnameModule.getRevision();
352 public ModuleImport getImport(final String prefix) {
353 return imports.get(prefix);
356 public Map<String, ModuleImport> getImports() {
360 public ModuleBuilder getImportedModule(final String prefix) {
361 return importedModules.get(prefix);
364 public void addImportedModule(final String prefix, final ModuleBuilder module) {
366 importedModules.put(prefix, module);
369 public Map<String, Date> getIncludedModules() {
370 return includedModules;
373 public void addInclude(final String name, final Date revision) {
374 includedModules.put(name, revision);
377 protected String getSource() {
381 public boolean isSubmodule() {
385 public String getBelongsTo() {
389 public void setBelongsTo(final String belongsTo) {
390 this.belongsTo = belongsTo;
393 public void markActualNodeDirty() {
394 final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) getActualNode();
395 dirtyNodes.add(nodeBuilder);
398 public void setRevision(final Date revision) {
399 this.qnameModule = QNameModule.create(qnameModule.getNamespace(), revision);
402 public void setPrefix(final String prefix) {
403 this.prefix = prefix;
406 public void setYangVersion(final String yangVersion) {
407 this.yangVersion = yangVersion;
410 public void setOrganization(final String organization) {
411 this.organization = organization;
414 public void setContact(final String contact) {
415 this.contact = contact;
418 public void addModuleImport(final String moduleName, final Date revision, final String prefix) {
421 final ModuleImport moduleImport = createModuleImport(moduleName, revision, prefix);
422 imports.put(prefix, moduleImport);
425 private void checkPrefix(final String prefix) {
426 if (prefix == null || prefix.isEmpty()) {
427 throw new IllegalArgumentException("Cannot add imported module with undefined prefix");
429 if (prefix.equals(this.prefix)) {
430 throw new IllegalArgumentException("Cannot add imported module with prefix equals to module prefix");
434 public ExtensionBuilder addExtension(final QName qname, final int line, final SchemaPath path) {
436 Builder parent = getActualNode();
437 if (!(parent.equals(this))) {
438 throw new YangParseException(name, line, "extension can be defined only in module or submodule");
441 final String extName = qname.getLocalName();
442 for (ExtensionBuilder addedExtension : addedExtensions) {
443 if (addedExtension.getQName().getLocalName().equals(extName)) {
444 raiseYangParserException("extension", "node", extName, line, addedExtension.getLine());
447 final ExtensionBuilder builder = new ExtensionBuilderImpl(name, line, qname, path);
448 builder.setParent(parent);
449 addedExtensions.add(builder);
453 public ContainerSchemaNodeBuilder addContainerNode(final int line, final QName qname, final SchemaPath schemaPath) {
455 final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
457 Builder parent = getActualNode();
458 builder.setParent(parent);
459 addChildToParent(parent, builder, qname.getLocalName());
464 public ListSchemaNodeBuilder addListNode(final int line, final QName qname, final SchemaPath schemaPath) {
466 final ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(name, line, qname, schemaPath);
468 Builder parent = getActualNode();
469 builder.setParent(parent);
470 addChildToParent(parent, builder, qname.getLocalName());
471 allLists.add(builder);
476 public LeafSchemaNodeBuilder addLeafNode(final int line, final QName qname, final SchemaPath schemaPath) {
478 final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(name, line, qname, schemaPath);
480 Builder parent = getActualNode();
481 builder.setParent(parent);
482 addChildToParent(parent, builder, qname.getLocalName());
487 public LeafListSchemaNodeBuilder addLeafListNode(final int line, final QName qname, final SchemaPath schemaPath) {
489 final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(name, line, qname, schemaPath);
491 Builder parent = getActualNode();
492 builder.setParent(parent);
493 addChildToParent(parent, builder, qname.getLocalName());
498 public GroupingBuilder addGrouping(final int line, final QName qname, final SchemaPath path) {
500 final GroupingBuilder builder = new GroupingBuilderImpl(name, line, qname, path);
502 Builder parent = getActualNode();
503 builder.setParent(parent);
505 String groupingName = qname.getLocalName();
506 if (parent.equals(this)) {
507 for (GroupingBuilder addedGrouping : getGroupingBuilders()) {
508 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
509 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
512 addGrouping(builder);
514 if (parent instanceof DataNodeContainerBuilder) {
515 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
516 for (GroupingBuilder addedGrouping : parentNode.getGroupingBuilders()) {
517 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
518 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
521 parentNode.addGrouping(builder);
522 } else if (parent instanceof RpcDefinitionBuilder) {
523 RpcDefinitionBuilder parentNode = (RpcDefinitionBuilder) parent;
524 for (GroupingBuilder child : parentNode.getGroupings()) {
525 if (child.getQName().getLocalName().equals(groupingName)) {
526 raiseYangParserException("", "Grouping", groupingName, line, child.getLine());
529 parentNode.addGrouping(builder);
531 throw new YangParseException(name, line, "Unresolved parent of grouping " + groupingName);
535 allGroupings.add(builder);
539 public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr,
540 final SchemaPath targetPath, final int order) {
542 final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr,
545 Builder parent = getActualNode();
546 builder.setParent(parent);
548 if (parent.equals(this)) {
549 // augment can be declared only under 'module' ...
550 if (!(augmentTargetStr.startsWith("/"))) {
551 throw new YangParseException(
554 "If the 'augment' statement is on the top level in a module, the absolute form of a schema node identifier MUST be used.");
556 augmentBuilders.add(builder);
558 // ... or 'uses' statement
559 if (parent instanceof UsesNodeBuilder) {
560 if (augmentTargetStr.startsWith("/")) {
561 throw new YangParseException(name, line,
562 "If 'augment' statement is a substatement to the 'uses' statement, it cannot contain absolute path ("
563 + augmentTargetStr + ")");
565 ((UsesNodeBuilder) parent).addAugment(builder);
567 throw new YangParseException(name, line, "Augment can be declared only under module or uses statement.");
570 allAugments.add(builder);
575 public UsesNodeBuilder addUsesNode(final int line, final SchemaPath grouping) {
577 final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(name, line, grouping);
579 Builder parent = getActualNode();
580 usesBuilder.setParent(parent);
582 if (parent.equals(this)) {
583 addUsesNode(usesBuilder);
585 if (!(parent instanceof DataNodeContainerBuilder)) {
586 throw new YangParseException(name, line, "Unresolved parent of uses '" + grouping + "'.");
588 ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder);
590 if (parent instanceof AugmentationSchemaBuilder) {
591 usesBuilder.setAugmenting(true);
594 allUsesNodes.add(usesBuilder);
598 public void addRefine(final RefineHolderImpl refine) {
600 final Builder parent = getActualNode();
601 if (!(parent instanceof UsesNodeBuilder)) {
602 throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
604 ((UsesNodeBuilder) parent).addRefine(refine);
605 refine.setParent(parent);
608 public RpcDefinitionBuilder addRpc(final int line, final QName qname, final SchemaPath path) {
610 Builder parent = getActualNode();
611 if (!(parent.equals(this))) {
612 throw new YangParseException(name, line, "rpc can be defined only in module or submodule");
615 final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(name, line, qname, path);
616 rpcBuilder.setParent(parent);
618 String rpcName = qname.getLocalName();
619 checkNotConflictingInDataNamespace(rpcName, line);
620 addedRpcs.add(rpcBuilder);
624 private void checkNotConflictingInDataNamespace(final String rpcName, final int line) {
625 for (DataSchemaNodeBuilder addedChild : getChildNodeBuilders()) {
626 if (addedChild.getQName().getLocalName().equals(rpcName)) {
627 raiseYangParserException("rpc", "node", rpcName, line, addedChild.getLine());
630 for (RpcDefinitionBuilder rpc : addedRpcs) {
631 if (rpc.getQName().getLocalName().equals(rpcName)) {
632 raiseYangParserException("", "rpc", rpcName, line, rpc.getLine());
635 for (NotificationBuilder addedNotification : addedNotifications) {
636 if (addedNotification.getQName().getLocalName().equals(rpcName)) {
637 raiseYangParserException("rpc", "notification", rpcName, line, addedNotification.getLine());
642 public ContainerSchemaNodeBuilder addRpcInput(final int line, final QName qname, final SchemaPath schemaPath) {
644 final Builder parent = getActualNode();
645 if (!(parent instanceof RpcDefinitionBuilder)) {
646 throw new YangParseException(name, line, "input can be defined only in rpc statement");
648 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
650 final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
651 inputBuilder.setParent(rpc);
653 rpc.setInput(inputBuilder);
657 public ContainerSchemaNodeBuilder addRpcOutput(final SchemaPath schemaPath, final QName qname, final int line) {
659 final Builder parent = getActualNode();
660 if (!(parent instanceof RpcDefinitionBuilder)) {
661 throw new YangParseException(name, line, "output can be defined only in rpc statement");
663 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
665 final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
666 outputBuilder.setParent(rpc);
668 rpc.setOutput(outputBuilder);
669 return outputBuilder;
672 public void addNotification(final NotificationDefinition notification) {
674 notifications.add(notification);
677 public NotificationBuilder addNotification(final int line, final QName qname, final SchemaPath path) {
679 final Builder parent = getActualNode();
680 if (!(parent.equals(this))) {
681 throw new YangParseException(name, line, "notification can be defined only in module or submodule");
684 String notificationName = qname.getLocalName();
685 checkNotConflictingInDataNamespace(notificationName, line);
687 final NotificationBuilder builder = new NotificationBuilder(name, line, qname, path);
688 builder.setParent(parent);
689 addedNotifications.add(builder);
694 public FeatureBuilder addFeature(final int line, final QName qname, final SchemaPath path) {
695 Builder parent = getActualNode();
696 if (!(parent.equals(this))) {
697 throw new YangParseException(name, line, "feature can be defined only in module or submodule");
700 final FeatureBuilder builder = new FeatureBuilder(name, line, qname, path);
701 builder.setParent(parent);
703 String featureName = qname.getLocalName();
704 for (FeatureBuilder addedFeature : addedFeatures) {
705 if (addedFeature.getQName().getLocalName().equals(featureName)) {
706 raiseYangParserException("", "feature", featureName, line, addedFeature.getLine());
709 addedFeatures.add(builder);
713 public ChoiceBuilder addChoice(final int line, final QName qname, final SchemaPath path) {
714 final ChoiceBuilder builder = new ChoiceBuilder(name, line, qname, path);
716 Builder parent = getActualNode();
717 builder.setParent(parent);
718 addChildToParent(parent, builder, qname.getLocalName());
723 public ChoiceCaseBuilder addCase(final int line, final QName qname, final SchemaPath path) {
724 Builder parent = getActualNode();
725 if (parent == null || parent.equals(this)) {
726 throw new YangParseException(name, line, "'case' parent not found");
729 final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(name, line, qname, path);
730 builder.setParent(parent);
732 if (parent instanceof ChoiceBuilder) {
733 ((ChoiceBuilder) parent).addCase(builder);
734 } else if (parent instanceof AugmentationSchemaBuilder) {
735 ((AugmentationSchemaBuilder) parent).addChildNode(builder);
737 throw new YangParseException(name, line, "Unresolved parent of 'case' " + qname.getLocalName());
743 public AnyXmlBuilder addAnyXml(final int line, final QName qname, final SchemaPath schemaPath) {
744 final AnyXmlBuilder builder = new AnyXmlBuilder(name, line, qname, schemaPath);
746 Builder parent = getActualNode();
747 builder.setParent(parent);
748 addChildToParent(parent, builder, qname.getLocalName());
754 public void addTypedef(final TypeDefinitionBuilder typedefBuilder) {
755 String nodeName = typedefBuilder.getQName().getLocalName();
756 for (TypeDefinitionBuilder tdb : getTypeDefinitionBuilders()) {
757 if (tdb.getQName().getLocalName().equals(nodeName)) {
758 raiseYangParserException("", "typedef", nodeName, typedefBuilder.getLine(), tdb.getLine());
761 super.addTypedef(typedefBuilder);
764 public TypeDefinitionBuilderImpl addTypedef(final int line, final QName qname, final SchemaPath path) {
765 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(name, line, qname, path);
767 Builder parent = getActualNode();
768 builder.setParent(parent);
770 String typedefName = qname.getLocalName();
771 if (parent.equals(this)) {
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 QNameModule module) {
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 SchemaPath 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 parent.addUnknownNodeBuilder(builder);
886 } else if (parent instanceof DataNodeContainerBuilder) {
887 parent.addUnknownNodeBuilder(builder);
888 } else if (parent instanceof RefineHolderImpl) {
889 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 ByteSource byteSource) throws IOException {
921 try (InputStream stream = byteSource.openStream()) {
922 setSource(IOUtils.toString(stream));
926 public void setSource(final String source) {
927 this.source = source;
931 * Add child to parent. Method checks for duplicates and add given child
932 * node to parent. If node with same name is found, throws exception. If
933 * parent is null, child node will be added directly to module.
939 private void addChildToParent(final Builder parent, final DataSchemaNodeBuilder child, final String childName) {
940 final int lineNum = child.getLine();
941 if (parent.equals(this)) {
942 addChildToModule(child, childName, lineNum);
944 addChildToSubnodeOfModule(parent, child, childName, lineNum);
948 public String getYangVersion() {
952 public String getContact() {
956 public String getOrganization() {
961 * Adds child node <code>child</code> to the set of nodes child nodes.
963 * The method reduces the complexity of the method
964 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
968 * data schema node builder for child node
970 * string with name of child node
972 * line number in YANG file where is the node with the name equal
973 * to <code>childName</code> is defined
975 private void addChildToModule(final DataSchemaNodeBuilder child, final String childName, final int lineNum) {
976 // if parent == null => node is defined under module
977 // All leafs, leaf-lists, lists, containers, choices, rpcs,
978 // notifications, and anyxmls defined within a parent node or at the
979 // top level of the module or its submodules share the same
980 // identifier namespace.
981 for (DataSchemaNodeBuilder childNode : getChildNodeBuilders()) {
982 if (childNode.getQName().getLocalName().equals(childName)) {
983 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
986 for (RpcDefinitionBuilder rpc : addedRpcs) {
987 if (rpc.getQName().getLocalName().equals(childName)) {
988 raiseYangParserException("'" + child + "'", "rpc", childName, lineNum, rpc.getLine());
991 for (NotificationBuilder notification : addedNotifications) {
992 if (notification.getQName().getLocalName().equals(childName)) {
993 raiseYangParserException("'" + child + "'", "notification", childName, lineNum, notification.getLine());
1000 * Adds child node <code>child</code> to the group of child nodes of the
1001 * <code>parent</code>
1003 * The method reduces the complexity of the method
1004 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1005 * addChildToParent}. *
1008 * builder of node which is parent for <code>child</code>
1010 * data schema node builder for child node
1012 * string with name of child node
1014 * line number in YANG file where is the node with the name equal
1015 * to <code>childName</code> is defined
1017 private void addChildToSubnodeOfModule(final Builder parent, final DataSchemaNodeBuilder child,
1018 final String childName, final int lineNum) {
1019 // no need for checking rpc and notification because they can be
1020 // defined only under module or submodule
1021 if (parent instanceof DataNodeContainerBuilder) {
1022 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
1023 for (DataSchemaNodeBuilder childNode : parentNode.getChildNodeBuilders()) {
1024 if (childNode.getQName().getLocalName().equals(childName)) {
1025 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
1028 parentNode.addChildNode(child);
1029 } else if (parent instanceof ChoiceBuilder) {
1030 ChoiceBuilder parentNode = (ChoiceBuilder) parent;
1031 for (ChoiceCaseBuilder caseBuilder : parentNode.getCases()) {
1032 if (caseBuilder.getQName().getLocalName().equals(childName)) {
1033 raiseYangParserException("'" + child + "'", "node", childName, lineNum, caseBuilder.getLine());
1036 parentNode.addCase(child);
1038 throw new YangParseException(name, lineNum, "Unresolved parent of node '" + childName + "'.");
1042 private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) {
1043 return new ModuleImportImpl(moduleName, revision, prefix);
1046 private void raiseYangParserException(final String cantAddType, final String type, final String name,
1047 final int currentLine, final int duplicateLine) {
1049 StringBuilder msgPrefix = new StringBuilder("");
1050 if (cantAddType != null && !cantAddType.isEmpty()) {
1051 msgPrefix.append("Can not add ");
1052 msgPrefix.append(cantAddType);
1053 msgPrefix.append(": ");
1056 String msg = String.format("%s%s with same name '%s' already declared at line %d.", msgPrefix, type, name,
1058 throw new YangParseException(getModuleName(), currentLine, msg);
1062 public int hashCode() {
1063 final int prime = 31;
1065 result = prime * result + ((name == null) ? 0 : name.hashCode());
1066 result = prime * result + qnameModule.hashCode();
1067 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1073 public boolean equals(final Object obj) {
1080 if (getClass() != obj.getClass()) {
1083 ModuleBuilder other = (ModuleBuilder) obj;
1085 if (other.name != null) {
1088 } else if (!name.equals(other.name)) {
1091 if (!qnameModule.equals(other.qnameModule)) {
1094 if (prefix == null) {
1095 if (other.prefix != null) {
1098 } else if (!prefix.equals(other.prefix)) {
1104 public List<UnknownSchemaNode> getExtensionInstances() {
1105 return unknownNodes;