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.HashSet;
15 import java.util.LinkedHashSet;
16 import java.util.LinkedList;
17 import java.util.List;
20 import java.util.TreeMap;
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.parser.builder.api.AbstractDataNodeContainerBuilder;
40 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
41 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
42 import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder;
43 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
44 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
45 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
46 import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
47 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
48 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
49 import org.opendaylight.yangtools.yang.parser.util.Comparators;
50 import org.opendaylight.yangtools.yang.parser.util.RefineHolder;
51 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
54 * Builder of Module object. If this module is dependent on external
55 * module/modules, these dependencies must be resolved before module is built,
56 * otherwise result may not be valid.
58 public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
59 private final ModuleImpl instance;
60 private final String name;
61 private final SchemaPath schemaPath;
62 private URI namespace;
63 private String prefix;
64 private Date revision;
66 private final LinkedList<Builder> actualPath = new LinkedList<Builder>();
67 private final Set<TypeAwareBuilder> dirtyNodes = new HashSet<TypeAwareBuilder>();
69 private final Set<ModuleImport> imports = new HashSet<ModuleImport>();
70 private final List<AugmentationSchemaBuilder> addedAugments = new ArrayList<AugmentationSchemaBuilder>();
71 private final List<AugmentationSchemaBuilder> allAugments = new ArrayList<AugmentationSchemaBuilder>();
72 private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
73 private final List<UsesNodeBuilder> allUsesNodes = new ArrayList<UsesNodeBuilder>();
74 private final Set<RpcDefinitionBuilder> addedRpcs = new HashSet<RpcDefinitionBuilder>();
75 private final Set<NotificationBuilder> addedNotifications = new HashSet<NotificationBuilder>();
76 private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<IdentitySchemaNodeBuilder>();
77 private final Set<FeatureBuilder> addedFeatures = new HashSet<FeatureBuilder>();
78 private final Set<DeviationBuilder> addedDeviations = new HashSet<DeviationBuilder>();
79 private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
80 private final List<ExtensionBuilder> addedExtensions = new ArrayList<ExtensionBuilder>();
81 private final List<UnknownSchemaNodeBuilder> allUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
83 public ModuleBuilder(final String name) {
86 schemaPath = new SchemaPath(Collections.<QName>emptyList(), true);
87 instance = new ModuleImpl(name);
88 actualPath.push(this);
92 * Build new Module object based on this builder.
95 public Module build() {
96 instance.setPrefix(prefix);
97 instance.setRevision(revision);
98 instance.setImports(imports);
99 instance.setNamespace(namespace);
102 final Set<TypeDefinition<?>> typedefs = new TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
103 for (TypeDefinitionBuilder tdb : addedTypedefs) {
104 typedefs.add(tdb.build());
106 instance.setTypeDefinitions(typedefs);
109 final Map<QName, DataSchemaNode> children = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
110 for (DataSchemaNodeBuilder child : addedChildNodes) {
111 children.put(child.getQName(), child.build());
113 instance.setChildNodes(children);
116 final Set<GroupingDefinition> groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
117 for (GroupingBuilder gb : addedGroupings) {
118 groupings.add(gb.build());
120 instance.setGroupings(groupings);
123 final Set<UsesNode> usesDefinitions = new HashSet<UsesNode>();
124 for (UsesNodeBuilder unb : addedUsesNodes) {
125 usesDefinitions.add(unb.build());
127 instance.setUses(usesDefinitions);
130 final Set<FeatureDefinition> features = new TreeSet<FeatureDefinition>(Comparators.SCHEMA_NODE_COMP);
131 for (FeatureBuilder fb : addedFeatures) {
132 features.add(fb.build());
134 instance.setFeatures(features);
137 final Set<NotificationDefinition> notifications = new TreeSet<NotificationDefinition>(
138 Comparators.SCHEMA_NODE_COMP);
139 for (NotificationBuilder entry : addedNotifications) {
140 notifications.add(entry.build());
142 instance.setNotifications(notifications);
145 final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
146 for (AugmentationSchemaBuilder builder : addedAugments) {
147 augmentations.add(builder.build());
149 instance.setAugmentations(augmentations);
152 final Set<RpcDefinition> rpcs = new TreeSet<RpcDefinition>(Comparators.SCHEMA_NODE_COMP);
153 for (RpcDefinitionBuilder rpc : addedRpcs) {
154 rpcs.add(rpc.build());
156 instance.setRpcs(rpcs);
159 final Set<Deviation> deviations = new HashSet<Deviation>();
160 for (DeviationBuilder entry : addedDeviations) {
161 deviations.add(entry.build());
163 instance.setDeviations(deviations);
166 final List<ExtensionDefinition> extensions = new ArrayList<ExtensionDefinition>();
167 for (ExtensionBuilder eb : addedExtensions) {
168 extensions.add(eb.build());
170 Collections.sort(extensions, Comparators.SCHEMA_NODE_COMP);
171 instance.setExtensionSchemaNodes(extensions);
174 final Set<IdentitySchemaNode> identities = new TreeSet<IdentitySchemaNode>(Comparators.SCHEMA_NODE_COMP);
175 for (IdentitySchemaNodeBuilder id : addedIdentities) {
176 identities.add(id.build());
178 instance.setIdentities(identities);
181 final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
182 for (UnknownSchemaNodeBuilder unb : addedUnknownNodes) {
183 unknownNodes.add(unb.build());
185 instance.setUnknownSchemaNodes(unknownNodes);
190 public boolean isAllUsesDataCollected() {
191 for(UsesNodeBuilder usesNode : allUsesNodes) {
192 if(!usesNode.isDataCollected()) {
200 public void setParent(Builder parent) {
201 throw new YangParseException(name, 0, "Can not set parent to module");
205 public SchemaPath getPath() {
210 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
211 return addedTypedefs;
214 public void enterNode(final Builder node) {
215 actualPath.push(node);
218 public void exitNode() {
222 public Builder getActualNode() {
223 if (actualPath.isEmpty()) {
226 return actualPath.get(0);
230 public Builder getActualParent() {
231 if (actualPath.size() < 2) {
234 return actualPath.get(1);
238 public Set<TypeAwareBuilder> getDirtyNodes() {
242 public List<AugmentationSchemaBuilder> getAllAugments() {
246 public Set<IdentitySchemaNodeBuilder> getIdentities() {
247 return addedIdentities;
250 public List<UsesNodeBuilder> getAllUsesNodes() {
254 public Set<DeviationBuilder> getDeviations() {
255 return addedDeviations;
258 public List<ExtensionBuilder> getExtensions() {
259 return addedExtensions;
262 public List<UnknownSchemaNodeBuilder> getAllUnknownNodes() {
263 return allUnknownNodes;
266 public String getName() {
270 public URI getNamespace() {
274 public void setNamespace(final URI namespace) {
275 this.namespace = namespace;
278 public String getPrefix() {
282 public Date getRevision() {
286 public void markActualNodeDirty() {
287 final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) getActualNode();
288 dirtyNodes.add(nodeBuilder);
291 public void setRevision(final Date revision) {
292 this.revision = revision;
295 public void setPrefix(final String prefix) {
296 this.prefix = prefix;
299 public void setYangVersion(final String yangVersion) {
300 instance.setYangVersion(yangVersion);
303 public void setDescription(final String description) {
304 instance.setDescription(description);
307 public void setReference(final String reference) {
308 instance.setReference(reference);
311 public void setOrganization(final String organization) {
312 instance.setOrganization(organization);
315 public void setContact(final String contact) {
316 instance.setContact(contact);
319 public boolean addModuleImport(final String moduleName, final Date revision, final String prefix) {
320 final ModuleImport moduleImport = createModuleImport(moduleName, revision, prefix);
321 return imports.add(moduleImport);
324 public Set<ModuleImport> getModuleImports() {
328 public ExtensionBuilder addExtension(final QName qname, final int line) {
329 final String extName = qname.getLocalName();
330 for (ExtensionBuilder addedExtension : addedExtensions) {
331 if (addedExtension.getQName().getLocalName().equals(extName)) {
332 throw new YangParseException(moduleName, line, "Can not add extension '" + extName
333 + "': extension with same name already declared at line " + addedExtension.getLine());
336 final ExtensionBuilder builder = new ExtensionBuilder(name, line, qname);
337 addedExtensions.add(builder);
341 public ContainerSchemaNodeBuilder addContainerNode(final int line, final QName qname, final SchemaPath schemaPath) {
342 final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
344 Builder parent = getActualNode();
345 builder.setParent(parent);
346 addChildToParent(parent, builder, qname.getLocalName());
351 public ListSchemaNodeBuilder addListNode(final int line, final QName qname, final SchemaPath schemaPath) {
352 final ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(name, line, qname, schemaPath);
354 Builder parent = getActualNode();
355 builder.setParent(parent);
356 addChildToParent(parent, builder, qname.getLocalName());
361 public LeafSchemaNodeBuilder addLeafNode(final int line, final QName qname, final SchemaPath schemaPath) {
362 final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(name, line, qname, schemaPath);
364 Builder parent = getActualNode();
365 builder.setParent(parent);
366 addChildToParent(parent, builder, qname.getLocalName());
371 public LeafListSchemaNodeBuilder addLeafListNode(final int line, final QName qname, final SchemaPath schemaPath) {
372 final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(name, line, qname, schemaPath);
374 Builder parent = getActualNode();
375 builder.setParent(parent);
376 addChildToParent(parent, builder, qname.getLocalName());
381 public GroupingBuilder addGrouping(final int line, final QName qname) {
382 final GroupingBuilder builder = new GroupingBuilderImpl(name, line, qname);
384 Builder parent = getActualNode();
385 builder.setParent(parent);
387 String groupingName = qname.getLocalName();
388 if (parent.equals(this)) {
389 for (GroupingBuilder addedGrouping : addedGroupings) {
390 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
391 throw new YangParseException(name, line, "grouping with same name '" + groupingName
392 + "' already declared at line " + addedGrouping.getLine());
395 addedGroupings.add(builder);
397 if (parent instanceof DataNodeContainerBuilder) {
398 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
399 for (GroupingBuilder addedGrouping : parentNode.getGroupingBuilders()) {
400 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
401 throw new YangParseException(name, line, "grouping with same name '" + groupingName
402 + "' already declared at line " + addedGrouping.getLine());
405 parentNode.addGrouping(builder);
406 } else if (parent instanceof RpcDefinitionBuilder) {
407 RpcDefinitionBuilder parentNode = (RpcDefinitionBuilder) parent;
408 for (GroupingBuilder child : parentNode.getGroupings()) {
409 if (child.getQName().getLocalName().equals(groupingName)) {
410 throw new YangParseException(name, line, "grouping with same name '" + groupingName
411 + "' already declared at line " + child.getLine());
414 parentNode.addGrouping(builder);
416 throw new YangParseException(name, line, "Unresolved parent of grouping " + groupingName);
423 public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr) {
424 final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr);
426 Builder parent = getActualNode();
427 builder.setParent(parent);
429 if (parent.equals(this)) {
430 // augment can be declared only under 'module' ...
431 addedAugments.add(builder);
433 // ... or 'uses' statement
434 if (parent instanceof UsesNodeBuilder) {
435 ((UsesNodeBuilder) parent).addAugment(builder);
437 throw new YangParseException(name, line, "Augment can be declared only under module or uses statement.");
440 allAugments.add(builder);
446 public Set<UsesNodeBuilder> getUsesNodes() {
447 return addedUsesNodes;
451 public void addUsesNode(UsesNodeBuilder usesBuilder) {
452 addedUsesNodes.add(usesBuilder);
453 allUsesNodes.add(usesBuilder);
456 public UsesNodeBuilder addUsesNode(final int line, final String groupingPathStr) {
457 final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(name, line, groupingPathStr);
459 Builder parent = getActualNode();
460 usesBuilder.setParent(parent);
462 if (parent.equals(this)) {
463 addedUsesNodes.add(usesBuilder);
465 if (!(parent instanceof DataNodeContainerBuilder)) {
466 throw new YangParseException(name, line, "Unresolved parent of uses '" + groupingPathStr + "'.");
468 ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder);
470 allUsesNodes.add(usesBuilder);
474 public void addRefine(final RefineHolder refine) {
475 final Builder parent = getActualNode();
476 if (!(parent instanceof UsesNodeBuilder)) {
477 throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
479 ((UsesNodeBuilder) parent).addRefine(refine);
480 refine.setParent(parent);
483 public RpcDefinitionBuilder addRpc(final int line, final QName qname) {
484 Builder parent = getActualNode();
485 if (!(parent.equals(this))) {
486 throw new YangParseException(name, line, "rpc can be defined only in module or submodule");
489 final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(name, line, qname);
490 rpcBuilder.setParent(parent);
492 String rpcName = qname.getLocalName();
493 for (RpcDefinitionBuilder rpc : addedRpcs) {
494 if (rpc.getQName().getLocalName().equals(rpcName)) {
495 throw new YangParseException(name, line, "rpc with same name '" + rpcName
496 + "' already declared at line " + rpc.getLine());
499 for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
500 if (addedChild.getQName().getLocalName().equals(rpcName)) {
501 throw new YangParseException(name, line, "Can not add rpc: node with same name '" + rpcName
502 + "' already declared at line " + addedChild.getLine());
505 for (NotificationBuilder addedNotification : addedNotifications) {
506 if (addedNotification.getQName().getLocalName().equals(rpcName)) {
507 throw new YangParseException(name, line, "Can not add rpc: notification with same name '" + rpcName
508 + "' already declared at line " + addedNotification.getLine());
511 addedRpcs.add(rpcBuilder);
515 public ContainerSchemaNodeBuilder addRpcInput(final int line, final QName qname, final SchemaPath schemaPath) {
516 final Builder parent = getActualNode();
517 if (!(parent instanceof RpcDefinitionBuilder)) {
518 throw new YangParseException(name, line, "input can be defined only in rpc statement");
520 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
522 final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
523 inputBuilder.setParent(rpc);
525 rpc.setInput(inputBuilder);
529 public ContainerSchemaNodeBuilder addRpcOutput(final SchemaPath schemaPath, final QName qname, final int line) {
530 final Builder parent = getActualNode();
531 if (!(parent instanceof RpcDefinitionBuilder)) {
532 throw new YangParseException(name, line, "output can be defined only in rpc statement");
534 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
536 final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
537 outputBuilder.setParent(rpc);
539 rpc.setOutput(outputBuilder);
540 return outputBuilder;
543 public NotificationBuilder addNotification(final int line, final QName qname) {
544 final Builder parent = getActualNode();
545 if (!(parent.equals(this))) {
546 throw new YangParseException(name, line, "notification can be defined only in module or submodule");
549 String notificationName = qname.getLocalName();
550 for (NotificationBuilder nb : addedNotifications) {
551 if (nb.getQName().equals(qname)) {
552 throw new YangParseException(name, line, "notification with same name '" + notificationName
553 + "' already declared at line " + nb.getLine());
556 for (RpcDefinitionBuilder rpc : addedRpcs) {
557 if (rpc.getQName().getLocalName().equals(notificationName)) {
558 throw new YangParseException(name, line, "Can not add notification: rpc with same name '"
559 + notificationName + "' already declared at line " + rpc.getLine());
562 for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
563 if (addedChild.getQName().getLocalName().equals(notificationName)) {
564 throw new YangParseException(name, line, "Can not add notification: node with same name '"
565 + notificationName + "' already declared at 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 throw new YangParseException(name, line, "feature with same name '" + featureName
589 + "' already declared at line " + addedFeature.getLine());
592 addedFeatures.add(builder);
596 public ChoiceBuilder addChoice(final int line, final QName qname) {
597 final ChoiceBuilder builder = new ChoiceBuilder(name, line, qname);
599 Builder parent = getActualNode();
600 builder.setParent(parent);
601 addChildToParent(parent, builder, qname.getLocalName());
606 public ChoiceCaseBuilder addCase(final int line, final QName qname) {
607 Builder parent = getActualNode();
608 if (parent == null || parent.equals(this)) {
609 throw new YangParseException(name, line, "'case' parent not found");
612 final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(name, line, qname);
613 builder.setParent(parent);
615 if (parent instanceof ChoiceBuilder) {
616 ((ChoiceBuilder) parent).addCase(builder);
617 } else if (parent instanceof AugmentationSchemaBuilder) {
618 ((AugmentationSchemaBuilder) parent).addChildNode(builder);
620 throw new YangParseException(name, line, "Unresolved parent of 'case' " + qname.getLocalName());
626 public AnyXmlBuilder addAnyXml(final int line, final QName qname, final SchemaPath schemaPath) {
627 final AnyXmlBuilder builder = new AnyXmlBuilder(name, line, qname, schemaPath);
629 Builder parent = getActualNode();
630 builder.setParent(parent);
631 addChildToParent(parent, builder, qname.getLocalName());
637 public void addTypedef(TypeDefinitionBuilder typedefBuilder) {
638 String nodeName = typedefBuilder.getQName().getLocalName();
639 for (TypeDefinitionBuilder tdb : addedTypedefs) {
640 if (tdb.getQName().getLocalName().equals(nodeName)) {
641 throw new YangParseException(name, typedefBuilder.getLine(), "typedef with same name '" + nodeName
642 + "' already declared at line " + tdb.getLine());
645 addedTypedefs.add(typedefBuilder);
648 public TypeDefinitionBuilderImpl addTypedef(final int line, final QName qname) {
649 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(name, line, qname);
651 Builder parent = getActualNode();
652 builder.setParent(parent);
654 String typedefName = qname.getLocalName();
655 if (parent.equals(this)) {
656 for (TypeDefinitionBuilder tdb : addedTypedefs) {
657 if (tdb.getQName().getLocalName().equals(typedefName)) {
658 throw new YangParseException(name, line, "typedef with same name '" + typedefName
659 + "' already declared at line " + tdb.getLine());
662 addedTypedefs.add(builder);
664 if (parent instanceof DataNodeContainerBuilder) {
665 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
666 for (TypeDefinitionBuilder child : parentNode.getTypeDefinitionBuilders()) {
667 if (child.getQName().getLocalName().equals(typedefName)) {
668 throw new YangParseException(name, line, "typedef with same name '" + typedefName
669 + "' already declared at line " + child.getLine());
672 parentNode.addTypedef(builder);
673 } else if (parent instanceof RpcDefinitionBuilder) {
674 RpcDefinitionBuilder rpcParent = (RpcDefinitionBuilder) parent;
675 for (TypeDefinitionBuilder tdb : rpcParent.getTypeDefinitions()) {
676 if (tdb.getQName().getLocalName().equals(builder.getQName().getLocalName())) {
677 throw new YangParseException(name, line, "typedef with same name '" + typedefName
678 + "' already declared at line " + tdb.getLine());
681 rpcParent.addTypedef(builder);
683 throw new YangParseException(name, line, "Unresolved parent of typedef " + typedefName);
690 public void setType(final TypeDefinition<?> type) {
691 Builder parent = getActualNode();
692 if (parent == null || !(parent instanceof TypeAwareBuilder)) {
693 throw new YangParseException("Failed to set type '" + type.getQName().getLocalName()
694 + "'. Invalid parent node: " + parent);
696 ((TypeAwareBuilder) parent).setType(type);
699 public UnionTypeBuilder addUnionType(final int line, final URI namespace, final Date revision) {
700 final Builder parent = getActualNode();
701 if (parent == null) {
702 throw new YangParseException(name, line, "Unresolved parent of union type");
704 final UnionTypeBuilder union = new UnionTypeBuilder(name, line);
705 if (parent instanceof TypeAwareBuilder) {
706 ((TypeAwareBuilder) parent).setTypedef(union);
709 throw new YangParseException(name, line, "Invalid parent of union type.");
714 public void addIdentityrefType(final int line, final SchemaPath schemaPath, final String baseString) {
715 final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(name, line, baseString, schemaPath);
717 final Builder parent = getActualNode();
718 if (parent == null) {
719 throw new YangParseException(name, line, "Unresolved parent of identityref type.");
721 if (parent instanceof TypeAwareBuilder) {
722 final TypeAwareBuilder typeParent = (TypeAwareBuilder) parent;
723 typeParent.setTypedef(identityref);
724 dirtyNodes.add(typeParent);
726 throw new YangParseException(name, line, "Invalid parent of identityref type.");
731 public DeviationBuilder addDeviation(final int line, final String targetPath) {
732 Builder parent = getActualNode();
733 if (!(parent.equals(this))) {
734 throw new YangParseException(name, line, "deviation can be defined only in module or submodule");
737 final DeviationBuilder builder = new DeviationBuilder(name, line, targetPath);
738 builder.setParent(parent);
739 addedDeviations.add(builder);
743 public IdentitySchemaNodeBuilder addIdentity(final QName qname, final int line) {
744 Builder parent = getActualNode();
745 if (!(parent.equals(this))) {
746 throw new YangParseException(name, line, "identity can be defined only in module or submodule");
748 String identityName = qname.getLocalName();
749 for (IdentitySchemaNodeBuilder idBuilder : addedIdentities) {
750 if (idBuilder.getQName().equals(qname)) {
751 throw new YangParseException(name, line, "identity with same name '" + identityName
752 + "' already declared at 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()
793 public String toString() {
794 return "module " + name;
797 private final class ModuleImpl implements Module {
798 private URI namespace;
799 private final String name;
800 private Date revision;
801 private String prefix;
802 private String yangVersion;
803 private String description;
804 private String reference;
805 private String organization;
806 private String contact;
807 private Set<ModuleImport> imports = Collections.emptySet();
808 private Set<FeatureDefinition> features = Collections.emptySet();
809 private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
810 private Set<NotificationDefinition> notifications = Collections.emptySet();
811 private Set<AugmentationSchema> augmentations = Collections.emptySet();
812 private Set<RpcDefinition> rpcs = Collections.emptySet();
813 private Set<Deviation> deviations = Collections.emptySet();
814 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
815 private Set<GroupingDefinition> groupings = Collections.emptySet();
816 private Set<UsesNode> uses = Collections.emptySet();
817 private List<ExtensionDefinition> extensionNodes = Collections.emptyList();
818 private Set<IdentitySchemaNode> identities = Collections.emptySet();
819 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
821 private ModuleImpl(String name) {
826 public URI getNamespace() {
830 private void setNamespace(URI namespace) {
831 this.namespace = namespace;
835 public String getName() {
840 public Date getRevision() {
844 private void setRevision(Date revision) {
845 this.revision = revision;
849 public String getPrefix() {
853 private void setPrefix(String prefix) {
854 this.prefix = prefix;
858 public String getYangVersion() {
862 private void setYangVersion(String yangVersion) {
863 this.yangVersion = yangVersion;
867 public String getDescription() {
871 private void setDescription(String description) {
872 this.description = description;
876 public String getReference() {
880 private void setReference(String reference) {
881 this.reference = reference;
885 public String getOrganization() {
889 private void setOrganization(String organization) {
890 this.organization = organization;
894 public String getContact() {
898 private void setContact(String contact) {
899 this.contact = contact;
903 public Set<ModuleImport> getImports() {
907 private void setImports(Set<ModuleImport> imports) {
908 if (imports != null) {
909 this.imports = imports;
914 public Set<FeatureDefinition> getFeatures() {
918 private void setFeatures(Set<FeatureDefinition> features) {
919 if (features != null) {
920 this.features = features;
925 public Set<TypeDefinition<?>> getTypeDefinitions() {
926 return typeDefinitions;
929 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
930 if (typeDefinitions != null) {
931 this.typeDefinitions = typeDefinitions;
936 public Set<NotificationDefinition> getNotifications() {
937 return notifications;
940 private void setNotifications(Set<NotificationDefinition> notifications) {
941 if (notifications != null) {
942 this.notifications = notifications;
947 public Set<AugmentationSchema> getAugmentations() {
948 return augmentations;
951 private void setAugmentations(Set<AugmentationSchema> augmentations) {
952 if (augmentations != null) {
953 this.augmentations = augmentations;
958 public Set<RpcDefinition> getRpcs() {
962 private void setRpcs(Set<RpcDefinition> rpcs) {
969 public Set<Deviation> getDeviations() {
973 private void setDeviations(Set<Deviation> deviations) {
974 if (deviations != null) {
975 this.deviations = deviations;
980 public Set<DataSchemaNode> getChildNodes() {
981 return new LinkedHashSet<DataSchemaNode>(childNodes.values());
984 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
985 if (childNodes != null) {
986 this.childNodes = childNodes;
991 public Set<GroupingDefinition> getGroupings() {
995 private void setGroupings(Set<GroupingDefinition> groupings) {
996 if (groupings != null) {
997 this.groupings = groupings;
1002 public Set<UsesNode> getUses() {
1006 private void setUses(Set<UsesNode> uses) {
1013 public List<ExtensionDefinition> getExtensionSchemaNodes() {
1014 return extensionNodes;
1017 private void setExtensionSchemaNodes(final List<ExtensionDefinition> extensionNodes) {
1018 if (extensionNodes != null) {
1019 this.extensionNodes = extensionNodes;
1024 public Set<IdentitySchemaNode> getIdentities() {
1028 private void setIdentities(final Set<IdentitySchemaNode> identities) {
1029 if (identities != null) {
1030 this.identities = identities;
1035 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
1036 return unknownNodes;
1039 private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
1040 if (unknownNodes != null) {
1041 this.unknownNodes = unknownNodes;
1046 public DataSchemaNode getDataChildByName(QName name) {
1047 return childNodes.get(name);
1051 public DataSchemaNode getDataChildByName(String name) {
1052 DataSchemaNode result = null;
1053 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
1054 if (entry.getKey().getLocalName().equals(name)) {
1055 result = entry.getValue();
1063 public int hashCode() {
1064 final int prime = 31;
1066 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1067 result = prime * result + ((name == null) ? 0 : name.hashCode());
1068 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1069 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1070 result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());
1075 public boolean equals(Object obj) {
1082 if (getClass() != obj.getClass()) {
1085 ModuleImpl other = (ModuleImpl) obj;
1086 if (namespace == null) {
1087 if (other.namespace != null) {
1090 } else if (!namespace.equals(other.namespace)) {
1094 if (other.name != null) {
1097 } else if (!name.equals(other.name)) {
1100 if (revision == null) {
1101 if (other.revision != null) {
1104 } else if (!revision.equals(other.revision)) {
1107 if (prefix == null) {
1108 if (other.prefix != null) {
1111 } else if (!prefix.equals(other.prefix)) {
1114 if (yangVersion == null) {
1115 if (other.yangVersion != null) {
1118 } else if (!yangVersion.equals(other.yangVersion)) {
1125 public String toString() {
1126 StringBuilder sb = new StringBuilder(ModuleImpl.class.getSimpleName());
1128 sb.append("name=" + name);
1129 sb.append(", namespace=" + namespace);
1130 sb.append(", revision=" + revision);
1131 sb.append(", prefix=" + prefix);
1132 sb.append(", yangVersion=" + yangVersion);
1134 return sb.toString();
1139 * Add child to parent. Method checks for duplicates and add given child
1140 * node to parent. If node with same name is found, throws exception. If
1141 * parent is null, child node will be added directly to module.
1147 private void addChildToParent(final Builder parent, final DataSchemaNodeBuilder child, final String childName) {
1148 final int line = child.getLine();
1149 if (parent.equals(this)) {
1150 // if parent == null => node is defined under module
1151 // All leafs, leaf-lists, lists, containers, choices, rpcs,
1152 // notifications, and anyxmls defined within a parent node or at the
1153 // top level of the module or its submodules share the same
1154 // identifier namespace.
1155 for (DataSchemaNodeBuilder childNode : addedChildNodes) {
1156 if (childNode.getQName().getLocalName().equals(childName)) {
1157 throw new YangParseException(name, line, "Can not add '" + child
1158 + "': node with same name already declared at line " + childNode.getLine());
1161 for (RpcDefinitionBuilder rpc : addedRpcs) {
1162 if (rpc.getQName().getLocalName().equals(childName)) {
1163 throw new YangParseException(name, line, "Can not add '" + child
1164 + "': rpc with same name already declared at line " + rpc.getLine());
1167 for (NotificationBuilder notification : addedNotifications) {
1168 if (notification.getQName().getLocalName().equals(childName)) {
1169 throw new YangParseException(name, line, "Can not add '" + child
1170 + "': notification with same name already declared at line " + notification.getLine());
1173 addedChildNodes.add(child);
1175 if(parent instanceof AugmentationSchemaBuilder) {
1176 child.setAugmenting(true);
1178 // no need for checking rpc and notification because they can be
1179 // defined only under module or submodule
1180 if (parent instanceof DataNodeContainerBuilder) {
1181 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
1182 for (DataSchemaNodeBuilder childNode : parentNode.getChildNodeBuilders()) {
1183 if (childNode.getQName().getLocalName().equals(childName)) {
1184 throw new YangParseException(name, line, "Can not add '" + child + "': node with same name '"
1185 + childName + "' already declared at line " + childNode.getLine());
1188 parentNode.addChildNode(child);
1189 } else if (parent instanceof ChoiceBuilder) {
1190 ChoiceBuilder parentNode = (ChoiceBuilder) parent;
1191 for (ChoiceCaseBuilder caseBuilder : parentNode.getCases()) {
1192 if (caseBuilder.getQName().getLocalName().equals(childName)) {
1193 throw new YangParseException(name, line, "Can not add '" + child + "': case with same name '"
1194 + childName + "' already declared at line " + caseBuilder.getLine());
1197 parentNode.addCase(child);
1199 throw new YangParseException(name, line, "Unresolved parent of node '" + childName + "'.");
1204 private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) {
1205 final ModuleImport moduleImport = new ModuleImport() {
1207 public String getModuleName() {
1212 public Date getRevision() {
1217 public String getPrefix() {
1222 public int hashCode() {
1223 final int prime = 31;
1225 result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
1226 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1227 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1232 public boolean equals(Object obj) {
1239 if (getClass() != obj.getClass()) {
1242 ModuleImport other = (ModuleImport) obj;
1243 if (getModuleName() == null) {
1244 if (other.getModuleName() != null) {
1247 } else if (!getModuleName().equals(other.getModuleName())) {
1250 if (getRevision() == null) {
1251 if (other.getRevision() != null) {
1254 } else if (!getRevision().equals(other.getRevision())) {
1257 if (getPrefix() == null) {
1258 if (other.getPrefix() != null) {
1261 } else if (!getPrefix().equals(other.getPrefix())) {
1268 public String toString() {
1269 return "ModuleImport[moduleName=" + moduleName + ", revision=" + revision + ", prefix=" + prefix + "]";
1272 return moduleImport;