/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.yang.parser.builder.impl; import java.net.URI; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.model.api.AugmentationSchema; import org.opendaylight.controller.yang.model.api.DataSchemaNode; import org.opendaylight.controller.yang.model.api.Deviation; import org.opendaylight.controller.yang.model.api.ExtensionDefinition; import org.opendaylight.controller.yang.model.api.FeatureDefinition; import org.opendaylight.controller.yang.model.api.GroupingDefinition; import org.opendaylight.controller.yang.model.api.IdentitySchemaNode; import org.opendaylight.controller.yang.model.api.Module; import org.opendaylight.controller.yang.model.api.ModuleImport; import org.opendaylight.controller.yang.model.api.NotificationDefinition; import org.opendaylight.controller.yang.model.api.RpcDefinition; import org.opendaylight.controller.yang.model.api.SchemaPath; import org.opendaylight.controller.yang.model.api.TypeDefinition; import org.opendaylight.controller.yang.model.api.UnknownSchemaNode; import org.opendaylight.controller.yang.model.api.UsesNode; import org.opendaylight.controller.yang.parser.builder.api.AbstractDataNodeContainerBuilder; import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder; import org.opendaylight.controller.yang.parser.builder.api.Builder; import org.opendaylight.controller.yang.parser.builder.api.DataNodeContainerBuilder; import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder; import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder; import org.opendaylight.controller.yang.parser.builder.api.SchemaNodeBuilder; import org.opendaylight.controller.yang.parser.builder.api.TypeAwareBuilder; import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder; import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder; import org.opendaylight.controller.yang.parser.util.Comparators; import org.opendaylight.controller.yang.parser.util.RefineHolder; import org.opendaylight.controller.yang.parser.util.YangParseException; /** * Builder of Module object. If this module is dependent on external * module/modules, these dependencies must be resolved before module is built, * otherwise result may not be valid. */ public class ModuleBuilder extends AbstractDataNodeContainerBuilder { private final ModuleImpl instance; private final String name; private URI namespace; private String prefix; private Date revision; private int augmentsResolved; private final LinkedList actualPath = new LinkedList(); private final Set dirtyNodes = new HashSet(); private final Set imports = new HashSet(); private final List addedAugments = new ArrayList(); private final List allAugments = new ArrayList(); private final Set addedUsesNodes = new HashSet(); private final List allUsesNodes = new ArrayList(); private final Set addedRpcs = new HashSet(); private final Set addedNotifications = new HashSet(); private final Set addedIdentities = new HashSet(); private final Set addedFeatures = new HashSet(); private final Set addedDeviations = new HashSet(); private final Set addedTypedefs = new HashSet(); private final List addedExtensions = new ArrayList(); private final List addedUnknownNodes = new ArrayList(); private final List allUnknownNodes = new ArrayList(); public ModuleBuilder(final String name) { super(0, null); this.name = name; instance = new ModuleImpl(name); } /** * Build new Module object based on this builder. */ @Override public Module build() { instance.setPrefix(prefix); instance.setRevision(revision); instance.setImports(imports); instance.setNamespace(namespace); // TYPEDEFS final Set> typedefs = new TreeSet>(Comparators.SCHEMA_NODE_COMP); for (TypeDefinitionBuilder tdb : addedTypedefs) { typedefs.add(tdb.build()); } instance.setTypeDefinitions(typedefs); // CHILD NODES final Map children = new TreeMap(Comparators.QNAME_COMP); for (DataSchemaNodeBuilder child : addedChildNodes) { children.put(child.getQName(), child.build()); } instance.setChildNodes(children); // GROUPINGS final Set groupings = new TreeSet(Comparators.SCHEMA_NODE_COMP); for (GroupingBuilder gb : addedGroupings) { groupings.add(gb.build()); } instance.setGroupings(groupings); // USES final Set usesDefinitions = new HashSet(); for (UsesNodeBuilder unb : addedUsesNodes) { usesDefinitions.add(unb.build()); } instance.setUses(usesDefinitions); // FEATURES final Set features = new TreeSet(Comparators.SCHEMA_NODE_COMP); for (FeatureBuilder fb : addedFeatures) { features.add(fb.build()); } instance.setFeatures(features); // NOTIFICATIONS final Set notifications = new TreeSet( Comparators.SCHEMA_NODE_COMP); for (NotificationBuilder entry : addedNotifications) { notifications.add(entry.build()); } instance.setNotifications(notifications); // AUGMENTATIONS final Set augmentations = new HashSet(); for (AugmentationSchemaBuilder builder : addedAugments) { augmentations.add(builder.build()); } instance.setAugmentations(augmentations); // RPCs final Set rpcs = new TreeSet(Comparators.SCHEMA_NODE_COMP); for (RpcDefinitionBuilder rpc : addedRpcs) { rpcs.add(rpc.build()); } instance.setRpcs(rpcs); // DEVIATIONS final Set deviations = new HashSet(); for (DeviationBuilder entry : addedDeviations) { deviations.add(entry.build()); } instance.setDeviations(deviations); // EXTENSIONS final List extensions = new ArrayList(); for (ExtensionBuilder eb : addedExtensions) { extensions.add(eb.build()); } Collections.sort(extensions, Comparators.SCHEMA_NODE_COMP); instance.setExtensionSchemaNodes(extensions); // IDENTITIES final Set identities = new TreeSet(Comparators.SCHEMA_NODE_COMP); for (IdentitySchemaNodeBuilder id : addedIdentities) { identities.add(id.build()); } instance.setIdentities(identities); // UNKNOWN NODES final List unknownNodes = new ArrayList(); for (UnknownSchemaNodeBuilder unb : addedUnknownNodes) { unknownNodes.add(unb.build()); } instance.setUnknownSchemaNodes(unknownNodes); return instance; } @Override public void setParent(Builder parent) { throw new YangParseException(name, 0, "Can not set parent to module"); } @Override public SchemaPath getPath() { return null; } @Override public Set getTypeDefinitionBuilders() { return addedTypedefs; } public void enterNode(final Builder node) { actualPath.push(node); } public void exitNode() { actualPath.pop(); } public Builder getActualNode() { if (actualPath.isEmpty()) { return null; } else { return actualPath.get(0); } } public Builder getActualParent() { if (actualPath.size() < 2) { return null; } else { return actualPath.get(1); } } public Set getDirtyNodes() { return dirtyNodes; } public List getAllAugments() { return allAugments; } public Set getIdentities() { return addedIdentities; } public List getAllUsesNodes() { return allUsesNodes; } public Set getDeviations() { return addedDeviations; } public List getAllUnknownNodes() { return allUnknownNodes; } public String getName() { return name; } public URI getNamespace() { return namespace; } public void setNamespace(final URI namespace) { this.namespace = namespace; } public String getPrefix() { return prefix; } public Date getRevision() { return revision; } public int getAugmentsResolved() { return augmentsResolved; } public void augmentResolved() { augmentsResolved++; } public void markActualNodeDirty() { final TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) getActualNode(); dirtyNodes.add(nodeBuilder); } public void setRevision(final Date revision) { this.revision = revision; } public void setPrefix(final String prefix) { this.prefix = prefix; } public void setYangVersion(final String yangVersion) { instance.setYangVersion(yangVersion); } public void setDescription(final String description) { instance.setDescription(description); } public void setReference(final String reference) { instance.setReference(reference); } public void setOrganization(final String organization) { instance.setOrganization(organization); } public void setContact(final String contact) { instance.setContact(contact); } public boolean addModuleImport(final String moduleName, final Date revision, final String prefix) { final ModuleImport moduleImport = createModuleImport(moduleName, revision, prefix); return imports.add(moduleImport); } public Set getModuleImports() { return imports; } public ExtensionBuilder addExtension(final QName qname, final int line) { final ExtensionBuilder builder = new ExtensionBuilder(line, qname); addedExtensions.add(builder); return builder; } public ContainerSchemaNodeBuilder addContainerNode(final int line, final QName containerName, final SchemaPath schemaPath) { final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(line, containerName, schemaPath); Builder parent = getActualNode(); builder.setParent(parent); addChildToParent(parent, builder, containerName.getLocalName()); return builder; } public ListSchemaNodeBuilder addListNode(final int line, final QName listName, final SchemaPath schemaPath) { final ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(line, listName, schemaPath); Builder parent = getActualNode(); builder.setParent(parent); addChildToParent(parent, builder, listName.getLocalName()); return builder; } public LeafSchemaNodeBuilder addLeafNode(final int line, final QName leafName, final SchemaPath schemaPath) { final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(leafName, schemaPath, line); Builder parent = getActualNode(); builder.setParent(parent); addChildToParent(parent, builder, leafName.getLocalName()); return builder; } public LeafListSchemaNodeBuilder addLeafListNode(final int line, final QName leafListName, final SchemaPath schemaPath) { final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(line, leafListName, schemaPath); Builder parent = getActualNode(); builder.setParent(parent); addChildToParent(parent, builder, leafListName.getLocalName()); return builder; } public GroupingBuilder addGrouping(final int line, final QName qname) { final GroupingBuilder builder = new GroupingBuilderImpl(qname, line); Builder parent = getActualNode(); builder.setParent(parent); if (parent == null) { for (GroupingBuilder child : addedGroupings) { if (child.getQName().getLocalName().equals(qname.getLocalName())) { throw new YangParseException(name, line, "Duplicate node found at line " + child.getLine()); } } addedGroupings.add(builder); } else { if (parent instanceof DataNodeContainerBuilder) { DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent; for (DataSchemaNodeBuilder child : parentNode.getChildNodeBuilders()) { if (child.getQName().getLocalName().equals(qname.getLocalName())) { throw new YangParseException(name, line, "Duplicate node found at line " + child.getLine()); } } parentNode.addGrouping(builder); } else if (parent instanceof RpcDefinitionBuilder) { RpcDefinitionBuilder parentNode = (RpcDefinitionBuilder) parent; for (GroupingBuilder child : parentNode.getGroupings()) { if (child.getQName().getLocalName().equals(qname.getLocalName())) { throw new YangParseException(name, line, "Duplicate node found at line " + child.getLine()); } } parentNode.addGrouping(builder); } else { throw new YangParseException(name, line, "Unresolved parent of grouping " + qname.getLocalName()); } } return builder; } public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr) { final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(line, augmentTargetStr); Builder parent = getActualNode(); builder.setParent(parent); if (parent == null) { addedAugments.add(builder); } else { // augment can only be in 'module' or 'uses' statement if (parent instanceof UsesNodeBuilder) { ((UsesNodeBuilder) parent).addAugment(builder); } else { throw new YangParseException(name, line, "Augment can be declared only under module or uses."); } } allAugments.add(builder); return builder; } @Override public void addUsesNode(UsesNodeBuilder usesBuilder) { addedUsesNodes.add(usesBuilder); allUsesNodes.add(usesBuilder); } public UsesNodeBuilder addUsesNode(final int line, final String groupingPathStr) { final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(line, groupingPathStr); Builder parent = getActualNode(); usesBuilder.setParent(parent); if (parent == null) { addedUsesNodes.add(usesBuilder); } else { if (!(parent instanceof DataNodeContainerBuilder)) { throw new YangParseException(name, line, "Unresolved parent of uses '" + groupingPathStr + "'."); } if (parent instanceof AugmentationSchemaBuilder) { usesBuilder.setAugmenting(true); } ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder); } allUsesNodes.add(usesBuilder); return usesBuilder; } public void addRefine(final RefineHolder refine, final List parentPath) { final List path = new ArrayList(parentPath); if (actualPath.isEmpty()) { throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement"); } else { final Builder parent = getActualNode(); if (parent instanceof UsesNodeBuilder) { ((UsesNodeBuilder) parent).addRefine(refine); } else { throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement"); } refine.setParent(parent); } path.add(refine.getName()); } public RpcDefinitionBuilder addRpc(final int line, final QName qname) { Builder parent = getActualNode(); if (parent != null) { throw new YangParseException(name, line, "rpc can be defined only in module or submodule"); } final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(line, qname); for (RpcDefinitionBuilder rpc : addedRpcs) { if (rpc.getQName().getLocalName().equals(qname.getLocalName())) { throw new YangParseException(name, line, "Duplicate node found at line " + rpc.getLine()); } } addedRpcs.add(rpcBuilder); return rpcBuilder; } public ContainerSchemaNodeBuilder addRpcInput(final SchemaPath schemaPath, final QName inputQName, final int line) { final Builder parent = getActualNode(); if (!(parent instanceof RpcDefinitionBuilder)) { throw new YangParseException(name, line, "input can be defined only in rpc statement"); } final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent; final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(line, inputQName, schemaPath); inputBuilder.setParent(rpc); rpc.setInput(inputBuilder); return inputBuilder; } public ContainerSchemaNodeBuilder addRpcOutput(final SchemaPath schemaPath, final QName outputQName, final int line) { final Builder parent = actualPath.getFirst(); if (!(parent instanceof RpcDefinitionBuilder)) { throw new YangParseException(name, line, "output can be defined only in rpc statement"); } final RpcDefinitionBuilder rpc = (RpcDefinitionBuilder) parent; final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(line, outputQName, schemaPath); outputBuilder.setParent(rpc); rpc.setOutput(outputBuilder); return outputBuilder; } public NotificationBuilder addNotification(final QName notificationName, final List parentPath, final int line) { if (!(actualPath.isEmpty())) { throw new YangParseException(name, line, "notification can be defined only in module or submodule"); } for (NotificationBuilder nb : addedNotifications) { if (nb.getQName().equals(notificationName)) { throw new YangParseException(name, line, "Duplicate node found at line " + nb.getLine()); } } final NotificationBuilder builder = new NotificationBuilder(line, notificationName); addedNotifications.add(builder); return builder; } public FeatureBuilder addFeature(final int line, final QName featureName) { Builder parent = getActualNode(); if (parent != null) { throw new YangParseException(name, line, "feature can be defined only in module or submodule"); } final FeatureBuilder builder = new FeatureBuilder(line, featureName); for (FeatureBuilder fb : addedFeatures) { if (fb.getQName().getLocalName().equals(featureName.getLocalName())) { throw new YangParseException(name, line, "Duplicate node found at line " + fb.getLine()); } } addedFeatures.add(builder); return builder; } public ChoiceBuilder addChoice(final int line, final QName choiceName) { final ChoiceBuilder builder = new ChoiceBuilder(line, choiceName); Builder parent = getActualNode(); builder.setParent(parent); addChildToParent(parent, builder, choiceName.getLocalName()); return builder; } public ChoiceCaseBuilder addCase(final int line, final QName caseName) { Builder parent = getActualNode(); if (parent == null) { throw new YangParseException(name, line, "'case' parent not found"); } final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(line, caseName); builder.setParent(parent); if (parent instanceof ChoiceBuilder) { ((ChoiceBuilder) parent).addChildNode(builder); } else if (parent instanceof AugmentationSchemaBuilder) { ((AugmentationSchemaBuilder) parent).addChildNode(builder); } else { throw new YangParseException(name, line, "Unresolved parent of 'case' " + caseName.getLocalName()); } return builder; } public AnyXmlBuilder addAnyXml(final int line, final QName anyXmlName, final SchemaPath schemaPath) { final AnyXmlBuilder builder = new AnyXmlBuilder(line, anyXmlName, schemaPath); Builder parent = getActualNode(); builder.setParent(parent); addChildToParent(parent, builder, anyXmlName.getLocalName()); return builder; } @Override public void addTypedef(TypeDefinitionBuilder typedefBuilder) { for (TypeDefinitionBuilder tdb : addedTypedefs) { if (tdb.getQName().getLocalName().equals(typedefBuilder.getQName().getLocalName())) { throw new YangParseException(name, typedefBuilder.getLine(), "Duplicate node found at line " + tdb.getLine()); } } addedTypedefs.add(typedefBuilder); } public TypeDefinitionBuilderImpl addTypedef(final int line, final QName typeDefName) { final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(typeDefName, line); Builder parent = getActualNode(); builder.setParent(parent); if (parent == null) { for (TypeDefinitionBuilder tdb : addedTypedefs) { if (tdb.getQName().getLocalName().equals(builder.getQName().getLocalName())) { throw new YangParseException(name, builder.getLine(), "Duplicate node found at line " + tdb.getLine()); } } addedTypedefs.add(builder); } else { if (parent instanceof DataNodeContainerBuilder) { DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent; for (DataSchemaNodeBuilder child : parentNode.getChildNodeBuilders()) { if (child.getQName().getLocalName().equals(typeDefName.getLocalName())) { throw new YangParseException(name, line, "Duplicate node found at line " + child.getLine()); } } parentNode.addTypedef(builder); } else if (parent instanceof RpcDefinitionBuilder) { RpcDefinitionBuilder rpcParent = (RpcDefinitionBuilder) parent; for (TypeDefinitionBuilder tdb : rpcParent.getTypeDefinitions()) { if (tdb.getQName().getLocalName().equals(builder.getQName().getLocalName())) { throw new YangParseException(name, builder.getLine(), "Duplicate node found at line " + tdb.getLine()); } } rpcParent.addTypedef(builder); } else { throw new YangParseException(name, line, "Unresolved parent of typedef " + typeDefName.getLocalName()); } } return builder; } public void setType(final TypeDefinition type) { Builder parent = getActualNode(); if (parent == null || !(parent instanceof TypeAwareBuilder)) { throw new YangParseException("Failed to set type '" + type.getQName().getLocalName() + "'. Invalid parent node: " + parent); } ((TypeAwareBuilder) parent).setType(type); } public UnionTypeBuilder addUnionType(final int line, final URI namespace, final Date revision) { final Builder parent = getActualNode(); if (parent == null) { throw new YangParseException(line, "Error while parsing union type"); } else { final UnionTypeBuilder union = new UnionTypeBuilder(line); if (parent instanceof TypeAwareBuilder) { ((TypeAwareBuilder) parent).setTypedef(union); return union; } else { throw new YangParseException(name, line, "Invalid parent of union type."); } } } public void addIdentityrefType(final int line, final SchemaPath schemaPath, final String baseString) { final IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(baseString, schemaPath, line); final Builder parent = getActualNode(); if (parent == null) { throw new YangParseException(line, "Error while parsing identityref type."); } else { if (parent instanceof TypeAwareBuilder) { final TypeAwareBuilder typeParent = (TypeAwareBuilder) parent; typeParent.setTypedef(identityref); dirtyNodes.add(typeParent); } else { throw new YangParseException(name, line, "Invalid parent of identityref type."); } } } public DeviationBuilder addDeviation(final int line, final String targetPath) { Builder parent = getActualNode(); if (parent != null) { throw new YangParseException(name, line, "deviation can be defined only in module or submodule"); } final DeviationBuilder builder = new DeviationBuilder(line, targetPath); addedDeviations.add(builder); return builder; } public IdentitySchemaNodeBuilder addIdentity(final QName qname, final List parentPath, final int line) { Builder parent = getActualNode(); if (parent != null) { throw new YangParseException(name, line, "identity can be defined only in module or submodule"); } for (IdentitySchemaNodeBuilder idBuilder : addedIdentities) { if (idBuilder.getQName().equals(qname)) { throw new YangParseException(name, line, "Duplicate node found at line " + idBuilder.getLine()); } } final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(line, qname); addedIdentities.add(builder); return builder; } @Override public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder builder) { addedUnknownNodes.add(builder); allUnknownNodes.add(builder); } public UnknownSchemaNodeBuilder addUnknownSchemaNode(final int line, final QName qname) { final Builder parent = getActualNode(); final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(line, qname); builder.setParent(parent); allUnknownNodes.add(builder); if (parent == null) { addedUnknownNodes.add(builder); } else { if (parent instanceof SchemaNodeBuilder) { ((SchemaNodeBuilder) parent).addUnknownSchemaNode(builder); } else if (parent instanceof DataNodeContainerBuilder) { ((DataNodeContainerBuilder) parent).addUnknownSchemaNode(builder); } else if (parent instanceof RefineHolder) { ((RefineHolder) parent).addUnknownSchemaNode(builder); } else { throw new YangParseException(name, line, "Unresolved parent of unknown node '" + qname.getLocalName() + "'"); } } return builder; } @Override public String toString() { return "module " + name; } private final class ModuleImpl implements Module { private URI namespace; private final String name; private Date revision; private String prefix; private String yangVersion; private String description; private String reference; private String organization; private String contact; private Set imports = Collections.emptySet(); private Set features = Collections.emptySet(); private Set> typeDefinitions = Collections.emptySet(); private Set notifications = Collections.emptySet(); private Set augmentations = Collections.emptySet(); private Set rpcs = Collections.emptySet(); private Set deviations = Collections.emptySet(); private Map childNodes = Collections.emptyMap(); private Set groupings = Collections.emptySet(); private Set uses = Collections.emptySet(); private List extensionNodes = Collections.emptyList(); private Set identities = Collections.emptySet(); private List unknownNodes = Collections.emptyList(); private ModuleImpl(String name) { this.name = name; } @Override public URI getNamespace() { return namespace; } private void setNamespace(URI namespace) { this.namespace = namespace; } @Override public String getName() { return name; } @Override public Date getRevision() { return revision; } private void setRevision(Date revision) { this.revision = revision; } @Override public String getPrefix() { return prefix; } private void setPrefix(String prefix) { this.prefix = prefix; } @Override public String getYangVersion() { return yangVersion; } private void setYangVersion(String yangVersion) { this.yangVersion = yangVersion; } @Override public String getDescription() { return description; } private void setDescription(String description) { this.description = description; } @Override public String getReference() { return reference; } private void setReference(String reference) { this.reference = reference; } @Override public String getOrganization() { return organization; } private void setOrganization(String organization) { this.organization = organization; } @Override public String getContact() { return contact; } private void setContact(String contact) { this.contact = contact; } @Override public Set getImports() { return imports; } private void setImports(Set imports) { if (imports != null) { this.imports = imports; } } @Override public Set getFeatures() { return features; } private void setFeatures(Set features) { if (features != null) { this.features = features; } } @Override public Set> getTypeDefinitions() { return typeDefinitions; } private void setTypeDefinitions(Set> typeDefinitions) { if (typeDefinitions != null) { this.typeDefinitions = typeDefinitions; } } @Override public Set getNotifications() { return notifications; } private void setNotifications(Set notifications) { if (notifications != null) { this.notifications = notifications; } } @Override public Set getAugmentations() { return augmentations; } private void setAugmentations(Set augmentations) { if (augmentations != null) { this.augmentations = augmentations; } } @Override public Set getRpcs() { return rpcs; } private void setRpcs(Set rpcs) { if (rpcs != null) { this.rpcs = rpcs; } } @Override public Set getDeviations() { return deviations; } private void setDeviations(Set deviations) { if (deviations != null) { this.deviations = deviations; } } @Override public Set getChildNodes() { return new LinkedHashSet(childNodes.values()); } private void setChildNodes(Map childNodes) { if (childNodes != null) { this.childNodes = childNodes; } } @Override public Set getGroupings() { return groupings; } private void setGroupings(Set groupings) { if (groupings != null) { this.groupings = groupings; } } @Override public Set getUses() { return uses; } private void setUses(Set uses) { if (uses != null) { this.uses = uses; } } @Override public List getExtensionSchemaNodes() { return extensionNodes; } private void setExtensionSchemaNodes(final List extensionNodes) { if (extensionNodes != null) { this.extensionNodes = extensionNodes; } } @Override public Set getIdentities() { return identities; } private void setIdentities(final Set identities) { if (identities != null) { this.identities = identities; } } @Override public List getUnknownSchemaNodes() { return unknownNodes; } private void setUnknownSchemaNodes(final List unknownNodes) { if (unknownNodes != null) { this.unknownNodes = unknownNodes; } } @Override public DataSchemaNode getDataChildByName(QName name) { return childNodes.get(name); } @Override public DataSchemaNode getDataChildByName(String name) { DataSchemaNode result = null; for (Map.Entry entry : childNodes.entrySet()) { if (entry.getKey().getLocalName().equals(name)) { result = entry.getValue(); break; } } return result; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((namespace == null) ? 0 : namespace.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((revision == null) ? 0 : revision.hashCode()); result = prime * result + ((prefix == null) ? 0 : prefix.hashCode()); result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } ModuleImpl other = (ModuleImpl) obj; if (namespace == null) { if (other.namespace != null) { return false; } } else if (!namespace.equals(other.namespace)) { return false; } if (name == null) { if (other.name != null) { return false; } } else if (!name.equals(other.name)) { return false; } if (revision == null) { if (other.revision != null) { return false; } } else if (!revision.equals(other.revision)) { return false; } if (prefix == null) { if (other.prefix != null) { return false; } } else if (!prefix.equals(other.prefix)) { return false; } if (yangVersion == null) { if (other.yangVersion != null) { return false; } } else if (!yangVersion.equals(other.yangVersion)) { return false; } return true; } @Override public String toString() { StringBuilder sb = new StringBuilder(ModuleImpl.class.getSimpleName()); sb.append("["); sb.append("name=" + name); sb.append(", namespace=" + namespace); sb.append(", revision=" + revision); sb.append(", prefix=" + prefix); sb.append(", yangVersion=" + yangVersion); sb.append("]"); return sb.toString(); } } private void addChildToParent(final Builder parent, final DataSchemaNodeBuilder child, final String childLocalName) { final int line = child.getLine(); if (parent == null) { // if parent == null => node is defined under module // All leafs, leaf-lists, lists, containers, choices, rpcs, // notifications, and anyxmls defined within a parent node or at the // top level of the module or its submodules share the same // identifier namespace. for (DataSchemaNodeBuilder childNode : addedChildNodes) { if (childNode.getQName().getLocalName().equals(childLocalName)) { throw new YangParseException(name, line, "Duplicate node found at line " + childNode.getLine()); } } for (RpcDefinitionBuilder rpc : addedRpcs) { if (rpc.getQName().getLocalName().equals(childLocalName)) { throw new YangParseException(name, line, "Duplicate node found at line " + rpc.getLine()); } } for (NotificationBuilder notification : addedNotifications) { if (notification.getQName().getLocalName().equals(childLocalName)) { throw new YangParseException(name, line, "Duplicate node found at line " + notification.getLine()); } } addedChildNodes.add(child); } else { // no need for checking rpc and notification because they can be // defined only under module or submodule if (parent instanceof DataNodeContainerBuilder) { DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent; for (DataSchemaNodeBuilder childNode : parentNode.getChildNodeBuilders()) { if (childNode.getQName().getLocalName().equals(childLocalName)) { throw new YangParseException(name, line, "Duplicate node found at line " + childNode.getLine()); } } parentNode.addChildNode(child); } else if (parent instanceof ChoiceBuilder) { ChoiceBuilder parentNode = (ChoiceBuilder) parent; for (ChoiceCaseBuilder caseBuilder : parentNode.getCases()) { if (caseBuilder.getQName().getLocalName().equals(childLocalName)) { throw new YangParseException(name, line, "Duplicate node found at line " + caseBuilder.getLine()); } } parentNode.addChildNode(child); } else { throw new YangParseException(name, line, "Unresolved parent of node '" + childLocalName + "'."); } } } private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) { final ModuleImport moduleImport = new ModuleImport() { @Override public String getModuleName() { return moduleName; } @Override public Date getRevision() { return revision; } @Override public String getPrefix() { return prefix; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode()); result = prime * result + ((revision == null) ? 0 : revision.hashCode()); result = prime * result + ((prefix == null) ? 0 : prefix.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } ModuleImport other = (ModuleImport) obj; if (getModuleName() == null) { if (other.getModuleName() != null) { return false; } } else if (!getModuleName().equals(other.getModuleName())) { return false; } if (getRevision() == null) { if (other.getRevision() != null) { return false; } } else if (!getRevision().equals(other.getRevision())) { return false; } if (getPrefix() == null) { if (other.getPrefix() != null) { return false; } } else if (!getPrefix().equals(other.getPrefix())) { return false; } return true; } @Override public String toString() { return "ModuleImport[moduleName=" + moduleName + ", revision=" + revision + ", prefix=" + prefix + "]"; } }; return moduleImport; } }