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.controller.yang.parser.builder.impl;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.LinkedList;
17 import java.util.List;
21 import org.opendaylight.controller.yang.common.QName;
22 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
23 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
24 import org.opendaylight.controller.yang.model.api.Deviation;
25 import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
26 import org.opendaylight.controller.yang.model.api.FeatureDefinition;
27 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
28 import org.opendaylight.controller.yang.model.api.IdentitySchemaNode;
29 import org.opendaylight.controller.yang.model.api.Module;
30 import org.opendaylight.controller.yang.model.api.ModuleImport;
31 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
32 import org.opendaylight.controller.yang.model.api.RpcDefinition;
33 import org.opendaylight.controller.yang.model.api.SchemaPath;
34 import org.opendaylight.controller.yang.model.api.TypeDefinition;
35 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
36 import org.opendaylight.controller.yang.model.api.UsesNode;
37 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
38 import org.opendaylight.controller.yang.parser.builder.api.Builder;
39 import org.opendaylight.controller.yang.parser.builder.api.DataNodeContainerBuilder;
40 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
41 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
42 import org.opendaylight.controller.yang.parser.builder.api.SchemaNodeBuilder;
43 import org.opendaylight.controller.yang.parser.builder.api.TypeAwareBuilder;
44 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionAwareBuilder;
45 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
46 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
47 import org.opendaylight.controller.yang.parser.util.RefineHolder;
48 import org.opendaylight.controller.yang.parser.util.YangParseException;
51 * This builder builds Module object. If this module is dependent on external
52 * module/modules, these dependencies must be resolved before module is built,
53 * otherwise result may not be valid.
55 public class ModuleBuilder implements DataNodeContainerBuilder {
56 private final ModuleImpl instance;
57 private final String name;
58 private URI namespace;
59 private String prefix;
60 private Date revision;
62 private int augmentsResolved;
64 private final Set<ModuleImport> imports = new HashSet<ModuleImport>();
67 * Holds all child (DataSchemaNode) nodes: anyxml, choice, case, container,
68 * list, leaf, leaf-list.
70 private final Map<List<String>, DataSchemaNodeBuilder> childNodes = new HashMap<List<String>, DataSchemaNodeBuilder>();
72 private final Map<List<String>, GroupingBuilder> addedGroupings = new HashMap<List<String>, GroupingBuilder>();
73 private final List<AugmentationSchemaBuilder> addedAugments = new ArrayList<AugmentationSchemaBuilder>();
74 private final Map<List<String>, UsesNodeBuilder> addedUsesNodes = new HashMap<List<String>, UsesNodeBuilder>();
75 private final Map<List<String>, RpcDefinitionBuilder> addedRpcs = new HashMap<List<String>, RpcDefinitionBuilder>();
76 private final Set<NotificationBuilder> addedNotifications = new HashSet<NotificationBuilder>();
77 private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<IdentitySchemaNodeBuilder>();
78 private final Map<List<String>, FeatureBuilder> addedFeatures = new HashMap<List<String>, FeatureBuilder>();
79 private final Map<List<String>, DeviationBuilder> addedDeviations = new HashMap<List<String>, DeviationBuilder>();
80 private final Map<List<String>, TypeDefinitionBuilder> addedTypedefs = new HashMap<List<String>, TypeDefinitionBuilder>();
81 private final Map<List<String>, UnionTypeBuilder> addedUnionTypes = new HashMap<List<String>, UnionTypeBuilder>();
82 private final List<ExtensionBuilder> addedExtensions = new ArrayList<ExtensionBuilder>();
83 private final Map<List<String>, List<UnknownSchemaNodeBuilder>> addedUnknownNodes = new HashMap<List<String>, List<UnknownSchemaNodeBuilder>>();
85 private final Map<List<String>, TypeAwareBuilder> dirtyNodes = new HashMap<List<String>, TypeAwareBuilder>();
87 private final LinkedList<Builder> actualPath = new LinkedList<Builder>();
89 public ModuleBuilder(final String name) {
91 instance = new ModuleImpl(name);
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 = buildModuleTypedefs(addedTypedefs);
106 instance.setTypeDefinitions(typedefs);
109 final Map<QName, DataSchemaNode> children = buildModuleChildNodes(childNodes);
110 instance.setChildNodes(children);
113 final Set<GroupingDefinition> groupings = buildModuleGroupings(addedGroupings);
114 instance.setGroupings(groupings);
117 final Set<UsesNode> usesDefinitions = buildUsesNodes(addedUsesNodes);
118 instance.setUses(usesDefinitions);
121 final Set<FeatureDefinition> features = buildModuleFeatures(addedFeatures);
122 instance.setFeatures(features);
125 final Set<NotificationDefinition> notifications = new HashSet<NotificationDefinition>();
126 for (NotificationBuilder entry : addedNotifications) {
127 notifications.add(entry.build());
129 instance.setNotifications(notifications);
132 final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
133 for (AugmentationSchemaBuilder builder : addedAugments) {
134 augmentations.add(builder.build());
136 instance.setAugmentations(augmentations);
139 final Set<RpcDefinition> rpcs = buildModuleRpcs(addedRpcs);
140 instance.setRpcs(rpcs);
143 final Set<Deviation> deviations = new HashSet<Deviation>();
144 for (Map.Entry<List<String>, DeviationBuilder> entry : addedDeviations.entrySet()) {
145 deviations.add(entry.getValue().build());
147 instance.setDeviations(deviations);
150 final List<ExtensionDefinition> extensions = new ArrayList<ExtensionDefinition>();
151 for (ExtensionBuilder b : addedExtensions) {
152 extensions.add(b.build());
154 instance.setExtensionSchemaNodes(extensions);
157 final Set<IdentitySchemaNode> identities = new HashSet<IdentitySchemaNode>();
158 for (IdentitySchemaNodeBuilder idBuilder : addedIdentities) {
159 identities.add(idBuilder.build());
161 instance.setIdentities(identities);
164 final List<UnknownSchemaNode> unknownNodes = buildModuleUnknownNodes(addedUnknownNodes);
165 instance.setUnknownSchemaNodes(unknownNodes);
171 public int getLine() {
176 public QName getQName() {
177 return new QName(namespace, revision, prefix, name);
181 public SchemaPath getPath() {
186 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
187 final Set<TypeDefinitionBuilder> typeDefinitions = new HashSet<TypeDefinitionBuilder>();
188 for (final Map.Entry<List<String>, TypeDefinitionBuilder> entry : addedTypedefs.entrySet()) {
189 final List<String> key = entry.getKey();
190 final TypeDefinitionBuilder typedefBuilder = entry.getValue();
191 if (key.size() == 2) {
192 typeDefinitions.add(typedefBuilder);
196 return typeDefinitions;
199 public void enterNode(final Builder node) {
200 actualPath.push(node);
203 public void exitNode() {
207 public Builder getActualNode() {
208 if (actualPath.isEmpty()) {
211 return actualPath.get(0);
215 public Builder getActualParent() {
216 if (actualPath.size() < 2) {
219 return actualPath.get(1);
223 public Builder getModuleNode(final List<String> path) {
224 return childNodes.get(path);
227 public GroupingBuilder getGrouping(final List<String> path) {
228 return addedGroupings.get(path);
232 public Set<GroupingDefinition> getGroupings() {
233 return Collections.emptySet();
237 public Set<GroupingBuilder> getGroupingBuilders() {
238 final Set<GroupingBuilder> result = new HashSet<GroupingBuilder>();
239 for (Map.Entry<List<String>, GroupingBuilder> entry : addedGroupings.entrySet()) {
240 if (entry.getKey().size() == 2) {
241 result.add(entry.getValue());
247 public Builder getModuleTypedef(final List<String> path) {
248 return addedTypedefs.get(path);
252 public Set<DataSchemaNode> getChildNodes() {
253 return Collections.emptySet();
256 public Set<DataSchemaNodeBuilder> getChildNodeBuilders() {
257 final Set<DataSchemaNodeBuilder> children = new HashSet<DataSchemaNodeBuilder>();
258 for (Map.Entry<List<String>, DataSchemaNodeBuilder> entry : childNodes.entrySet()) {
259 final List<String> path = entry.getKey();
260 final DataSchemaNodeBuilder child = entry.getValue();
261 if (path.size() == 2) {
268 public Map<List<String>, TypeAwareBuilder> getDirtyNodes() {
272 public List<AugmentationSchemaBuilder> getAugments() {
273 return addedAugments;
276 public Set<IdentitySchemaNodeBuilder> getIdentities() {
277 return addedIdentities;
280 public Map<List<String>, UsesNodeBuilder> getUsesNodes() {
281 return addedUsesNodes;
284 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
285 List<UnknownSchemaNodeBuilder> result = new ArrayList<UnknownSchemaNodeBuilder>();
286 for (List<UnknownSchemaNodeBuilder> entry : addedUnknownNodes.values()) {
287 result.addAll(entry);
292 public Set<TypeDefinitionBuilder> getModuleTypedefs() {
293 final Set<TypeDefinitionBuilder> typedefs = new HashSet<TypeDefinitionBuilder>();
294 for (Map.Entry<List<String>, TypeDefinitionBuilder> entry : addedTypedefs.entrySet()) {
295 if (entry.getKey().size() == 2) {
296 typedefs.add(entry.getValue());
302 public Set<GroupingBuilder> getModuleGroupings() {
303 final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
304 for (Map.Entry<List<String>, GroupingBuilder> entry : addedGroupings.entrySet()) {
305 if (entry.getKey().size() == 2) {
306 groupings.add(entry.getValue());
312 public String getName() {
316 public URI getNamespace() {
320 public void setNamespace(final URI namespace) {
321 this.namespace = namespace;
324 public String getPrefix() {
328 public Date getRevision() {
332 public int getAugmentsResolved() {
333 return augmentsResolved;
336 public void augmentResolved() {
340 public void addDirtyNode(final List<String> path) {
341 final List<String> dirtyNodePath = new ArrayList<String>(path);
342 final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) actualPath.getFirst();
343 dirtyNodes.put(dirtyNodePath, nodeBuilder);
346 public void setRevision(final Date revision) {
347 this.revision = revision;
350 public void setPrefix(final String prefix) {
351 this.prefix = prefix;
354 public void setYangVersion(final String yangVersion) {
355 instance.setYangVersion(yangVersion);
358 public void setDescription(final String description) {
359 instance.setDescription(description);
362 public void setReference(final String reference) {
363 instance.setReference(reference);
366 public void setOrganization(final String organization) {
367 instance.setOrganization(organization);
370 public void setContact(final String contact) {
371 instance.setContact(contact);
374 public boolean addModuleImport(final String moduleName, final Date revision, final String prefix) {
375 final ModuleImport moduleImport = createModuleImport(moduleName, revision, prefix);
376 return imports.add(moduleImport);
379 public Set<ModuleImport> getModuleImports() {
383 public ExtensionBuilder addExtension(final QName qname, final int line) {
384 final ExtensionBuilder builder = new ExtensionBuilder(qname, line);
385 addedExtensions.add(builder);
390 public void addChildNode(DataSchemaNodeBuilder child) {
391 final List<String> pathToChild = new ArrayList<String>();
392 for (QName qname : child.getPath().getPath()) {
393 pathToChild.add(qname.getLocalName());
395 if (childNodes.containsKey(pathToChild)) {
396 throw new YangParseException(this.name, child.getLine(), "Failed to add child node "
397 + child.getQName().getLocalName() + ": node already exists in context.");
399 childNodes.put(pathToChild, child);
402 public ContainerSchemaNodeBuilder addContainerNode(final SchemaPath schemaPath, final QName containerName,
403 final List<String> parentPath, final int line) {
404 final List<String> pathToNode = new ArrayList<String>(parentPath);
405 final ContainerSchemaNodeBuilder containerBuilder = new ContainerSchemaNodeBuilder(containerName, schemaPath, line);
406 updateParent(containerBuilder, line, "container");
408 pathToNode.add(containerName.getLocalName());
409 childNodes.put(pathToNode, containerBuilder);
411 return containerBuilder;
414 public ListSchemaNodeBuilder addListNode(final SchemaPath schemaPath, final QName listName, final List<String> parentPath, final int line) {
415 final List<String> pathToNode = new ArrayList<String>(parentPath);
416 final ListSchemaNodeBuilder listBuilder = new ListSchemaNodeBuilder(listName, schemaPath, line);
417 updateParent(listBuilder, line, "list");
419 pathToNode.add(listName.getLocalName());
420 childNodes.put(pathToNode, listBuilder);
425 public LeafSchemaNodeBuilder addLeafNode(final SchemaPath schemaPath, final QName leafName, final List<String> parentPath, final int line) {
426 final List<String> pathToNode = new ArrayList<String>(parentPath);
427 final LeafSchemaNodeBuilder leafBuilder = new LeafSchemaNodeBuilder(leafName, schemaPath, line);
428 updateParent(leafBuilder, line, "leaf");
430 pathToNode.add(leafName.getLocalName());
431 childNodes.put(pathToNode, leafBuilder);
436 public LeafListSchemaNodeBuilder addLeafListNode(final SchemaPath schemaPath, final QName qname, final List<String> parentPath, final int line) {
437 final List<String> pathToNode = new ArrayList<String>(parentPath);
438 final LeafListSchemaNodeBuilder leafListBuilder = new LeafListSchemaNodeBuilder(qname, schemaPath, line);
439 updateParent(leafListBuilder, line, "leaf-list");
441 pathToNode.add(qname.getLocalName());
442 childNodes.put(pathToNode, leafListBuilder);
444 return leafListBuilder;
448 public void addGrouping(GroupingBuilder groupingBuilder) {
449 final List<String> pathToGroup = new ArrayList<String>();
450 for (QName qname : groupingBuilder.getPath().getPath()) {
451 pathToGroup.add(qname.getLocalName());
453 if (addedGroupings.containsKey(pathToGroup)) {
454 throw new YangParseException(this.name, groupingBuilder.getLine(), "Failed to add grouping "
455 + groupingBuilder.getQName().getLocalName() + ": grouping already exists in context.");
457 addedGroupings.put(pathToGroup, groupingBuilder);
460 public GroupingBuilder addGrouping(final QName qname, final List<String> parentPath, final int line) {
461 final List<String> pathToGroup = new ArrayList<String>(parentPath);
462 final GroupingBuilder builder = new GroupingBuilderImpl(qname, line);
464 if (!(actualPath.isEmpty())) {
465 final Builder parent = actualPath.getFirst();
466 if (parent instanceof DataNodeContainerBuilder) {
467 ((DataNodeContainerBuilder) parent).addGrouping(builder);
469 throw new YangParseException(name, line, "Unresolved parent of grouping " + qname.getLocalName());
473 pathToGroup.add(qname.getLocalName());
474 addedGroupings.put(pathToGroup, builder);
479 public AugmentationSchemaBuilder addAugment(final String name, final List<String> parentPath, final int line) {
480 final List<String> pathToAugment = new ArrayList<String>(parentPath);
481 Builder parent = null;
482 if (!(actualPath.isEmpty())) {
483 parent = actualPath.getFirst();
485 final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, parent);
487 // augment can only be in 'module' or 'uses' statement
488 if (parent != null) {
489 if (parent instanceof UsesNodeBuilder) {
490 ((UsesNodeBuilder) parent).addAugment(builder);
492 throw new YangParseException(this.name, line, "Unresolved parent of augment " + name);
496 pathToAugment.add(name);
497 addedAugments.add(builder);
503 public void addUsesNode(UsesNodeBuilder usesBuilder) {
504 final List<String> pathToTypedef = new ArrayList<String>();
505 for (QName qname : usesBuilder.getParent().getPath().getPath()) {
506 pathToTypedef.add(qname.getLocalName());
508 if (addedUsesNodes.containsKey(pathToTypedef)) {
509 throw new YangParseException(this.name, usesBuilder.getLine(), "Failed to add uses node "
510 + usesBuilder.getGroupingName() + ": uses already exists in context.");
512 addedUsesNodes.put(pathToTypedef, usesBuilder);
515 public UsesNodeBuilder addUsesNode(final String groupingPathStr, final List<String> parentPath, final int line) {
516 final List<String> pathToUses = new ArrayList<String>(parentPath);
517 Builder parent = null;
518 if (!actualPath.isEmpty()) {
519 parent = actualPath.getFirst();
521 if (parent != null && !(parent instanceof DataNodeContainerBuilder)) {
522 throw new YangParseException(name, line, "Unresolved parent of uses " + groupingPathStr);
524 final UsesNodeBuilder usesBuilder;
525 if (parent == null) {
526 usesBuilder = new UsesNodeBuilderImpl(groupingPathStr, line, this);
528 usesBuilder = new UsesNodeBuilderImpl(groupingPathStr, line, (DataNodeContainerBuilder) parent);
529 if (parent instanceof AugmentationSchemaBuilder) {
530 usesBuilder.setAugmenting(true);
532 ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder);
535 pathToUses.add(groupingPathStr);
536 addedUsesNodes.put(pathToUses, usesBuilder);
540 public void addRefine(final RefineHolder refine, final List<String> parentPath) {
541 final List<String> path = new ArrayList<String>(parentPath);
543 if (actualPath.isEmpty()) {
544 throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
546 final Builder parent = actualPath.getFirst();
547 if (parent instanceof UsesNodeBuilder) {
548 ((UsesNodeBuilder) parent).addRefine(refine);
550 throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
554 path.add(refine.getName());
557 public RpcDefinitionBuilder addRpc(final QName qname, final List<String> parentPath, final int line) {
559 if (!(actualPath.isEmpty())) {
560 throw new YangParseException(name, line, "rpc can be defined only in module or submodule");
563 final List<String> pathToRpc = new ArrayList<String>(parentPath);
564 final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(qname, line);
566 pathToRpc.add(qname.getLocalName());
567 addedRpcs.put(pathToRpc, rpcBuilder);
572 public ContainerSchemaNodeBuilder addRpcInput(final SchemaPath schemaPath, final QName inputQName, final int line) {
573 final Builder parent = actualPath.getFirst();
574 if (!(parent instanceof RpcDefinitionBuilder)) {
575 throw new YangParseException(name, line, "input can be defined only in rpc statement");
577 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
579 final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(inputQName, schemaPath, line);
580 rpc.setInput(inputBuilder);
584 public ContainerSchemaNodeBuilder addRpcOutput(final SchemaPath schemaPath, final QName outputQName, final int line) {
585 final Builder parent = actualPath.getFirst();
586 if (!(parent instanceof RpcDefinitionBuilder)) {
587 throw new YangParseException(name, line, "output can be defined only in rpc statement");
589 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
591 final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(outputQName, schemaPath, line);
592 rpc.setOutput(outputBuilder);
593 return outputBuilder;
596 public NotificationBuilder addNotification(final QName notificationName, final List<String> parentPath,
598 if (!(actualPath.isEmpty())) {
599 throw new YangParseException(name, line, "notification can be defined only in module or submodule");
601 final NotificationBuilder builder = new NotificationBuilder(notificationName, line);
603 final List<String> notificationPath = new ArrayList<String>(parentPath);
604 notificationPath.add(notificationName.getLocalName());
605 addedNotifications.add(builder);
610 public FeatureBuilder addFeature(final QName featureName, final List<String> parentPath, final int line) {
611 if (!(actualPath.isEmpty())) {
612 throw new YangParseException(name, line, "feature can be defined only in module or submodule");
615 final List<String> pathToFeature = new ArrayList<String>(parentPath);
616 pathToFeature.add(featureName.getLocalName());
618 final FeatureBuilder builder = new FeatureBuilder(featureName, line);
619 addedFeatures.put(pathToFeature, builder);
623 public ChoiceBuilder addChoice(final QName choiceName, final List<String> parentPath, final int line) {
624 final List<String> pathToChoice = new ArrayList<String>(parentPath);
625 final ChoiceBuilder builder = new ChoiceBuilder(choiceName, line);
627 if (!(actualPath.isEmpty())) {
628 final Builder parent = actualPath.getFirst();
629 if (parent instanceof DataNodeContainerBuilder) {
630 if (parent instanceof AugmentationSchemaBuilder) {
631 builder.setAugmenting(true);
633 ((DataNodeContainerBuilder) parent).addChildNode(builder);
635 throw new YangParseException(name, line, "Unresolved parent of choice " + choiceName.getLocalName());
639 pathToChoice.add(choiceName.getLocalName());
640 childNodes.put(pathToChoice, builder);
645 public ChoiceCaseBuilder addCase(final QName caseName, final List<String> parentPath, final int line) {
646 Builder parent = getActualNode();
648 final List<String> pathToCase = new ArrayList<String>(parentPath);
649 ChoiceCaseBuilder builder = null;
650 if (parent instanceof ChoiceBuilder) {
651 builder = new ChoiceCaseBuilder((ChoiceBuilder) parent, caseName, line);
653 builder = new ChoiceCaseBuilder(null, caseName, line);
656 if (actualPath.isEmpty()) {
657 throw new YangParseException(name, line, "'case' parent not found");
659 if (parent instanceof ChoiceBuilder) {
660 ((ChoiceBuilder) parent).addChildNode(builder);
661 } else if (parent instanceof AugmentationSchemaBuilder) {
662 builder.setAugmenting(true);
663 ((AugmentationSchemaBuilder) parent).addChildNode(builder);
665 throw new YangParseException(name, line, "Unresolved parent of 'case' " + caseName.getLocalName());
669 pathToCase.add(caseName.getLocalName());
670 childNodes.put(pathToCase, builder);
675 public AnyXmlBuilder addAnyXml(final SchemaPath schemaPath, final QName anyXmlName, final List<String> parentPath, final int line) {
676 final List<String> pathToAnyXml = new ArrayList<String>(parentPath);
677 final AnyXmlBuilder builder = new AnyXmlBuilder(anyXmlName, schemaPath, line);
678 updateParent(builder, line, "anyxml");
680 pathToAnyXml.add(anyXmlName.getLocalName());
681 childNodes.put(pathToAnyXml, builder);
687 public void addTypedef(TypeDefinitionBuilder typedefBuilder) {
688 final List<String> pathToTypedef = new ArrayList<String>();
689 for (QName qname : typedefBuilder.getPath().getPath()) {
690 pathToTypedef.add(qname.getLocalName());
692 if (addedTypedefs.containsKey(pathToTypedef)) {
693 throw new YangParseException(this.name, typedefBuilder.getLine(), "Failed to add typedef "
694 + typedefBuilder.getQName().getLocalName() + ": typedef already exists in context.");
696 addedTypedefs.put(pathToTypedef, typedefBuilder);
699 public TypeDefinitionBuilderImpl addTypedef(final QName typeDefName, final List<String> parentPath, final int line) {
700 final List<String> pathToType = new ArrayList<String>(parentPath);
701 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(typeDefName, line);
703 if (!(actualPath.isEmpty())) {
704 final Builder parent = actualPath.getFirst();
705 if (parent instanceof TypeDefinitionAwareBuilder) {
706 ((TypeDefinitionAwareBuilder) parent).addTypedef(builder);
708 throw new YangParseException(name, line, "Unresolved parent of typedef " + typeDefName.getLocalName());
712 pathToType.add(typeDefName.getLocalName());
713 addedTypedefs.put(pathToType, builder);
717 public void setType(final TypeDefinition<?> type, final List<String> parentPath) {
718 if (!(actualPath.isEmpty())) {
719 final Builder parent = actualPath.getFirst();
720 if (parent instanceof TypeAwareBuilder) {
721 ((TypeAwareBuilder) parent).setType(type);
723 throw new YangParseException("Failed to set type '" + type.getQName().getLocalName()
724 + "'. Unknown parent node: " + parent);
729 public UnionTypeBuilder addUnionType(final List<String> currentPath, final URI namespace, final Date revision,
731 final List<String> pathToUnion = new ArrayList<String>(currentPath);
732 final UnionTypeBuilder union = new UnionTypeBuilder(line);
734 if (actualPath.isEmpty()) {
735 throw new YangParseException(line, "union error");
737 final Builder parent = actualPath.getFirst();
738 if (parent instanceof TypeAwareBuilder) {
740 ((TypeAwareBuilder) parent).setTypedef(union);
742 final List<String> path = new ArrayList<String>(pathToUnion);
745 addedUnionTypes.put(path, union);
748 throw new YangParseException(name, line, "Unresolved parent of union type.");
753 public void addIdentityrefType(final String baseString, final List<String> parentPath, final SchemaPath schemaPath,
755 final List<String> pathToIdentityref = new ArrayList<String>(parentPath);
756 final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(baseString, schemaPath, line);
758 if (actualPath.isEmpty()) {
759 throw new YangParseException(line, "identityref error");
761 final Builder parent = actualPath.getFirst();
762 if (parent instanceof TypeAwareBuilder) {
763 final TypeAwareBuilder typeParent = (TypeAwareBuilder) parent;
764 typeParent.setTypedef(identityref);
765 dirtyNodes.put(pathToIdentityref, typeParent);
767 throw new YangParseException(name, line, "Unresolved parent of identityref type.");
772 public DeviationBuilder addDeviation(final String targetPath, final List<String> parentPath, final int line) {
773 if (!(actualPath.isEmpty())) {
774 throw new YangParseException(name, line, "deviation can be defined only in module or submodule");
777 final List<String> pathToDeviation = new ArrayList<String>(parentPath);
778 pathToDeviation.add(targetPath);
779 final DeviationBuilder builder = new DeviationBuilder(targetPath, line);
780 addedDeviations.put(pathToDeviation, builder);
784 public IdentitySchemaNodeBuilder addIdentity(final QName qname, final List<String> parentPath, final int line) {
785 if (!(actualPath.isEmpty())) {
786 throw new YangParseException(name, line, "identity can be defined only in module or submodule");
789 final List<String> pathToIdentity = new ArrayList<String>(parentPath);
790 final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(qname, line);
791 pathToIdentity.add(qname.getLocalName());
792 addedIdentities.add(builder);
797 public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownNode) {
798 final List<String> unPath = new ArrayList<String>();
799 for (QName qname : unknownNode.getPath().getPath()) {
800 unPath.add(qname.getLocalName());
803 if (addedUnknownNodes.containsKey(unPath)) {
804 addedUnknownNodes.get(unPath).add(unknownNode);
806 List<UnknownSchemaNodeBuilder> nodes = new ArrayList<UnknownSchemaNodeBuilder>();
807 nodes.add(unknownNode);
808 addedUnknownNodes.put(unPath, nodes);
812 public UnknownSchemaNodeBuilder addUnknownSchemaNode(final QName qname, final List<String> parentPath,
814 final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(qname, line);
816 if (!(actualPath.isEmpty())) {
817 final Builder parent = actualPath.getFirst();
818 if (parent instanceof SchemaNodeBuilder) {
819 ((SchemaNodeBuilder) parent).addUnknownSchemaNode(builder);
820 } else if (parent instanceof RefineHolder) {
821 ((RefineHolder) parent).addUnknownSchemaNode(builder);
823 throw new YangParseException(name, line, "Unresolved parent of unknown node '" + qname.getLocalName()
827 final List<String> unPath = new ArrayList<String>(parentPath);
828 unPath.add(qname.getLocalName());
830 if (addedUnknownNodes.containsKey(unPath)) {
831 addedUnknownNodes.get(unPath).add(builder);
833 List<UnknownSchemaNodeBuilder> nodes = new ArrayList<UnknownSchemaNodeBuilder>();
835 addedUnknownNodes.put(unPath, nodes);
841 public String toString() {
842 return ModuleBuilder.class.getSimpleName() + "[" + name + "]";
845 private final class ModuleImpl implements Module {
846 private URI namespace;
847 private final String name;
848 private Date revision;
849 private String prefix;
850 private String yangVersion;
851 private String description;
852 private String reference;
853 private String organization;
854 private String contact;
855 private Set<ModuleImport> imports = Collections.emptySet();
856 private Set<FeatureDefinition> features = Collections.emptySet();
857 private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
858 private Set<NotificationDefinition> notifications = Collections.emptySet();
859 private Set<AugmentationSchema> augmentations = Collections.emptySet();
860 private Set<RpcDefinition> rpcs = Collections.emptySet();
861 private Set<Deviation> deviations = Collections.emptySet();
862 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
863 private Set<GroupingDefinition> groupings = Collections.emptySet();
864 private Set<UsesNode> uses = Collections.emptySet();
865 private List<ExtensionDefinition> extensionNodes = Collections.emptyList();
866 private Set<IdentitySchemaNode> identities = Collections.emptySet();
867 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
869 private ModuleImpl(String name) {
874 public URI getNamespace() {
878 private void setNamespace(URI namespace) {
879 this.namespace = namespace;
883 public String getName() {
888 public Date getRevision() {
892 private void setRevision(Date revision) {
893 this.revision = revision;
897 public String getPrefix() {
901 private void setPrefix(String prefix) {
902 this.prefix = prefix;
906 public String getYangVersion() {
910 private void setYangVersion(String yangVersion) {
911 this.yangVersion = yangVersion;
915 public String getDescription() {
919 private void setDescription(String description) {
920 this.description = description;
924 public String getReference() {
928 private void setReference(String reference) {
929 this.reference = reference;
933 public String getOrganization() {
937 private void setOrganization(String organization) {
938 this.organization = organization;
942 public String getContact() {
946 private void setContact(String contact) {
947 this.contact = contact;
951 public Set<ModuleImport> getImports() {
955 private void setImports(Set<ModuleImport> imports) {
956 if (imports != null) {
957 this.imports = imports;
962 public Set<FeatureDefinition> getFeatures() {
966 private void setFeatures(Set<FeatureDefinition> features) {
967 if (features != null) {
968 this.features = features;
973 public Set<TypeDefinition<?>> getTypeDefinitions() {
974 return typeDefinitions;
977 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
978 if (typeDefinitions != null) {
979 this.typeDefinitions = typeDefinitions;
984 public Set<NotificationDefinition> getNotifications() {
985 return notifications;
988 private void setNotifications(Set<NotificationDefinition> notifications) {
989 if (notifications != null) {
990 this.notifications = notifications;
995 public Set<AugmentationSchema> getAugmentations() {
996 return augmentations;
999 private void setAugmentations(Set<AugmentationSchema> augmentations) {
1000 if (augmentations != null) {
1001 this.augmentations = augmentations;
1006 public Set<RpcDefinition> getRpcs() {
1010 private void setRpcs(Set<RpcDefinition> rpcs) {
1017 public Set<Deviation> getDeviations() {
1021 private void setDeviations(Set<Deviation> deviations) {
1022 if (deviations != null) {
1023 this.deviations = deviations;
1028 public Set<DataSchemaNode> getChildNodes() {
1029 return new HashSet<DataSchemaNode>(childNodes.values());
1032 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
1033 if (childNodes != null) {
1034 this.childNodes = childNodes;
1039 public Set<GroupingDefinition> getGroupings() {
1043 private void setGroupings(Set<GroupingDefinition> groupings) {
1044 if (groupings != null) {
1045 this.groupings = groupings;
1050 public Set<UsesNode> getUses() {
1054 private void setUses(Set<UsesNode> uses) {
1061 public List<ExtensionDefinition> getExtensionSchemaNodes() {
1062 return extensionNodes;
1065 private void setExtensionSchemaNodes(final List<ExtensionDefinition> extensionNodes) {
1066 if (extensionNodes != null) {
1067 this.extensionNodes = extensionNodes;
1072 public Set<IdentitySchemaNode> getIdentities() {
1076 private void setIdentities(final Set<IdentitySchemaNode> identities) {
1077 if (identities != null) {
1078 this.identities = identities;
1083 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
1084 return unknownNodes;
1087 private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
1088 if (unknownNodes != null) {
1089 this.unknownNodes = unknownNodes;
1094 public DataSchemaNode getDataChildByName(QName name) {
1095 return childNodes.get(name);
1099 public DataSchemaNode getDataChildByName(String name) {
1100 DataSchemaNode result = null;
1101 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
1102 if (entry.getKey().getLocalName().equals(name)) {
1103 result = entry.getValue();
1111 public int hashCode() {
1112 final int prime = 31;
1114 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
1115 result = prime * result + ((name == null) ? 0 : name.hashCode());
1116 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1117 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1118 result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());
1123 public boolean equals(Object obj) {
1130 if (getClass() != obj.getClass()) {
1133 ModuleImpl other = (ModuleImpl) obj;
1134 if (namespace == null) {
1135 if (other.namespace != null) {
1138 } else if (!namespace.equals(other.namespace)) {
1142 if (other.name != null) {
1145 } else if (!name.equals(other.name)) {
1148 if (revision == null) {
1149 if (other.revision != null) {
1152 } else if (!revision.equals(other.revision)) {
1155 if (prefix == null) {
1156 if (other.prefix != null) {
1159 } else if (!prefix.equals(other.prefix)) {
1162 if (yangVersion == null) {
1163 if (other.yangVersion != null) {
1166 } else if (!yangVersion.equals(other.yangVersion)) {
1173 public String toString() {
1174 StringBuilder sb = new StringBuilder(ModuleImpl.class.getSimpleName());
1176 sb.append("name=" + name);
1177 sb.append(", namespace=" + namespace);
1178 sb.append(", revision=" + revision);
1179 sb.append(", prefix=" + prefix);
1180 sb.append(", yangVersion=" + yangVersion);
1182 return sb.toString();
1186 private void updateParent(DataSchemaNodeBuilder nodeBuilder, int line, String nodeTypeName) {
1187 if (!(actualPath.isEmpty())) {
1188 final Builder parent = actualPath.getFirst();
1189 if (parent instanceof DataNodeContainerBuilder) {
1190 if (parent instanceof AugmentationSchemaBuilder) {
1191 nodeBuilder.setAugmenting(true);
1193 ((DataNodeContainerBuilder) parent).addChildNode(nodeBuilder);
1194 } else if (parent instanceof ChoiceBuilder) {
1195 ((ChoiceBuilder) parent).addChildNode(nodeBuilder);
1197 throw new YangParseException(name, line, "Unresolved parent of " + nodeTypeName + " "
1198 + nodeBuilder.getQName().getLocalName());
1203 private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) {
1204 final ModuleImport moduleImport = new ModuleImport() {
1206 public String getModuleName() {
1211 public Date getRevision() {
1216 public String getPrefix() {
1221 public int hashCode() {
1222 final int prime = 31;
1224 result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
1225 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1226 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1231 public boolean equals(Object obj) {
1238 if (getClass() != obj.getClass()) {
1241 ModuleImport other = (ModuleImport) obj;
1242 if (getModuleName() == null) {
1243 if (other.getModuleName() != null) {
1246 } else if (!getModuleName().equals(other.getModuleName())) {
1249 if (getRevision() == null) {
1250 if (other.getRevision() != null) {
1253 } else if (!getRevision().equals(other.getRevision())) {
1256 if (getPrefix() == null) {
1257 if (other.getPrefix() != null) {
1260 } else if (!getPrefix().equals(other.getPrefix())) {
1267 public String toString() {
1268 return "ModuleImport[moduleName=" + moduleName + ", revision=" + revision + ", prefix=" + prefix + "]";
1271 return moduleImport;
1275 * Traverse through given addedChilds and add only direct module childs.
1276 * Direct module child path size is 2 (1. module name, 2. child name).
1278 * @param addedChilds
1279 * @return map of children, where key is child QName and value is child
1282 private Map<QName, DataSchemaNode> buildModuleChildNodes(Map<List<String>, DataSchemaNodeBuilder> addedChilds) {
1283 final Map<QName, DataSchemaNode> childNodes = new HashMap<QName, DataSchemaNode>();
1284 for (Map.Entry<List<String>, DataSchemaNodeBuilder> entry : addedChilds.entrySet()) {
1285 List<String> path = entry.getKey();
1286 DataSchemaNodeBuilder child = entry.getValue();
1287 if (path.size() == 2) {
1288 DataSchemaNode node = child.build();
1289 QName qname = node.getQName();
1290 childNodes.put(qname, node);
1297 * Traverse through given addedGroupings and add only direct module
1298 * groupings. Direct module grouping path size is 2 (1. module name, 2.
1301 * @param addedGroupings
1302 * @return set of built GroupingDefinition objects
1304 private Set<GroupingDefinition> buildModuleGroupings(Map<List<String>, GroupingBuilder> addedGroupings) {
1305 final Set<GroupingDefinition> groupings = new HashSet<GroupingDefinition>();
1306 for (Map.Entry<List<String>, GroupingBuilder> entry : addedGroupings.entrySet()) {
1307 if (entry.getKey().size() == 2) {
1308 groupings.add(entry.getValue().build());
1315 * Traverse through given addedRpcs and build RpcDefinition objects.
1318 * @return set of built RpcDefinition objects
1320 private Set<RpcDefinition> buildModuleRpcs(Map<List<String>, RpcDefinitionBuilder> addedRpcs) {
1321 final Set<RpcDefinition> rpcs = new HashSet<RpcDefinition>();
1322 RpcDefinitionBuilder builder;
1323 for (Map.Entry<List<String>, RpcDefinitionBuilder> entry : addedRpcs.entrySet()) {
1324 builder = entry.getValue();
1325 RpcDefinition rpc = builder.build();
1332 * Traverse through given addedTypedefs and add only direct module typedef
1333 * statements. Direct module typedef path size is 2 (1. module name, 2.
1336 * @param addedTypedefs
1337 * @return set of built module typedef statements
1339 private Set<TypeDefinition<?>> buildModuleTypedefs(Map<List<String>, TypeDefinitionBuilder> addedTypedefs) {
1340 Set<TypeDefinition<?>> typedefs = new HashSet<TypeDefinition<?>>();
1341 for (Map.Entry<List<String>, TypeDefinitionBuilder> entry : addedTypedefs.entrySet()) {
1342 List<String> key = entry.getKey();
1343 TypeDefinitionBuilder typedefBuilder = entry.getValue();
1344 if (key.size() == 2) {
1345 TypeDefinition<? extends TypeDefinition<?>> node = typedefBuilder.build();
1353 * Traverse through given addedUsesNodes and add only direct module uses
1354 * nodes. Direct module uses node path size is 2 (1. module name, 2. uses
1357 * @param addedUsesNodes
1358 * @return set of built module uses nodes
1360 private Set<UsesNode> buildUsesNodes(Map<List<String>, UsesNodeBuilder> addedUsesNodes) {
1361 final Set<UsesNode> usesNodeDefs = new HashSet<UsesNode>();
1362 for (Map.Entry<List<String>, UsesNodeBuilder> entry : addedUsesNodes.entrySet()) {
1363 if (entry.getKey().size() == 2) {
1364 usesNodeDefs.add(entry.getValue().build());
1367 return usesNodeDefs;
1371 * Traverse through given addedFeatures and add only direct module features.
1372 * Direct module feature path size is 2 (1. module name, 2. feature name).
1374 * @param addedFeatures
1375 * @return set of built module features
1377 private Set<FeatureDefinition> buildModuleFeatures(Map<List<String>, FeatureBuilder> addedFeatures) {
1378 Set<FeatureDefinition> features = new HashSet<FeatureDefinition>();
1379 for (Map.Entry<List<String>, FeatureBuilder> entry : addedFeatures.entrySet()) {
1380 if (entry.getKey().size() == 2) {
1381 features.add(entry.getValue().build());
1387 private List<UnknownSchemaNode> buildModuleUnknownNodes(
1388 final Map<List<String>, List<UnknownSchemaNodeBuilder>> addedUnknownNodes) {
1389 final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
1390 for (Map.Entry<List<String>, List<UnknownSchemaNodeBuilder>> entry : addedUnknownNodes.entrySet()) {
1391 final List<String> path = entry.getKey();
1392 final List<UnknownSchemaNodeBuilder> child = entry.getValue();
1393 for (UnknownSchemaNodeBuilder un : child) {
1394 if (path.size() == 2) {
1395 final UnknownSchemaNode node = un.build();
1396 unknownNodes.add(node);
1400 return unknownNodes;