2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.builder.impl;
11 import java.util.ArrayList;
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;
21 import java.util.TreeSet;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Deviation;
27 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
28 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
29 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
30 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
33 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
34 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
35 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.UsesNode;
39 import org.opendaylight.yangtools.yang.model.api.YangNode;
40 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
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.GroupingBuilder;
46 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
47 import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
48 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
49 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
50 import org.opendaylight.yangtools.yang.parser.util.Comparators;
51 import org.opendaylight.yangtools.yang.parser.util.ModuleImportImpl;
52 import org.opendaylight.yangtools.yang.parser.util.RefineHolder;
53 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
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 AbstractDataNodeContainerBuilder {
62 private final ModuleImpl instance;
63 private final String name;
64 private final SchemaPath schemaPath;
65 private URI namespace;
66 private String prefix;
67 private Date revision;
69 private final Deque<Builder> actualPath = new LinkedList<>();
70 private final Set<TypeAwareBuilder> dirtyNodes = new HashSet<>();
72 private final Set<ModuleImport> imports = new HashSet<ModuleImport>();
74 private final Set<AugmentationSchema> augments = new HashSet<>();
75 private final List<AugmentationSchemaBuilder> augmentBuilders = new ArrayList<>();
76 private final List<AugmentationSchemaBuilder> allAugments = new ArrayList<>();
78 private final List<GroupingBuilder> allGroupings = new ArrayList<>();
80 private final List<UsesNodeBuilder> allUsesNodes = new ArrayList<>();
82 private final Set<RpcDefinition> rpcs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
83 private final Set<RpcDefinitionBuilder> addedRpcs = new HashSet<>();
85 private final Set<NotificationDefinition> notifications = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
86 private final Set<NotificationBuilder> addedNotifications = new HashSet<>();
88 private final Set<IdentitySchemaNode> identities = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
89 private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<>();
91 private final Set<FeatureDefinition> features = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
92 private final Set<FeatureBuilder> addedFeatures = new HashSet<>();
94 private final Set<Deviation> deviations = new HashSet<>();
95 private final Set<DeviationBuilder> deviationBuilders = new HashSet<>();
97 private final List<ExtensionDefinition> extensions = new ArrayList<>();
98 private final List<ExtensionBuilder> addedExtensions = new ArrayList<>();
100 private final List<UnknownSchemaNodeBuilder> allUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
102 public ModuleBuilder(final String name) {
103 super(name, 0, null);
105 schemaPath = new SchemaPath(Collections.<QName> emptyList(), true);
106 instance = new ModuleImpl(name);
107 actualPath.push(this);
110 public Module build() {
115 * Build new Module object based on this builder.
118 public Module build(YangNode parent) {
119 instance.setPrefix(prefix);
120 instance.setRevision(revision);
121 instance.setImports(imports);
122 instance.setNamespace(namespace);
125 for (TypeDefinitionBuilder tdb : addedTypedefs) {
126 typedefs.add(tdb.build(instance));
128 instance.setTypeDefinitions(typedefs);
131 for (DataSchemaNodeBuilder child : addedChildNodes) {
132 DataSchemaNode childNode = child.build(instance);
133 childNodes.put(childNode.getQName(), childNode);
135 instance.setChildNodes(childNodes);
138 for (GroupingBuilder gb : addedGroupings) {
139 groupings.add(gb.build(instance));
141 instance.setGroupings(groupings);
144 for (UsesNodeBuilder unb : addedUsesNodes) {
145 usesNodes.add(unb.build(instance));
147 instance.setUses(usesNodes);
150 for (FeatureBuilder fb : addedFeatures) {
151 features.add(fb.build(instance));
153 instance.setFeatures(features);
156 for (NotificationBuilder entry : addedNotifications) {
157 notifications.add(entry.build(instance));
159 instance.setNotifications(notifications);
162 for (AugmentationSchemaBuilder builder : augmentBuilders) {
163 augments.add(builder.build(instance));
165 instance.setAugmentations(augments);
168 for (RpcDefinitionBuilder rpc : addedRpcs) {
169 rpcs.add(rpc.build(instance));
171 instance.setRpcs(rpcs);
174 for (DeviationBuilder entry : deviationBuilders) {
175 deviations.add(entry.build(instance));
177 instance.setDeviations(deviations);
180 for (ExtensionBuilder eb : addedExtensions) {
181 extensions.add(eb.build(instance));
183 Collections.sort(extensions, Comparators.SCHEMA_NODE_COMP);
184 instance.setExtensionSchemaNodes(extensions);
187 for (IdentitySchemaNodeBuilder id : addedIdentities) {
188 identities.add(id.build(instance));
190 instance.setIdentities(identities);
193 for (UnknownSchemaNodeBuilder unb : addedUnknownNodes) {
194 unknownNodes.add(unb.build(instance));
196 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
197 instance.setUnknownSchemaNodes(unknownNodes);
202 public boolean isAllUsesDataCollected() {
203 for (UsesNodeBuilder usesNode : allUsesNodes) {
204 if (!usesNode.isDataCollected()) {
212 public void setParent(Builder parent) {
213 throw new YangParseException(name, 0, "Can not set parent to module");
217 public SchemaPath getPath() {
222 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
223 return addedTypedefs;
226 public void enterNode(final Builder node) {
227 actualPath.push(node);
230 public void exitNode() {
234 public Builder getActualNode() {
235 if (actualPath.isEmpty()) {
238 return actualPath.peekFirst();
242 public Builder getActualParent() {
243 if (actualPath.size() < 2) {
246 Builder builderChild = actualPath.removeFirst();
247 Builder builderParent = actualPath.peekFirst();
248 actualPath.addFirst(builderChild);
249 return builderParent;
253 public Set<TypeAwareBuilder> getDirtyNodes() {
257 public List<AugmentationSchemaBuilder> getAllAugments() {
261 public Set<IdentitySchemaNodeBuilder> getIdentities() {
262 return addedIdentities;
265 public List<GroupingBuilder> getAllGroupings() {
269 public List<UsesNodeBuilder> getAllUsesNodes() {
273 public Set<DeviationBuilder> getDeviations() {
274 return deviationBuilders;
277 public List<ExtensionBuilder> getExtensions() {
278 return addedExtensions;
281 public List<UnknownSchemaNodeBuilder> getAllUnknownNodes() {
282 return allUnknownNodes;
285 public String getName() {
289 public URI getNamespace() {
293 public void setNamespace(final URI namespace) {
294 this.namespace = namespace;
297 public String getPrefix() {
301 public Date getRevision() {
305 public void markActualNodeDirty() {
306 final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) getActualNode();
307 dirtyNodes.add(nodeBuilder);
310 public void setRevision(final Date revision) {
311 this.revision = revision;
314 public void setPrefix(final String prefix) {
315 this.prefix = prefix;
318 public void setYangVersion(final String yangVersion) {
319 instance.setYangVersion(yangVersion);
322 public void setDescription(final String description) {
323 instance.setDescription(description);
326 public void setReference(final String reference) {
327 instance.setReference(reference);
330 public void setOrganization(final String organization) {
331 instance.setOrganization(organization);
334 public void setContact(final String contact) {
335 instance.setContact(contact);
338 public boolean addModuleImport(final String moduleName, final Date revision, final String prefix) {
339 final ModuleImport moduleImport = createModuleImport(moduleName, revision, prefix);
340 return imports.add(moduleImport);
343 public Set<ModuleImport> getModuleImports() {
347 public ExtensionBuilder addExtension(final QName qname, final int line) {
348 Builder parent = getActualNode();
349 if (!(parent.equals(this))) {
350 throw new YangParseException(name, line, "extension can be defined only in module or submodule");
353 final String extName = qname.getLocalName();
354 for (ExtensionBuilder addedExtension : addedExtensions) {
355 if (addedExtension.getQName().getLocalName().equals(extName)) {
356 raiseYangParserException("extension", "node", extName, line, addedExtension.getLine());
359 final ExtensionBuilder builder = new ExtensionBuilder(name, line, qname);
360 builder.setParent(parent);
361 addedExtensions.add(builder);
365 public ContainerSchemaNodeBuilder addContainerNode(final int line, final QName qname, final SchemaPath schemaPath) {
366 final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
368 Builder parent = getActualNode();
369 builder.setParent(parent);
370 addChildToParent(parent, builder, qname.getLocalName());
375 public ListSchemaNodeBuilder addListNode(final int line, final QName qname, final SchemaPath schemaPath) {
376 final ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(name, line, qname, schemaPath);
378 Builder parent = getActualNode();
379 builder.setParent(parent);
380 addChildToParent(parent, builder, qname.getLocalName());
385 public LeafSchemaNodeBuilder addLeafNode(final int line, final QName qname, final SchemaPath schemaPath) {
386 final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(name, line, qname, schemaPath);
388 Builder parent = getActualNode();
389 builder.setParent(parent);
390 addChildToParent(parent, builder, qname.getLocalName());
395 public LeafListSchemaNodeBuilder addLeafListNode(final int line, final QName qname, final SchemaPath schemaPath) {
396 final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(name, line, qname, schemaPath);
398 Builder parent = getActualNode();
399 builder.setParent(parent);
400 addChildToParent(parent, builder, qname.getLocalName());
405 public GroupingBuilder addGrouping(final int line, final QName qname) {
406 final GroupingBuilder builder = new GroupingBuilderImpl(name, line, qname);
408 Builder parent = getActualNode();
409 builder.setParent(parent);
411 String groupingName = qname.getLocalName();
412 if (parent.equals(this)) {
413 for (GroupingBuilder addedGrouping : addedGroupings) {
414 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
415 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
418 addedGroupings.add(builder);
420 if (parent instanceof DataNodeContainerBuilder) {
421 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
422 for (GroupingBuilder addedGrouping : parentNode.getGroupingBuilders()) {
423 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
424 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
427 parentNode.addGrouping(builder);
428 } else if (parent instanceof RpcDefinitionBuilder) {
429 RpcDefinitionBuilder parentNode = (RpcDefinitionBuilder) parent;
430 for (GroupingBuilder child : parentNode.getGroupings()) {
431 if (child.getQName().getLocalName().equals(groupingName)) {
432 raiseYangParserException("", "Grouping", groupingName, line, child.getLine());
435 parentNode.addGrouping(builder);
437 throw new YangParseException(name, line, "Unresolved parent of grouping " + groupingName);
441 allGroupings.add(builder);
445 public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr) {
446 final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr);
448 Builder parent = getActualNode();
449 builder.setParent(parent);
451 if (parent.equals(this)) {
452 // augment can be declared only under 'module' ...
453 augmentBuilders.add(builder);
455 // ... or 'uses' statement
456 if (parent instanceof UsesNodeBuilder) {
457 if (augmentTargetStr.startsWith("/")) {
458 throw new YangParseException(name, line,
459 "If 'augment' statement is a substatement to the 'uses' statement, it cannot contain absolute path ("
460 + augmentTargetStr + ")");
462 ((UsesNodeBuilder) parent).addAugment(builder);
464 throw new YangParseException(name, line, "Augment can be declared only under module or uses statement.");
467 allAugments.add(builder);
473 public void addUsesNode(UsesNodeBuilder usesBuilder) {
474 addedUsesNodes.add(usesBuilder);
475 allUsesNodes.add(usesBuilder);
478 public UsesNodeBuilder addUsesNode(final int line, final String groupingPathStr) {
479 final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(name, line, groupingPathStr);
481 Builder parent = getActualNode();
482 usesBuilder.setParent(parent);
484 if (parent.equals(this)) {
485 addedUsesNodes.add(usesBuilder);
487 if (!(parent instanceof DataNodeContainerBuilder)) {
488 throw new YangParseException(name, line, "Unresolved parent of uses '" + groupingPathStr + "'.");
490 ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder);
492 if(parent instanceof AugmentationSchemaBuilder) {
493 usesBuilder.setAugmenting(true);
494 usesBuilder.setParentAugment((AugmentationSchemaBuilder)parent);
497 allUsesNodes.add(usesBuilder);
501 public void addRefine(final RefineHolder refine) {
502 final Builder parent = getActualNode();
503 if (!(parent instanceof UsesNodeBuilder)) {
504 throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
506 ((UsesNodeBuilder) parent).addRefine(refine);
507 refine.setParent(parent);
510 public RpcDefinitionBuilder addRpc(final int line, final QName qname) {
511 Builder parent = getActualNode();
512 if (!(parent.equals(this))) {
513 throw new YangParseException(name, line, "rpc can be defined only in module or submodule");
516 final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(name, line, qname);
517 rpcBuilder.setParent(parent);
519 String rpcName = qname.getLocalName();
520 for (RpcDefinitionBuilder rpc : addedRpcs) {
521 if (rpc.getQName().getLocalName().equals(rpcName)) {
522 raiseYangParserException("", "rpc", rpcName, line, rpc.getLine());
525 for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
526 if (addedChild.getQName().getLocalName().equals(rpcName)) {
527 raiseYangParserException("rpc", "node", rpcName, line, addedChild.getLine());
530 for (NotificationBuilder addedNotification : addedNotifications) {
531 if (addedNotification.getQName().getLocalName().equals(rpcName)) {
532 raiseYangParserException("rpc", "notification", rpcName, line, addedNotification.getLine());
535 addedRpcs.add(rpcBuilder);
539 public ContainerSchemaNodeBuilder addRpcInput(final int line, final QName qname, final SchemaPath schemaPath) {
540 final Builder parent = getActualNode();
541 if (!(parent instanceof RpcDefinitionBuilder)) {
542 throw new YangParseException(name, line, "input can be defined only in rpc statement");
544 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
546 final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
547 inputBuilder.setParent(rpc);
549 rpc.setInput(inputBuilder);
553 public ContainerSchemaNodeBuilder addRpcOutput(final SchemaPath schemaPath, final QName qname, final int line) {
554 final Builder parent = getActualNode();
555 if (!(parent instanceof RpcDefinitionBuilder)) {
556 throw new YangParseException(name, line, "output can be defined only in rpc statement");
558 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
560 final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
561 outputBuilder.setParent(rpc);
563 rpc.setOutput(outputBuilder);
564 return outputBuilder;
567 public void addNotification(NotificationDefinition notification) {
568 notifications.add(notification);
571 public NotificationBuilder addNotification(final int line, final QName qname) {
572 final Builder parent = getActualNode();
573 if (!(parent.equals(this))) {
574 throw new YangParseException(name, line, "notification can be defined only in module or submodule");
577 String notificationName = qname.getLocalName();
578 for (NotificationBuilder nb : addedNotifications) {
579 if (nb.getQName().equals(qname)) {
580 raiseYangParserException("", "notification", notificationName, line, nb.getLine());
583 for (RpcDefinitionBuilder rpc : addedRpcs) {
584 if (rpc.getQName().getLocalName().equals(notificationName)) {
585 raiseYangParserException("notification", "rpc", notificationName, line, rpc.getLine());
588 for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
589 if (addedChild.getQName().getLocalName().equals(notificationName)) {
590 raiseYangParserException("notification", "node", notificationName, line, addedChild.getLine());
594 final NotificationBuilder builder = new NotificationBuilder(name, line, qname);
595 builder.setParent(parent);
596 addedNotifications.add(builder);
601 public FeatureBuilder addFeature(final int line, final QName qname) {
602 Builder parent = getActualNode();
603 if (!(parent.equals(this))) {
604 throw new YangParseException(name, line, "feature can be defined only in module or submodule");
607 final FeatureBuilder builder = new FeatureBuilder(name, line, qname);
608 builder.setParent(parent);
610 String featureName = qname.getLocalName();
611 for (FeatureBuilder addedFeature : addedFeatures) {
612 if (addedFeature.getQName().getLocalName().equals(featureName)) {
613 raiseYangParserException("", "feature", featureName, line, addedFeature.getLine());
616 addedFeatures.add(builder);
620 public ChoiceBuilder addChoice(final int line, final QName qname) {
621 final ChoiceBuilder builder = new ChoiceBuilder(name, line, qname);
623 Builder parent = getActualNode();
624 builder.setParent(parent);
625 addChildToParent(parent, builder, qname.getLocalName());
630 public ChoiceCaseBuilder addCase(final int line, final QName qname) {
631 Builder parent = getActualNode();
632 if (parent == null || parent.equals(this)) {
633 throw new YangParseException(name, line, "'case' parent not found");
636 final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(name, line, qname);
637 builder.setParent(parent);
639 if (parent instanceof ChoiceBuilder) {
640 ((ChoiceBuilder) parent).addCase(builder);
641 } else if (parent instanceof AugmentationSchemaBuilder) {
642 ((AugmentationSchemaBuilder) parent).addChildNode(builder);
644 throw new YangParseException(name, line, "Unresolved parent of 'case' " + qname.getLocalName());
650 public AnyXmlBuilder addAnyXml(final int line, final QName qname, final SchemaPath schemaPath) {
651 final AnyXmlBuilder builder = new AnyXmlBuilder(name, line, qname, schemaPath);
653 Builder parent = getActualNode();
654 builder.setParent(parent);
655 addChildToParent(parent, builder, qname.getLocalName());
661 public void addTypedef(TypeDefinitionBuilder typedefBuilder) {
662 String nodeName = typedefBuilder.getQName().getLocalName();
663 for (TypeDefinitionBuilder tdb : addedTypedefs) {
664 if (tdb.getQName().getLocalName().equals(nodeName)) {
665 raiseYangParserException("", "typedef", nodeName, typedefBuilder.getLine(), tdb.getLine());
668 addedTypedefs.add(typedefBuilder);
671 public TypeDefinitionBuilderImpl addTypedef(final int line, final QName qname) {
672 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(name, line, qname);
674 Builder parent = getActualNode();
675 builder.setParent(parent);
677 String typedefName = qname.getLocalName();
678 if (parent.equals(this)) {
679 for (TypeDefinitionBuilder tdb : addedTypedefs) {
680 if (tdb.getQName().getLocalName().equals(typedefName)) {
681 raiseYangParserException("", "typedef", typedefName, line, tdb.getLine());
684 addedTypedefs.add(builder);
686 if (parent instanceof DataNodeContainerBuilder) {
687 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
688 for (TypeDefinitionBuilder child : parentNode.getTypeDefinitionBuilders()) {
689 if (child.getQName().getLocalName().equals(typedefName)) {
690 raiseYangParserException("", "typedef", typedefName, line, child.getLine());
693 parentNode.addTypedef(builder);
694 } else if (parent instanceof RpcDefinitionBuilder) {
695 RpcDefinitionBuilder rpcParent = (RpcDefinitionBuilder) parent;
696 for (TypeDefinitionBuilder tdb : rpcParent.getTypeDefinitions()) {
697 if (tdb.getQName().getLocalName().equals(builder.getQName().getLocalName())) {
698 raiseYangParserException("", "typedef", typedefName, line, tdb.getLine());
701 rpcParent.addTypedef(builder);
703 throw new YangParseException(name, line, "Unresolved parent of typedef " + typedefName);
710 public void setType(final TypeDefinition<?> type) {
711 Builder parent = getActualNode();
712 if (!(parent instanceof TypeAwareBuilder)) {
713 throw new YangParseException("Failed to set type '" + type.getQName().getLocalName()
714 + "'. Invalid parent node: " + parent);
716 ((TypeAwareBuilder) parent).setType(type);
719 public UnionTypeBuilder addUnionType(final int line, final URI namespace, final Date revision) {
720 final Builder parent = getActualNode();
721 if (parent == null) {
722 throw new YangParseException(name, line, "Unresolved parent of union type");
724 final UnionTypeBuilder union = new UnionTypeBuilder(name, line);
725 if (parent instanceof TypeAwareBuilder) {
726 ((TypeAwareBuilder) parent).setTypedef(union);
729 throw new YangParseException(name, line, "Invalid parent of union type.");
734 public void addIdentityrefType(final int line, final SchemaPath schemaPath, final String baseString) {
735 final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(name, line, baseString, schemaPath);
737 final Builder parent = getActualNode();
738 if (parent == null) {
739 throw new YangParseException(name, line, "Unresolved parent of identityref type.");
741 if (parent instanceof TypeAwareBuilder) {
742 final TypeAwareBuilder typeParent = (TypeAwareBuilder) parent;
743 typeParent.setTypedef(identityref);
744 dirtyNodes.add(typeParent);
746 throw new YangParseException(name, line, "Invalid parent of identityref type.");
751 public DeviationBuilder addDeviation(final int line, final String targetPath) {
752 Builder parent = getActualNode();
753 if (!(parent.equals(this))) {
754 throw new YangParseException(name, line, "deviation can be defined only in module or submodule");
757 final DeviationBuilder builder = new DeviationBuilder(name, line, targetPath);
758 builder.setParent(parent);
759 deviationBuilders.add(builder);
763 public IdentitySchemaNodeBuilder addIdentity(final QName qname, final int line) {
764 Builder parent = getActualNode();
765 if (!(parent.equals(this))) {
766 throw new YangParseException(name, line, "identity can be defined only in module or submodule");
768 String identityName = qname.getLocalName();
769 for (IdentitySchemaNodeBuilder idBuilder : addedIdentities) {
770 if (idBuilder.getQName().equals(qname)) {
771 raiseYangParserException("", "identity", identityName, line, idBuilder.getLine());
775 final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(name, line, qname);
776 builder.setParent(parent);
777 addedIdentities.add(builder);
782 public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder builder) {
783 addedUnknownNodes.add(builder);
784 allUnknownNodes.add(builder);
787 public UnknownSchemaNodeBuilder addUnknownSchemaNode(final int line, final QName qname) {
788 final Builder parent = getActualNode();
789 final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(name, line, qname);
790 builder.setParent(parent);
791 allUnknownNodes.add(builder);
793 if (parent.equals(this)) {
794 addedUnknownNodes.add(builder);
796 if (parent instanceof SchemaNodeBuilder) {
797 ((SchemaNodeBuilder) parent).addUnknownNodeBuilder(builder);
798 } else if (parent instanceof DataNodeContainerBuilder) {
799 ((DataNodeContainerBuilder) parent).addUnknownNodeBuilder(builder);
800 } else if (parent instanceof RefineHolder) {
801 ((RefineHolder) parent).addUnknownNodeBuilder(builder);
803 throw new YangParseException(name, line, "Unresolved parent of unknown node '" + qname.getLocalName()
811 public Set<RpcDefinitionBuilder> getRpcs() {
815 public Set<NotificationBuilder> getNotifications() {
816 return addedNotifications;
820 public String toString() {
821 return "module " + name;
824 private final class ModuleImpl implements Module {
825 private URI namespace;
826 private final String name;
827 private Date revision;
828 private String prefix;
829 private String yangVersion;
830 private String description;
831 private String reference;
832 private String organization;
833 private String contact;
834 private Set<ModuleImport> imports = Collections.emptySet();
835 private Set<FeatureDefinition> features = Collections.emptySet();
836 private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
837 private Set<NotificationDefinition> notifications = Collections.emptySet();
838 private Set<AugmentationSchema> augmentations = Collections.emptySet();
839 private Set<RpcDefinition> rpcs = Collections.emptySet();
840 private Set<Deviation> deviations = Collections.emptySet();
841 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
842 private Set<GroupingDefinition> groupings = Collections.emptySet();
843 private Set<UsesNode> uses = Collections.emptySet();
844 private List<ExtensionDefinition> extensionNodes = Collections.emptyList();
845 private Set<IdentitySchemaNode> identities = Collections.emptySet();
846 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
848 private ModuleImpl(String name) {
853 public YangNode getParent() {
858 public URI getNamespace() {
862 private void setNamespace(URI namespace) {
863 this.namespace = namespace;
867 public String getName() {
872 public Date getRevision() {
876 private void setRevision(Date revision) {
877 this.revision = revision;
881 public String getPrefix() {
885 private void setPrefix(String prefix) {
886 this.prefix = prefix;
890 public String getYangVersion() {
894 private void setYangVersion(String yangVersion) {
895 this.yangVersion = yangVersion;
899 public String getDescription() {
903 private void setDescription(String description) {
904 this.description = description;
908 public String getReference() {
912 private void setReference(String reference) {
913 this.reference = reference;
917 public String getOrganization() {
921 private void setOrganization(String organization) {
922 this.organization = organization;
926 public String getContact() {
930 private void setContact(String contact) {
931 this.contact = contact;
935 public Set<ModuleImport> getImports() {
939 private void setImports(Set<ModuleImport> imports) {
940 if (imports != null) {
941 this.imports = imports;
946 public Set<FeatureDefinition> getFeatures() {
950 private void setFeatures(Set<FeatureDefinition> features) {
951 if (features != null) {
952 this.features = features;
957 public Set<TypeDefinition<?>> getTypeDefinitions() {
958 return typeDefinitions;
961 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
962 if (typeDefinitions != null) {
963 this.typeDefinitions = typeDefinitions;
968 public Set<NotificationDefinition> getNotifications() {
969 return notifications;
972 private void setNotifications(Set<NotificationDefinition> notifications) {
973 if (notifications != null) {
974 this.notifications = notifications;
979 public Set<AugmentationSchema> getAugmentations() {
980 return augmentations;
983 private void setAugmentations(Set<AugmentationSchema> augmentations) {
984 if (augmentations != null) {
985 this.augmentations = augmentations;
990 public Set<RpcDefinition> getRpcs() {
994 private void setRpcs(Set<RpcDefinition> rpcs) {
1001 public Set<Deviation> getDeviations() {
1005 private void setDeviations(Set<Deviation> deviations) {
1006 if (deviations != null) {
1007 this.deviations = deviations;
1012 public Set<DataSchemaNode> getChildNodes() {
1013 return new LinkedHashSet<DataSchemaNode>(childNodes.values());
1016 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
1017 if (childNodes != null) {
1018 this.childNodes = childNodes;
1023 public Set<GroupingDefinition> getGroupings() {
1027 private void setGroupings(Set<GroupingDefinition> groupings) {
1028 if (groupings != null) {
1029 this.groupings = groupings;
1034 public Set<UsesNode> getUses() {
1038 private void setUses(Set<UsesNode> uses) {
1045 public List<ExtensionDefinition> getExtensionSchemaNodes() {
1046 return extensionNodes;
1049 private void setExtensionSchemaNodes(final List<ExtensionDefinition> extensionNodes) {
1050 if (extensionNodes != null) {
1051 this.extensionNodes = extensionNodes;
1056 public Set<IdentitySchemaNode> getIdentities() {
1060 private void setIdentities(final Set<IdentitySchemaNode> identities) {
1061 if (identities != null) {
1062 this.identities = identities;
1067 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
1068 return unknownNodes;
1071 private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
1072 if (unknownNodes != null) {
1073 this.unknownNodes = unknownNodes;
1078 public DataSchemaNode getDataChildByName(QName name) {
1079 return childNodes.get(name);
1083 public DataSchemaNode getDataChildByName(String name) {
1084 DataSchemaNode result = null;
1085 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
1086 if (entry.getKey().getLocalName().equals(name)) {
1087 result = entry.getValue();
1095 public int hashCode() {
1096 final int prime = 31;
1098 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1099 result = prime * result + ((name == null) ? 0 : name.hashCode());
1100 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1101 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1102 result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());
1107 public boolean equals(Object obj) {
1114 if (getClass() != obj.getClass()) {
1117 ModuleImpl other = (ModuleImpl) obj;
1118 if (namespace == null) {
1119 if (other.namespace != null) {
1122 } else if (!namespace.equals(other.namespace)) {
1126 if (other.name != null) {
1129 } else if (!name.equals(other.name)) {
1132 if (revision == null) {
1133 if (other.revision != null) {
1136 } else if (!revision.equals(other.revision)) {
1139 if (prefix == null) {
1140 if (other.prefix != null) {
1143 } else if (!prefix.equals(other.prefix)) {
1146 if (yangVersion == null) {
1147 if (other.yangVersion != null) {
1150 } else if (!yangVersion.equals(other.yangVersion)) {
1157 public String toString() {
1158 StringBuilder sb = new StringBuilder(ModuleImpl.class.getSimpleName());
1160 sb.append("name=" + name);
1161 sb.append(", namespace=" + namespace);
1162 sb.append(", revision=" + revision);
1163 sb.append(", prefix=" + prefix);
1164 sb.append(", yangVersion=" + yangVersion);
1166 return sb.toString();
1171 * Add child to parent. Method checks for duplicates and add given child
1172 * node to parent. If node with same name is found, throws exception. If
1173 * parent is null, child node will be added directly to module.
1179 private void addChildToParent(final Builder parent, final DataSchemaNodeBuilder child, final String childName) {
1180 final int lineNum = child.getLine();
1181 if (parent.equals(this)) {
1182 addChildToModule(child, childName, lineNum);
1184 addChildToSubnodeOfModule(parent, child, childName, lineNum);
1189 * Adds child node <code>child</code> to the set of nodes child nodes.
1191 * The method reduces the complexity of the method
1192 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1193 * addChildToParent}.
1196 * data schema node builder for child node
1198 * string with name of child node
1200 * line number in YANG file where is the node with the name equal
1201 * to <code>childName</code> is defined
1203 private void addChildToModule(final DataSchemaNodeBuilder child, final String childName, final int lineNum) {
1204 // if parent == null => node is defined under module
1205 // All leafs, leaf-lists, lists, containers, choices, rpcs,
1206 // notifications, and anyxmls defined within a parent node or at the
1207 // top level of the module or its submodules share the same
1208 // identifier namespace.
1209 for (DataSchemaNodeBuilder childNode : addedChildNodes) {
1210 if (childNode.getQName().getLocalName().equals(childName)) {
1211 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
1214 for (RpcDefinitionBuilder rpc : addedRpcs) {
1215 if (rpc.getQName().getLocalName().equals(childName)) {
1216 raiseYangParserException("'" + child + "'", "rpc", childName, lineNum, rpc.getLine());
1219 for (NotificationBuilder notification : addedNotifications) {
1220 if (notification.getQName().getLocalName().equals(childName)) {
1221 raiseYangParserException("'" + child + "'", "notification", childName, lineNum, notification.getLine());
1224 addedChildNodes.add(child);
1228 * Adds child node <code>child</code> to the group of child nodes of the
1229 * <code>parent</code>
1231 * The method reduces the complexity of the method
1232 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1233 * addChildToParent}. *
1236 * builder of node which is parent for <code>child</code>
1238 * data schema node builder for child node
1240 * string with name of child node
1242 * line number in YANG file where is the node with the name equal
1243 * to <code>childName</code> is defined
1245 private void addChildToSubnodeOfModule(final Builder parent, final DataSchemaNodeBuilder child,
1246 final String childName, final int lineNum) {
1247 // no need for checking rpc and notification because they can be
1248 // defined only under module or submodule
1249 if (parent instanceof DataNodeContainerBuilder) {
1250 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
1251 for (DataSchemaNodeBuilder childNode : parentNode.getChildNodeBuilders()) {
1252 if (childNode.getQName().getLocalName().equals(childName)) {
1253 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
1256 parentNode.addChildNode(child);
1257 } else if (parent instanceof ChoiceBuilder) {
1258 ChoiceBuilder parentNode = (ChoiceBuilder) parent;
1259 for (ChoiceCaseBuilder caseBuilder : parentNode.getCases()) {
1260 if (caseBuilder.getQName().getLocalName().equals(childName)) {
1261 raiseYangParserException("'" + child + "'", "node", childName, lineNum, caseBuilder.getLine());
1264 parentNode.addCase(child);
1266 throw new YangParseException(name, lineNum, "Unresolved parent of node '" + childName + "'.");
1270 private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) {
1271 final ModuleImport moduleImport = new ModuleImportImpl(moduleName, revision, prefix);
1272 return moduleImport;
1275 private void raiseYangParserException(final String cantAddType, final String type, final String name,
1276 final int currentLine, final int duplicateLine) {
1278 StringBuilder msgPrefix = new StringBuilder("");
1279 if (cantAddType != null && !cantAddType.isEmpty()) {
1280 msgPrefix.append("Can not add ");
1281 msgPrefix.append(cantAddType);
1282 msgPrefix.append(": ");
1285 String msg = String.format("%s%s with same name '%s' already declared at line %d.", msgPrefix, type, name,
1287 throw new YangParseException(moduleName, currentLine, msg);
1291 public int hashCode() {
1292 final int prime = 31;
1294 result = prime * result + ((name == null) ? 0 : name.hashCode());
1295 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1296 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1297 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1303 public boolean equals(Object obj) {
1310 if (getClass() != obj.getClass()) {
1313 ModuleBuilder other = (ModuleBuilder) obj;
1315 if (other.name != null) {
1318 } else if (!name.equals(other.name)) {
1321 if (namespace == null) {
1322 if (other.namespace != null) {
1325 } else if (!namespace.equals(other.namespace)) {
1328 if (prefix == null) {
1329 if (other.prefix != null) {
1332 } else if (!prefix.equals(other.prefix)) {
1335 if (revision == null) {
1336 if (other.revision != null) {
1339 } else if (!revision.equals(other.revision)) {