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 Builder {
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>, UnknownSchemaNodeBuilder> addedUnknownNodes = new HashMap<List<String>, 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() {
175 public void enterNode(final Builder node) {
176 actualPath.push(node);
179 public void exitNode() {
183 public Builder getActualNode() {
184 if (actualPath.isEmpty()) {
187 return actualPath.get(0);
191 public Builder getModuleNode(final List<String> path) {
192 return childNodes.get(path);
195 public GroupingBuilder getGrouping(final List<String> path) {
196 return addedGroupings.get(path);
199 public Builder getModuleTypedef(final List<String> path) {
200 return addedTypedefs.get(path);
203 public Set<DataSchemaNodeBuilder> getChildNodes() {
204 final Set<DataSchemaNodeBuilder> children = new HashSet<DataSchemaNodeBuilder>();
205 for (Map.Entry<List<String>, DataSchemaNodeBuilder> entry : childNodes.entrySet()) {
206 final List<String> path = entry.getKey();
207 final DataSchemaNodeBuilder child = entry.getValue();
208 if (path.size() == 2) {
215 public Map<List<String>, TypeAwareBuilder> getDirtyNodes() {
219 public List<AugmentationSchemaBuilder> getAugments() {
220 return addedAugments;
223 public Set<IdentitySchemaNodeBuilder> getIdentities() {
224 return addedIdentities;
227 public Map<List<String>, UsesNodeBuilder> getUsesNodes() {
228 return addedUsesNodes;
231 public Set<UnknownSchemaNodeBuilder> getUnknownNodes() {
232 return new HashSet<UnknownSchemaNodeBuilder>(addedUnknownNodes.values());
235 public Set<TypeDefinitionBuilder> getModuleTypedefs() {
236 final Set<TypeDefinitionBuilder> typedefs = new HashSet<TypeDefinitionBuilder>();
237 for (Map.Entry<List<String>, TypeDefinitionBuilder> entry : addedTypedefs.entrySet()) {
238 if (entry.getKey().size() == 2) {
239 typedefs.add(entry.getValue());
245 public Set<GroupingBuilder> getModuleGroupings() {
246 final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
247 for (Map.Entry<List<String>, GroupingBuilder> entry : addedGroupings.entrySet()) {
248 if (entry.getKey().size() == 2) {
249 groupings.add(entry.getValue());
255 public String getName() {
259 public URI getNamespace() {
263 public void setNamespace(final URI namespace) {
264 this.namespace = namespace;
267 public String getPrefix() {
271 public Date getRevision() {
275 public int getAugmentsResolved() {
276 return augmentsResolved;
279 public void augmentResolved() {
283 public void addDirtyNode(final List<String> path) {
284 final List<String> dirtyNodePath = new ArrayList<String>(path);
285 final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) actualPath.getFirst();
286 dirtyNodes.put(dirtyNodePath, nodeBuilder);
289 public void setRevision(final Date revision) {
290 this.revision = revision;
293 public void setPrefix(final String prefix) {
294 this.prefix = prefix;
297 public void setYangVersion(final String yangVersion) {
298 instance.setYangVersion(yangVersion);
301 public void setDescription(final String description) {
302 instance.setDescription(description);
305 public void setReference(final String reference) {
306 instance.setReference(reference);
309 public void setOrganization(final String organization) {
310 instance.setOrganization(organization);
313 public void setContact(final String contact) {
314 instance.setContact(contact);
317 public boolean addModuleImport(final String moduleName, final Date revision, final String prefix) {
318 final ModuleImport moduleImport = createModuleImport(moduleName, revision, prefix);
319 return imports.add(moduleImport);
322 public Set<ModuleImport> getModuleImports() {
326 public ExtensionBuilder addExtension(final QName qname, final int line) {
327 final ExtensionBuilder builder = new ExtensionBuilder(qname, line);
328 addedExtensions.add(builder);
332 public ContainerSchemaNodeBuilder addContainerNode(final QName containerName, final List<String> parentPath,
334 final List<String> pathToNode = new ArrayList<String>(parentPath);
335 final ContainerSchemaNodeBuilder containerBuilder = new ContainerSchemaNodeBuilder(containerName, line);
336 updateParent(containerBuilder, line, "container");
338 pathToNode.add(containerName.getLocalName());
339 childNodes.put(pathToNode, containerBuilder);
341 return containerBuilder;
344 public ListSchemaNodeBuilder addListNode(final QName listName, final List<String> parentPath, final int line) {
345 final List<String> pathToNode = new ArrayList<String>(parentPath);
346 final ListSchemaNodeBuilder listBuilder = new ListSchemaNodeBuilder(listName, line);
347 updateParent(listBuilder, line, "list");
349 pathToNode.add(listName.getLocalName());
350 childNodes.put(pathToNode, listBuilder);
355 public LeafSchemaNodeBuilder addLeafNode(final QName leafName, final List<String> parentPath, final int line) {
356 final List<String> pathToNode = new ArrayList<String>(parentPath);
357 final LeafSchemaNodeBuilder leafBuilder = new LeafSchemaNodeBuilder(leafName, line);
358 updateParent(leafBuilder, line, "leaf");
360 pathToNode.add(leafName.getLocalName());
361 childNodes.put(pathToNode, leafBuilder);
366 public LeafListSchemaNodeBuilder addLeafListNode(final QName qname, final List<String> parentPath, final int line) {
367 final List<String> pathToNode = new ArrayList<String>(parentPath);
368 final LeafListSchemaNodeBuilder leafListBuilder = new LeafListSchemaNodeBuilder(qname, line);
369 updateParent(leafListBuilder, line, "leaf-list");
371 pathToNode.add(qname.getLocalName());
372 childNodes.put(pathToNode, leafListBuilder);
374 return leafListBuilder;
377 public GroupingBuilder addGrouping(final QName qname, final List<String> parentPath, final int line) {
378 final List<String> pathToGroup = new ArrayList<String>(parentPath);
379 final GroupingBuilder builder = new GroupingBuilderImpl(qname, line);
381 if (!(actualPath.isEmpty())) {
382 final Builder parent = actualPath.getFirst();
383 if (parent instanceof DataNodeContainerBuilder) {
384 ((DataNodeContainerBuilder) parent).addGrouping(builder);
386 throw new YangParseException(name, line, "Unresolved parent of grouping " + qname.getLocalName());
390 pathToGroup.add(qname.getLocalName());
391 addedGroupings.put(pathToGroup, builder);
396 public AugmentationSchemaBuilder addAugment(final String name, final List<String> parentPath, final int line) {
397 final List<String> pathToAugment = new ArrayList<String>(parentPath);
398 final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line);
400 // augment can only be in 'module' or 'uses' statement
401 if (!(actualPath.isEmpty())) {
402 final Builder parent = actualPath.getFirst();
403 if (parent instanceof UsesNodeBuilder) {
404 ((UsesNodeBuilder) parent).addAugment(builder);
406 throw new YangParseException(this.name, line, "Unresolved parent of augment " + name);
410 pathToAugment.add(name);
411 addedAugments.add(builder);
416 public UsesNodeBuilder addUsesNode(final String groupingPathStr, final List<String> parentPath, final int line) {
417 final List<String> pathToUses = new ArrayList<String>(parentPath);
418 final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(groupingPathStr, line);
420 if (!(actualPath.isEmpty())) {
421 final Builder parent = actualPath.getFirst();
422 if (parent instanceof DataNodeContainerBuilder) {
423 if (parent instanceof AugmentationSchemaBuilder) {
424 usesBuilder.setAugmenting(true);
426 ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder);
428 throw new YangParseException(name, line, "Unresolved parent of uses " + groupingPathStr);
432 pathToUses.add(groupingPathStr);
433 addedUsesNodes.put(pathToUses, usesBuilder);
437 public void addRefine(final RefineHolder refine, final List<String> parentPath) {
438 final List<String> path = new ArrayList<String>(parentPath);
440 if (actualPath.isEmpty()) {
441 throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
443 final Builder parent = actualPath.getFirst();
444 if (parent instanceof UsesNodeBuilder) {
445 ((UsesNodeBuilder) parent).addRefine(refine);
447 throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
451 path.add(refine.getName());
454 public RpcDefinitionBuilder addRpc(final QName qname, final List<String> parentPath, final int line) {
456 if (!(actualPath.isEmpty())) {
457 throw new YangParseException(name, line, "rpc can be defined only in module or submodule");
460 final List<String> pathToRpc = new ArrayList<String>(parentPath);
461 final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(qname, line);
463 pathToRpc.add(qname.getLocalName());
464 addedRpcs.put(pathToRpc, rpcBuilder);
469 public ContainerSchemaNodeBuilder addRpcInput(final QName inputQName, final int line) {
470 final Builder parent = actualPath.getFirst();
471 if (!(parent instanceof RpcDefinitionBuilder)) {
472 throw new YangParseException(name, line, "input can be defined only in rpc statement");
474 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
476 final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(inputQName, line);
477 rpc.setInput(inputBuilder);
481 public ContainerSchemaNodeBuilder addRpcOutput(final QName outputQName, final int line) {
482 final Builder parent = actualPath.getFirst();
483 if (!(parent instanceof RpcDefinitionBuilder)) {
484 throw new YangParseException(name, line, "output can be defined only in rpc statement");
486 final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent;
488 final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(outputQName, line);
489 rpc.setOutput(outputBuilder);
490 return outputBuilder;
493 public NotificationBuilder addNotification(final QName notificationName, final List<String> parentPath,
495 if (!(actualPath.isEmpty())) {
496 throw new YangParseException(name, line, "notification can be defined only in module or submodule");
498 final NotificationBuilder builder = new NotificationBuilder(notificationName, line);
500 final List<String> notificationPath = new ArrayList<String>(parentPath);
501 notificationPath.add(notificationName.getLocalName());
502 addedNotifications.add(builder);
507 public FeatureBuilder addFeature(final QName featureName, final List<String> parentPath, final int line) {
508 if (!(actualPath.isEmpty())) {
509 throw new YangParseException(name, line, "feature can be defined only in module or submodule");
512 final List<String> pathToFeature = new ArrayList<String>(parentPath);
513 pathToFeature.add(featureName.getLocalName());
515 final FeatureBuilder builder = new FeatureBuilder(featureName, line);
516 addedFeatures.put(pathToFeature, builder);
520 public ChoiceBuilder addChoice(final QName choiceName, final List<String> parentPath, final int line) {
521 final List<String> pathToChoice = new ArrayList<String>(parentPath);
522 final ChoiceBuilder builder = new ChoiceBuilder(choiceName, line);
524 if (!(actualPath.isEmpty())) {
525 final Builder parent = actualPath.getFirst();
526 if (parent instanceof DataNodeContainerBuilder) {
527 if (parent instanceof AugmentationSchemaBuilder) {
528 builder.setAugmenting(true);
530 ((DataNodeContainerBuilder) parent).addChildNode(builder);
532 throw new YangParseException(name, line, "Unresolved parent of choice " + choiceName.getLocalName());
536 pathToChoice.add(choiceName.getLocalName());
537 childNodes.put(pathToChoice, builder);
542 public ChoiceCaseBuilder addCase(final QName caseName, final List<String> parentPath, final int line) {
543 final List<String> pathToCase = new ArrayList<String>(parentPath);
544 final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(caseName, line);
546 if (actualPath.isEmpty()) {
547 throw new YangParseException(name, line, "'case' parent not found");
549 final Builder parent = actualPath.getFirst();
550 if (parent instanceof ChoiceBuilder) {
551 ((ChoiceBuilder) parent).addChildNode(builder);
552 } else if (parent instanceof AugmentationSchemaBuilder) {
553 builder.setAugmenting(true);
554 ((AugmentationSchemaBuilder) parent).addChildNode(builder);
556 throw new YangParseException(name, line, "Unresolved parent of 'case' " + caseName.getLocalName());
560 pathToCase.add(caseName.getLocalName());
561 childNodes.put(pathToCase, builder);
566 public AnyXmlBuilder addAnyXml(final QName anyXmlName, final List<String> parentPath, final int line) {
567 final List<String> pathToAnyXml = new ArrayList<String>(parentPath);
568 final AnyXmlBuilder builder = new AnyXmlBuilder(anyXmlName, line);
569 updateParent(builder, line, "anyxml");
571 pathToAnyXml.add(anyXmlName.getLocalName());
572 childNodes.put(pathToAnyXml, builder);
577 public TypeDefinitionBuilderImpl addTypedef(final QName typeDefName, final List<String> parentPath, final int line) {
578 final List<String> pathToType = new ArrayList<String>(parentPath);
579 final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(typeDefName, line);
581 if (!(actualPath.isEmpty())) {
582 final Builder parent = actualPath.getFirst();
583 if (parent instanceof TypeDefinitionAwareBuilder) {
584 ((TypeDefinitionAwareBuilder) parent).addTypedef(builder);
586 throw new YangParseException(name, line, "Unresolved parent of typedef " + typeDefName.getLocalName());
590 pathToType.add(typeDefName.getLocalName());
591 addedTypedefs.put(pathToType, builder);
595 public void setType(final TypeDefinition<?> type, final List<String> parentPath) {
596 if (!(actualPath.isEmpty())) {
597 final Builder parent = actualPath.getFirst();
598 if (parent instanceof TypeAwareBuilder) {
599 ((TypeAwareBuilder) parent).setType(type);
601 throw new YangParseException("Failed to set type '" + type.getQName().getLocalName()
602 + "'. Unknown parent node: " + parent);
607 public UnionTypeBuilder addUnionType(final List<String> currentPath, final URI namespace, final Date revision,
609 final List<String> pathToUnion = new ArrayList<String>(currentPath);
610 final UnionTypeBuilder union = new UnionTypeBuilder(line);
612 if (actualPath.isEmpty()) {
613 throw new YangParseException(line, "union error");
615 final Builder parent = actualPath.getFirst();
616 if (parent instanceof TypeAwareBuilder) {
618 ((TypeAwareBuilder) parent).setTypedef(union);
620 final List<String> path = new ArrayList<String>(pathToUnion);
623 addedUnionTypes.put(path, union);
626 throw new YangParseException(name, line, "Unresolved parent of union type.");
631 public void addIdentityrefType(final String baseString, final List<String> parentPath, final SchemaPath schemaPath,
633 final List<String> pathToIdentityref = new ArrayList<String>(parentPath);
634 final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(baseString, schemaPath, line);
636 if (actualPath.isEmpty()) {
637 throw new YangParseException(line, "identityref error");
639 final Builder parent = actualPath.getFirst();
640 if (parent instanceof TypeAwareBuilder) {
641 final TypeAwareBuilder typeParent = (TypeAwareBuilder) parent;
642 typeParent.setTypedef(identityref);
643 dirtyNodes.put(pathToIdentityref, typeParent);
645 throw new YangParseException(name, line, "Unresolved parent of identityref type.");
650 public DeviationBuilder addDeviation(final String targetPath, final List<String> parentPath, final int line) {
651 if (!(actualPath.isEmpty())) {
652 throw new YangParseException(name, line, "deviation can be defined only in module or submodule");
655 final List<String> pathToDeviation = new ArrayList<String>(parentPath);
656 pathToDeviation.add(targetPath);
657 final DeviationBuilder builder = new DeviationBuilder(targetPath, line);
658 addedDeviations.put(pathToDeviation, builder);
662 public IdentitySchemaNodeBuilder addIdentity(final QName qname, final List<String> parentPath, final int line) {
663 if (!(actualPath.isEmpty())) {
664 throw new YangParseException(name, line, "identity can be defined only in module or submodule");
667 final List<String> pathToIdentity = new ArrayList<String>(parentPath);
668 final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(qname, line);
669 pathToIdentity.add(qname.getLocalName());
670 addedIdentities.add(builder);
674 public void addConfiguration(final boolean configuration, final List<String> parentPath, final int line) {
675 if (actualPath.isEmpty()) {
676 throw new YangParseException(name, line, "Parent node of config statement not found.");
678 final Builder parent = actualPath.getFirst();
679 if (parent instanceof DataSchemaNodeBuilder) {
680 ((DataSchemaNodeBuilder) parent).setConfiguration(configuration);
681 } else if (parent instanceof RefineHolder) {
682 ((RefineHolder) parent).setConfig(configuration);
683 } else if (parent instanceof DeviationBuilder) {
684 // skip: set config to deviation (deviate stmt) not supported by
688 throw new YangParseException(name, line, "Unresolved parent of config statement.");
693 public UnknownSchemaNodeBuilder addUnknownSchemaNode(final QName qname, final List<String> parentPath,
695 final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(qname, line);
697 if (!(actualPath.isEmpty())) {
698 final Builder parent = actualPath.getFirst();
699 if (parent instanceof SchemaNodeBuilder) {
700 ((SchemaNodeBuilder) parent).addUnknownSchemaNode(builder);
701 } else if (parent instanceof RefineHolder) {
702 ((RefineHolder) parent).addUnknownSchemaNode(builder);
704 throw new YangParseException(name, line, "Unresolved parent of unknown node '" + qname.getLocalName()
708 final List<String> unPath = new ArrayList<String>(parentPath);
709 unPath.add(qname.getLocalName());
710 addedUnknownNodes.put(unPath, builder);
715 public String toString() {
716 return ModuleBuilder.class.getSimpleName() + "[" + name + "]";
719 private final class ModuleImpl implements Module {
720 private URI namespace;
721 private final String name;
722 private Date revision;
723 private String prefix;
724 private String yangVersion;
725 private String description;
726 private String reference;
727 private String organization;
728 private String contact;
729 private Set<ModuleImport> imports = Collections.emptySet();
730 private Set<FeatureDefinition> features = Collections.emptySet();
731 private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
732 private Set<NotificationDefinition> notifications = Collections.emptySet();
733 private Set<AugmentationSchema> augmentations = Collections.emptySet();
734 private Set<RpcDefinition> rpcs = Collections.emptySet();
735 private Set<Deviation> deviations = Collections.emptySet();
736 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
737 private Set<GroupingDefinition> groupings = Collections.emptySet();
738 private Set<UsesNode> uses = Collections.emptySet();
739 private List<ExtensionDefinition> extensionNodes = Collections.emptyList();
740 private Set<IdentitySchemaNode> identities = Collections.emptySet();
741 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
743 private ModuleImpl(String name) {
748 public URI getNamespace() {
752 private void setNamespace(URI namespace) {
753 this.namespace = namespace;
757 public String getName() {
762 public Date getRevision() {
766 private void setRevision(Date revision) {
767 this.revision = revision;
771 public String getPrefix() {
775 private void setPrefix(String prefix) {
776 this.prefix = prefix;
780 public String getYangVersion() {
784 private void setYangVersion(String yangVersion) {
785 this.yangVersion = yangVersion;
789 public String getDescription() {
793 private void setDescription(String description) {
794 this.description = description;
798 public String getReference() {
802 private void setReference(String reference) {
803 this.reference = reference;
807 public String getOrganization() {
811 private void setOrganization(String organization) {
812 this.organization = organization;
816 public String getContact() {
820 private void setContact(String contact) {
821 this.contact = contact;
825 public Set<ModuleImport> getImports() {
829 private void setImports(Set<ModuleImport> imports) {
830 if (imports != null) {
831 this.imports = imports;
836 public Set<FeatureDefinition> getFeatures() {
840 private void setFeatures(Set<FeatureDefinition> features) {
841 if (features != null) {
842 this.features = features;
847 public Set<TypeDefinition<?>> getTypeDefinitions() {
848 return typeDefinitions;
851 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
852 if (typeDefinitions != null) {
853 this.typeDefinitions = typeDefinitions;
858 public Set<NotificationDefinition> getNotifications() {
859 return notifications;
862 private void setNotifications(Set<NotificationDefinition> notifications) {
863 if (notifications != null) {
864 this.notifications = notifications;
869 public Set<AugmentationSchema> getAugmentations() {
870 return augmentations;
873 private void setAugmentations(Set<AugmentationSchema> augmentations) {
874 if (augmentations != null) {
875 this.augmentations = augmentations;
880 public Set<RpcDefinition> getRpcs() {
884 private void setRpcs(Set<RpcDefinition> rpcs) {
891 public Set<Deviation> getDeviations() {
895 private void setDeviations(Set<Deviation> deviations) {
896 if (deviations != null) {
897 this.deviations = deviations;
902 public Set<DataSchemaNode> getChildNodes() {
903 return new HashSet<DataSchemaNode>(childNodes.values());
906 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
907 if (childNodes != null) {
908 this.childNodes = childNodes;
913 public Set<GroupingDefinition> getGroupings() {
917 private void setGroupings(Set<GroupingDefinition> groupings) {
918 if (groupings != null) {
919 this.groupings = groupings;
924 public Set<UsesNode> getUses() {
928 private void setUses(Set<UsesNode> uses) {
935 public List<ExtensionDefinition> getExtensionSchemaNodes() {
936 return extensionNodes;
939 private void setExtensionSchemaNodes(final List<ExtensionDefinition> extensionNodes) {
940 if (extensionNodes != null) {
941 this.extensionNodes = extensionNodes;
946 public Set<IdentitySchemaNode> getIdentities() {
950 private void setIdentities(final Set<IdentitySchemaNode> identities) {
951 if (identities != null) {
952 this.identities = identities;
957 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
961 private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
962 if (unknownNodes != null) {
963 this.unknownNodes = unknownNodes;
968 public DataSchemaNode getDataChildByName(QName name) {
969 return childNodes.get(name);
973 public DataSchemaNode getDataChildByName(String name) {
974 DataSchemaNode result = null;
975 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
976 if (entry.getKey().getLocalName().equals(name)) {
977 result = entry.getValue();
985 public int hashCode() {
986 final int prime = 31;
988 result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
989 result = prime * result + ((name == null) ? 0 : name.hashCode());
990 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
991 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
992 result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());
997 public boolean equals(Object obj) {
1004 if (getClass() != obj.getClass()) {
1007 ModuleImpl other = (ModuleImpl) obj;
1008 if (namespace == null) {
1009 if (other.namespace != null) {
1012 } else if (!namespace.equals(other.namespace)) {
1016 if (other.name != null) {
1019 } else if (!name.equals(other.name)) {
1022 if (revision == null) {
1023 if (other.revision != null) {
1026 } else if (!revision.equals(other.revision)) {
1029 if (prefix == null) {
1030 if (other.prefix != null) {
1033 } else if (!prefix.equals(other.prefix)) {
1036 if (yangVersion == null) {
1037 if (other.yangVersion != null) {
1040 } else if (!yangVersion.equals(other.yangVersion)) {
1047 public String toString() {
1048 StringBuilder sb = new StringBuilder(ModuleImpl.class.getSimpleName());
1050 sb.append("name=" + name);
1051 sb.append(", namespace=" + namespace);
1052 sb.append(", revision=" + revision);
1053 sb.append(", prefix=" + prefix);
1054 sb.append(", yangVersion=" + yangVersion);
1056 return sb.toString();
1060 private void updateParent(DataSchemaNodeBuilder nodeBuilder, int line, String nodeTypeName) {
1061 if (!(actualPath.isEmpty())) {
1062 final Builder parent = actualPath.getFirst();
1063 if (parent instanceof DataNodeContainerBuilder) {
1064 if (parent instanceof AugmentationSchemaBuilder) {
1065 nodeBuilder.setAugmenting(true);
1067 ((DataNodeContainerBuilder) parent).addChildNode(nodeBuilder);
1068 } else if (parent instanceof ChoiceBuilder) {
1069 ((ChoiceBuilder) parent).addChildNode(nodeBuilder);
1071 throw new YangParseException(name, line, "Unresolved parent of " + nodeTypeName + " "
1072 + nodeBuilder.getQName().getLocalName());
1077 private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) {
1078 final ModuleImport moduleImport = new ModuleImport() {
1080 public String getModuleName() {
1085 public Date getRevision() {
1090 public String getPrefix() {
1095 public int hashCode() {
1096 final int prime = 31;
1098 result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
1099 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
1100 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
1105 public boolean equals(Object obj) {
1112 if (getClass() != obj.getClass()) {
1115 ModuleImport other = (ModuleImport) obj;
1116 if (getModuleName() == null) {
1117 if (other.getModuleName() != null) {
1120 } else if (!getModuleName().equals(other.getModuleName())) {
1123 if (getRevision() == null) {
1124 if (other.getRevision() != null) {
1127 } else if (!getRevision().equals(other.getRevision())) {
1130 if (getPrefix() == null) {
1131 if (other.getPrefix() != null) {
1134 } else if (!getPrefix().equals(other.getPrefix())) {
1141 public String toString() {
1142 return "ModuleImport[moduleName=" + moduleName + ", revision=" + revision + ", prefix=" + prefix + "]";
1145 return moduleImport;
1149 * Traverse through given addedChilds and add only direct module childs.
1150 * Direct module child path size is 2 (1. module name, 2. child name).
1152 * @param addedChilds
1153 * @return map of children, where key is child QName and value is child
1156 private Map<QName, DataSchemaNode> buildModuleChildNodes(Map<List<String>, DataSchemaNodeBuilder> addedChilds) {
1157 final Map<QName, DataSchemaNode> childNodes = new HashMap<QName, DataSchemaNode>();
1158 for (Map.Entry<List<String>, DataSchemaNodeBuilder> entry : addedChilds.entrySet()) {
1159 List<String> path = entry.getKey();
1160 DataSchemaNodeBuilder child = entry.getValue();
1161 if (path.size() == 2) {
1162 DataSchemaNode node = child.build();
1163 QName qname = node.getQName();
1164 childNodes.put(qname, node);
1171 * Traverse through given addedGroupings and add only direct module
1172 * groupings. Direct module grouping path size is 2 (1. module name, 2.
1175 * @param addedGroupings
1176 * @return set of built GroupingDefinition objects
1178 private Set<GroupingDefinition> buildModuleGroupings(Map<List<String>, GroupingBuilder> addedGroupings) {
1179 final Set<GroupingDefinition> groupings = new HashSet<GroupingDefinition>();
1180 for (Map.Entry<List<String>, GroupingBuilder> entry : addedGroupings.entrySet()) {
1181 if (entry.getKey().size() == 2) {
1182 groupings.add(entry.getValue().build());
1189 * Traverse through given addedRpcs and build RpcDefinition objects.
1192 * @return set of built RpcDefinition objects
1194 private Set<RpcDefinition> buildModuleRpcs(Map<List<String>, RpcDefinitionBuilder> addedRpcs) {
1195 final Set<RpcDefinition> rpcs = new HashSet<RpcDefinition>();
1196 RpcDefinitionBuilder builder;
1197 for (Map.Entry<List<String>, RpcDefinitionBuilder> entry : addedRpcs.entrySet()) {
1198 builder = entry.getValue();
1199 RpcDefinition rpc = builder.build();
1206 * Traverse through given addedTypedefs and add only direct module typedef
1207 * statements. Direct module typedef path size is 2 (1. module name, 2.
1210 * @param addedTypedefs
1211 * @return set of built module typedef statements
1213 private Set<TypeDefinition<?>> buildModuleTypedefs(Map<List<String>, TypeDefinitionBuilder> addedTypedefs) {
1214 Set<TypeDefinition<?>> typedefs = new HashSet<TypeDefinition<?>>();
1215 for (Map.Entry<List<String>, TypeDefinitionBuilder> entry : addedTypedefs.entrySet()) {
1216 List<String> key = entry.getKey();
1217 TypeDefinitionBuilder typedefBuilder = entry.getValue();
1218 if (key.size() == 2) {
1219 TypeDefinition<? extends TypeDefinition<?>> node = typedefBuilder.build();
1227 * Traverse through given addedUsesNodes and add only direct module uses
1228 * nodes. Direct module uses node path size is 2 (1. module name, 2. uses
1231 * @param addedUsesNodes
1232 * @return set of built module uses nodes
1234 private Set<UsesNode> buildUsesNodes(Map<List<String>, UsesNodeBuilder> addedUsesNodes) {
1235 final Set<UsesNode> usesNodeDefs = new HashSet<UsesNode>();
1236 for (Map.Entry<List<String>, UsesNodeBuilder> entry : addedUsesNodes.entrySet()) {
1237 if (entry.getKey().size() == 2) {
1238 usesNodeDefs.add(entry.getValue().build());
1241 return usesNodeDefs;
1245 * Traverse through given addedFeatures and add only direct module features.
1246 * Direct module feature path size is 2 (1. module name, 2. feature name).
1248 * @param addedFeatures
1249 * @return set of built module features
1251 private Set<FeatureDefinition> buildModuleFeatures(Map<List<String>, FeatureBuilder> addedFeatures) {
1252 Set<FeatureDefinition> features = new HashSet<FeatureDefinition>();
1253 for (Map.Entry<List<String>, FeatureBuilder> entry : addedFeatures.entrySet()) {
1254 if (entry.getKey().size() == 2) {
1255 features.add(entry.getValue().build());
1261 private List<UnknownSchemaNode> buildModuleUnknownNodes(
1262 final Map<List<String>, UnknownSchemaNodeBuilder> addedUnknownNodes) {
1263 final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
1264 for (Map.Entry<List<String>, UnknownSchemaNodeBuilder> entry : addedUnknownNodes.entrySet()) {
1265 final List<String> path = entry.getKey();
1266 final UnknownSchemaNodeBuilder child = entry.getValue();
1267 if (path.size() == 2) {
1268 final UnknownSchemaNode node = child.build();
1269 unknownNodes.add(node);
1272 return unknownNodes;