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.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<Builder>();
70 private final Set<TypeAwareBuilder> dirtyNodes = new HashSet<TypeAwareBuilder>();
72 private final Set<ModuleImport> imports = new HashSet<ModuleImport>();
73 private final List<AugmentationSchemaBuilder> addedAugments = new ArrayList<AugmentationSchemaBuilder>();
74 private final List<AugmentationSchemaBuilder> allAugments = new ArrayList<AugmentationSchemaBuilder>();
75 private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
76 private final List<UsesNodeBuilder> allUsesNodes = new ArrayList<UsesNodeBuilder>();
77 private final Set<RpcDefinitionBuilder> addedRpcs = new HashSet<RpcDefinitionBuilder>();
78 private final Set<NotificationBuilder> addedNotifications = new HashSet<NotificationBuilder>();
79 private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<IdentitySchemaNodeBuilder>();
80 private final Set<FeatureBuilder> addedFeatures = new HashSet<FeatureBuilder>();
81 private final Set<DeviationBuilder> addedDeviations = new HashSet<DeviationBuilder>();
82 private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
83 private final List<ExtensionBuilder> addedExtensions = new ArrayList<ExtensionBuilder>();
84 private final List<UnknownSchemaNodeBuilder> allUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
86 public ModuleBuilder(final String name) {
89 schemaPath = new SchemaPath(Collections.<QName> emptyList(), true);
90 instance = new ModuleImpl(name);
91 actualPath.push(this);
95 * Build new Module object based on this builder.
98 public Module build() {
99 instance.setPrefix(prefix);
100 instance.setRevision(revision);
101 instance.setImports(imports);
102 instance.setNamespace(namespace);
105 final Set<TypeDefinition<?>> typedefs = new TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
106 for (TypeDefinitionBuilder tdb : addedTypedefs) {
107 typedefs.add(tdb.build());
109 instance.setTypeDefinitions(typedefs);
112 final Map<QName, DataSchemaNode> children = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
113 for (DataSchemaNodeBuilder child : addedChildNodes) {
114 children.put(child.getQName(), child.build());
116 instance.setChildNodes(children);
119 final Set<GroupingDefinition> groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
120 for (GroupingBuilder gb : addedGroupings) {
121 groupings.add(gb.build());
123 instance.setGroupings(groupings);
126 final Set<UsesNode> usesDefinitions = new HashSet<UsesNode>();
127 for (UsesNodeBuilder unb : addedUsesNodes) {
128 usesDefinitions.add(unb.build());
130 instance.setUses(usesDefinitions);
133 final Set<FeatureDefinition> features = new TreeSet<FeatureDefinition>(Comparators.SCHEMA_NODE_COMP);
134 for (FeatureBuilder fb : addedFeatures) {
135 features.add(fb.build());
137 instance.setFeatures(features);
140 final Set<NotificationDefinition> notifications = new TreeSet<NotificationDefinition>(
141 Comparators.SCHEMA_NODE_COMP);
142 for (NotificationBuilder entry : addedNotifications) {
143 notifications.add(entry.build());
145 instance.setNotifications(notifications);
148 final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
149 for (AugmentationSchemaBuilder builder : addedAugments) {
150 augmentations.add(builder.build());
152 instance.setAugmentations(augmentations);
155 final Set<RpcDefinition> rpcs = new TreeSet<RpcDefinition>(Comparators.SCHEMA_NODE_COMP);
156 for (RpcDefinitionBuilder rpc : addedRpcs) {
157 rpcs.add(rpc.build());
159 instance.setRpcs(rpcs);
162 final Set<Deviation> deviations = new HashSet<Deviation>();
163 for (DeviationBuilder entry : addedDeviations) {
164 deviations.add(entry.build());
166 instance.setDeviations(deviations);
169 final List<ExtensionDefinition> extensions = new ArrayList<ExtensionDefinition>();
170 for (ExtensionBuilder eb : addedExtensions) {
171 extensions.add(eb.build());
173 Collections.sort(extensions, Comparators.SCHEMA_NODE_COMP);
174 instance.setExtensionSchemaNodes(extensions);
177 final Set<IdentitySchemaNode> identities = new TreeSet<IdentitySchemaNode>(Comparators.SCHEMA_NODE_COMP);
178 for (IdentitySchemaNodeBuilder id : addedIdentities) {
179 identities.add(id.build());
181 instance.setIdentities(identities);
184 final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
185 for (UnknownSchemaNodeBuilder unb : addedUnknownNodes) {
186 unknownNodes.add(unb.build());
188 instance.setUnknownSchemaNodes(unknownNodes);
193 public boolean isAllUsesDataCollected() {
194 for (UsesNodeBuilder usesNode : allUsesNodes) {
195 if (!usesNode.isDataCollected()) {
203 public void setParent(Builder parent) {
204 throw new YangParseException(name, 0, "Can not set parent to module");
208 public SchemaPath getPath() {
213 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
214 return addedTypedefs;
217 public void enterNode(final Builder node) {
218 actualPath.push(node);
221 public void exitNode() {
225 public Builder getActualNode() {
226 if (actualPath.isEmpty()) {
229 return actualPath.peekFirst();
233 public Builder getActualParent() {
234 if (actualPath.size() < 2) {
237 Builder builderChild = actualPath.removeFirst();
238 Builder builderParent = actualPath.peekFirst();
239 actualPath.addFirst(builderChild);
240 return builderParent;
244 public Set<TypeAwareBuilder> getDirtyNodes() {
248 public List<AugmentationSchemaBuilder> getAllAugments() {
252 public Set<IdentitySchemaNodeBuilder> getIdentities() {
253 return addedIdentities;
256 public List<UsesNodeBuilder> getAllUsesNodes() {
260 public Set<DeviationBuilder> getDeviations() {
261 return addedDeviations;
264 public List<ExtensionBuilder> getExtensions() {
265 return addedExtensions;
268 public List<UnknownSchemaNodeBuilder> getAllUnknownNodes() {
269 return allUnknownNodes;
272 public String getName() {
276 public URI getNamespace() {
280 public void setNamespace(final URI namespace) {
281 this.namespace = namespace;
284 public String getPrefix() {
288 public Date getRevision() {
292 public void markActualNodeDirty() {
293 final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) getActualNode();
294 dirtyNodes.add(nodeBuilder);
297 public void setRevision(final Date revision) {
298 this.revision = revision;
301 public void setPrefix(final String prefix) {
302 this.prefix = prefix;
305 public void setYangVersion(final String yangVersion) {
306 instance.setYangVersion(yangVersion);
309 public void setDescription(final String description) {
310 instance.setDescription(description);
313 public void setReference(final String reference) {
314 instance.setReference(reference);
317 public void setOrganization(final String organization) {
318 instance.setOrganization(organization);
321 public void setContact(final String contact) {
322 instance.setContact(contact);
325 public boolean addModuleImport(final String moduleName, final Date revision, final String prefix) {
326 final ModuleImport moduleImport = createModuleImport(moduleName, revision, prefix);
327 return imports.add(moduleImport);
330 public Set<ModuleImport> getModuleImports() {
334 public ExtensionBuilder addExtension(final QName qname, final int line) {
335 final String extName = qname.getLocalName();
336 for (ExtensionBuilder addedExtension : addedExtensions) {
337 if (addedExtension.getQName().getLocalName().equals(extName)) {
338 raiseYangParserException("extension", "node", extName, line, addedExtension.getLine());
341 final ExtensionBuilder builder = new ExtensionBuilder(name, line, qname);
342 addedExtensions.add(builder);
346 public ContainerSchemaNodeBuilder addContainerNode(final int line, final QName qname, final SchemaPath schemaPath) {
347 final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
349 Builder parent = getActualNode();
350 builder.setParent(parent);
351 addChildToParent(parent, builder, qname.getLocalName());
356 public ListSchemaNodeBuilder addListNode(final int line, final QName qname, final SchemaPath schemaPath) {
357 final ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(name, line, qname, schemaPath);
359 Builder parent = getActualNode();
360 builder.setParent(parent);
361 addChildToParent(parent, builder, qname.getLocalName());
366 public LeafSchemaNodeBuilder addLeafNode(final int line, final QName qname, final SchemaPath schemaPath) {
367 final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(name, line, qname, schemaPath);
369 Builder parent = getActualNode();
370 builder.setParent(parent);
371 addChildToParent(parent, builder, qname.getLocalName());
376 public LeafListSchemaNodeBuilder addLeafListNode(final int line, final QName qname, final SchemaPath schemaPath) {
377 final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(name, line, qname, schemaPath);
379 Builder parent = getActualNode();
380 builder.setParent(parent);
381 addChildToParent(parent, builder, qname.getLocalName());
386 public GroupingBuilder addGrouping(final int line, final QName qname) {
387 final GroupingBuilder builder = new GroupingBuilderImpl(name, line, qname);
389 Builder parent = getActualNode();
390 builder.setParent(parent);
392 String groupingName = qname.getLocalName();
393 if (parent.equals(this)) {
394 for (GroupingBuilder addedGrouping : addedGroupings) {
395 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
396 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
399 addedGroupings.add(builder);
401 if (parent instanceof DataNodeContainerBuilder) {
402 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
403 for (GroupingBuilder addedGrouping : parentNode.getGroupingBuilders()) {
404 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
405 raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
408 parentNode.addGrouping(builder);
409 } else if (parent instanceof RpcDefinitionBuilder) {
410 RpcDefinitionBuilder parentNode = (RpcDefinitionBuilder) parent;
411 for (GroupingBuilder child : parentNode.getGroupings()) {
412 if (child.getQName().getLocalName().equals(groupingName)) {
413 raiseYangParserException("", "Grouping", groupingName, line, child.getLine());
416 parentNode.addGrouping(builder);
418 throw new YangParseException(name, line, "Unresolved parent of grouping " + groupingName);
425 public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr) {
426 final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr);
428 Builder parent = getActualNode();
429 builder.setParent(parent);
431 if (parent.equals(this)) {
432 // augment can be declared only under 'module' ...
433 addedAugments.add(builder);
435 // ... or 'uses' statement
436 if (parent instanceof UsesNodeBuilder) {
437 if (augmentTargetStr.startsWith("/")) {
438 throw new YangParseException(name, line,
439 "If 'augment' statement is a substatement to the 'uses' statement, it cannot contain absolute path ("
440 + augmentTargetStr + ")");
442 ((UsesNodeBuilder) parent).addAugment(builder);
444 throw new YangParseException(name, line, "Augment can be declared only under module or uses statement.");
447 allAugments.add(builder);
453 public Set<UsesNodeBuilder> getUsesNodes() {
454 return addedUsesNodes;
458 public void addUsesNode(UsesNodeBuilder usesBuilder) {
459 addedUsesNodes.add(usesBuilder);
460 allUsesNodes.add(usesBuilder);
463 public UsesNodeBuilder addUsesNode(final int line, final String groupingPathStr) {
464 final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(name, line, groupingPathStr);
466 Builder parent = getActualNode();
467 usesBuilder.setParent(parent);
469 if (parent.equals(this)) {
470 addedUsesNodes.add(usesBuilder);
472 if (!(parent instanceof DataNodeContainerBuilder)) {
473 throw new YangParseException(name, line, "Unresolved parent of uses '" + groupingPathStr + "'.");
475 ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder);
477 if(parent instanceof AugmentationSchemaBuilder) {
478 usesBuilder.setAugmenting(true);
479 usesBuilder.setParentAugment((AugmentationSchemaBuilder)parent);
482 allUsesNodes.add(usesBuilder);
486 public void addRefine(final RefineHolder refine) {
487 final Builder parent = getActualNode();
488 if (!(parent instanceof UsesNodeBuilder)) {
489 throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
491 ((UsesNodeBuilder) parent).addRefine(refine);
492 refine.setParent(parent);
495 public RpcDefinitionBuilder addRpc(final int line, final QName qname) {
496 Builder parent = getActualNode();
497 if (!(parent.equals(this))) {
498 throw new YangParseException(name, line, "rpc can be defined only in module or submodule");
501 final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(name, line, qname);
502 rpcBuilder.setParent(parent);
504 String rpcName = qname.getLocalName();
505 for (RpcDefinitionBuilder rpc : addedRpcs) {
506 if (rpc.getQName().getLocalName().equals(rpcName)) {
507 raiseYangParserException("", "rpc", rpcName, line, rpc.getLine());
510 for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
511 if (addedChild.getQName().getLocalName().equals(rpcName)) {
512 raiseYangParserException("rpc", "node", rpcName, line, addedChild.getLine());
515 for (NotificationBuilder addedNotification : addedNotifications) {
516 if (addedNotification.getQName().getLocalName().equals(rpcName)) {
517 raiseYangParserException("rpc", "notification", rpcName, line, addedNotification.getLine());
520 addedRpcs.add(rpcBuilder);
524 public ContainerSchemaNodeBuilder addRpcInput(final int line, final QName qname, final SchemaPath schemaPath) {
525 final Builder parent = getActualNode();
526 if (!(parent instanceof RpcDefinitionBuilder)) {
527 throw new YangParseException(name, line, "input can be defined only in rpc statement");
529 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
531 final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
532 inputBuilder.setParent(rpc);
534 rpc.setInput(inputBuilder);
538 public ContainerSchemaNodeBuilder addRpcOutput(final SchemaPath schemaPath, final QName qname, final int line) {
539 final Builder parent = getActualNode();
540 if (!(parent instanceof RpcDefinitionBuilder)) {
541 throw new YangParseException(name, line, "output can be defined only in rpc statement");
543 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
545 final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
546 outputBuilder.setParent(rpc);
548 rpc.setOutput(outputBuilder);
549 return outputBuilder;
552 public NotificationBuilder addNotification(final int line, final QName qname) {
553 final Builder parent = getActualNode();
554 if (!(parent.equals(this))) {
555 throw new YangParseException(name, line, "notification can be defined only in module or submodule");
558 String notificationName = qname.getLocalName();
559 for (NotificationBuilder nb : addedNotifications) {
560 if (nb.getQName().equals(qname)) {
561 raiseYangParserException("", "notification", notificationName, line, nb.getLine());
564 for (RpcDefinitionBuilder rpc : addedRpcs) {
565 if (rpc.getQName().getLocalName().equals(notificationName)) {
566 raiseYangParserException("notification", "rpc", notificationName, line, rpc.getLine());
569 for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
570 if (addedChild.getQName().getLocalName().equals(notificationName)) {
571 raiseYangParserException("notification", "node", notificationName, line, addedChild.getLine());
575 final NotificationBuilder builder = new NotificationBuilder(name, line, qname);
576 builder.setParent(parent);
577 addedNotifications.add(builder);
582 public FeatureBuilder addFeature(final int line, final QName qname) {
583 Builder parent = getActualNode();
584 if (!(parent.equals(this))) {
585 throw new YangParseException(name, line, "feature can be defined only in module or submodule");
588 final FeatureBuilder builder = new FeatureBuilder(name, line, qname);
589 builder.setParent(parent);
591 String featureName = qname.getLocalName();
592 for (FeatureBuilder addedFeature : addedFeatures) {
593 if (addedFeature.getQName().getLocalName().equals(featureName)) {
594 raiseYangParserException("", "feature", featureName, line, addedFeature.getLine());
597 addedFeatures.add(builder);
601 public ChoiceBuilder addChoice(final int line, final QName qname) {
602 final ChoiceBuilder builder = new ChoiceBuilder(name, line, qname);
604 Builder parent = getActualNode();
605 builder.setParent(parent);
606 addChildToParent(parent, builder, qname.getLocalName());
611 public ChoiceCaseBuilder addCase(final int line, final QName qname) {
612 Builder parent = getActualNode();
613 if (parent == null || parent.equals(this)) {
614 throw new YangParseException(name, line, "'case' parent not found");
617 final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(name, line, qname);
618 builder.setParent(parent);
620 if (parent instanceof ChoiceBuilder) {
621 ((ChoiceBuilder) parent).addCase(builder);
622 } else if (parent instanceof AugmentationSchemaBuilder) {
623 ((AugmentationSchemaBuilder) parent).addChildNode(builder);
625 throw new YangParseException(name, line, "Unresolved parent of 'case' " + qname.getLocalName());
631 public AnyXmlBuilder addAnyXml(final int line, final QName qname, final SchemaPath schemaPath) {
632 final AnyXmlBuilder builder = new AnyXmlBuilder(name, line, qname, schemaPath);
634 Builder parent = getActualNode();
635 builder.setParent(parent);
636 addChildToParent(parent, builder, qname.getLocalName());
642 public void addTypedef(TypeDefinitionBuilder typedefBuilder) {
643 String nodeName = typedefBuilder.getQName().getLocalName();
644 for (TypeDefinitionBuilder tdb : addedTypedefs) {
645 if (tdb.getQName().getLocalName().equals(nodeName)) {
646 raiseYangParserException("", "typedef", nodeName, typedefBuilder.getLine(), tdb.getLine());
649 addedTypedefs.add(typedefBuilder);
652 public TypeDefinitionBuilderImpl addTypedef(final int line, final QName qname) {
653 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(name, line, qname);
655 Builder parent = getActualNode();
656 builder.setParent(parent);
658 String typedefName = qname.getLocalName();
659 if (parent.equals(this)) {
660 for (TypeDefinitionBuilder tdb : addedTypedefs) {
661 if (tdb.getQName().getLocalName().equals(typedefName)) {
662 raiseYangParserException("", "typedef", typedefName, line, tdb.getLine());
665 addedTypedefs.add(builder);
667 if (parent instanceof DataNodeContainerBuilder) {
668 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
669 for (TypeDefinitionBuilder child : parentNode.getTypeDefinitionBuilders()) {
670 if (child.getQName().getLocalName().equals(typedefName)) {
671 raiseYangParserException("", "typedef", typedefName, line, child.getLine());
674 parentNode.addTypedef(builder);
675 } else if (parent instanceof RpcDefinitionBuilder) {
676 RpcDefinitionBuilder rpcParent = (RpcDefinitionBuilder) parent;
677 for (TypeDefinitionBuilder tdb : rpcParent.getTypeDefinitions()) {
678 if (tdb.getQName().getLocalName().equals(builder.getQName().getLocalName())) {
679 raiseYangParserException("", "typedef", typedefName, line, tdb.getLine());
682 rpcParent.addTypedef(builder);
684 throw new YangParseException(name, line, "Unresolved parent of typedef " + typedefName);
691 public void setType(final TypeDefinition<?> type) {
692 Builder parent = getActualNode();
693 if (!(parent instanceof TypeAwareBuilder)) {
694 throw new YangParseException("Failed to set type '" + type.getQName().getLocalName()
695 + "'. Invalid parent node: " + parent);
697 ((TypeAwareBuilder) parent).setType(type);
700 public UnionTypeBuilder addUnionType(final int line, final URI namespace, final Date revision) {
701 final Builder parent = getActualNode();
702 if (parent == null) {
703 throw new YangParseException(name, line, "Unresolved parent of union type");
705 final UnionTypeBuilder union = new UnionTypeBuilder(name, line);
706 if (parent instanceof TypeAwareBuilder) {
707 ((TypeAwareBuilder) parent).setTypedef(union);
710 throw new YangParseException(name, line, "Invalid parent of union type.");
715 public void addIdentityrefType(final int line, final SchemaPath schemaPath, final String baseString) {
716 final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(name, line, baseString, schemaPath);
718 final Builder parent = getActualNode();
719 if (parent == null) {
720 throw new YangParseException(name, line, "Unresolved parent of identityref type.");
722 if (parent instanceof TypeAwareBuilder) {
723 final TypeAwareBuilder typeParent = (TypeAwareBuilder) parent;
724 typeParent.setTypedef(identityref);
725 dirtyNodes.add(typeParent);
727 throw new YangParseException(name, line, "Invalid parent of identityref type.");
732 public DeviationBuilder addDeviation(final int line, final String targetPath) {
733 Builder parent = getActualNode();
734 if (!(parent.equals(this))) {
735 throw new YangParseException(name, line, "deviation can be defined only in module or submodule");
738 final DeviationBuilder builder = new DeviationBuilder(name, line, targetPath);
739 builder.setParent(parent);
740 addedDeviations.add(builder);
744 public IdentitySchemaNodeBuilder addIdentity(final QName qname, final int line) {
745 Builder parent = getActualNode();
746 if (!(parent.equals(this))) {
747 throw new YangParseException(name, line, "identity can be defined only in module or submodule");
749 String identityName = qname.getLocalName();
750 for (IdentitySchemaNodeBuilder idBuilder : addedIdentities) {
751 if (idBuilder.getQName().equals(qname)) {
752 raiseYangParserException("", "identity", identityName, line, idBuilder.getLine());
756 final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(name, line, qname);
757 builder.setParent(parent);
758 addedIdentities.add(builder);
763 public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder builder) {
764 addedUnknownNodes.add(builder);
765 allUnknownNodes.add(builder);
768 public UnknownSchemaNodeBuilder addUnknownSchemaNode(final int line, final QName qname) {
769 final Builder parent = getActualNode();
770 final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(name, line, qname);
771 builder.setParent(parent);
772 allUnknownNodes.add(builder);
774 if (parent.equals(this)) {
775 addedUnknownNodes.add(builder);
777 if (parent instanceof SchemaNodeBuilder) {
778 ((SchemaNodeBuilder) parent).addUnknownNodeBuilder(builder);
779 } else if (parent instanceof DataNodeContainerBuilder) {
780 ((DataNodeContainerBuilder) parent).addUnknownNodeBuilder(builder);
781 } else if (parent instanceof RefineHolder) {
782 ((RefineHolder) parent).addUnknownNodeBuilder(builder);
784 throw new YangParseException(name, line, "Unresolved parent of unknown node '" + qname.getLocalName()
792 public Set<RpcDefinitionBuilder> getRpcs() {
796 public Set<NotificationBuilder> getNotifications() {
797 return addedNotifications;
801 public String toString() {
802 return "module " + name;
805 private final class ModuleImpl implements Module {
806 private URI namespace;
807 private final String name;
808 private Date revision;
809 private String prefix;
810 private String yangVersion;
811 private String description;
812 private String reference;
813 private String organization;
814 private String contact;
815 private Set<ModuleImport> imports = Collections.emptySet();
816 private Set<FeatureDefinition> features = Collections.emptySet();
817 private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
818 private Set<NotificationDefinition> notifications = Collections.emptySet();
819 private Set<AugmentationSchema> augmentations = Collections.emptySet();
820 private Set<RpcDefinition> rpcs = Collections.emptySet();
821 private Set<Deviation> deviations = Collections.emptySet();
822 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
823 private Set<GroupingDefinition> groupings = Collections.emptySet();
824 private Set<UsesNode> uses = Collections.emptySet();
825 private List<ExtensionDefinition> extensionNodes = Collections.emptyList();
826 private Set<IdentitySchemaNode> identities = Collections.emptySet();
827 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
829 private ModuleImpl(String name) {
834 public URI getNamespace() {
838 private void setNamespace(URI namespace) {
839 this.namespace = namespace;
843 public String getName() {
848 public Date getRevision() {
852 private void setRevision(Date revision) {
853 this.revision = revision;
857 public String getPrefix() {
861 private void setPrefix(String prefix) {
862 this.prefix = prefix;
866 public String getYangVersion() {
870 private void setYangVersion(String yangVersion) {
871 this.yangVersion = yangVersion;
875 public String getDescription() {
879 private void setDescription(String description) {
880 this.description = description;
884 public String getReference() {
888 private void setReference(String reference) {
889 this.reference = reference;
893 public String getOrganization() {
897 private void setOrganization(String organization) {
898 this.organization = organization;
902 public String getContact() {
906 private void setContact(String contact) {
907 this.contact = contact;
911 public Set<ModuleImport> getImports() {
915 private void setImports(Set<ModuleImport> imports) {
916 if (imports != null) {
917 this.imports = imports;
922 public Set<FeatureDefinition> getFeatures() {
926 private void setFeatures(Set<FeatureDefinition> features) {
927 if (features != null) {
928 this.features = features;
933 public Set<TypeDefinition<?>> getTypeDefinitions() {
934 return typeDefinitions;
937 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
938 if (typeDefinitions != null) {
939 this.typeDefinitions = typeDefinitions;
944 public Set<NotificationDefinition> getNotifications() {
945 return notifications;
948 private void setNotifications(Set<NotificationDefinition> notifications) {
949 if (notifications != null) {
950 this.notifications = notifications;
955 public Set<AugmentationSchema> getAugmentations() {
956 return augmentations;
959 private void setAugmentations(Set<AugmentationSchema> augmentations) {
960 if (augmentations != null) {
961 this.augmentations = augmentations;
966 public Set<RpcDefinition> getRpcs() {
970 private void setRpcs(Set<RpcDefinition> rpcs) {
977 public Set<Deviation> getDeviations() {
981 private void setDeviations(Set<Deviation> deviations) {
982 if (deviations != null) {
983 this.deviations = deviations;
988 public Set<DataSchemaNode> getChildNodes() {
989 return new LinkedHashSet<DataSchemaNode>(childNodes.values());
992 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
993 if (childNodes != null) {
994 this.childNodes = childNodes;
999 public Set<GroupingDefinition> getGroupings() {
1003 private void setGroupings(Set<GroupingDefinition> groupings) {
1004 if (groupings != null) {
1005 this.groupings = groupings;
1010 public Set<UsesNode> getUses() {
1014 private void setUses(Set<UsesNode> uses) {
1021 public List<ExtensionDefinition> getExtensionSchemaNodes() {
1022 return extensionNodes;
1025 private void setExtensionSchemaNodes(final List<ExtensionDefinition> extensionNodes) {
1026 if (extensionNodes != null) {
1027 this.extensionNodes = extensionNodes;
1032 public Set<IdentitySchemaNode> getIdentities() {
1036 private void setIdentities(final Set<IdentitySchemaNode> identities) {
1037 if (identities != null) {
1038 this.identities = identities;
1043 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
1044 return unknownNodes;
1047 private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
1048 if (unknownNodes != null) {
1049 this.unknownNodes = unknownNodes;
1054 public DataSchemaNode getDataChildByName(QName name) {
1055 return childNodes.get(name);
1059 public DataSchemaNode getDataChildByName(String name) {
1060 DataSchemaNode result = null;
1061 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
1062 if (entry.getKey().getLocalName().equals(name)) {
1063 result = entry.getValue();
1071 public int hashCode() {
1072 final int prime = 31;
1074 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1075 result = prime * result + ((name == null) ? 0 : name.hashCode());
1076 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1077 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1078 result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());
1083 public boolean equals(Object obj) {
1090 if (getClass() != obj.getClass()) {
1093 ModuleImpl other = (ModuleImpl) obj;
1094 if (namespace == null) {
1095 if (other.namespace != null) {
1098 } else if (!namespace.equals(other.namespace)) {
1102 if (other.name != null) {
1105 } else if (!name.equals(other.name)) {
1108 if (revision == null) {
1109 if (other.revision != null) {
1112 } else if (!revision.equals(other.revision)) {
1115 if (prefix == null) {
1116 if (other.prefix != null) {
1119 } else if (!prefix.equals(other.prefix)) {
1122 if (yangVersion == null) {
1123 if (other.yangVersion != null) {
1126 } else if (!yangVersion.equals(other.yangVersion)) {
1133 public String toString() {
1134 StringBuilder sb = new StringBuilder(ModuleImpl.class.getSimpleName());
1136 sb.append("name=" + name);
1137 sb.append(", namespace=" + namespace);
1138 sb.append(", revision=" + revision);
1139 sb.append(", prefix=" + prefix);
1140 sb.append(", yangVersion=" + yangVersion);
1142 return sb.toString();
1147 * Add child to parent. Method checks for duplicates and add given child
1148 * node to parent. If node with same name is found, throws exception. If
1149 * parent is null, child node will be added directly to module.
1155 private void addChildToParent(final Builder parent, final DataSchemaNodeBuilder child, final String childName) {
1156 final int lineNum = child.getLine();
1157 if (parent.equals(this)) {
1158 addChildToModule(child, childName, lineNum);
1160 addChildToSubnodeOfModule(parent, child, childName, lineNum);
1165 * Adds child node <code>child</code> to the set of nodes child nodes.
1167 * The method reduces the complexity of the method
1168 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1169 * addChildToParent}.
1172 * data schema node builder for child node
1174 * string with name of child node
1176 * line number in YANG file where is the node with the name equal
1177 * to <code>childName</code> is defined
1179 private void addChildToModule(final DataSchemaNodeBuilder child, final String childName, final int lineNum) {
1180 // if parent == null => node is defined under module
1181 // All leafs, leaf-lists, lists, containers, choices, rpcs,
1182 // notifications, and anyxmls defined within a parent node or at the
1183 // top level of the module or its submodules share the same
1184 // identifier namespace.
1185 for (DataSchemaNodeBuilder childNode : addedChildNodes) {
1186 if (childNode.getQName().getLocalName().equals(childName)) {
1187 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
1190 for (RpcDefinitionBuilder rpc : addedRpcs) {
1191 if (rpc.getQName().getLocalName().equals(childName)) {
1192 raiseYangParserException("'" + child + "'", "rpc", childName, lineNum, rpc.getLine());
1195 for (NotificationBuilder notification : addedNotifications) {
1196 if (notification.getQName().getLocalName().equals(childName)) {
1197 raiseYangParserException("'" + child + "'", "notification", childName, lineNum, notification.getLine());
1200 addedChildNodes.add(child);
1204 * Adds child node <code>child</code> to the group of child nodes of the
1205 * <code>parent</code>
1207 * The method reduces the complexity of the method
1208 * {@link #addChildToParent(Builder, DataSchemaNodeBuilder, String)
1209 * addChildToParent}. *
1212 * builder of node which is parent for <code>child</code>
1214 * data schema node builder for child node
1216 * string with name of child node
1218 * line number in YANG file where is the node with the name equal
1219 * to <code>childName</code> is defined
1221 private void addChildToSubnodeOfModule(final Builder parent, final DataSchemaNodeBuilder child,
1222 final String childName, final int lineNum) {
1223 // no need for checking rpc and notification because they can be
1224 // defined only under module or submodule
1225 if (parent instanceof DataNodeContainerBuilder) {
1226 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
1227 for (DataSchemaNodeBuilder childNode : parentNode.getChildNodeBuilders()) {
1228 if (childNode.getQName().getLocalName().equals(childName)) {
1229 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
1232 parentNode.addChildNode(child);
1233 } else if (parent instanceof ChoiceBuilder) {
1234 ChoiceBuilder parentNode = (ChoiceBuilder) parent;
1235 for (ChoiceCaseBuilder caseBuilder : parentNode.getCases()) {
1236 if (caseBuilder.getQName().getLocalName().equals(childName)) {
1237 raiseYangParserException("'" + child + "'", "node", childName, lineNum, caseBuilder.getLine());
1240 parentNode.addCase(child);
1242 throw new YangParseException(name, lineNum, "Unresolved parent of node '" + childName + "'.");
1246 private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) {
1247 final ModuleImport moduleImport = new ModuleImportImpl(moduleName, revision, prefix);
1248 return moduleImport;
1251 private void raiseYangParserException(final String cantAddType, final String type, final String name,
1252 final int currentLine, final int duplicateLine) {
1254 StringBuilder msgPrefix = new StringBuilder("");
1255 if (cantAddType != null && !cantAddType.isEmpty()) {
1256 msgPrefix.append("Can not add ");
1257 msgPrefix.append(cantAddType);
1258 msgPrefix.append(": ");
1261 String msg = String.format("%s%s with same name '%s' already declared at line %d.", msgPrefix, type, name,
1263 throw new YangParseException(moduleName, currentLine, msg);
1267 public int hashCode() {
1268 final int prime = 31;
1270 result = prime * result + ((name == null) ? 0 : name.hashCode());
1271 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1272 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1273 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1279 public boolean equals(Object obj) {
1286 if (getClass() != obj.getClass()) {
1289 ModuleBuilder other = (ModuleBuilder) obj;
1291 if (other.name != null) {
1294 } else if (!name.equals(other.name)) {
1297 if (namespace == null) {
1298 if (other.namespace != null) {
1301 } else if (!namespace.equals(other.namespace)) {
1304 if (prefix == null) {
1305 if (other.prefix != null) {
1308 } else if (!prefix.equals(other.prefix)) {
1311 if (revision == null) {
1312 if (other.revision != null) {
1315 } else if (!revision.equals(other.revision)) {