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.TreeMap;
22 import java.util.TreeSet;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Deviation;
28 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
29 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
30 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
31 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
34 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
35 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
36 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
37 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.UsesNode;
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.RefineHolder;
52 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
55 * Builder of Module object. If this module is dependent on external
56 * module/modules, these dependencies must be resolved before module is built,
57 * otherwise result may not be valid.
59 public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
61 private final ModuleImpl instance;
62 private final String name;
63 private final SchemaPath schemaPath;
64 private URI namespace;
65 private String prefix;
66 private Date revision;
68 private final Deque<Builder> actualPath = new LinkedList<Builder>();
69 private final Set<TypeAwareBuilder> dirtyNodes = new HashSet<TypeAwareBuilder>();
71 private final Set<ModuleImport> imports = new HashSet<ModuleImport>();
72 private final List<AugmentationSchemaBuilder> addedAugments = new ArrayList<AugmentationSchemaBuilder>();
73 private final List<AugmentationSchemaBuilder> allAugments = new ArrayList<AugmentationSchemaBuilder>();
74 private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
75 private final List<UsesNodeBuilder> allUsesNodes = new ArrayList<UsesNodeBuilder>();
76 private final Set<RpcDefinitionBuilder> addedRpcs = new HashSet<RpcDefinitionBuilder>();
77 private final Set<NotificationBuilder> addedNotifications = new HashSet<NotificationBuilder>();
78 private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<IdentitySchemaNodeBuilder>();
79 private final Set<FeatureBuilder> addedFeatures = new HashSet<FeatureBuilder>();
80 private final Set<DeviationBuilder> addedDeviations = new HashSet<DeviationBuilder>();
81 private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
82 private final List<ExtensionBuilder> addedExtensions = new ArrayList<ExtensionBuilder>();
83 private final List<UnknownSchemaNodeBuilder> allUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
85 public ModuleBuilder(final String name) {
88 schemaPath = new SchemaPath(Collections.<QName> emptyList(), true);
89 instance = new ModuleImpl(name);
90 actualPath.push(this);
94 * Build new Module object based on this builder.
97 public Module build() {
98 instance.setPrefix(prefix);
99 instance.setRevision(revision);
100 instance.setImports(imports);
101 instance.setNamespace(namespace);
104 final Set<TypeDefinition<?>> typedefs = new TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
105 for (TypeDefinitionBuilder tdb : addedTypedefs) {
106 typedefs.add(tdb.build());
108 instance.setTypeDefinitions(typedefs);
111 final Map<QName, DataSchemaNode> children = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
112 for (DataSchemaNodeBuilder child : addedChildNodes) {
113 children.put(child.getQName(), child.build());
115 instance.setChildNodes(children);
118 final Set<GroupingDefinition> groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
119 for (GroupingBuilder gb : addedGroupings) {
120 groupings.add(gb.build());
122 instance.setGroupings(groupings);
125 final Set<UsesNode> usesDefinitions = new HashSet<UsesNode>();
126 for (UsesNodeBuilder unb : addedUsesNodes) {
127 usesDefinitions.add(unb.build());
129 instance.setUses(usesDefinitions);
132 final Set<FeatureDefinition> features = new TreeSet<FeatureDefinition>(Comparators.SCHEMA_NODE_COMP);
133 for (FeatureBuilder fb : addedFeatures) {
134 features.add(fb.build());
136 instance.setFeatures(features);
139 final Set<NotificationDefinition> notifications = new TreeSet<NotificationDefinition>(
140 Comparators.SCHEMA_NODE_COMP);
141 for (NotificationBuilder entry : addedNotifications) {
142 notifications.add(entry.build());
144 instance.setNotifications(notifications);
147 final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
148 for (AugmentationSchemaBuilder builder : addedAugments) {
149 augmentations.add(builder.build());
151 instance.setAugmentations(augmentations);
154 final Set<RpcDefinition> rpcs = new TreeSet<RpcDefinition>(Comparators.SCHEMA_NODE_COMP);
155 for (RpcDefinitionBuilder rpc : addedRpcs) {
156 rpcs.add(rpc.build());
158 instance.setRpcs(rpcs);
161 final Set<Deviation> deviations = new HashSet<Deviation>();
162 for (DeviationBuilder entry : addedDeviations) {
163 deviations.add(entry.build());
165 instance.setDeviations(deviations);
168 final List<ExtensionDefinition> extensions = new ArrayList<ExtensionDefinition>();
169 for (ExtensionBuilder eb : addedExtensions) {
170 extensions.add(eb.build());
172 Collections.sort(extensions, Comparators.SCHEMA_NODE_COMP);
173 instance.setExtensionSchemaNodes(extensions);
176 final Set<IdentitySchemaNode> identities = new TreeSet<IdentitySchemaNode>(Comparators.SCHEMA_NODE_COMP);
177 for (IdentitySchemaNodeBuilder id : addedIdentities) {
178 identities.add(id.build());
180 instance.setIdentities(identities);
183 final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
184 for (UnknownSchemaNodeBuilder unb : addedUnknownNodes) {
185 unknownNodes.add(unb.build());
187 instance.setUnknownSchemaNodes(unknownNodes);
192 public boolean isAllUsesDataCollected() {
193 for (UsesNodeBuilder usesNode : allUsesNodes) {
194 if (!usesNode.isDataCollected()) {
202 public void setParent(Builder parent) {
203 throw new YangParseException(name, 0, "Can not set parent to module");
207 public SchemaPath getPath() {
212 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
213 return addedTypedefs;
216 public void enterNode(final Builder node) {
217 actualPath.push(node);
220 public void exitNode() {
224 public Builder getActualNode() {
225 if (actualPath.isEmpty()) {
228 return actualPath.peekFirst();
232 public Builder getActualParent() {
233 if (actualPath.size() < 2) {
236 Builder builderChild = actualPath.removeFirst();
237 Builder builderParent = actualPath.peekFirst();
238 actualPath.addFirst(builderChild);
239 return builderParent;
243 public Set<TypeAwareBuilder> getDirtyNodes() {
247 public List<AugmentationSchemaBuilder> getAllAugments() {
251 public Set<IdentitySchemaNodeBuilder> getIdentities() {
252 return addedIdentities;
255 public List<UsesNodeBuilder> getAllUsesNodes() {
259 public Set<DeviationBuilder> getDeviations() {
260 return addedDeviations;
263 public List<ExtensionBuilder> getExtensions() {
264 return addedExtensions;
267 public List<UnknownSchemaNodeBuilder> getAllUnknownNodes() {
268 return allUnknownNodes;
271 public String getName() {
275 public URI getNamespace() {
279 public void setNamespace(final URI namespace) {
280 this.namespace = namespace;
283 public String getPrefix() {
287 public Date getRevision() {
291 public void markActualNodeDirty() {
292 final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) getActualNode();
293 dirtyNodes.add(nodeBuilder);
296 public void setRevision(final Date revision) {
297 this.revision = revision;
300 public void setPrefix(final String prefix) {
301 this.prefix = prefix;
304 public void setYangVersion(final String yangVersion) {
305 instance.setYangVersion(yangVersion);
308 public void setDescription(final String description) {
309 instance.setDescription(description);
312 public void setReference(final String reference) {
313 instance.setReference(reference);
316 public void setOrganization(final String organization) {
317 instance.setOrganization(organization);
320 public void setContact(final String contact) {
321 instance.setContact(contact);
324 public boolean addModuleImport(final String moduleName, final Date revision, final String prefix) {
325 final ModuleImport moduleImport = createModuleImport(moduleName, revision, prefix);
326 return imports.add(moduleImport);
329 public Set<ModuleImport> getModuleImports() {
333 public ExtensionBuilder addExtension(final QName qname, final int line) {
334 final String extName = qname.getLocalName();
335 for (ExtensionBuilder addedExtension : addedExtensions) {
336 if (addedExtension.getQName().getLocalName().equals(extName)) {
337 raiseYangParserException("extension", "node", extName, line, addedExtension.getLine());
340 final ExtensionBuilder builder = new ExtensionBuilder(name, line, qname);
341 addedExtensions.add(builder);
345 public ContainerSchemaNodeBuilder addContainerNode(final int line, final QName qname, final SchemaPath schemaPath) {
346 final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
348 Builder parent = getActualNode();
349 builder.setParent(parent);
350 addChildToParent(parent, builder, qname.getLocalName());
355 public ListSchemaNodeBuilder addListNode(final int line, final QName qname, final SchemaPath schemaPath) {
356 final ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(name, line, qname, schemaPath);
358 Builder parent = getActualNode();
359 builder.setParent(parent);
360 addChildToParent(parent, builder, qname.getLocalName());
365 public LeafSchemaNodeBuilder addLeafNode(final int line, final QName qname, final SchemaPath schemaPath) {
366 final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(name, line, qname, schemaPath);
368 Builder parent = getActualNode();
369 builder.setParent(parent);
370 addChildToParent(parent, builder, qname.getLocalName());
375 public LeafListSchemaNodeBuilder addLeafListNode(final int line, final QName qname, final SchemaPath schemaPath) {
376 final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(name, line, qname, schemaPath);
378 Builder parent = getActualNode();
379 builder.setParent(parent);
380 addChildToParent(parent, builder, qname.getLocalName());
385 public GroupingBuilder addGrouping(final int line, final QName qname) {
386 final GroupingBuilder builder = new GroupingBuilderImpl(name, line, qname);
388 Builder parent = getActualNode();
389 builder.setParent(parent);
391 String groupingName = qname.getLocalName();
392 if (parent.equals(this)) {
393 for (GroupingBuilder addedGrouping : addedGroupings) {
394 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
395 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
398 addedGroupings.add(builder);
400 if (parent instanceof DataNodeContainerBuilder) {
401 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
402 for (GroupingBuilder addedGrouping : parentNode.getGroupingBuilders()) {
403 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
404 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
407 parentNode.addGrouping(builder);
408 } else if (parent instanceof RpcDefinitionBuilder) {
409 RpcDefinitionBuilder parentNode = (RpcDefinitionBuilder) parent;
410 for (GroupingBuilder child : parentNode.getGroupings()) {
411 if (child.getQName().getLocalName().equals(groupingName)) {
412 raiseYangParserException("", "Grouping", groupingName, line, child.getLine());
415 parentNode.addGrouping(builder);
417 throw new YangParseException(name, line, "Unresolved parent of grouping " + groupingName);
424 public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr) {
425 final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr);
427 Builder parent = getActualNode();
428 builder.setParent(parent);
430 if (parent.equals(this)) {
431 // augment can be declared only under 'module' ...
432 addedAugments.add(builder);
434 // ... or 'uses' statement
435 if (parent instanceof UsesNodeBuilder) {
436 ((UsesNodeBuilder) parent).addAugment(builder);
438 throw new YangParseException(name, line, "Augment can be declared only under module or uses statement.");
441 allAugments.add(builder);
447 public Set<UsesNodeBuilder> getUsesNodes() {
448 return addedUsesNodes;
452 public void addUsesNode(UsesNodeBuilder usesBuilder) {
453 addedUsesNodes.add(usesBuilder);
454 allUsesNodes.add(usesBuilder);
457 public UsesNodeBuilder addUsesNode(final int line, final String groupingPathStr) {
458 final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(name, line, groupingPathStr);
460 Builder parent = getActualNode();
461 usesBuilder.setParent(parent);
463 if (parent.equals(this)) {
464 addedUsesNodes.add(usesBuilder);
466 if (!(parent instanceof DataNodeContainerBuilder)) {
467 throw new YangParseException(name, line, "Unresolved parent of uses '" + groupingPathStr + "'.");
469 ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder);
471 if(parent instanceof AugmentationSchemaBuilder) {
472 usesBuilder.setAugmenting(true);
473 usesBuilder.setParentAugment((AugmentationSchemaBuilder)parent);
476 allUsesNodes.add(usesBuilder);
480 public void addRefine(final RefineHolder refine) {
481 final Builder parent = getActualNode();
482 if (!(parent instanceof UsesNodeBuilder)) {
483 throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
485 ((UsesNodeBuilder) parent).addRefine(refine);
486 refine.setParent(parent);
489 public RpcDefinitionBuilder addRpc(final int line, final QName qname) {
490 Builder parent = getActualNode();
491 if (!(parent.equals(this))) {
492 throw new YangParseException(name, line, "rpc can be defined only in module or submodule");
495 final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(name, line, qname);
496 rpcBuilder.setParent(parent);
498 String rpcName = qname.getLocalName();
499 for (RpcDefinitionBuilder rpc : addedRpcs) {
500 if (rpc.getQName().getLocalName().equals(rpcName)) {
501 raiseYangParserException("", "rpc", rpcName, line, rpc.getLine());
504 for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
505 if (addedChild.getQName().getLocalName().equals(rpcName)) {
506 raiseYangParserException("rpc", "node", rpcName, line, addedChild.getLine());
509 for (NotificationBuilder addedNotification : addedNotifications) {
510 if (addedNotification.getQName().getLocalName().equals(rpcName)) {
511 raiseYangParserException("rpc", "notification", rpcName, line, addedNotification.getLine());
514 addedRpcs.add(rpcBuilder);
518 public ContainerSchemaNodeBuilder addRpcInput(final int line, final QName qname, final SchemaPath schemaPath) {
519 final Builder parent = getActualNode();
520 if (!(parent instanceof RpcDefinitionBuilder)) {
521 throw new YangParseException(name, line, "input can be defined only in rpc statement");
523 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
525 final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
526 inputBuilder.setParent(rpc);
528 rpc.setInput(inputBuilder);
532 public ContainerSchemaNodeBuilder addRpcOutput(final SchemaPath schemaPath, final QName qname, final int line) {
533 final Builder parent = getActualNode();
534 if (!(parent instanceof RpcDefinitionBuilder)) {
535 throw new YangParseException(name, line, "output can be defined only in rpc statement");
537 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
539 final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
540 outputBuilder.setParent(rpc);
542 rpc.setOutput(outputBuilder);
543 return outputBuilder;
546 public NotificationBuilder addNotification(final int line, final QName qname) {
547 final Builder parent = getActualNode();
548 if (!(parent.equals(this))) {
549 throw new YangParseException(name, line, "notification can be defined only in module or submodule");
552 String notificationName = qname.getLocalName();
553 for (NotificationBuilder nb : addedNotifications) {
554 if (nb.getQName().equals(qname)) {
555 raiseYangParserException("", "notification", notificationName, line, nb.getLine());
558 for (RpcDefinitionBuilder rpc : addedRpcs) {
559 if (rpc.getQName().getLocalName().equals(notificationName)) {
560 raiseYangParserException("notification", "rpc", notificationName, line, rpc.getLine());
563 for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
564 if (addedChild.getQName().getLocalName().equals(notificationName)) {
565 raiseYangParserException("notification", "node", notificationName, line, addedChild.getLine());
569 final NotificationBuilder builder = new NotificationBuilder(name, line, qname);
570 builder.setParent(parent);
571 addedNotifications.add(builder);
576 public FeatureBuilder addFeature(final int line, final QName qname) {
577 Builder parent = getActualNode();
578 if (!(parent.equals(this))) {
579 throw new YangParseException(name, line, "feature can be defined only in module or submodule");
582 final FeatureBuilder builder = new FeatureBuilder(name, line, qname);
583 builder.setParent(parent);
585 String featureName = qname.getLocalName();
586 for (FeatureBuilder addedFeature : addedFeatures) {
587 if (addedFeature.getQName().getLocalName().equals(featureName)) {
588 raiseYangParserException("", "feature", featureName, line, addedFeature.getLine());
591 addedFeatures.add(builder);
595 public ChoiceBuilder addChoice(final int line, final QName qname) {
596 final ChoiceBuilder builder = new ChoiceBuilder(name, line, qname);
598 Builder parent = getActualNode();
599 builder.setParent(parent);
600 addChildToParent(parent, builder, qname.getLocalName());
605 public ChoiceCaseBuilder addCase(final int line, final QName qname) {
606 Builder parent = getActualNode();
607 if (parent == null || parent.equals(this)) {
608 throw new YangParseException(name, line, "'case' parent not found");
611 final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(name, line, qname);
612 builder.setParent(parent);
614 if (parent instanceof ChoiceBuilder) {
615 ((ChoiceBuilder) parent).addCase(builder);
616 } else if (parent instanceof AugmentationSchemaBuilder) {
617 ((AugmentationSchemaBuilder) parent).addChildNode(builder);
619 throw new YangParseException(name, line, "Unresolved parent of 'case' " + qname.getLocalName());
625 public AnyXmlBuilder addAnyXml(final int line, final QName qname, final SchemaPath schemaPath) {
626 final AnyXmlBuilder builder = new AnyXmlBuilder(name, line, qname, schemaPath);
628 Builder parent = getActualNode();
629 builder.setParent(parent);
630 addChildToParent(parent, builder, qname.getLocalName());
636 public void addTypedef(TypeDefinitionBuilder typedefBuilder) {
637 String nodeName = typedefBuilder.getQName().getLocalName();
638 for (TypeDefinitionBuilder tdb : addedTypedefs) {
639 if (tdb.getQName().getLocalName().equals(nodeName)) {
640 raiseYangParserException("", "typedef", nodeName, typedefBuilder.getLine(), tdb.getLine());
643 addedTypedefs.add(typedefBuilder);
646 public TypeDefinitionBuilderImpl addTypedef(final int line, final QName qname) {
647 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(name, line, qname);
649 Builder parent = getActualNode();
650 builder.setParent(parent);
652 String typedefName = qname.getLocalName();
653 if (parent.equals(this)) {
654 for (TypeDefinitionBuilder tdb : addedTypedefs) {
655 if (tdb.getQName().getLocalName().equals(typedefName)) {
656 raiseYangParserException("", "typedef", typedefName, line, tdb.getLine());
659 addedTypedefs.add(builder);
661 if (parent instanceof DataNodeContainerBuilder) {
662 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
663 for (TypeDefinitionBuilder child : parentNode.getTypeDefinitionBuilders()) {
664 if (child.getQName().getLocalName().equals(typedefName)) {
665 raiseYangParserException("", "typedef", typedefName, line, child.getLine());
668 parentNode.addTypedef(builder);
669 } else if (parent instanceof RpcDefinitionBuilder) {
670 RpcDefinitionBuilder rpcParent = (RpcDefinitionBuilder) parent;
671 for (TypeDefinitionBuilder tdb : rpcParent.getTypeDefinitions()) {
672 if (tdb.getQName().getLocalName().equals(builder.getQName().getLocalName())) {
673 raiseYangParserException("", "typedef", typedefName, line, tdb.getLine());
676 rpcParent.addTypedef(builder);
678 throw new YangParseException(name, line, "Unresolved parent of typedef " + typedefName);
685 public void setType(final TypeDefinition<?> type) {
686 Builder parent = getActualNode();
687 if (!(parent instanceof TypeAwareBuilder)) {
688 throw new YangParseException("Failed to set type '" + type.getQName().getLocalName()
689 + "'. Invalid parent node: " + parent);
691 ((TypeAwareBuilder) parent).setType(type);
694 public UnionTypeBuilder addUnionType(final int line, final URI namespace, final Date revision) {
695 final Builder parent = getActualNode();
696 if (parent == null) {
697 throw new YangParseException(name, line, "Unresolved parent of union type");
699 final UnionTypeBuilder union = new UnionTypeBuilder(name, line);
700 if (parent instanceof TypeAwareBuilder) {
701 ((TypeAwareBuilder) parent).setTypedef(union);
704 throw new YangParseException(name, line, "Invalid parent of union type.");
709 public void addIdentityrefType(final int line, final SchemaPath schemaPath, final String baseString) {
710 final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(name, line, baseString, schemaPath);
712 final Builder parent = getActualNode();
713 if (parent == null) {
714 throw new YangParseException(name, line, "Unresolved parent of identityref type.");
716 if (parent instanceof TypeAwareBuilder) {
717 final TypeAwareBuilder typeParent = (TypeAwareBuilder) parent;
718 typeParent.setTypedef(identityref);
719 dirtyNodes.add(typeParent);
721 throw new YangParseException(name, line, "Invalid parent of identityref type.");
726 public DeviationBuilder addDeviation(final int line, final String targetPath) {
727 Builder parent = getActualNode();
728 if (!(parent.equals(this))) {
729 throw new YangParseException(name, line, "deviation can be defined only in module or submodule");
732 final DeviationBuilder builder = new DeviationBuilder(name, line, targetPath);
733 builder.setParent(parent);
734 addedDeviations.add(builder);
738 public IdentitySchemaNodeBuilder addIdentity(final QName qname, final int line) {
739 Builder parent = getActualNode();
740 if (!(parent.equals(this))) {
741 throw new YangParseException(name, line, "identity can be defined only in module or submodule");
743 String identityName = qname.getLocalName();
744 for (IdentitySchemaNodeBuilder idBuilder : addedIdentities) {
745 if (idBuilder.getQName().equals(qname)) {
746 raiseYangParserException("", "identity", identityName, line, idBuilder.getLine());
750 final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(name, line, qname);
751 builder.setParent(parent);
752 addedIdentities.add(builder);
757 public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder builder) {
758 addedUnknownNodes.add(builder);
759 allUnknownNodes.add(builder);
762 public UnknownSchemaNodeBuilder addUnknownSchemaNode(final int line, final QName qname) {
763 final Builder parent = getActualNode();
764 final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(name, line, qname);
765 builder.setParent(parent);
766 allUnknownNodes.add(builder);
768 if (parent.equals(this)) {
769 addedUnknownNodes.add(builder);
771 if (parent instanceof SchemaNodeBuilder) {
772 ((SchemaNodeBuilder) parent).addUnknownNodeBuilder(builder);
773 } else if (parent instanceof DataNodeContainerBuilder) {
774 ((DataNodeContainerBuilder) parent).addUnknownNodeBuilder(builder);
775 } else if (parent instanceof RefineHolder) {
776 ((RefineHolder) parent).addUnknownNodeBuilder(builder);
778 throw new YangParseException(name, line, "Unresolved parent of unknown node '" + qname.getLocalName()
786 public Set<RpcDefinitionBuilder> getRpcs() {
790 public Set<NotificationBuilder> getNotifications() {
791 return addedNotifications;
795 public String toString() {
796 return "module " + name;
799 private final class ModuleImpl implements Module {
800 private URI namespace;
801 private final String name;
802 private Date revision;
803 private String prefix;
804 private String yangVersion;
805 private String description;
806 private String reference;
807 private String organization;
808 private String contact;
809 private Set<ModuleImport> imports = Collections.emptySet();
810 private Set<FeatureDefinition> features = Collections.emptySet();
811 private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
812 private Set<NotificationDefinition> notifications = Collections.emptySet();
813 private Set<AugmentationSchema> augmentations = Collections.emptySet();
814 private Set<RpcDefinition> rpcs = Collections.emptySet();
815 private Set<Deviation> deviations = Collections.emptySet();
816 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
817 private Set<GroupingDefinition> groupings = Collections.emptySet();
818 private Set<UsesNode> uses = Collections.emptySet();
819 private List<ExtensionDefinition> extensionNodes = Collections.emptyList();
820 private Set<IdentitySchemaNode> identities = Collections.emptySet();
821 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
823 private ModuleImpl(String name) {
828 public URI getNamespace() {
832 private void setNamespace(URI namespace) {
833 this.namespace = namespace;
837 public String getName() {
842 public Date getRevision() {
846 private void setRevision(Date revision) {
847 this.revision = revision;
851 public String getPrefix() {
855 private void setPrefix(String prefix) {
856 this.prefix = prefix;
860 public String getYangVersion() {
864 private void setYangVersion(String yangVersion) {
865 this.yangVersion = yangVersion;
869 public String getDescription() {
873 private void setDescription(String description) {
874 this.description = description;
878 public String getReference() {
882 private void setReference(String reference) {
883 this.reference = reference;
887 public String getOrganization() {
891 private void setOrganization(String organization) {
892 this.organization = organization;
896 public String getContact() {
900 private void setContact(String contact) {
901 this.contact = contact;
905 public Set<ModuleImport> getImports() {
909 private void setImports(Set<ModuleImport> imports) {
910 if (imports != null) {
911 this.imports = imports;
916 public Set<FeatureDefinition> getFeatures() {
920 private void setFeatures(Set<FeatureDefinition> features) {
921 if (features != null) {
922 this.features = features;
927 public Set<TypeDefinition<?>> getTypeDefinitions() {
928 return typeDefinitions;
931 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
932 if (typeDefinitions != null) {
933 this.typeDefinitions = typeDefinitions;
938 public Set<NotificationDefinition> getNotifications() {
939 return notifications;
942 private void setNotifications(Set<NotificationDefinition> notifications) {
943 if (notifications != null) {
944 this.notifications = notifications;
949 public Set<AugmentationSchema> getAugmentations() {
950 return augmentations;
953 private void setAugmentations(Set<AugmentationSchema> augmentations) {
954 if (augmentations != null) {
955 this.augmentations = augmentations;
960 public Set<RpcDefinition> getRpcs() {
964 private void setRpcs(Set<RpcDefinition> rpcs) {
971 public Set<Deviation> getDeviations() {
975 private void setDeviations(Set<Deviation> deviations) {
976 if (deviations != null) {
977 this.deviations = deviations;
982 public Set<DataSchemaNode> getChildNodes() {
983 return new LinkedHashSet<DataSchemaNode>(childNodes.values());
986 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
987 if (childNodes != null) {
988 this.childNodes = childNodes;
993 public Set<GroupingDefinition> getGroupings() {
997 private void setGroupings(Set<GroupingDefinition> groupings) {
998 if (groupings != null) {
999 this.groupings = groupings;
1004 public Set<UsesNode> getUses() {
1008 private void setUses(Set<UsesNode> uses) {
1015 public List<ExtensionDefinition> getExtensionSchemaNodes() {
1016 return extensionNodes;
1019 private void setExtensionSchemaNodes(final List<ExtensionDefinition> extensionNodes) {
1020 if (extensionNodes != null) {
1021 this.extensionNodes = extensionNodes;
1026 public Set<IdentitySchemaNode> getIdentities() {
1030 private void setIdentities(final Set<IdentitySchemaNode> identities) {
1031 if (identities != null) {
1032 this.identities = identities;
1037 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
1038 return unknownNodes;
1041 private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
1042 if (unknownNodes != null) {
1043 this.unknownNodes = unknownNodes;
1048 public DataSchemaNode getDataChildByName(QName name) {
1049 return childNodes.get(name);
1053 public DataSchemaNode getDataChildByName(String name) {
1054 DataSchemaNode result = null;
1055 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
1056 if (entry.getKey().getLocalName().equals(name)) {
1057 result = entry.getValue();
1065 public int hashCode() {
1066 final int prime = 31;
1068 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1069 result = prime * result + ((name == null) ? 0 : name.hashCode());
1070 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1071 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1072 result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());
1077 public boolean equals(Object obj) {
1084 if (getClass() != obj.getClass()) {
1087 ModuleImpl other = (ModuleImpl) obj;
1088 if (namespace == null) {
1089 if (other.namespace != null) {
1092 } else if (!namespace.equals(other.namespace)) {
1096 if (other.name != null) {
1099 } else if (!name.equals(other.name)) {
1102 if (revision == null) {
1103 if (other.revision != null) {
1106 } else if (!revision.equals(other.revision)) {
1109 if (prefix == null) {
1110 if (other.prefix != null) {
1113 } else if (!prefix.equals(other.prefix)) {
1116 if (yangVersion == null) {
1117 if (other.yangVersion != null) {
1120 } else if (!yangVersion.equals(other.yangVersion)) {
1127 public String toString() {
1128 StringBuilder sb = new StringBuilder(ModuleImpl.class.getSimpleName());
1130 sb.append("name=" + name);
1131 sb.append(", namespace=" + namespace);
1132 sb.append(", revision=" + revision);
1133 sb.append(", prefix=" + prefix);
1134 sb.append(", yangVersion=" + yangVersion);
1136 return sb.toString();
1141 * Add child to parent. Method checks for duplicates and add given child
1142 * node to parent. If node with same name is found, throws exception. If
1143 * parent is null, child node will be added directly to module.
1149 private void addChildToParent(final Builder parent, final DataSchemaNodeBuilder child, final String childName) {
1150 final int lineNum = child.getLine();
1151 if (parent.equals(this)) {
1152 addChildToModule(child, childName, lineNum);
1154 addChildToSubnodeOfModule(parent, child, childName, lineNum);
1159 * Adds child node <code>child</code> to the set of nodes child nodes.
1161 * The method reduces the complexity of the method
1162 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1163 * addChildToParent}.
1166 * data schema node builder for child node
1168 * string with name of child node
1170 * line number in YANG file where is the node with the name equal
1171 * to <code>childName</code> is defined
1173 private void addChildToModule(final DataSchemaNodeBuilder child, final String childName, final int lineNum) {
1174 // if parent == null => node is defined under module
1175 // All leafs, leaf-lists, lists, containers, choices, rpcs,
1176 // notifications, and anyxmls defined within a parent node or at the
1177 // top level of the module or its submodules share the same
1178 // identifier namespace.
1179 for (DataSchemaNodeBuilder childNode : addedChildNodes) {
1180 if (childNode.getQName().getLocalName().equals(childName)) {
1181 raiseYangParserException("'"+child+"'", "node", childName, lineNum, childNode.getLine());
1184 for (RpcDefinitionBuilder rpc : addedRpcs) {
1185 if (rpc.getQName().getLocalName().equals(childName)) {
1186 raiseYangParserException("'"+child+"'", "rpc", childName, lineNum, rpc.getLine());
1189 for (NotificationBuilder notification : addedNotifications) {
1190 if (notification.getQName().getLocalName().equals(childName)) {
1191 raiseYangParserException("'"+child+"'", "notification", childName, lineNum, notification.getLine());
1194 addedChildNodes.add(child);
1198 * Adds child node <code>child</code> to the group of child nodes of the
1199 * <code>parent</code>
1201 * The method reduces the complexity of the method
1202 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1203 * addChildToParent}. *
1206 * builder of node which is parent for <code>child</code>
1208 * data schema node builder for child node
1210 * string with name of child node
1212 * line number in YANG file where is the node with the name equal
1213 * to <code>childName</code> is defined
1215 private void addChildToSubnodeOfModule(final Builder parent, final DataSchemaNodeBuilder child,
1216 final String childName, final int lineNum) {
1217 if (parent instanceof AugmentationSchemaBuilder) {
1218 child.setAugmenting(true);
1220 // no need for checking rpc and notification because they can be
1221 // defined only under module or submodule
1222 if (parent instanceof DataNodeContainerBuilder) {
1223 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
1224 for (DataSchemaNodeBuilder childNode : parentNode.getChildNodeBuilders()) {
1225 if (childNode.getQName().getLocalName().equals(childName)) {
1226 raiseYangParserException("'"+child+"'", "node", childName, lineNum, childNode.getLine());
1229 parentNode.addChildNode(child);
1230 } else if (parent instanceof ChoiceBuilder) {
1231 ChoiceBuilder parentNode = (ChoiceBuilder) parent;
1232 for (ChoiceCaseBuilder caseBuilder : parentNode.getCases()) {
1233 if (caseBuilder.getQName().getLocalName().equals(childName)) {
1234 raiseYangParserException("'"+child+"'", "node", childName, lineNum, caseBuilder.getLine());
1237 parentNode.addCase(child);
1239 throw new YangParseException(name, lineNum, "Unresolved parent of node '" + childName + "'.");
1245 * Implementation of <code>ModuleImport</code> interface only for the method
1246 * {@link ModuleBuilder#createModuleImport(String, Date, String)
1247 * createModuleImport}.
1250 private class ModuleImportImpl implements ModuleImport {
1251 final String moduleName;
1252 final Date revision;
1253 final String prefix;
1255 private ModuleImportImpl(final String moduleName, final Date revision, final String prefix) {
1256 this.moduleName = moduleName;
1257 this.revision = revision;
1258 this.prefix = prefix;
1262 public String getModuleName() {
1267 public Date getRevision() {
1272 public String getPrefix() {
1277 public int hashCode() {
1278 final int prime = 31;
1280 result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
1281 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1282 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1287 public boolean equals(Object obj) {
1294 if (getClass() != obj.getClass()) {
1297 ModuleImport other = (ModuleImport) obj;
1298 if (getModuleName() == null) {
1299 if (other.getModuleName() != null) {
1302 } else if (!getModuleName().equals(other.getModuleName())) {
1305 if (getRevision() == null) {
1306 if (other.getRevision() != null) {
1309 } else if (!getRevision().equals(other.getRevision())) {
1312 if (getPrefix() == null) {
1313 if (other.getPrefix() != null) {
1316 } else if (!getPrefix().equals(other.getPrefix())) {
1323 public String toString() {
1324 return "ModuleImport[moduleName=" + moduleName + ", revision=" + revision + ", prefix=" + prefix + "]";
1328 private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) {
1329 final ModuleImport moduleImport = new ModuleImportImpl(moduleName, revision, prefix);
1330 return moduleImport;
1333 private void raiseYangParserException(final String cantAddType, final String type, final String name,
1334 final int currentLine, final int duplicateLine) {
1336 StringBuilder msgPrefix = new StringBuilder("");
1337 if (cantAddType != null && !cantAddType.isEmpty()) {
1338 msgPrefix.append("Can not add ");
1339 msgPrefix.append(cantAddType);
1340 msgPrefix.append(": ");
1343 String msg = String.format("%s%s with same name '%s' already declared at line %d.", msgPrefix, type, name,
1345 throw new YangParseException(moduleName, currentLine, msg);