2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 package org.opendaylight.yangtools.yang.parser.builder.impl;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.Deque;
15 import java.util.HashSet;
16 import java.util.LinkedHashSet;
17 import java.util.LinkedList;
18 import java.util.List;
20 import java.util.TreeSet;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
24 import org.opendaylight.yangtools.yang.model.api.Deviation;
25 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
26 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
27 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
30 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
31 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
33 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
35 import org.opendaylight.yangtools.yang.model.util.ModuleImportImpl;
36 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
37 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
38 import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder;
39 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
40 import org.opendaylight.yangtools.yang.parser.builder.api.DocumentedNodeBuilder;
41 import org.opendaylight.yangtools.yang.parser.builder.api.ExtensionBuilder;
42 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
43 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
44 import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
45 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
46 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
47 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
48 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
49 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
50 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
51 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
53 import com.google.common.collect.ImmutableSet;
56 * Builder of Module object. If this module is dependent on external
57 * module/modules, these dependencies must be resolved before module is built,
58 * otherwise result may not be valid.
60 public class ModuleBuilder extends AbstractDocumentedDataNodeContainerBuilder implements DocumentedNodeBuilder {
62 private ModuleImpl instance;
63 private final String name;
64 private final String sourcePath;
65 private static final SchemaPath SCHEMA_PATH = SchemaPath.create(Collections.<QName> emptyList(), true);
66 private URI namespace;
67 private String prefix;
68 private Date revision;
70 private final boolean submodule;
71 private String belongsTo;
72 private ModuleBuilder parent;
75 private final Deque<Builder> actualPath = new LinkedList<>();
76 private final Set<TypeAwareBuilder> dirtyNodes = new HashSet<>();
78 private final Set<ModuleImport> imports = new HashSet<ModuleImport>();
80 private final Set<AugmentationSchema> augments = new LinkedHashSet<>();
81 private final List<AugmentationSchemaBuilder> augmentBuilders = new ArrayList<>();
82 private final List<AugmentationSchemaBuilder> allAugments = new ArrayList<>();
84 private final List<GroupingBuilder> allGroupings = new ArrayList<>();
86 private final List<UsesNodeBuilder> allUsesNodes = new ArrayList<>();
88 private final Set<RpcDefinition> rpcs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
89 private final Set<RpcDefinitionBuilder> addedRpcs = new HashSet<>();
91 private final Set<NotificationDefinition> notifications = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
92 private final Set<NotificationBuilder> addedNotifications = new HashSet<>();
94 private final Set<IdentitySchemaNode> identities = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
95 private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<>();
97 private final Set<FeatureDefinition> features = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
98 private final Set<FeatureBuilder> addedFeatures = new HashSet<>();
100 private final Set<Deviation> deviations = new HashSet<>();
101 private final Set<DeviationBuilder> deviationBuilders = new HashSet<>();
103 private final List<ExtensionDefinition> extensions = new ArrayList<>();
104 private final List<ExtensionBuilder> addedExtensions = new ArrayList<>();
106 private final List<UnknownSchemaNodeBuilder> allUnknownNodes = new ArrayList<>();
108 private final List<ListSchemaNodeBuilder> allLists = new ArrayList<>();
110 private String source;
111 private String yangVersion;
112 private String organization;
113 private String contact;
115 public ModuleBuilder(final String name, final String sourcePath) {
116 this(name, false, sourcePath);
120 protected String getStatementName() {
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);
132 public ModuleBuilder(final Module base) {
133 super(base.getName(), 0, new QName(base.getNamespace(), base.getRevision(), base.getPrefix(), base.getName()),
135 this.name = base.getName();
136 this.sourcePath = base.getModuleSourcePath();
139 yangVersion = base.getYangVersion();
140 actualPath.push(this);
141 namespace = base.getNamespace();
142 prefix = base.getPrefix();
143 revision = base.getRevision();
145 augments.addAll(base.getAugmentations());
146 rpcs.addAll(base.getRpcs());
147 notifications.addAll(base.getNotifications());
149 for (IdentitySchemaNode identityNode : base.getIdentities()) {
150 addedIdentities.add(new IdentitySchemaNodeBuilder(name, identityNode));
153 features.addAll(base.getFeatures());
154 deviations.addAll(base.getDeviations());
155 extensions.addAll(base.getExtensionSchemaNodes());
156 unknownNodes.addAll(base.getUnknownSchemaNodes());
160 * Build new Module object based on this builder.
163 public Module build() {
165 instance = new ModuleImpl(name, sourcePath, this);
167 for (FeatureBuilder fb : addedFeatures) {
168 features.add(fb.build());
170 instance.setFeatures(features);
173 for (NotificationBuilder entry : addedNotifications) {
174 notifications.add(entry.build());
176 instance.setNotifications(notifications);
179 for (AugmentationSchemaBuilder builder : augmentBuilders) {
180 augments.add(builder.build());
182 instance.setAugmentations(augments);
185 for (RpcDefinitionBuilder rpc : addedRpcs) {
186 rpcs.add(rpc.build());
188 instance.setRpcs(rpcs);
191 for (DeviationBuilder entry : deviationBuilders) {
192 deviations.add(entry.build());
194 instance.setDeviations(deviations);
197 for (ExtensionBuilder eb : addedExtensions) {
198 extensions.add(eb.build());
200 Collections.sort(extensions, Comparators.SCHEMA_NODE_COMP);
201 instance.setExtensionSchemaNodes(extensions);
204 for (IdentitySchemaNodeBuilder id : addedIdentities) {
205 identities.add(id.build());
207 instance.setIdentities(identities);
210 for (UnknownSchemaNodeBuilder unb : addedUnknownNodes) {
211 unknownNodes.add(unb.build());
213 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
214 instance.setUnknownSchemaNodes(unknownNodes);
216 instance.setSource(source);
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() {
332 public void setNamespace(final URI namespace) {
333 this.namespace = namespace;
336 public String getPrefix() {
340 public Date getRevision() {
344 public boolean isSubmodule() {
348 public String getBelongsTo() {
352 public void setBelongsTo(final String belongsTo) {
353 this.belongsTo = belongsTo;
356 public void markActualNodeDirty() {
357 final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) getActualNode();
358 dirtyNodes.add(nodeBuilder);
361 public void setRevision(final Date revision) {
362 this.revision = revision;
365 public void setPrefix(final String prefix) {
366 this.prefix = prefix;
369 public void setYangVersion(final String yangVersion) {
370 this.yangVersion = yangVersion;
373 public void setOrganization(final String organization) {
374 this.organization = organization;
377 public void setContact(final String contact) {
378 this.contact = contact;
381 public boolean addModuleImport(final String moduleName, final Date revision, final String prefix) {
382 final ModuleImport moduleImport = createModuleImport(moduleName, revision, prefix);
383 return imports.add(moduleImport);
386 public Set<ModuleImport> getModuleImports() {
390 public ExtensionBuilder addExtension(final QName qname, final int line, final SchemaPath path) {
391 Builder parent = getActualNode();
392 if (!(parent.equals(this))) {
393 throw new YangParseException(name, line, "extension can be defined only in module or submodule");
396 final String extName = qname.getLocalName();
397 for (ExtensionBuilder addedExtension : addedExtensions) {
398 if (addedExtension.getQName().getLocalName().equals(extName)) {
399 raiseYangParserException("extension", "node", extName, line, addedExtension.getLine());
402 final ExtensionBuilder builder = new ExtensionBuilderImpl(name, line, qname, path);
403 builder.setParent(parent);
404 addedExtensions.add(builder);
408 public ContainerSchemaNodeBuilder addContainerNode(final int line, final QName qname, final SchemaPath schemaPath) {
409 final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
411 Builder parent = getActualNode();
412 builder.setParent(parent);
413 addChildToParent(parent, builder, qname.getLocalName());
418 public ListSchemaNodeBuilder addListNode(final int line, final QName qname, final SchemaPath schemaPath) {
419 final ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(name, line, qname, schemaPath);
421 Builder parent = getActualNode();
422 builder.setParent(parent);
423 addChildToParent(parent, builder, qname.getLocalName());
424 allLists.add(builder);
429 public LeafSchemaNodeBuilder addLeafNode(final int line, final QName qname, final SchemaPath schemaPath) {
430 final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(name, line, qname, schemaPath);
432 Builder parent = getActualNode();
433 builder.setParent(parent);
434 addChildToParent(parent, builder, qname.getLocalName());
439 public LeafListSchemaNodeBuilder addLeafListNode(final int line, final QName qname, final SchemaPath schemaPath) {
440 final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(name, line, qname, schemaPath);
442 Builder parent = getActualNode();
443 builder.setParent(parent);
444 addChildToParent(parent, builder, qname.getLocalName());
449 public GroupingBuilder addGrouping(final int line, final QName qname, final SchemaPath path) {
450 final GroupingBuilder builder = new GroupingBuilderImpl(name, line, qname, path);
452 Builder parent = getActualNode();
453 builder.setParent(parent);
455 String groupingName = qname.getLocalName();
456 if (parent.equals(this)) {
457 for (GroupingBuilder addedGrouping : getGroupingBuilders()) {
458 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
459 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
462 addGrouping(builder);
464 if (parent instanceof DataNodeContainerBuilder) {
465 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
466 for (GroupingBuilder addedGrouping : parentNode.getGroupingBuilders()) {
467 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
468 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
471 parentNode.addGrouping(builder);
472 } else if (parent instanceof RpcDefinitionBuilder) {
473 RpcDefinitionBuilder parentNode = (RpcDefinitionBuilder) parent;
474 for (GroupingBuilder child : parentNode.getGroupings()) {
475 if (child.getQName().getLocalName().equals(groupingName)) {
476 raiseYangParserException("", "Grouping", groupingName, line, child.getLine());
479 parentNode.addGrouping(builder);
481 throw new YangParseException(name, line, "Unresolved parent of grouping " + groupingName);
485 allGroupings.add(builder);
489 public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr, final int order) {
490 final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr, order);
492 Builder parent = getActualNode();
493 builder.setParent(parent);
495 if (parent.equals(this)) {
496 // augment can be declared only under 'module' ...
497 if (!(augmentTargetStr.startsWith("/"))) {
498 throw new YangParseException(
501 "If the 'augment' statement is on the top level in a module, the absolute form of a schema node identifier MUST be used.");
503 augmentBuilders.add(builder);
505 // ... or 'uses' statement
506 if (parent instanceof UsesNodeBuilder) {
507 if (augmentTargetStr.startsWith("/")) {
508 throw new YangParseException(name, line,
509 "If 'augment' statement is a substatement to the 'uses' statement, it cannot contain absolute path ("
510 + augmentTargetStr + ")");
512 ((UsesNodeBuilder) parent).addAugment(builder);
514 throw new YangParseException(name, line, "Augment can be declared only under module or uses statement.");
517 allAugments.add(builder);
522 public UsesNodeBuilder addUsesNode(final int line, final String groupingPathStr) {
523 final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(name, line, groupingPathStr);
525 Builder parent = getActualNode();
526 usesBuilder.setParent(parent);
528 if (parent.equals(this)) {
529 addUsesNode(usesBuilder);
531 if (!(parent instanceof DataNodeContainerBuilder)) {
532 throw new YangParseException(name, line, "Unresolved parent of uses '" + groupingPathStr + "'.");
534 ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder);
536 if (parent instanceof AugmentationSchemaBuilder) {
537 usesBuilder.setAugmenting(true);
540 allUsesNodes.add(usesBuilder);
544 public void addRefine(final RefineHolderImpl refine) {
545 final Builder parent = getActualNode();
546 if (!(parent instanceof UsesNodeBuilder)) {
547 throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
549 ((UsesNodeBuilder) parent).addRefine(refine);
550 refine.setParent(parent);
553 public RpcDefinitionBuilder addRpc(final int line, final QName qname, final SchemaPath path) {
554 Builder parent = getActualNode();
555 if (!(parent.equals(this))) {
556 throw new YangParseException(name, line, "rpc can be defined only in module or submodule");
559 final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(name, line, qname, path);
560 rpcBuilder.setParent(parent);
562 String rpcName = qname.getLocalName();
563 checkNotConflictingInDataNamespace(rpcName, line);
564 addedRpcs.add(rpcBuilder);
568 private void checkNotConflictingInDataNamespace(final String rpcName, final int line) {
569 for (DataSchemaNodeBuilder addedChild : getChildNodeBuilders()) {
570 if (addedChild.getQName().getLocalName().equals(rpcName)) {
571 raiseYangParserException("rpc", "node", rpcName, line, addedChild.getLine());
574 for (RpcDefinitionBuilder rpc : addedRpcs) {
575 if (rpc.getQName().getLocalName().equals(rpcName)) {
576 raiseYangParserException("", "rpc", rpcName, line, rpc.getLine());
579 for (NotificationBuilder addedNotification : addedNotifications) {
580 if (addedNotification.getQName().getLocalName().equals(rpcName)) {
581 raiseYangParserException("rpc", "notification", rpcName, line, addedNotification.getLine());
586 public ContainerSchemaNodeBuilder addRpcInput(final int line, final QName qname, final SchemaPath schemaPath) {
587 final Builder parent = getActualNode();
588 if (!(parent instanceof RpcDefinitionBuilder)) {
589 throw new YangParseException(name, line, "input can be defined only in rpc statement");
591 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
593 final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
594 inputBuilder.setParent(rpc);
596 rpc.setInput(inputBuilder);
600 public ContainerSchemaNodeBuilder addRpcOutput(final SchemaPath schemaPath, final QName qname, final int line) {
601 final Builder parent = getActualNode();
602 if (!(parent instanceof RpcDefinitionBuilder)) {
603 throw new YangParseException(name, line, "output can be defined only in rpc statement");
605 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
607 final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
608 outputBuilder.setParent(rpc);
610 rpc.setOutput(outputBuilder);
611 return outputBuilder;
614 public void addNotification(final NotificationDefinition notification) {
615 notifications.add(notification);
618 public NotificationBuilder addNotification(final int line, final QName qname, final SchemaPath path) {
619 final Builder parent = getActualNode();
620 if (!(parent.equals(this))) {
621 throw new YangParseException(name, line, "notification can be defined only in module or submodule");
624 String notificationName = qname.getLocalName();
625 checkNotConflictingInDataNamespace(notificationName, line);
627 final NotificationBuilder builder = new NotificationBuilder(name, line, qname, path);
628 builder.setParent(parent);
629 addedNotifications.add(builder);
634 public FeatureBuilder addFeature(final int line, final QName qname, final SchemaPath path) {
635 Builder parent = getActualNode();
636 if (!(parent.equals(this))) {
637 throw new YangParseException(name, line, "feature can be defined only in module or submodule");
640 final FeatureBuilder builder = new FeatureBuilder(name, line, qname, path);
641 builder.setParent(parent);
643 String featureName = qname.getLocalName();
644 for (FeatureBuilder addedFeature : addedFeatures) {
645 if (addedFeature.getQName().getLocalName().equals(featureName)) {
646 raiseYangParserException("", "feature", featureName, line, addedFeature.getLine());
649 addedFeatures.add(builder);
653 public ChoiceBuilder addChoice(final int line, final QName qname, final SchemaPath path) {
654 final ChoiceBuilder builder = new ChoiceBuilder(name, line, qname, path);
656 Builder parent = getActualNode();
657 builder.setParent(parent);
658 addChildToParent(parent, builder, qname.getLocalName());
663 public ChoiceCaseBuilder addCase(final int line, final QName qname, final SchemaPath path) {
664 Builder parent = getActualNode();
665 if (parent == null || parent.equals(this)) {
666 throw new YangParseException(name, line, "'case' parent not found");
669 final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(name, line, qname, path);
670 builder.setParent(parent);
672 if (parent instanceof ChoiceBuilder) {
673 ((ChoiceBuilder) parent).addCase(builder);
674 } else if (parent instanceof AugmentationSchemaBuilder) {
675 ((AugmentationSchemaBuilder) parent).addChildNode(builder);
677 throw new YangParseException(name, line, "Unresolved parent of 'case' " + qname.getLocalName());
683 public AnyXmlBuilder addAnyXml(final int line, final QName qname, final SchemaPath schemaPath) {
684 final AnyXmlBuilder builder = new AnyXmlBuilder(name, line, qname, schemaPath);
686 Builder parent = getActualNode();
687 builder.setParent(parent);
688 addChildToParent(parent, builder, qname.getLocalName());
697 public void addTypedef(final TypeDefinitionBuilder typedefBuilder) {
698 String nodeName = typedefBuilder.getQName().getLocalName();
699 for (TypeDefinitionBuilder tdb : getTypeDefinitionBuilders()) {
700 if (tdb.getQName().getLocalName().equals(nodeName)) {
701 raiseYangParserException("", "typedef", nodeName, typedefBuilder.getLine(), tdb.getLine());
704 super.addTypedef(typedefBuilder);
707 public TypeDefinitionBuilderImpl addTypedef(final int line, final QName qname, final SchemaPath path) {
708 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(name, line, qname, path);
710 Builder parent = getActualNode();
711 builder.setParent(parent);
713 String typedefName = qname.getLocalName();
714 if (parent.equals(this)) {
717 if (parent instanceof DataNodeContainerBuilder) {
718 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
719 for (TypeDefinitionBuilder child : parentNode.getTypeDefinitionBuilders()) {
720 if (child.getQName().getLocalName().equals(typedefName)) {
721 raiseYangParserException("", "typedef", typedefName, line, child.getLine());
724 parentNode.addTypedef(builder);
725 } else if (parent instanceof RpcDefinitionBuilder) {
726 RpcDefinitionBuilder rpcParent = (RpcDefinitionBuilder) parent;
727 for (TypeDefinitionBuilder tdb : rpcParent.getTypeDefinitions()) {
728 if (tdb.getQName().getLocalName().equals(builder.getQName().getLocalName())) {
729 raiseYangParserException("", "typedef", typedefName, line, tdb.getLine());
732 rpcParent.addTypedef(builder);
734 throw new YangParseException(name, line, "Unresolved parent of typedef " + typedefName);
741 public void setType(final TypeDefinition<?> type) {
742 Builder parent = getActualNode();
743 if (!(parent instanceof TypeAwareBuilder)) {
744 throw new YangParseException("Failed to set type '" + type.getQName().getLocalName()
745 + "'. Invalid parent node: " + parent);
747 ((TypeAwareBuilder) parent).setType(type);
750 public UnionTypeBuilder addUnionType(final int line, final URI namespace, final Date revision) {
751 final Builder parent = getActualNode();
752 if (parent == null) {
753 throw new YangParseException(name, line, "Unresolved parent of union type");
755 final UnionTypeBuilder union = new UnionTypeBuilder(name, line);
756 if (parent instanceof TypeAwareBuilder) {
757 ((TypeAwareBuilder) parent).setTypedef(union);
760 throw new YangParseException(name, line, "Invalid parent of union type.");
765 public void addIdentityrefType(final int line, final SchemaPath schemaPath, final String baseString) {
766 final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(name, line, baseString, schemaPath);
768 final Builder parent = getActualNode();
769 if (parent == null) {
770 throw new YangParseException(name, line, "Unresolved parent of identityref type.");
772 if (parent instanceof TypeAwareBuilder) {
773 final TypeAwareBuilder typeParent = (TypeAwareBuilder) parent;
774 typeParent.setTypedef(identityref);
775 dirtyNodes.add(typeParent);
777 throw new YangParseException(name, line, "Invalid parent of identityref type.");
782 public DeviationBuilder addDeviation(final int line, final String targetPath) {
783 Builder parent = getActualNode();
784 if (!(parent.equals(this))) {
785 throw new YangParseException(name, line, "deviation can be defined only in module or submodule");
788 final DeviationBuilder builder = new DeviationBuilder(name, line, targetPath);
789 builder.setParent(parent);
790 deviationBuilders.add(builder);
794 public IdentitySchemaNodeBuilder addIdentity(final QName qname, final int line, final SchemaPath path) {
795 Builder parent = getActualNode();
796 if (!(parent.equals(this))) {
797 throw new YangParseException(name, line, "identity can be defined only in module or submodule");
799 String identityName = qname.getLocalName();
800 for (IdentitySchemaNodeBuilder idBuilder : addedIdentities) {
801 if (idBuilder.getQName().equals(qname)) {
802 raiseYangParserException("", "identity", identityName, line, idBuilder.getLine());
806 final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(name, line, qname, path);
807 builder.setParent(parent);
808 addedIdentities.add(builder);
813 public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder builder) {
814 addedUnknownNodes.add(builder);
815 allUnknownNodes.add(builder);
818 public UnknownSchemaNodeBuilderImpl addUnknownSchemaNode(final int line, final QName qname, final SchemaPath path) {
819 final Builder parent = getActualNode();
820 final UnknownSchemaNodeBuilderImpl builder = new UnknownSchemaNodeBuilderImpl(name, line, qname, path);
821 builder.setParent(parent);
822 allUnknownNodes.add(builder);
824 if (parent.equals(this)) {
825 addedUnknownNodes.add(builder);
827 if (parent instanceof SchemaNodeBuilder) {
828 ((SchemaNodeBuilder) parent).addUnknownNodeBuilder(builder);
829 } else if (parent instanceof DataNodeContainerBuilder) {
830 ((DataNodeContainerBuilder) parent).addUnknownNodeBuilder(builder);
831 } else if (parent instanceof RefineHolderImpl) {
832 ((RefineHolderImpl) parent).addUnknownNodeBuilder(builder);
834 throw new YangParseException(name, line, "Unresolved parent of unknown node '" + qname.getLocalName()
842 public Set<RpcDefinition> getRpcs() {
846 public Set<RpcDefinitionBuilder> getAddedRpcs() {
850 public Set<NotificationDefinition> getNotifications() {
851 return notifications;
854 public Set<NotificationBuilder> getAddedNotifications() {
855 return addedNotifications;
859 public String toString() {
860 return "module " + name;
863 public void setSource(final String source) {
864 this.source = source;
867 public static final class ModuleImpl extends AbstractDocumentedDataNodeContainer implements Module {
868 private final URI namespace;
869 private final String name;
870 private final String sourcePath;
871 private final Date revision;
872 private final String prefix;
873 private final String yangVersion;
874 private final String organization;
875 private final String contact;
876 private final Set<ModuleImport> imports;
877 private final Set<FeatureDefinition> features = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
878 private final Set<NotificationDefinition> notifications = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
879 private final Set<AugmentationSchema> augmentations = new HashSet<>();
880 private final Set<RpcDefinition> rpcs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
881 private final Set<Deviation> deviations = new HashSet<>();
882 private final List<ExtensionDefinition> extensionNodes = new ArrayList<>();
883 private final Set<IdentitySchemaNode> identities = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
884 private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
885 private String source;
887 private ModuleImpl(final String name, final String sourcePath, final ModuleBuilder builder) {
890 this.sourcePath = sourcePath;
891 this.imports = ImmutableSet.<ModuleImport>copyOf(builder.imports);
892 this.namespace = builder.getNamespace();
893 this.prefix = builder.getPrefix();
894 this.revision = builder.getRevision();
895 this.yangVersion = builder.getYangVersion();
896 this.organization = builder.getOrganization();
897 this.contact = builder.getContact();
901 public String getModuleSourcePath() {
906 public URI getNamespace() {
911 public String getName() {
916 public Date getRevision() {
921 public String getPrefix() {
926 public String getYangVersion() {
931 public String getOrganization() {
936 public String getContact() {
941 public Set<ModuleImport> getImports() {
946 public Set<FeatureDefinition> getFeatures() {
950 private void setFeatures(final Set<FeatureDefinition> features) {
951 if (features != null) {
952 this.features.addAll(features);
957 public Set<NotificationDefinition> getNotifications() {
958 return notifications;
961 private void setNotifications(final Set<NotificationDefinition> notifications) {
962 if (notifications != null) {
963 this.notifications.addAll(notifications);
968 public Set<AugmentationSchema> getAugmentations() {
969 return augmentations;
972 private void setAugmentations(final Set<AugmentationSchema> augmentations) {
973 if (augmentations != null) {
974 this.augmentations.addAll(augmentations);
979 public Set<RpcDefinition> getRpcs() {
983 private void setRpcs(final Set<RpcDefinition> rpcs) {
985 this.rpcs.addAll(rpcs);
990 public Set<Deviation> getDeviations() {
994 private void setDeviations(final Set<Deviation> deviations) {
995 if (deviations != null) {
996 this.deviations.addAll(deviations);
1001 public List<ExtensionDefinition> getExtensionSchemaNodes() {
1002 Collections.sort(extensionNodes, Comparators.SCHEMA_NODE_COMP);
1003 return extensionNodes;
1006 private void setExtensionSchemaNodes(final List<ExtensionDefinition> extensionNodes) {
1007 if (extensionNodes != null) {
1008 this.extensionNodes.addAll(extensionNodes);
1013 public Set<IdentitySchemaNode> getIdentities() {
1017 private void setIdentities(final Set<IdentitySchemaNode> identities) {
1018 if (identities != null) {
1019 this.identities.addAll(identities);
1024 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
1025 return unknownNodes;
1028 private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
1029 if (unknownNodes != null) {
1030 this.unknownNodes.addAll(unknownNodes);
1034 void setSource(final String source) {
1035 this.source = source;
1038 public String getSource() {
1043 public int hashCode() {
1044 final int prime = 31;
1046 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1047 result = prime * result + ((name == null) ? 0 : name.hashCode());
1048 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1049 result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());
1054 public boolean equals(final Object obj) {
1061 if (getClass() != obj.getClass()) {
1064 ModuleImpl other = (ModuleImpl) obj;
1065 if (namespace == null) {
1066 if (other.namespace != null) {
1069 } else if (!namespace.equals(other.namespace)) {
1073 if (other.name != null) {
1076 } else if (!name.equals(other.name)) {
1079 if (revision == null) {
1080 if (other.revision != null) {
1083 } else if (!revision.equals(other.revision)) {
1086 if (yangVersion == null) {
1087 if (other.yangVersion != null) {
1090 } else if (!yangVersion.equals(other.yangVersion)) {
1097 public String toString() {
1098 StringBuilder sb = new StringBuilder(ModuleImpl.class.getSimpleName());
1100 sb.append("name=" + name);
1101 sb.append(", namespace=" + namespace);
1102 sb.append(", revision=" + revision);
1103 sb.append(", prefix=" + prefix);
1104 sb.append(", yangVersion=" + yangVersion);
1106 return sb.toString();
1111 * Add child to parent. Method checks for duplicates and add given child
1112 * node to parent. If node with same name is found, throws exception. If
1113 * parent is null, child node will be added directly to module.
1119 private void addChildToParent(final Builder parent, final DataSchemaNodeBuilder child, final String childName) {
1120 final int lineNum = child.getLine();
1121 if (parent.equals(this)) {
1122 addChildToModule(child, childName, lineNum);
1124 addChildToSubnodeOfModule(parent, child, childName, lineNum);
1128 public String getYangVersion() {
1132 public String getContact() {
1136 public String getOrganization() {
1137 return organization;
1141 * Adds child node <code>child</code> to the set of nodes child nodes.
1143 * The method reduces the complexity of the method
1144 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1145 * addChildToParent}.
1148 * data schema node builder for child node
1150 * string with name of child node
1152 * line number in YANG file where is the node with the name equal
1153 * to <code>childName</code> is defined
1155 private void addChildToModule(final DataSchemaNodeBuilder child, final String childName, final int lineNum) {
1156 // if parent == null => node is defined under module
1157 // All leafs, leaf-lists, lists, containers, choices, rpcs,
1158 // notifications, and anyxmls defined within a parent node or at the
1159 // top level of the module or its submodules share the same
1160 // identifier namespace.
1161 for (DataSchemaNodeBuilder childNode : getChildNodeBuilders()) {
1162 if (childNode.getQName().getLocalName().equals(childName)) {
1163 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
1166 for (RpcDefinitionBuilder rpc : addedRpcs) {
1167 if (rpc.getQName().getLocalName().equals(childName)) {
1168 raiseYangParserException("'" + child + "'", "rpc", childName, lineNum, rpc.getLine());
1171 for (NotificationBuilder notification : addedNotifications) {
1172 if (notification.getQName().getLocalName().equals(childName)) {
1173 raiseYangParserException("'" + child + "'", "notification", childName, lineNum, notification.getLine());
1176 addChildNode(child);
1180 * Adds child node <code>child</code> to the group of child nodes of the
1181 * <code>parent</code>
1183 * The method reduces the complexity of the method
1184 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1185 * addChildToParent}. *
1188 * builder of node which is parent for <code>child</code>
1190 * data schema node builder for child node
1192 * string with name of child node
1194 * line number in YANG file where is the node with the name equal
1195 * to <code>childName</code> is defined
1197 private void addChildToSubnodeOfModule(final Builder parent, final DataSchemaNodeBuilder child,
1198 final String childName, final int lineNum) {
1199 // no need for checking rpc and notification because they can be
1200 // defined only under module or submodule
1201 if (parent instanceof DataNodeContainerBuilder) {
1202 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
1203 for (DataSchemaNodeBuilder childNode : parentNode.getChildNodeBuilders()) {
1204 if (childNode.getQName().getLocalName().equals(childName)) {
1205 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
1208 parentNode.addChildNode(child);
1209 } else if (parent instanceof ChoiceBuilder) {
1210 ChoiceBuilder parentNode = (ChoiceBuilder) parent;
1211 for (ChoiceCaseBuilder caseBuilder : parentNode.getCases()) {
1212 if (caseBuilder.getQName().getLocalName().equals(childName)) {
1213 raiseYangParserException("'" + child + "'", "node", childName, lineNum, caseBuilder.getLine());
1216 parentNode.addCase(child);
1218 throw new YangParseException(name, lineNum, "Unresolved parent of node '" + childName + "'.");
1222 private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) {
1223 final ModuleImport moduleImport = new ModuleImportImpl(moduleName, revision, prefix);
1224 return moduleImport;
1227 private void raiseYangParserException(final String cantAddType, final String type, final String name,
1228 final int currentLine, final int duplicateLine) {
1230 StringBuilder msgPrefix = new StringBuilder("");
1231 if (cantAddType != null && !cantAddType.isEmpty()) {
1232 msgPrefix.append("Can not add ");
1233 msgPrefix.append(cantAddType);
1234 msgPrefix.append(": ");
1237 String msg = String.format("%s%s with same name '%s' already declared at line %d.", msgPrefix, type, name,
1239 throw new YangParseException(getModuleName(), currentLine, msg);
1243 public int hashCode() {
1244 final int prime = 31;
1246 result = prime * result + ((name == null) ? 0 : name.hashCode());
1247 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1248 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1249 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1255 public boolean equals(final Object obj) {
1262 if (getClass() != obj.getClass()) {
1265 ModuleBuilder other = (ModuleBuilder) obj;
1267 if (other.name != null) {
1270 } else if (!name.equals(other.name)) {
1273 if (namespace == null) {
1274 if (other.namespace != null) {
1277 } else if (!namespace.equals(other.namespace)) {
1280 if (prefix == null) {
1281 if (other.prefix != null) {
1284 } else if (!prefix.equals(other.prefix)) {
1287 if (revision == null) {
1288 if (other.revision != null) {
1291 } else if (!revision.equals(other.revision)) {