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 static final String GROUPING_STR = "Grouping";
65 private static final String TYPEDEF_STR = "typedef";
66 private ModuleImpl instance;
67 private final String name;
68 private final String sourcePath;
69 private static final SchemaPath SCHEMA_PATH = SchemaPath.create(Collections.<QName> emptyList(), true);
70 private String prefix;
71 private QNameModule qnameModule = QNameModule.create(null, null);
73 private final boolean submodule;
74 private String belongsTo;
75 private ModuleBuilder parent;
77 private final Deque<Builder> actualPath = new LinkedList<>();
78 private final Set<TypeAwareBuilder> dirtyNodes = new HashSet<>();
80 final Map<String, ModuleImport> imports = new HashMap<>();
81 final Map<String, ModuleBuilder> importedModules = new HashMap<>();
83 final Set<ModuleBuilder> addedSubmodules = new HashSet<>();
84 final Set<Module> submodules = new HashSet<>();
85 final Map<String, Date> includedModules = new HashMap<>();
87 private final Set<AugmentationSchema> augments = new LinkedHashSet<>();
88 private final List<AugmentationSchemaBuilder> augmentBuilders = new ArrayList<>();
89 private final List<AugmentationSchemaBuilder> allAugments = new ArrayList<>();
91 private final List<GroupingBuilder> allGroupings = new ArrayList<>();
93 private final List<UsesNodeBuilder> allUsesNodes = new ArrayList<>();
95 private final Set<RpcDefinition> rpcs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
96 private final Set<RpcDefinitionBuilder> addedRpcs = new HashSet<>();
98 private final Set<NotificationDefinition> notifications = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
99 private final Set<NotificationBuilder> addedNotifications = new HashSet<>();
101 private final Set<IdentitySchemaNode> identities = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
102 private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<>();
104 private final Set<FeatureDefinition> features = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
105 private final Set<FeatureBuilder> addedFeatures = new HashSet<>();
107 private final Set<Deviation> deviations = new HashSet<>();
108 private final Set<DeviationBuilder> deviationBuilders = new HashSet<>();
110 private final List<ExtensionDefinition> extensions = new ArrayList<>();
111 private final List<ExtensionBuilder> addedExtensions = new ArrayList<>();
113 private final List<UnknownSchemaNodeBuilder> allUnknownNodes = new ArrayList<>();
115 private final List<ListSchemaNodeBuilder> allLists = new ArrayList<>();
117 private String source;
118 private String yangVersion;
119 private String organization;
120 private String contact;
122 public ModuleBuilder(final String name, final String sourcePath) {
123 this(name, false, sourcePath);
126 public ModuleBuilder(final String name, final boolean submodule, final String sourcePath) {
127 super(name, 0, null);
129 this.sourcePath = sourcePath;
130 this.submodule = submodule;
131 actualPath.push(this);//FIXME: this escapes constructor
134 public ModuleBuilder(final Module base) {
135 super(base.getName(), 0, QName.create(base.getQNameModule(), base.getName()),
137 this.name = base.getName();
138 this.sourcePath = base.getModuleSourcePath();
141 yangVersion = base.getYangVersion();
142 actualPath.push(this);//FIXME: this escapes constructor
143 prefix = base.getPrefix();
144 qnameModule = base.getQNameModule();
146 augments.addAll(base.getAugmentations());
147 rpcs.addAll(base.getRpcs());
148 notifications.addAll(base.getNotifications());
150 for (IdentitySchemaNode identityNode : base.getIdentities()) {
151 addedIdentities.add(new IdentitySchemaNodeBuilder(name, identityNode));
154 features.addAll(base.getFeatures());
155 deviations.addAll(base.getDeviations());
156 extensions.addAll(base.getExtensionSchemaNodes());
157 unknownNodes.addAll(base.getUnknownSchemaNodes());
158 source = base.getSource();
162 protected String getStatementName() {
167 * Build new Module object based on this builder.
170 public Module build() {
171 if(instance != null) {
178 for (ModuleBuilder submodule : addedSubmodules) {
179 submodules.add(submodule.build());
183 for (FeatureBuilder fb : addedFeatures) {
184 features.add(fb.build());
188 for (NotificationBuilder entry : addedNotifications) {
189 notifications.add(entry.build());
193 for (AugmentationSchemaBuilder builder : augmentBuilders) {
194 augments.add(builder.build());
198 for (RpcDefinitionBuilder rpc : addedRpcs) {
199 rpcs.add(rpc.build());
203 for (DeviationBuilder entry : deviationBuilders) {
204 deviations.add(entry.build());
208 for (ExtensionBuilder eb : addedExtensions) {
209 extensions.add(eb.build());
211 Collections.sort(extensions, Comparators.SCHEMA_NODE_COMP);
215 for (IdentitySchemaNodeBuilder id : addedIdentities) {
216 identities.add(id.build());
220 for (UnknownSchemaNodeBuilder unb : addedUnknownNodes) {
221 unknownNodes.add(unb.build());
223 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
225 instance = new ModuleImpl(name, sourcePath, this);
229 public String getModuleSourcePath() {
234 public ModuleBuilder getParent() {
238 public void setParent(final ModuleBuilder parent) {
239 this.parent = parent;
243 public void setParent(final Builder parent) {
244 throw new YangParseException(name, 0, "Can not set parent to module");
248 public SchemaPath getPath() {
252 public void enterNode(final Builder node) {
253 actualPath.push(node);
256 public void exitNode() {
260 public Builder getActualNode() {
261 if (actualPath.isEmpty()) {
264 return actualPath.peekFirst();
268 public Set<TypeAwareBuilder> getDirtyNodes() {
272 public Set<AugmentationSchema> getAugments() {
276 public List<AugmentationSchemaBuilder> getAugmentBuilders() {
277 return augmentBuilders;
280 public List<AugmentationSchemaBuilder> getAllAugments() {
284 public Set<IdentitySchemaNode> getIdentities() {
288 public Set<IdentitySchemaNodeBuilder> getAddedIdentities() {
289 return addedIdentities;
292 public Set<FeatureDefinition> getFeatures() {
296 public Set<FeatureBuilder> getAddedFeatures() {
297 return addedFeatures;
300 public List<GroupingBuilder> getAllGroupings() {
304 public List<UsesNodeBuilder> getAllUsesNodes() {
308 public Set<Deviation> getDeviations() {
312 public Set<DeviationBuilder> getDeviationBuilders() {
313 return deviationBuilders;
316 public List<ExtensionDefinition> getExtensions() {
320 public List<ExtensionBuilder> getAddedExtensions() {
321 return addedExtensions;
324 public List<UnknownSchemaNodeBuilder> getAllUnknownNodes() {
325 return allUnknownNodes;
328 public List<ListSchemaNodeBuilder> getAllLists() {
332 public String getName() {
336 public URI getNamespace() {
337 return qnameModule.getNamespace();
340 public QNameModule getQNameModule() {
344 public void setQNameModule(final QNameModule qnameModule) {
345 this.qnameModule = Preconditions.checkNotNull(qnameModule);
348 public void setNamespace(final URI namespace) {
349 this.qnameModule = QNameModule.create(namespace, qnameModule.getRevision());
352 public String getPrefix() {
356 public Date getRevision() {
357 return qnameModule.getRevision();
360 public ModuleImport getImport(final String prefix) {
361 return imports.get(prefix);
364 public Map<String, ModuleImport> getImports() {
368 public ModuleBuilder getImportedModule(final String prefix) {
369 return importedModules.get(prefix);
372 public void addImportedModule(final String prefix, final ModuleBuilder module) {
374 importedModules.put(prefix, module);
377 public Map<String, Date> getIncludedModules() {
378 return includedModules;
381 public void addInclude(final String name, final Date revision) {
382 includedModules.put(name, revision);
385 public void addSubmodule(final ModuleBuilder submodule) {
386 addedSubmodules.add(submodule);
389 protected String getSource() {
393 public boolean isSubmodule() {
397 public String getBelongsTo() {
401 public void setBelongsTo(final String belongsTo) {
402 this.belongsTo = belongsTo;
405 public void markActualNodeDirty() {
406 final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) getActualNode();
407 dirtyNodes.add(nodeBuilder);
410 public void setRevision(final Date revision) {
411 this.qnameModule = QNameModule.create(qnameModule.getNamespace(), revision);
414 public void setPrefix(final String prefix) {
415 this.prefix = prefix;
418 public void setYangVersion(final String yangVersion) {
419 this.yangVersion = yangVersion;
422 public void setOrganization(final String organization) {
423 this.organization = organization;
426 public void setContact(final String contact) {
427 this.contact = contact;
430 public void addModuleImport(final String moduleName, final Date revision, final String prefix) {
433 final ModuleImport moduleImport = createModuleImport(moduleName, revision, prefix);
434 imports.put(prefix, moduleImport);
437 private void checkPrefix(final String prefix) {
438 if (prefix == null || prefix.isEmpty()) {
439 throw new IllegalArgumentException("Cannot add imported module with undefined prefix");
441 if (prefix.equals(this.prefix)) {
442 throw new IllegalArgumentException("Cannot add imported module with prefix equals to module prefix");
446 public ExtensionBuilder addExtension(final QName qname, final int line, final SchemaPath path) {
448 Builder parentBuilder = getActualNode();
449 if (!(parentBuilder.equals(this))) {
450 throw new YangParseException(name, line, "extension can be defined only in module or submodule");
453 final String extName = qname.getLocalName();
454 for (ExtensionBuilder addedExtension : addedExtensions) {
455 if (addedExtension.getQName().getLocalName().equals(extName)) {
456 raiseYangParserException("extension", "node", extName, line, addedExtension.getLine());
459 final ExtensionBuilder builder = new ExtensionBuilderImpl(name, line, qname, path);
460 builder.setParent(parentBuilder);
461 addedExtensions.add(builder);
465 public ContainerSchemaNodeBuilder addContainerNode(final int line, final QName qname, final SchemaPath schemaPath) {
467 final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
469 Builder parentBuilder = getActualNode();
470 builder.setParent(parentBuilder);
471 addChildToParent(parentBuilder, builder, qname.getLocalName());
476 public ListSchemaNodeBuilder addListNode(final int line, final QName qname, final SchemaPath schemaPath) {
478 final ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(name, line, qname, schemaPath);
480 Builder parentBuilder = getActualNode();
481 builder.setParent(parentBuilder);
482 addChildToParent(parentBuilder, builder, qname.getLocalName());
483 allLists.add(builder);
488 public LeafSchemaNodeBuilder addLeafNode(final int line, final QName qname, final SchemaPath schemaPath) {
490 final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(name, line, qname, schemaPath);
492 Builder parentBuilder = getActualNode();
493 builder.setParent(parentBuilder);
494 addChildToParent(parentBuilder, builder, qname.getLocalName());
499 public LeafListSchemaNodeBuilder addLeafListNode(final int line, final QName qname, final SchemaPath schemaPath) {
501 final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(name, line, qname, schemaPath);
503 Builder parentBuilder = getActualNode();
504 builder.setParent(parentBuilder);
505 addChildToParent(parentBuilder, builder, qname.getLocalName());
510 public GroupingBuilder addGrouping(final int line, final QName qname, final SchemaPath path) {
512 final GroupingBuilder builder = new GroupingBuilderImpl(name, line, qname, path);
514 Builder parentBuilder = getActualNode();
515 builder.setParent(parentBuilder);
517 String groupingName = qname.getLocalName();
518 if (parentBuilder.equals(this)) {
519 for (GroupingBuilder addedGrouping : getGroupingBuilders()) {
520 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
521 raiseYangParserException("", GROUPING_STR, groupingName, line, addedGrouping.getLine());
524 addGrouping(builder);
526 if (parentBuilder instanceof DataNodeContainerBuilder) {
527 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parentBuilder;
528 for (GroupingBuilder addedGrouping : parentNode.getGroupingBuilders()) {
529 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
530 raiseYangParserException("", GROUPING_STR, groupingName, line, addedGrouping.getLine());
533 parentNode.addGrouping(builder);
534 } else if (parentBuilder instanceof RpcDefinitionBuilder) {
535 RpcDefinitionBuilder parentNode = (RpcDefinitionBuilder) parentBuilder;
536 for (GroupingBuilder child : parentNode.getGroupings()) {
537 if (child.getQName().getLocalName().equals(groupingName)) {
538 raiseYangParserException("", GROUPING_STR, groupingName, line, child.getLine());
541 parentNode.addGrouping(builder);
543 throw new YangParseException(name, line, "Unresolved parent of grouping " + groupingName);
547 allGroupings.add(builder);
551 public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr,
552 final SchemaPath targetPath, final int order) {
554 final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr,
557 Builder parentBuilder = getActualNode();
558 builder.setParent(parentBuilder);
560 if (parentBuilder.equals(this)) {
561 // augment can be declared only under 'module' ...
562 if (!(augmentTargetStr.startsWith("/"))) {
563 throw new YangParseException(
566 "If the 'augment' statement is on the top level in a module, the absolute form of a schema node identifier MUST be used.");
568 augmentBuilders.add(builder);
570 // ... or 'uses' statement
571 if (parentBuilder instanceof UsesNodeBuilder) {
572 if (augmentTargetStr.startsWith("/")) {
573 throw new YangParseException(name, line,
574 "If 'augment' statement is a substatement to the 'uses' statement, it cannot contain absolute path ("
575 + augmentTargetStr + ")");
577 ((UsesNodeBuilder) parentBuilder).addAugment(builder);
579 throw new YangParseException(name, line, "Augment can be declared only under module or uses statement.");
582 allAugments.add(builder);
587 public UsesNodeBuilder addUsesNode(final int line, final SchemaPath grouping) {
589 final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(name, line, grouping);
591 Builder parentBuilder = getActualNode();
592 usesBuilder.setParent(parentBuilder);
594 if (parentBuilder.equals(this)) {
595 addUsesNode(usesBuilder);
597 if (!(parentBuilder instanceof DataNodeContainerBuilder)) {
598 throw new YangParseException(name, line, "Unresolved parent of uses '" + grouping + "'.");
600 ((DataNodeContainerBuilder) parentBuilder).addUsesNode(usesBuilder);
602 if (parentBuilder instanceof AugmentationSchemaBuilder) {
603 usesBuilder.setAugmenting(true);
606 allUsesNodes.add(usesBuilder);
610 public void addRefine(final RefineHolderImpl refine) {
612 final Builder parentBuilder = getActualNode();
613 if (!(parentBuilder instanceof UsesNodeBuilder)) {
614 throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
616 ((UsesNodeBuilder) parentBuilder).addRefine(refine);
617 refine.setParent(parentBuilder);
620 public RpcDefinitionBuilder addRpc(final int line, final QName qname, final SchemaPath path) {
622 Builder parentBuilder = getActualNode();
623 if (!(parentBuilder.equals(this))) {
624 throw new YangParseException(name, line, "rpc can be defined only in module or submodule");
627 final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(name, line, qname, path);
628 rpcBuilder.setParent(parentBuilder);
630 String rpcName = qname.getLocalName();
631 checkNotConflictingInDataNamespace(rpcName, line);
632 addedRpcs.add(rpcBuilder);
636 private void checkNotConflictingInDataNamespace(final String rpcName, final int line) {
637 for (DataSchemaNodeBuilder addedChild : getChildNodeBuilders()) {
638 if (addedChild.getQName().getLocalName().equals(rpcName)) {
639 raiseYangParserException("rpc", "node", rpcName, line, addedChild.getLine());
642 for (RpcDefinitionBuilder rpc : addedRpcs) {
643 if (rpc.getQName().getLocalName().equals(rpcName)) {
644 raiseYangParserException("", "rpc", rpcName, line, rpc.getLine());
647 for (NotificationBuilder addedNotification : addedNotifications) {
648 if (addedNotification.getQName().getLocalName().equals(rpcName)) {
649 raiseYangParserException("rpc", "notification", rpcName, line, addedNotification.getLine());
654 public ContainerSchemaNodeBuilder addRpcInput(final int line, final QName qname, final SchemaPath schemaPath) {
656 final Builder parentBuilder = getActualNode();
657 if (!(parentBuilder instanceof RpcDefinitionBuilder)) {
658 throw new YangParseException(name, line, "input can be defined only in rpc statement");
660 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parentBuilder;
662 final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
663 inputBuilder.setParent(rpc);
665 rpc.setInput(inputBuilder);
669 public ContainerSchemaNodeBuilder addRpcOutput(final SchemaPath schemaPath, final QName qname, final int line) {
671 final Builder parentBuilder = getActualNode();
672 if (!(parentBuilder instanceof RpcDefinitionBuilder)) {
673 throw new YangParseException(name, line, "output can be defined only in rpc statement");
675 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parentBuilder;
677 final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
678 outputBuilder.setParent(rpc);
680 rpc.setOutput(outputBuilder);
681 return outputBuilder;
684 public void addNotification(final NotificationDefinition notification) {
686 notifications.add(notification);
689 public NotificationBuilder addNotification(final int line, final QName qname, final SchemaPath path) {
691 final Builder parentBuilder = getActualNode();
692 if (!(parentBuilder.equals(this))) {
693 throw new YangParseException(name, line, "notification can be defined only in module or submodule");
696 String notificationName = qname.getLocalName();
697 checkNotConflictingInDataNamespace(notificationName, line);
699 final NotificationBuilder builder = new NotificationBuilder(name, line, qname, path);
700 builder.setParent(parentBuilder);
701 addedNotifications.add(builder);
706 public FeatureBuilder addFeature(final int line, final QName qname, final SchemaPath path) {
707 Builder parentBuilder = getActualNode();
708 if (!(parentBuilder.equals(this))) {
709 throw new YangParseException(name, line, "feature can be defined only in module or submodule");
712 final FeatureBuilder builder = new FeatureBuilder(name, line, qname, path);
713 builder.setParent(parentBuilder);
715 String featureName = qname.getLocalName();
716 for (FeatureBuilder addedFeature : addedFeatures) {
717 if (addedFeature.getQName().getLocalName().equals(featureName)) {
718 raiseYangParserException("", "feature", featureName, line, addedFeature.getLine());
721 addedFeatures.add(builder);
725 public ChoiceBuilder addChoice(final int line, final QName qname, final SchemaPath path) {
726 final ChoiceBuilder builder = new ChoiceBuilder(name, line, qname, path);
728 Builder parentBuilder = getActualNode();
729 builder.setParent(parentBuilder);
730 addChildToParent(parentBuilder, builder, qname.getLocalName());
735 public ChoiceCaseBuilder addCase(final int line, final QName qname, final SchemaPath path) {
736 Builder parentBuilder = getActualNode();
737 if (parentBuilder == null || parentBuilder.equals(this)) {
738 throw new YangParseException(name, line, "'case' parent not found");
741 final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(name, line, qname, path);
742 builder.setParent(parentBuilder);
744 if (parentBuilder instanceof ChoiceBuilder) {
745 ((ChoiceBuilder) parentBuilder).addCase(builder);
746 } else if (parentBuilder instanceof AugmentationSchemaBuilder) {
747 ((AugmentationSchemaBuilder) parentBuilder).addChildNode(builder);
749 throw new YangParseException(name, line, "Unresolved parent of 'case' " + qname.getLocalName());
755 public AnyXmlBuilder addAnyXml(final int line, final QName qname, final SchemaPath schemaPath) {
756 final AnyXmlBuilder builder = new AnyXmlBuilder(name, line, qname, schemaPath);
758 Builder parentBuilder = getActualNode();
759 builder.setParent(parentBuilder);
760 addChildToParent(parentBuilder, builder, qname.getLocalName());
766 public void addTypedef(final TypeDefinitionBuilder typedefBuilder) {
767 String nodeName = typedefBuilder.getQName().getLocalName();
768 for (TypeDefinitionBuilder tdb : getTypeDefinitionBuilders()) {
769 if (tdb.getQName().getLocalName().equals(nodeName)) {
770 raiseYangParserException("", TYPEDEF_STR, nodeName, typedefBuilder.getLine(), tdb.getLine());
773 super.addTypedef(typedefBuilder);
776 public TypeDefinitionBuilderImpl addTypedef(final int line, final QName qname, final SchemaPath path) {
777 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(name, line, qname, path);
779 Builder parentBuilder = getActualNode();
780 builder.setParent(parentBuilder);
782 String typedefName = qname.getLocalName();
783 if (parentBuilder.equals(this)) {
786 if (parentBuilder instanceof DataNodeContainerBuilder) {
787 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parentBuilder;
788 for (TypeDefinitionBuilder child : parentNode.getTypeDefinitionBuilders()) {
789 if (child.getQName().getLocalName().equals(typedefName)) {
790 raiseYangParserException("", TYPEDEF_STR, typedefName, line, child.getLine());
793 parentNode.addTypedef(builder);
794 } else if (parentBuilder instanceof RpcDefinitionBuilder) {
795 RpcDefinitionBuilder rpcParent = (RpcDefinitionBuilder) parentBuilder;
796 for (TypeDefinitionBuilder tdb : rpcParent.getTypeDefinitions()) {
797 if (tdb.getQName().getLocalName().equals(builder.getQName().getLocalName())) {
798 raiseYangParserException("", TYPEDEF_STR, typedefName, line, tdb.getLine());
801 rpcParent.addTypedef(builder);
803 throw new YangParseException(name, line, "Unresolved parent of typedef " + typedefName);
810 public void setType(final TypeDefinition<?> type) {
811 Builder parentBuilder = getActualNode();
812 if (!(parentBuilder instanceof TypeAwareBuilder)) {
813 throw new YangParseException("Failed to set type '" + type.getQName().getLocalName()
814 + "'. Invalid parent node: " + parentBuilder);
816 ((TypeAwareBuilder) parentBuilder).setType(type);
819 public UnionTypeBuilder addUnionType(final int line, final QNameModule module) {
820 final Builder parentBuilder = getActualNode();
821 if (parentBuilder == null) {
822 throw new YangParseException(name, line, "Unresolved parent of union type");
824 final UnionTypeBuilder union = new UnionTypeBuilder(name, line);
825 if (parentBuilder instanceof TypeAwareBuilder) {
826 ((TypeAwareBuilder) parentBuilder).setTypedef(union);
829 throw new YangParseException(name, line, "Invalid parent of union type.");
834 public void addIdentityrefType(final int line, final SchemaPath schemaPath, final String baseString) {
835 final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(name, line, baseString, schemaPath);
837 final Builder parentBuilder = getActualNode();
838 if (parentBuilder == null) {
839 throw new YangParseException(name, line, "Unresolved parent of identityref type.");
841 if (parentBuilder instanceof TypeAwareBuilder) {
842 final TypeAwareBuilder typeParent = (TypeAwareBuilder) parentBuilder;
843 typeParent.setTypedef(identityref);
844 dirtyNodes.add(typeParent);
846 throw new YangParseException(name, line, "Invalid parent of identityref type.");
851 public DeviationBuilder addDeviation(final int line, final SchemaPath targetPath) {
852 Builder parentBuilder = getActualNode();
853 if (!(parentBuilder.equals(this))) {
854 throw new YangParseException(name, line, "deviation can be defined only in module or submodule");
857 final DeviationBuilder builder = new DeviationBuilder(name, line, targetPath);
858 builder.setParent(parentBuilder);
859 deviationBuilders.add(builder);
863 public IdentitySchemaNodeBuilder addIdentity(final QName qname, final int line, final SchemaPath path) {
864 Builder parentBuilder = getActualNode();
865 if (!(parentBuilder.equals(this))) {
866 throw new YangParseException(name, line, "identity can be defined only in module or submodule");
868 String identityName = qname.getLocalName();
869 for (IdentitySchemaNodeBuilder idBuilder : addedIdentities) {
870 if (idBuilder.getQName().equals(qname)) {
871 raiseYangParserException("", "identity", identityName, line, idBuilder.getLine());
875 final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(name, line, qname, path);
876 builder.setParent(parentBuilder);
877 addedIdentities.add(builder);
882 public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder builder) {
883 addedUnknownNodes.add(builder);
884 allUnknownNodes.add(builder);
887 public UnknownSchemaNodeBuilderImpl addUnknownSchemaNode(final int line, final QName qname, final SchemaPath path) {
888 final Builder parentBuilder = getActualNode();
889 final UnknownSchemaNodeBuilderImpl builder = new UnknownSchemaNodeBuilderImpl(name, line, qname, path);
890 builder.setParent(parentBuilder);
891 allUnknownNodes.add(builder);
893 if (parentBuilder.equals(this)) {
894 addedUnknownNodes.add(builder);
896 if (parentBuilder instanceof SchemaNodeBuilder) {
897 parentBuilder.addUnknownNodeBuilder(builder);
898 } else if (parentBuilder instanceof DataNodeContainerBuilder) {
899 parentBuilder.addUnknownNodeBuilder(builder);
900 } else if (parentBuilder instanceof RefineHolderImpl) {
901 parentBuilder.addUnknownNodeBuilder(builder);
903 throw new YangParseException(name, line, "Unresolved parent of unknown node '" + qname.getLocalName()
911 public Set<RpcDefinition> getRpcs() {
915 public Set<RpcDefinitionBuilder> getAddedRpcs() {
919 public Set<NotificationDefinition> getNotifications() {
920 return notifications;
923 public Set<NotificationBuilder> getAddedNotifications() {
924 return addedNotifications;
928 public String toString() {
929 return "module " + name;
932 public void setSource(final ByteSource byteSource) throws IOException {
933 setSource(byteSource.asCharSource(Charsets.UTF_8).read());
936 public void setSource(final String source) {
937 this.source = source;
941 * Add child to parent. Method checks for duplicates and add given child
942 * node to parent. If node with same name is found, throws exception. If
943 * parent is null, child node will be added directly to module.
949 private void addChildToParent(final Builder parent, final DataSchemaNodeBuilder child, final String childName) {
950 final int lineNum = child.getLine();
951 if (parent.equals(this)) {
952 addChildToModule(child, childName, lineNum);
954 addChildToSubnodeOfModule(parent, child, childName, lineNum);
958 public String getYangVersion() {
962 public String getContact() {
966 public String getOrganization() {
971 * Adds child node <code>child</code> to the set of nodes child nodes.
973 * The method reduces the complexity of the method
974 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
978 * data schema node builder for child node
980 * string with name of child node
982 * line number in YANG file where is the node with the name equal
983 * to <code>childName</code> is defined
985 private void addChildToModule(final DataSchemaNodeBuilder child, final String childName, final int lineNum) {
986 // if parent == null => node is defined under module
987 // All leafs, leaf-lists, lists, containers, choices, rpcs,
988 // notifications, and anyxmls defined within a parent node or at the
989 // top level of the module or its submodules share the same
990 // identifier namespace.
991 for (DataSchemaNodeBuilder childNode : getChildNodeBuilders()) {
992 if (childNode.getQName().getLocalName().equals(childName)) {
993 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
996 for (RpcDefinitionBuilder rpc : addedRpcs) {
997 if (rpc.getQName().getLocalName().equals(childName)) {
998 raiseYangParserException("'" + child + "'", "rpc", childName, lineNum, rpc.getLine());
1001 for (NotificationBuilder notification : addedNotifications) {
1002 if (notification.getQName().getLocalName().equals(childName)) {
1003 raiseYangParserException("'" + child + "'", "notification", childName, lineNum, notification.getLine());
1006 addChildNode(child);
1010 * Adds child node <code>child</code> to the group of child nodes of the
1011 * <code>parent</code>
1013 * The method reduces the complexity of the method
1014 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1015 * addChildToParent}. *
1018 * builder of node which is parent for <code>child</code>
1020 * data schema node builder for child node
1022 * string with name of child node
1024 * line number in YANG file where is the node with the name equal
1025 * to <code>childName</code> is defined
1027 private void addChildToSubnodeOfModule(final Builder parent, final DataSchemaNodeBuilder child,
1028 final String childName, final int lineNum) {
1029 // no need for checking rpc and notification because they can be
1030 // defined only under module or submodule
1031 if (parent instanceof DataNodeContainerBuilder) {
1032 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
1033 for (DataSchemaNodeBuilder childNode : parentNode.getChildNodeBuilders()) {
1034 if (childNode.getQName().getLocalName().equals(childName)) {
1035 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
1038 parentNode.addChildNode(child);
1039 } else if (parent instanceof ChoiceBuilder) {
1040 ChoiceBuilder parentNode = (ChoiceBuilder) parent;
1041 for (ChoiceCaseBuilder caseBuilder : parentNode.getCases()) {
1042 if (caseBuilder.getQName().getLocalName().equals(childName)) {
1043 raiseYangParserException("'" + child + "'", "node", childName, lineNum, caseBuilder.getLine());
1046 parentNode.addCase(child);
1048 throw new YangParseException(name, lineNum, "Unresolved parent of node '" + childName + "'.");
1052 private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) {
1053 return new ModuleImportImpl(moduleName, revision, prefix);
1056 private void raiseYangParserException(final String cantAddType, final String type, final String name,
1057 final int currentLine, final int duplicateLine) {
1059 StringBuilder msgPrefix = new StringBuilder("");
1060 if (cantAddType != null && !cantAddType.isEmpty()) {
1061 msgPrefix.append("Can not add ");
1062 msgPrefix.append(cantAddType);
1063 msgPrefix.append(": ");
1066 String msg = String.format("%s%s with same name '%s' already declared at line %d.", msgPrefix, type, name,
1068 throw new YangParseException(getModuleName(), currentLine, msg);
1072 public int hashCode() {
1073 final int prime = 31;
1075 result = prime * result + ((name == null) ? 0 : name.hashCode());
1076 result = prime * result + qnameModule.hashCode();
1077 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1083 public boolean equals(final Object obj) {
1090 if (getClass() != obj.getClass()) {
1093 ModuleBuilder other = (ModuleBuilder) obj;
1095 if (other.name != null) {
1098 } else if (!name.equals(other.name)) {
1101 if (!qnameModule.equals(other.qnameModule)) {
1104 if (prefix == null) {
1105 if (other.prefix != null) {
1108 } else if (!prefix.equals(other.prefix)) {
1114 public List<UnknownSchemaNode> getExtensionInstances() {
1115 return unknownNodes;