-/*
- * 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.model.parser.builder.impl;
-
-import java.net.URI;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-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.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.TypeDefinition;
-import org.opendaylight.controller.yang.model.api.UsesNode;
-import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder;
-import org.opendaylight.controller.yang.model.parser.builder.api.Builder;
-import org.opendaylight.controller.yang.model.parser.builder.api.ChildNodeBuilder;
-import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
-import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
-import org.opendaylight.controller.yang.model.parser.builder.api.TypeAwareBuilder;
-import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionAwareBuilder;
-import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
-import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
-
-/**
- * This builder builds 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 implements Builder {
-
- private final ModuleImpl instance;
- private final String name;
- private String prefix;
- private Date revision;
-
- private final Set<ModuleImport> imports = new HashSet<ModuleImport>();
- private Set<AugmentationSchema> augmentations;
-
- /**
- * All nodes, that can contain other nodes
- */
- private final Map<List<String>, Builder> moduleNodes = new HashMap<List<String>, Builder>();
-
- /**
- * Holds all child (DataSchemaNode) nodes: anyxml, choice, case, container,
- * list, leaf, leaf-list.
- */
- private final Map<List<String>, DataSchemaNodeBuilder> addedChilds = new HashMap<List<String>, DataSchemaNodeBuilder>();
-
- private final Map<List<String>, GroupingBuilder> addedGroupings = new HashMap<List<String>, GroupingBuilder>();
- private final Set<AugmentationSchemaBuilder> addedAugments = new HashSet<AugmentationSchemaBuilder>();
- private final Map<List<String>, UsesNodeBuilder> addedUsesNodes = new HashMap<List<String>, UsesNodeBuilder>();
- private final Map<List<String>, RpcDefinitionBuilder> addedRpcs = new HashMap<List<String>, RpcDefinitionBuilder>();
- private final Set<NotificationBuilder> addedNotifications = new HashSet<NotificationBuilder>();
- private final Map<List<String>, FeatureBuilder> addedFeatures = new HashMap<List<String>, FeatureBuilder>();
- private final Map<String, DeviationBuilder> addedDeviations = new HashMap<String, DeviationBuilder>();
- private final Map<List<String>, TypeDefinitionBuilder> addedTypedefs = new HashMap<List<String>, TypeDefinitionBuilder>();
- private final List<ExtensionBuilder> addedExtensions = new ArrayList<ExtensionBuilder>();
-
- private final Map<List<String>, TypeAwareBuilder> dirtyNodes = new HashMap<List<String>, TypeAwareBuilder>();
-
- public ModuleBuilder(String name) {
- this.name = name;
- instance = new ModuleImpl(name);
- }
-
-
-
- /**
- * Build new Module object based on this builder.
- */
- @Override
- public Module build() {
- instance.setImports(imports);
-
- // TYPEDEFS
- final Set<TypeDefinition<?>> typedefs = buildModuleTypedefs(addedTypedefs);
- instance.setTypeDefinitions(typedefs);
-
- // CHILD NODES
- final Map<QName, DataSchemaNode> childNodes = buildModuleChildNodes(addedChilds);
- instance.setChildNodes(childNodes);
-
- // GROUPINGS
- final Set<GroupingDefinition> groupings = buildModuleGroupings(addedGroupings);
- instance.setGroupings(groupings);
-
- // USES
- final Set<UsesNode> usesNodeDefinitions = buildUsesNodes(addedUsesNodes);
- instance.setUses(usesNodeDefinitions);
-
- // FEATURES
- final Set<FeatureDefinition> features = buildModuleFeatures(addedFeatures);
- instance.setFeatures(features);
-
- // NOTIFICATIONS
- final Set<NotificationDefinition> notifications = new HashSet<NotificationDefinition>();
- for (NotificationBuilder entry : addedNotifications) {
- notifications.add((NotificationDefinition) entry.build());
- }
- instance.setNotifications(notifications);
-
- // AUGMENTATIONS
- instance.setAugmentations(augmentations);
-
- // RPCs
- final Set<RpcDefinition> rpcs = buildModuleRpcs(addedRpcs);
- instance.setRpcs(rpcs);
-
- // DEVIATIONS
- final Set<Deviation> deviations = new HashSet<Deviation>();
- for (Map.Entry<String, DeviationBuilder> entry : addedDeviations
- .entrySet()) {
- deviations.add(entry.getValue().build());
- }
- instance.setDeviations(deviations);
-
- // EXTENSIONS
- final List<ExtensionDefinition> extensions = new ArrayList<ExtensionDefinition>();
- for(ExtensionBuilder b : addedExtensions) {
- extensions.add(b.build());
- }
- instance.setExtensionSchemaNodes(extensions);
-
- return instance;
- }
-
- public Builder getNode(List<String> path) {
- return moduleNodes.get(path);
- }
-
- public Map<List<String>, TypeAwareBuilder> getDirtyNodes() {
- return dirtyNodes;
- }
-
- public String getName() {
- return name;
- }
-
- public String getPrefix() {
- return prefix;
- }
-
- public Date getRevision() {
- return revision;
- }
-
- public Set<AugmentationSchemaBuilder> getAddedAugments() {
- return addedAugments;
- }
-
- public void addDirtyNode(List<String> path) {
- List<String> dirtyNodePath = new ArrayList<String>(path);
- TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) moduleNodes
- .get(dirtyNodePath);
- dirtyNodes.put(dirtyNodePath, nodeBuilder);
- }
-
- public void setNamespace(URI namespace) {
- instance.setNamespace(namespace);
- }
-
- public void setRevision(Date revision) {
- this.revision = revision;
- instance.setRevision(revision);
- }
-
- public void setPrefix(String prefix) {
- this.prefix = prefix;
- instance.setPrefix(prefix);
- }
-
- public void setYangVersion(String yangVersion) {
- instance.setYangVersion(yangVersion);
- }
-
- public void setDescription(String description) {
- instance.setDescription(description);
- }
-
- public void setReference(String reference) {
- instance.setReference(reference);
- }
-
- public void setOrganization(String organization) {
- instance.setOrganization(organization);
- }
-
- public void setContact(String contact) {
- instance.setContact(contact);
- }
-
- public void setAugmentations(Set<AugmentationSchema> augmentations) {
- this.augmentations = augmentations;
- }
-
- public boolean addModuleImport(final String moduleName,
- final Date revision, final String prefix) {
- ModuleImport moduleImport = createModuleImport(moduleName, revision,
- prefix);
- return imports.add(moduleImport);
- }
-
- public Set<ModuleImport> getModuleImports() {
- return imports;
- }
-
- public ExtensionBuilder addExtension(QName qname) {
- ExtensionBuilder builder = new ExtensionBuilder(qname);
- return builder;
- }
-
- public ContainerSchemaNodeBuilder addContainerNode(QName containerName,
- List<String> parentPath) {
- List<String> pathToNode = new ArrayList<String>(parentPath);
-
- ContainerSchemaNodeBuilder containerBuilder = new ContainerSchemaNodeBuilder(
- containerName);
-
- ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes
- .get(pathToNode);
- if (parent != null) {
- if(parent instanceof AugmentationSchemaBuilder) {
- containerBuilder.setAugmenting(true);
- }
- parent.addChildNode(containerBuilder);
- }
-
- pathToNode.add(containerName.getLocalName());
- moduleNodes.put(pathToNode, containerBuilder);
- addedChilds.put(pathToNode, containerBuilder);
-
- return containerBuilder;
- }
-
- public ListSchemaNodeBuilder addListNode(QName listName,
- List<String> parentPath) {
- List<String> pathToNode = new ArrayList<String>(parentPath);
-
- ListSchemaNodeBuilder listBuilder = new ListSchemaNodeBuilder(listName);
-
- ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes
- .get(pathToNode);
- if (parent != null) {
- if(parent instanceof AugmentationSchemaBuilder) {
- listBuilder.setAugmenting(true);
- }
- parent.addChildNode(listBuilder);
- }
-
- pathToNode.add(listName.getLocalName());
- moduleNodes.put(pathToNode, listBuilder);
- addedChilds.put(pathToNode, listBuilder);
-
- return listBuilder;
- }
-
- public LeafSchemaNodeBuilder addLeafNode(QName leafName,
- List<String> parentPath) {
- List<String> pathToNode = new ArrayList<String>(parentPath);
-
- LeafSchemaNodeBuilder leafBuilder = new LeafSchemaNodeBuilder(leafName);
-
- ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes.get(pathToNode);
- if (parent != null) {
- if(parent instanceof AugmentationSchemaBuilder) {
- leafBuilder.setAugmenting(true);
- }
- parent.addChildNode(leafBuilder);
- }
-
- pathToNode.add(leafName.getLocalName());
- addedChilds.put(pathToNode, leafBuilder);
- moduleNodes.put(pathToNode, leafBuilder);
-
- return leafBuilder;
- }
-
- public LeafListSchemaNodeBuilder addLeafListNode(QName leafListName,
- List<String> parentPath) {
- List<String> pathToNode = new ArrayList<String>(parentPath);
-
- LeafListSchemaNodeBuilder leafListBuilder = new LeafListSchemaNodeBuilder(
- leafListName);
- ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes.get(pathToNode);
- if (parent != null) {
- if(parent instanceof AugmentationSchemaBuilder) {
- leafListBuilder.setAugmenting(true);
- }
- parent.addChildNode(leafListBuilder);
- }
-
- pathToNode.add(leafListName.getLocalName());
- addedChilds.put(pathToNode, leafListBuilder);
- moduleNodes.put(pathToNode, leafListBuilder);
-
- return leafListBuilder;
- }
-
- public GroupingBuilder addGrouping(QName qname, List<String> parentPath) {
- List<String> pathToGroup = new ArrayList<String>(parentPath);
-
- GroupingBuilder builder = new GroupingBuilderImpl(qname);
- ChildNodeBuilder parentNodeBuilder = (ChildNodeBuilder) moduleNodes.get(pathToGroup);
- if (parentNodeBuilder != null) {
- parentNodeBuilder.addGrouping(builder);
- }
-
- pathToGroup.add(qname.getLocalName());
- moduleNodes.put(pathToGroup, builder);
- addedGroupings.put(pathToGroup, builder);
-
- return builder;
- }
-
- public AugmentationSchemaBuilder addAugment(String name,
- List<String> parentPath) {
- List<String> pathToAugment = new ArrayList<String>(parentPath);
-
- AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name);
-
- // augment can only be in 'module' or 'uses' statement
- UsesNodeBuilder parent = addedUsesNodes.get(pathToAugment);
- if (parent != null) {
- parent.addAugment(builder);
- }
-
- pathToAugment.add(name);
- moduleNodes.put(pathToAugment, builder);
- addedAugments.add(builder);
-
- return builder;
- }
-
- public UsesNodeBuilder addUsesNode(String groupingPathStr,
- List<String> parentPath) {
- List<String> pathToUses = new ArrayList<String>(parentPath);
-
- UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(groupingPathStr);
-
- ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes.get(pathToUses);
- if (parent != null) {
- if(parent instanceof AugmentationSchemaBuilder) {
- usesBuilder.setAugmenting(true);
- }
- parent.addUsesNode(usesBuilder);
- }
-
- pathToUses.add(groupingPathStr);
- addedUsesNodes.put(pathToUses, usesBuilder);
-
- return usesBuilder;
- }
-
- public RpcDefinitionBuilder addRpc(QName qname, List<String> parentPath) {
- List<String> pathToRpc = new ArrayList<String>(parentPath);
-
- RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(qname);
-
- pathToRpc.add(qname.getLocalName());
- addedRpcs.put(pathToRpc, rpcBuilder);
-
- QName inputQName = new QName(qname.getNamespace(), qname.getRevision(),
- qname.getPrefix(), "input");
- ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(inputQName);
- List<String> pathToInput = new ArrayList<String>(pathToRpc);
- pathToInput.add("input");
- moduleNodes.put(pathToInput, inputBuilder);
- rpcBuilder.setInput(inputBuilder);
-
- QName outputQName = new QName(qname.getNamespace(),
- qname.getRevision(), qname.getPrefix(), "output");
- ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(outputQName);
- List<String> pathToOutput = new ArrayList<String>(pathToRpc);
- pathToOutput.add("output");
- moduleNodes.put(pathToOutput, outputBuilder);
- rpcBuilder.setOutput(outputBuilder);
-
- return rpcBuilder;
- }
-
- public NotificationBuilder addNotification(QName notificationName,
- List<String> parentPath) {
- List<String> pathToNotification = new ArrayList<String>(parentPath);
-
- NotificationBuilder notificationBuilder = new NotificationBuilder(
- notificationName);
-
- pathToNotification.add(notificationName.getLocalName());
- moduleNodes.put(pathToNotification, notificationBuilder);
- addedNotifications.add(notificationBuilder);
-
- return notificationBuilder;
- }
-
- public FeatureBuilder addFeature(QName featureName, List<String> parentPath) {
- List<String> pathToFeature = new ArrayList<String>(parentPath);
- pathToFeature.add(featureName.getLocalName());
-
- FeatureBuilder builder = new FeatureBuilder(featureName);
- addedFeatures.put(pathToFeature, builder);
- return builder;
- }
-
- public TypedefBuilder addTypedef(QName typeDefName, List<String> parentPath) {
- List<String> pathToType = new ArrayList<String>(parentPath);
- TypedefBuilder builder = new TypedefBuilder(typeDefName);
- TypeDefinitionAwareBuilder parent = (TypeDefinitionAwareBuilder) moduleNodes.get(pathToType);
- if (parent != null) {
- parent.addTypedef(builder);
- }
- pathToType.add(typeDefName.getLocalName());
- addedTypedefs.put(pathToType, builder);
- moduleNodes.put(pathToType, builder);
- return builder;
- }
-
- public Set<TypeDefinitionBuilder> getModuleTypedefs() {
- Set<TypeDefinitionBuilder> typedefs = new HashSet<TypeDefinitionBuilder>();
- for (Map.Entry<List<String>, TypeDefinitionBuilder> entry : addedTypedefs.entrySet()) {
- if (entry.getKey().size() == 2) {
- typedefs.add(entry.getValue());
- }
- }
- return typedefs;
- }
-
- public void setType(TypeDefinition<?> type, List<String> parentPath) {
- TypeAwareBuilder parent = (TypeAwareBuilder) moduleNodes.get(parentPath);
- parent.setType(type);
- }
-
- public DeviationBuilder addDeviation(String targetPath) {
- DeviationBuilder builder = new DeviationBuilder(targetPath);
- addedDeviations.put(targetPath, builder);
- return builder;
- }
-
- public void addConfiguration(boolean configuration, List<String> parentPath) {
- Builder builder = moduleNodes.get(parentPath);
- if (builder instanceof DeviationBuilder) {
- // skip
- // TODO
- } else {
- DataSchemaNodeBuilder configBuilder = (DataSchemaNodeBuilder) moduleNodes.get(parentPath);
- configBuilder.setConfiguration(configuration);
- }
- }
-
- public UnknownSchemaNodeBuilder addUnknownSchemaNode(QName qname, List<String> parentPath) {
- UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(qname);
- return builder;
- }
-
-
- private 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<ModuleImport> imports = Collections.emptySet();
- private Set<FeatureDefinition> features = Collections.emptySet();
- private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
- private Set<NotificationDefinition> notifications = Collections.emptySet();
- private Set<AugmentationSchema> augmentations = Collections.emptySet();
- private Set<RpcDefinition> rpcs = Collections.emptySet();
- private Set<Deviation> deviations = Collections.emptySet();
- private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
- private Set<GroupingDefinition> groupings = Collections.emptySet();
- private Set<UsesNode> uses = Collections.emptySet();
- private List<ExtensionDefinition> extensionSchemaNodes = 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<ModuleImport> getImports() {
- return imports;
- }
-
- private void setImports(Set<ModuleImport> imports) {
- this.imports = imports;
- }
-
- @Override
- public Set<FeatureDefinition> getFeatures() {
- return features;
- }
-
- private void setFeatures(Set<FeatureDefinition> features) {
- this.features = features;
- }
-
- @Override
- public Set<TypeDefinition<?>> getTypeDefinitions() {
- return typeDefinitions;
- }
-
- private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
- this.typeDefinitions = typeDefinitions;
- }
-
- @Override
- public Set<NotificationDefinition> getNotifications() {
- return notifications;
- }
-
- private void setNotifications(Set<NotificationDefinition> notifications) {
- this.notifications = notifications;
- }
-
- @Override
- public Set<AugmentationSchema> getAugmentations() {
- return augmentations;
- }
-
- private void setAugmentations(Set<AugmentationSchema> augmentations) {
- this.augmentations = augmentations;
- }
-
- @Override
- public Set<RpcDefinition> getRpcs() {
- return rpcs;
- }
-
- private void setRpcs(Set<RpcDefinition> rpcs) {
- this.rpcs = rpcs;
- }
-
- @Override
- public Set<Deviation> getDeviations() {
- return deviations;
- }
-
- private void setDeviations(Set<Deviation> deviations) {
- this.deviations = deviations;
- }
-
- @Override
- public Set<DataSchemaNode> getChildNodes() {
- return new HashSet<DataSchemaNode>(childNodes.values());
- }
-
- private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
- this.childNodes = childNodes;
- }
-
- @Override
- public Set<GroupingDefinition> getGroupings() {
- return groupings;
- }
-
- private void setGroupings(Set<GroupingDefinition> groupings) {
- this.groupings = groupings;
- }
-
- @Override
- public Set<UsesNode> getUses() {
- return uses;
- }
-
- private void setUses(Set<UsesNode> uses) {
- this.uses = uses;
- }
-
- @Override
- public List<ExtensionDefinition> getExtensionSchemaNodes() {
- return extensionSchemaNodes;
- }
-
- private void setExtensionSchemaNodes(List<ExtensionDefinition> extensionSchemaNodes) {
- if(extensionSchemaNodes != null) {
- this.extensionSchemaNodes = extensionSchemaNodes;
- }
- }
-
- @Override
- public DataSchemaNode getDataChildByName(QName name) {
- return childNodes.get(name);
- }
-
- @Override
- public DataSchemaNode getDataChildByName(String name) {
- DataSchemaNode result = null;
- for (Map.Entry<QName, DataSchemaNode> 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());
- result = prime * result + ((description == null) ? 0 : description.hashCode());
- result = prime * result + ((reference == null) ? 0 : reference.hashCode());
-
- result = prime * result + ((organization == null) ? 0 : organization.hashCode());
- result = prime * result + ((contact == null) ? 0 : contact.hashCode());
- result = prime * result + ((imports == null) ? 0 : imports.hashCode());
- result = prime * result + ((features == null) ? 0 : features.hashCode());
- result = prime * result + ((typeDefinitions == null) ? 0 : typeDefinitions.hashCode());
- result = prime * result + ((notifications == null) ? 0 : notifications.hashCode());
- result = prime * result + ((augmentations == null) ? 0 : augmentations.hashCode());
- result = prime * result + ((rpcs == null) ? 0 : rpcs.hashCode());
- result = prime * result + ((deviations == null) ? 0 : deviations.hashCode());
- result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
- result = prime * result + ((groupings == null) ? 0 : groupings.hashCode());
- result = prime * result + ((uses == null) ? 0 : uses.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;
- }
- if (description == null) {
- if (other.description != null) {
- return false;
- }
- } else if (!description.equals(other.description)) {
- return false;
- }
- if (reference == null) {
- if (other.reference != null) {
- return false;
- }
- } else if (!reference.equals(other.reference)) {
- return false;
- }
- if (organization == null) {
- if (other.organization != null) {
- return false;
- }
- } else if (!organization.equals(other.organization)) {
- return false;
- }
- if (contact == null) {
- if (other.contact != null) {
- return false;
- }
- } else if (!contact.equals(other.contact)) {
- return false;
- }
- if (imports == null) {
- if (other.imports != null) {
- return false;
- }
- } else if (!imports.equals(other.imports)) {
- return false;
- }
- if (features == null) {
- if (other.features != null) {
- return false;
- }
- } else if (!features.equals(other.features)) {
- return false;
- }
- if (typeDefinitions == null) {
- if (other.typeDefinitions != null) {
- return false;
- }
- } else if (!typeDefinitions.equals(other.typeDefinitions)) {
- return false;
- }
- if (notifications == null) {
- if (other.notifications != null) {
- return false;
- }
- } else if (!notifications.equals(other.notifications)) {
- return false;
- }
- if (augmentations == null) {
- if (other.augmentations != null) {
- return false;
- }
- } else if (!augmentations.equals(other.augmentations)) {
- return false;
- }
- if (rpcs == null) {
- if (other.rpcs != null) {
- return false;
- }
- } else if (!rpcs.equals(other.rpcs)) {
- return false;
- }
- if (deviations == null) {
- if (other.deviations != null) {
- return false;
- }
- } else if (!deviations.equals(other.deviations)) {
- return false;
- }
- if (childNodes == null) {
- if (other.childNodes != null) {
- return false;
- }
- } else if (!childNodes.equals(other.childNodes)) {
- return false;
- }
- if (groupings == null) {
- if (other.groupings != null) {
- return false;
- }
- } else if (!groupings.equals(other.groupings)) {
- return false;
- }
- if (uses == null) {
- if (other.uses != null) {
- return false;
- }
- } else if (!uses.equals(other.uses)) {
- return false;
- }
- return true;
- }
-
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder(
- ModuleImpl.class.getSimpleName());
- sb.append("[\n");
- sb.append("name=" + name + ",\n");
- sb.append("namespace=" + namespace + ",\n");
- sb.append("revision=" + revision + ",\n");
- sb.append("prefix=" + prefix + ",\n");
- sb.append("yangVersion=" + yangVersion + ",\n");
- sb.append("description=" + description + ",\n");
- sb.append("reference=" + reference + ",\n");
- sb.append("organization=" + organization + ",\n");
- sb.append("contact=" + contact + ",\n");
- sb.append("childNodes=" + childNodes.values() + ",\n");
- sb.append("groupings=" + groupings + ",\n");
- sb.append("imports=" + imports + ",\n");
- sb.append("features=" + features + ",\n");
- sb.append("typeDefinitions=" + typeDefinitions + ",\n");
- sb.append("notifications=" + notifications + ",\n");
- sb.append("augmentations=" + augmentations + ",\n");
- sb.append("rpcs=" + rpcs + ",\n");
- sb.append("deviations=" + deviations + "\n");
- sb.append("uses=" + uses + "\n");
- sb.append("]");
- return sb.toString();
- }
- }
-
- private ModuleImport createModuleImport(final String moduleName,
- final Date revision, final String prefix) {
- 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;
- }
-
- /**
- * Traverse through given addedChilds and add only direct module childs.
- * Direct module child path size is 2 (1. module name, 2. child name).
- *
- * @param addedChilds
- * @return map of children, where key is child QName and value is child
- * itself
- */
- private Map<QName, DataSchemaNode> buildModuleChildNodes(
- Map<List<String>, DataSchemaNodeBuilder> addedChilds) {
- final Map<QName, DataSchemaNode> childNodes = new HashMap<QName, DataSchemaNode>();
- for (Map.Entry<List<String>, DataSchemaNodeBuilder> entry : addedChilds
- .entrySet()) {
- if (entry.getKey().size() == 2) {
- DataSchemaNode node = entry.getValue().build();
- QName qname = entry.getValue().getQName();
- childNodes.put(qname, node);
- }
- }
- return childNodes;
- }
-
- /**
- * Traverse through given addedGroupings and add only direct module
- * groupings. Direct module grouping path size is 2 (1. module name, 2.
- * grouping name).
- *
- * @param addedGroupings
- * @return set of built GroupingDefinition objects
- */
- private Set<GroupingDefinition> buildModuleGroupings(
- Map<List<String>, GroupingBuilder> addedGroupings) {
- final Set<GroupingDefinition> groupings = new HashSet<GroupingDefinition>();
- for (Map.Entry<List<String>, GroupingBuilder> entry : addedGroupings
- .entrySet()) {
- if (entry.getKey().size() == 2) {
- groupings.add(entry.getValue().build());
- }
- }
- return groupings;
- }
-
- /**
- * Traverse through given addedRpcs and build RpcDefinition objects.
- *
- * @param addedRpcs
- * @return set of built RpcDefinition objects
- */
- private Set<RpcDefinition> buildModuleRpcs(
- Map<List<String>, RpcDefinitionBuilder> addedRpcs) {
- final Set<RpcDefinition> rpcs = new HashSet<RpcDefinition>();
- RpcDefinitionBuilder builder;
- for (Map.Entry<List<String>, RpcDefinitionBuilder> entry : addedRpcs
- .entrySet()) {
- builder = entry.getValue();
- RpcDefinition rpc = builder.build();
- rpcs.add(rpc);
- }
- return rpcs;
- }
-
- /**
- * Traverse through given addedTypedefs and add only direct module typedef
- * statements. Direct module typedef path size is 2 (1. module name, 2.
- * typedef name).
- *
- * @param addedTypedefs
- * @return set of built module typedef statements
- */
- private Set<TypeDefinition<?>> buildModuleTypedefs(
- Map<List<String>, TypeDefinitionBuilder> addedTypedefs) {
- Set<TypeDefinition<?>> typedefs = new HashSet<TypeDefinition<?>>();
- for (Map.Entry<List<String>, TypeDefinitionBuilder> entry : addedTypedefs
- .entrySet()) {
- if (entry.getKey().size() == 2) {
- TypeDefinition<? extends TypeDefinition<?>> node = entry
- .getValue().build();
- typedefs.add(node);
- }
- }
- return typedefs;
- }
-
- /**
- * Traverse through given addedUsesNodes and add only direct module uses
- * nodes. Direct module uses node path size is 2 (1. module name, 2. uses
- * name).
- *
- * @param addedUsesNodes
- * @return set of built module uses nodes
- */
- private Set<UsesNode> buildUsesNodes(
- Map<List<String>, UsesNodeBuilder> addedUsesNodes) {
- final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
- for (Map.Entry<List<String>, UsesNodeBuilder> entry : addedUsesNodes
- .entrySet()) {
- if (entry.getKey().size() == 2) {
- usesNodeDefinitions.add(entry.getValue().build());
- }
- }
- return usesNodeDefinitions;
- }
-
- /**
- * Traverse through given addedFeatures and add only direct module features.
- * Direct module feature path size is 2 (1. module name, 2. feature name).
- *
- * @param addedFeatures
- * @return set of built module features
- */
- private Set<FeatureDefinition> buildModuleFeatures(
- Map<List<String>, FeatureBuilder> addedFeatures) {
- Set<FeatureDefinition> features = new HashSet<FeatureDefinition>();
- for (Map.Entry<List<String>, FeatureBuilder> entry : addedFeatures
- .entrySet()) {
- if (entry.getKey().size() == 2) {
- features.add(entry.getValue().build());
- }
- }
- return features;
- }
-
-}
+/*\r
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+package org.opendaylight.controller.yang.model.parser.builder.impl;\r
+\r
+import java.net.URI;\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.Date;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.opendaylight.controller.yang.common.QName;\r
+import org.opendaylight.controller.yang.model.api.AugmentationSchema;\r
+import org.opendaylight.controller.yang.model.api.DataSchemaNode;\r
+import org.opendaylight.controller.yang.model.api.Deviation;\r
+import org.opendaylight.controller.yang.model.api.ExtensionDefinition;\r
+import org.opendaylight.controller.yang.model.api.FeatureDefinition;\r
+import org.opendaylight.controller.yang.model.api.GroupingDefinition;\r
+import org.opendaylight.controller.yang.model.api.Module;\r
+import org.opendaylight.controller.yang.model.api.ModuleImport;\r
+import org.opendaylight.controller.yang.model.api.NotificationDefinition;\r
+import org.opendaylight.controller.yang.model.api.RpcDefinition;\r
+import org.opendaylight.controller.yang.model.api.TypeDefinition;\r
+import org.opendaylight.controller.yang.model.api.UsesNode;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.Builder;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.ChildNodeBuilder;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.TypeAwareBuilder;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionAwareBuilder;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;\r
+import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;\r
+\r
+/**\r
+ * This builder builds Module object. If this module is dependent on external\r
+ * module/modules, these dependencies must be resolved before module is built,\r
+ * otherwise result may not be valid.\r
+ */\r
+public class ModuleBuilder implements Builder {\r
+\r
+ private final ModuleImpl instance;\r
+ private final String name;\r
+ private String prefix;\r
+ private Date revision;\r
+\r
+ private final Set<ModuleImport> imports = new HashSet<ModuleImport>();\r
+ private Set<AugmentationSchema> augmentations;\r
+\r
+ /**\r
+ * All nodes, that can contain other nodes\r
+ */\r
+ private final Map<List<String>, Builder> moduleNodes = new HashMap<List<String>, Builder>();\r
+\r
+ /**\r
+ * Holds all child (DataSchemaNode) nodes: anyxml, choice, case, container,\r
+ * list, leaf, leaf-list.\r
+ */\r
+ private final Map<List<String>, DataSchemaNodeBuilder> addedChilds = new HashMap<List<String>, DataSchemaNodeBuilder>();\r
+\r
+ private final Map<List<String>, GroupingBuilder> addedGroupings = new HashMap<List<String>, GroupingBuilder>();\r
+ private final Set<AugmentationSchemaBuilder> addedAugments = new HashSet<AugmentationSchemaBuilder>();\r
+ private final Map<List<String>, UsesNodeBuilder> addedUsesNodes = new HashMap<List<String>, UsesNodeBuilder>();\r
+ private final Map<List<String>, RpcDefinitionBuilder> addedRpcs = new HashMap<List<String>, RpcDefinitionBuilder>();\r
+ private final Set<NotificationBuilder> addedNotifications = new HashSet<NotificationBuilder>();\r
+ private final Map<List<String>, FeatureBuilder> addedFeatures = new HashMap<List<String>, FeatureBuilder>();\r
+ private final Map<String, DeviationBuilder> addedDeviations = new HashMap<String, DeviationBuilder>();\r
+ private final Map<List<String>, TypeDefinitionBuilder> addedTypedefs = new HashMap<List<String>, TypeDefinitionBuilder>();\r
+ private final List<ExtensionBuilder> addedExtensions = new ArrayList<ExtensionBuilder>();\r
+\r
+ private final Map<List<String>, TypeAwareBuilder> dirtyNodes = new HashMap<List<String>, TypeAwareBuilder>();\r
+ private final Map<List<String>, UnionTypeBuilder> unionTypes = new HashMap<List<String>, UnionTypeBuilder>();\r
+\r
+ public ModuleBuilder(String name) {\r
+ this.name = name;\r
+ instance = new ModuleImpl(name);\r
+ }\r
+\r
+\r
+\r
+ /**\r
+ * Build new Module object based on this builder.\r
+ */\r
+ @Override\r
+ public Module build() {\r
+ instance.setImports(imports);\r
+\r
+ // TYPEDEFS\r
+ final Set<TypeDefinition<?>> typedefs = buildModuleTypedefs(addedTypedefs);\r
+ instance.setTypeDefinitions(typedefs);\r
+\r
+ // CHILD NODES\r
+ final Map<QName, DataSchemaNode> childNodes = buildModuleChildNodes(addedChilds);\r
+ instance.setChildNodes(childNodes);\r
+\r
+ // GROUPINGS\r
+ final Set<GroupingDefinition> groupings = buildModuleGroupings(addedGroupings);\r
+ instance.setGroupings(groupings);\r
+\r
+ // USES\r
+ final Set<UsesNode> usesNodeDefinitions = buildUsesNodes(addedUsesNodes);\r
+ instance.setUses(usesNodeDefinitions);\r
+\r
+ // FEATURES\r
+ final Set<FeatureDefinition> features = buildModuleFeatures(addedFeatures);\r
+ instance.setFeatures(features);\r
+\r
+ // NOTIFICATIONS\r
+ final Set<NotificationDefinition> notifications = new HashSet<NotificationDefinition>();\r
+ for (NotificationBuilder entry : addedNotifications) {\r
+ notifications.add((NotificationDefinition) entry.build());\r
+ }\r
+ instance.setNotifications(notifications);\r
+\r
+ // AUGMENTATIONS\r
+ instance.setAugmentations(augmentations);\r
+\r
+ // RPCs\r
+ final Set<RpcDefinition> rpcs = buildModuleRpcs(addedRpcs);\r
+ instance.setRpcs(rpcs);\r
+\r
+ // DEVIATIONS\r
+ final Set<Deviation> deviations = new HashSet<Deviation>();\r
+ for (Map.Entry<String, DeviationBuilder> entry : addedDeviations\r
+ .entrySet()) {\r
+ deviations.add(entry.getValue().build());\r
+ }\r
+ instance.setDeviations(deviations);\r
+\r
+ // EXTENSIONS\r
+ final List<ExtensionDefinition> extensions = new ArrayList<ExtensionDefinition>();\r
+ for(ExtensionBuilder b : addedExtensions) {\r
+ extensions.add(b.build());\r
+ }\r
+ instance.setExtensionSchemaNodes(extensions);\r
+\r
+ return instance;\r
+ }\r
+\r
+ public Builder getNode(List<String> path) {\r
+ return moduleNodes.get(path);\r
+ }\r
+\r
+ public Map<List<String>, TypeAwareBuilder> getDirtyNodes() {\r
+ return dirtyNodes;\r
+ }\r
+\r
+ public String getName() {\r
+ return name;\r
+ }\r
+\r
+ public String getPrefix() {\r
+ return prefix;\r
+ }\r
+\r
+ public Date getRevision() {\r
+ return revision;\r
+ }\r
+\r
+ public Set<AugmentationSchemaBuilder> getAddedAugments() {\r
+ return addedAugments;\r
+ }\r
+\r
+ public void addDirtyNode(List<String> path) {\r
+ List<String> dirtyNodePath = new ArrayList<String>(path);\r
+ TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) moduleNodes\r
+ .get(dirtyNodePath);\r
+ dirtyNodes.put(dirtyNodePath, nodeBuilder);\r
+ }\r
+\r
+ public void setNamespace(URI namespace) {\r
+ instance.setNamespace(namespace);\r
+ }\r
+\r
+ public void setRevision(Date revision) {\r
+ this.revision = revision;\r
+ instance.setRevision(revision);\r
+ }\r
+\r
+ public void setPrefix(String prefix) {\r
+ this.prefix = prefix;\r
+ instance.setPrefix(prefix);\r
+ }\r
+\r
+ public void setYangVersion(String yangVersion) {\r
+ instance.setYangVersion(yangVersion);\r
+ }\r
+\r
+ public void setDescription(String description) {\r
+ instance.setDescription(description);\r
+ }\r
+\r
+ public void setReference(String reference) {\r
+ instance.setReference(reference);\r
+ }\r
+\r
+ public void setOrganization(String organization) {\r
+ instance.setOrganization(organization);\r
+ }\r
+\r
+ public void setContact(String contact) {\r
+ instance.setContact(contact);\r
+ }\r
+\r
+ public void setAugmentations(Set<AugmentationSchema> augmentations) {\r
+ this.augmentations = augmentations;\r
+ }\r
+\r
+ public boolean addModuleImport(final String moduleName,\r
+ final Date revision, final String prefix) {\r
+ ModuleImport moduleImport = createModuleImport(moduleName, revision,\r
+ prefix);\r
+ return imports.add(moduleImport);\r
+ }\r
+\r
+ public Set<ModuleImport> getModuleImports() {\r
+ return imports;\r
+ }\r
+\r
+ public ExtensionBuilder addExtension(QName qname) {\r
+ ExtensionBuilder builder = new ExtensionBuilder(qname);\r
+ return builder;\r
+ }\r
+\r
+ public ContainerSchemaNodeBuilder addContainerNode(QName containerName,\r
+ List<String> parentPath) {\r
+ List<String> pathToNode = new ArrayList<String>(parentPath);\r
+\r
+ ContainerSchemaNodeBuilder containerBuilder = new ContainerSchemaNodeBuilder(\r
+ containerName);\r
+\r
+ ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes\r
+ .get(pathToNode);\r
+ if (parent != null) {\r
+ if(parent instanceof AugmentationSchemaBuilder) {\r
+ containerBuilder.setAugmenting(true);\r
+ }\r
+ parent.addChildNode(containerBuilder);\r
+ }\r
+\r
+ pathToNode.add(containerName.getLocalName());\r
+ moduleNodes.put(pathToNode, containerBuilder);\r
+ addedChilds.put(pathToNode, containerBuilder);\r
+\r
+ return containerBuilder;\r
+ }\r
+\r
+ public ListSchemaNodeBuilder addListNode(QName listName,\r
+ List<String> parentPath) {\r
+ List<String> pathToNode = new ArrayList<String>(parentPath);\r
+\r
+ ListSchemaNodeBuilder listBuilder = new ListSchemaNodeBuilder(listName);\r
+\r
+ ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes\r
+ .get(pathToNode);\r
+ if (parent != null) {\r
+ if(parent instanceof AugmentationSchemaBuilder) {\r
+ listBuilder.setAugmenting(true);\r
+ }\r
+ parent.addChildNode(listBuilder);\r
+ }\r
+\r
+ pathToNode.add(listName.getLocalName());\r
+ moduleNodes.put(pathToNode, listBuilder);\r
+ addedChilds.put(pathToNode, listBuilder);\r
+\r
+ return listBuilder;\r
+ }\r
+\r
+ public LeafSchemaNodeBuilder addLeafNode(QName leafName,\r
+ List<String> parentPath) {\r
+ List<String> pathToNode = new ArrayList<String>(parentPath);\r
+\r
+ LeafSchemaNodeBuilder leafBuilder = new LeafSchemaNodeBuilder(leafName);\r
+\r
+ ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes.get(pathToNode);\r
+ if (parent != null) {\r
+ if(parent instanceof AugmentationSchemaBuilder) {\r
+ leafBuilder.setAugmenting(true);\r
+ }\r
+ parent.addChildNode(leafBuilder);\r
+ }\r
+\r
+ pathToNode.add(leafName.getLocalName());\r
+ addedChilds.put(pathToNode, leafBuilder);\r
+ moduleNodes.put(pathToNode, leafBuilder);\r
+\r
+ return leafBuilder;\r
+ }\r
+\r
+ public LeafListSchemaNodeBuilder addLeafListNode(QName leafListName,\r
+ List<String> parentPath) {\r
+ List<String> pathToNode = new ArrayList<String>(parentPath);\r
+\r
+ LeafListSchemaNodeBuilder leafListBuilder = new LeafListSchemaNodeBuilder(\r
+ leafListName);\r
+ ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes.get(pathToNode);\r
+ if (parent != null) {\r
+ if(parent instanceof AugmentationSchemaBuilder) {\r
+ leafListBuilder.setAugmenting(true);\r
+ }\r
+ parent.addChildNode(leafListBuilder);\r
+ }\r
+\r
+ pathToNode.add(leafListName.getLocalName());\r
+ addedChilds.put(pathToNode, leafListBuilder);\r
+ moduleNodes.put(pathToNode, leafListBuilder);\r
+\r
+ return leafListBuilder;\r
+ }\r
+\r
+ public GroupingBuilder addGrouping(QName qname, List<String> parentPath) {\r
+ List<String> pathToGroup = new ArrayList<String>(parentPath);\r
+\r
+ GroupingBuilder builder = new GroupingBuilderImpl(qname);\r
+ ChildNodeBuilder parentNodeBuilder = (ChildNodeBuilder) moduleNodes.get(pathToGroup);\r
+ if (parentNodeBuilder != null) {\r
+ parentNodeBuilder.addGrouping(builder);\r
+ }\r
+\r
+ pathToGroup.add(qname.getLocalName());\r
+ moduleNodes.put(pathToGroup, builder);\r
+ addedGroupings.put(pathToGroup, builder);\r
+\r
+ return builder;\r
+ }\r
+\r
+ public AugmentationSchemaBuilder addAugment(String name,\r
+ List<String> parentPath) {\r
+ List<String> pathToAugment = new ArrayList<String>(parentPath);\r
+\r
+ AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name);\r
+\r
+ // augment can only be in 'module' or 'uses' statement\r
+ UsesNodeBuilder parent = addedUsesNodes.get(pathToAugment);\r
+ if (parent != null) {\r
+ parent.addAugment(builder);\r
+ }\r
+\r
+ pathToAugment.add(name);\r
+ moduleNodes.put(pathToAugment, builder);\r
+ addedAugments.add(builder);\r
+\r
+ return builder;\r
+ }\r
+\r
+ public UsesNodeBuilder addUsesNode(String groupingPathStr,\r
+ List<String> parentPath) {\r
+ List<String> pathToUses = new ArrayList<String>(parentPath);\r
+\r
+ UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(groupingPathStr);\r
+\r
+ ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes.get(pathToUses);\r
+ if (parent != null) {\r
+ if(parent instanceof AugmentationSchemaBuilder) {\r
+ usesBuilder.setAugmenting(true);\r
+ }\r
+ parent.addUsesNode(usesBuilder);\r
+ }\r
+\r
+ pathToUses.add(groupingPathStr);\r
+ addedUsesNodes.put(pathToUses, usesBuilder);\r
+\r
+ return usesBuilder;\r
+ }\r
+\r
+ public RpcDefinitionBuilder addRpc(QName qname, List<String> parentPath) {\r
+ List<String> pathToRpc = new ArrayList<String>(parentPath);\r
+\r
+ RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(qname);\r
+\r
+ pathToRpc.add(qname.getLocalName());\r
+ addedRpcs.put(pathToRpc, rpcBuilder);\r
+\r
+ QName inputQName = new QName(qname.getNamespace(), qname.getRevision(),\r
+ qname.getPrefix(), "input");\r
+ ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(inputQName);\r
+ List<String> pathToInput = new ArrayList<String>(pathToRpc);\r
+ pathToInput.add("input");\r
+ moduleNodes.put(pathToInput, inputBuilder);\r
+ rpcBuilder.setInput(inputBuilder);\r
+\r
+ QName outputQName = new QName(qname.getNamespace(),\r
+ qname.getRevision(), qname.getPrefix(), "output");\r
+ ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(outputQName);\r
+ List<String> pathToOutput = new ArrayList<String>(pathToRpc);\r
+ pathToOutput.add("output");\r
+ moduleNodes.put(pathToOutput, outputBuilder);\r
+ rpcBuilder.setOutput(outputBuilder);\r
+\r
+ return rpcBuilder;\r
+ }\r
+\r
+ public NotificationBuilder addNotification(QName notificationName,\r
+ List<String> parentPath) {\r
+ List<String> pathToNotification = new ArrayList<String>(parentPath);\r
+\r
+ NotificationBuilder notificationBuilder = new NotificationBuilder(\r
+ notificationName);\r
+\r
+ pathToNotification.add(notificationName.getLocalName());\r
+ moduleNodes.put(pathToNotification, notificationBuilder);\r
+ addedNotifications.add(notificationBuilder);\r
+\r
+ return notificationBuilder;\r
+ }\r
+\r
+ public FeatureBuilder addFeature(QName featureName, List<String> parentPath) {\r
+ List<String> pathToFeature = new ArrayList<String>(parentPath);\r
+ pathToFeature.add(featureName.getLocalName());\r
+\r
+ FeatureBuilder builder = new FeatureBuilder(featureName);\r
+ addedFeatures.put(pathToFeature, builder);\r
+ return builder;\r
+ }\r
+\r
+ public TypedefBuilder addTypedef(QName typeDefName, List<String> parentPath) {\r
+ List<String> pathToType = new ArrayList<String>(parentPath);\r
+ TypedefBuilder builder = new TypedefBuilder(typeDefName);\r
+ TypeDefinitionAwareBuilder parent = (TypeDefinitionAwareBuilder) moduleNodes.get(pathToType);\r
+ if (parent != null) {\r
+ parent.addTypedef(builder);\r
+ }\r
+ pathToType.add(typeDefName.getLocalName());\r
+ addedTypedefs.put(pathToType, builder);\r
+ moduleNodes.put(pathToType, builder);\r
+ return builder;\r
+ }\r
+\r
+ public Set<TypeDefinitionBuilder> getModuleTypedefs() {\r
+ Set<TypeDefinitionBuilder> typedefs = new HashSet<TypeDefinitionBuilder>();\r
+ for (Map.Entry<List<String>, TypeDefinitionBuilder> entry : addedTypedefs.entrySet()) {\r
+ if (entry.getKey().size() == 2) {\r
+ typedefs.add(entry.getValue());\r
+ }\r
+ }\r
+ return typedefs;\r
+ }\r
+\r
+ public void setType(TypeDefinition<?> type, List<String> parentPath) {\r
+ TypeAwareBuilder parent = (TypeAwareBuilder) moduleNodes.get(parentPath);\r
+ parent.setType(type);\r
+ }\r
+\r
+ public void addUnionType(List<String> parentPath) {\r
+ TypeAwareBuilder parent = (TypeAwareBuilder) moduleNodes.get(parentPath);\r
+ UnionTypeBuilder union = new UnionTypeBuilder();\r
+ parent.setType(union.build());\r
+\r
+ List<String> path = new ArrayList<String>(parentPath);\r
+ path.add("union");\r
+\r
+ unionTypes.put(path, union);\r
+ moduleNodes.put(path, union);\r
+ }\r
+\r
+ public DeviationBuilder addDeviation(String targetPath) {\r
+ DeviationBuilder builder = new DeviationBuilder(targetPath);\r
+ addedDeviations.put(targetPath, builder);\r
+ return builder;\r
+ }\r
+\r
+ public void addConfiguration(boolean configuration, List<String> parentPath) {\r
+ Builder builder = moduleNodes.get(parentPath);\r
+ if (builder instanceof DeviationBuilder) {\r
+ // skip\r
+ // TODO\r
+ } else {\r
+ DataSchemaNodeBuilder configBuilder = (DataSchemaNodeBuilder) moduleNodes.get(parentPath);\r
+ configBuilder.setConfiguration(configuration);\r
+ }\r
+ }\r
+\r
+ public UnknownSchemaNodeBuilder addUnknownSchemaNode(QName qname, List<String> parentPath) {\r
+ UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(qname);\r
+ return builder;\r
+ }\r
+\r
+\r
+ private class ModuleImpl implements Module {\r
+ private URI namespace;\r
+ private final String name;\r
+ private Date revision;\r
+ private String prefix;\r
+ private String yangVersion;\r
+ private String description;\r
+ private String reference;\r
+ private String organization;\r
+ private String contact;\r
+ private Set<ModuleImport> imports = Collections.emptySet();\r
+ private Set<FeatureDefinition> features = Collections.emptySet();\r
+ private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();\r
+ private Set<NotificationDefinition> notifications = Collections.emptySet();\r
+ private Set<AugmentationSchema> augmentations = Collections.emptySet();\r
+ private Set<RpcDefinition> rpcs = Collections.emptySet();\r
+ private Set<Deviation> deviations = Collections.emptySet();\r
+ private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();\r
+ private Set<GroupingDefinition> groupings = Collections.emptySet();\r
+ private Set<UsesNode> uses = Collections.emptySet();\r
+ private List<ExtensionDefinition> extensionSchemaNodes = Collections.emptyList();\r
+\r
+ private ModuleImpl(String name) {\r
+ this.name = name;\r
+ }\r
+\r
+ @Override\r
+ public URI getNamespace() {\r
+ return namespace;\r
+ }\r
+\r
+ private void setNamespace(URI namespace) {\r
+ this.namespace = namespace;\r
+ }\r
+\r
+ @Override\r
+ public String getName() {\r
+ return name;\r
+ }\r
+\r
+ @Override\r
+ public Date getRevision() {\r
+ return revision;\r
+ }\r
+\r
+ private void setRevision(Date revision) {\r
+ this.revision = revision;\r
+ }\r
+\r
+ @Override\r
+ public String getPrefix() {\r
+ return prefix;\r
+ }\r
+\r
+ private void setPrefix(String prefix) {\r
+ this.prefix = prefix;\r
+ }\r
+\r
+ @Override\r
+ public String getYangVersion() {\r
+ return yangVersion;\r
+ }\r
+\r
+ private void setYangVersion(String yangVersion) {\r
+ this.yangVersion = yangVersion;\r
+ }\r
+\r
+ @Override\r
+ public String getDescription() {\r
+ return description;\r
+ }\r
+\r
+ private void setDescription(String description) {\r
+ this.description = description;\r
+ }\r
+\r
+ @Override\r
+ public String getReference() {\r
+ return reference;\r
+ }\r
+\r
+ private void setReference(String reference) {\r
+ this.reference = reference;\r
+ }\r
+\r
+ @Override\r
+ public String getOrganization() {\r
+ return organization;\r
+ }\r
+\r
+ private void setOrganization(String organization) {\r
+ this.organization = organization;\r
+ }\r
+\r
+ @Override\r
+ public String getContact() {\r
+ return contact;\r
+ }\r
+\r
+ private void setContact(String contact) {\r
+ this.contact = contact;\r
+ }\r
+\r
+ @Override\r
+ public Set<ModuleImport> getImports() {\r
+ return imports;\r
+ }\r
+\r
+ private void setImports(Set<ModuleImport> imports) {\r
+ if(imports != null) {\r
+ this.imports = imports;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Set<FeatureDefinition> getFeatures() {\r
+ return features;\r
+ }\r
+\r
+ private void setFeatures(Set<FeatureDefinition> features) {\r
+ if(features != null) {\r
+ this.features = features;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Set<TypeDefinition<?>> getTypeDefinitions() {\r
+ return typeDefinitions;\r
+ }\r
+\r
+ private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {\r
+ if(typeDefinitions != null) {\r
+ this.typeDefinitions = typeDefinitions;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Set<NotificationDefinition> getNotifications() {\r
+ return notifications;\r
+ }\r
+\r
+ private void setNotifications(Set<NotificationDefinition> notifications) {\r
+ if(notifications != null) {\r
+ this.notifications = notifications;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Set<AugmentationSchema> getAugmentations() {\r
+ return augmentations;\r
+ }\r
+\r
+ private void setAugmentations(Set<AugmentationSchema> augmentations) {\r
+ if(augmentations != null) {\r
+ this.augmentations = augmentations;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Set<RpcDefinition> getRpcs() {\r
+ return rpcs;\r
+ }\r
+\r
+ private void setRpcs(Set<RpcDefinition> rpcs) {\r
+ if(rpcs != null) {\r
+ this.rpcs = rpcs;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Set<Deviation> getDeviations() {\r
+ return deviations;\r
+ }\r
+\r
+ private void setDeviations(Set<Deviation> deviations) {\r
+ if(deviations != null) {\r
+ this.deviations = deviations;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Set<DataSchemaNode> getChildNodes() {\r
+ return new HashSet<DataSchemaNode>(childNodes.values());\r
+ }\r
+\r
+ private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {\r
+ if(childNodes != null) {\r
+ this.childNodes = childNodes;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Set<GroupingDefinition> getGroupings() {\r
+ return groupings;\r
+ }\r
+\r
+ private void setGroupings(Set<GroupingDefinition> groupings) {\r
+ if(groupings != null) {\r
+ this.groupings = groupings;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Set<UsesNode> getUses() {\r
+ return uses;\r
+ }\r
+\r
+ private void setUses(Set<UsesNode> uses) {\r
+ if(uses != null) {\r
+ this.uses = uses;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public List<ExtensionDefinition> getExtensionSchemaNodes() {\r
+ return extensionSchemaNodes;\r
+ }\r
+\r
+ private void setExtensionSchemaNodes(List<ExtensionDefinition> extensionSchemaNodes) {\r
+ if(extensionSchemaNodes != null) {\r
+ this.extensionSchemaNodes = extensionSchemaNodes;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public DataSchemaNode getDataChildByName(QName name) {\r
+ return childNodes.get(name);\r
+ }\r
+\r
+ @Override\r
+ public DataSchemaNode getDataChildByName(String name) {\r
+ DataSchemaNode result = null;\r
+ for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {\r
+ if (entry.getKey().getLocalName().equals(name)) {\r
+ result = entry.getValue();\r
+ break;\r
+ }\r
+ }\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ final int prime = 31;\r
+ int result = 1;\r
+ result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());\r
+ result = prime * result + ((name == null) ? 0 : name.hashCode());\r
+ result = prime * result + ((revision == null) ? 0 : revision.hashCode());\r
+ result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());\r
+ result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (this == obj) {\r
+ return true;\r
+ }\r
+ if (obj == null) {\r
+ return false;\r
+ }\r
+ if (getClass() != obj.getClass()) {\r
+ return false;\r
+ }\r
+ ModuleImpl other = (ModuleImpl) obj;\r
+ if (namespace == null) {\r
+ if (other.namespace != null) {\r
+ return false;\r
+ }\r
+ } else if (!namespace.equals(other.namespace)) {\r
+ return false;\r
+ }\r
+ if (name == null) {\r
+ if (other.name != null) {\r
+ return false;\r
+ }\r
+ } else if (!name.equals(other.name)) {\r
+ return false;\r
+ }\r
+ if (revision == null) {\r
+ if (other.revision != null) {\r
+ return false;\r
+ }\r
+ } else if (!revision.equals(other.revision)) {\r
+ return false;\r
+ }\r
+ if (prefix == null) {\r
+ if (other.prefix != null) {\r
+ return false;\r
+ }\r
+ } else if (!prefix.equals(other.prefix)) {\r
+ return false;\r
+ }\r
+ if (yangVersion == null) {\r
+ if (other.yangVersion != null) {\r
+ return false;\r
+ }\r
+ } else if (!yangVersion.equals(other.yangVersion)) {\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ StringBuilder sb = new StringBuilder(\r
+ ModuleImpl.class.getSimpleName());\r
+ sb.append("[\n");\r
+ sb.append("name=" + name + ",\n");\r
+ sb.append("namespace=" + namespace + ",\n");\r
+ sb.append("revision=" + revision + ",\n");\r
+ sb.append("prefix=" + prefix + ",\n");\r
+ sb.append("yangVersion=" + yangVersion + ",\n");\r
+ sb.append("description=" + description + ",\n");\r
+ sb.append("reference=" + reference + ",\n");\r
+ sb.append("organization=" + organization + ",\n");\r
+ sb.append("contact=" + contact + ",\n");\r
+ sb.append("childNodes=" + childNodes.values() + ",\n");\r
+ sb.append("groupings=" + groupings + ",\n");\r
+ sb.append("imports=" + imports + ",\n");\r
+ sb.append("features=" + features + ",\n");\r
+ sb.append("typeDefinitions=" + typeDefinitions + ",\n");\r
+ sb.append("notifications=" + notifications + ",\n");\r
+ sb.append("augmentations=" + augmentations + ",\n");\r
+ sb.append("rpcs=" + rpcs + ",\n");\r
+ sb.append("deviations=" + deviations + "\n");\r
+ sb.append("uses=" + uses + "\n");\r
+ sb.append("]");\r
+ return sb.toString();\r
+ }\r
+ }\r
+\r
+ private ModuleImport createModuleImport(final String moduleName,\r
+ final Date revision, final String prefix) {\r
+ ModuleImport moduleImport = new ModuleImport() {\r
+ @Override\r
+ public String getModuleName() {\r
+ return moduleName;\r
+ }\r
+\r
+ @Override\r
+ public Date getRevision() {\r
+ return revision;\r
+ }\r
+\r
+ @Override\r
+ public String getPrefix() {\r
+ return prefix;\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ final int prime = 31;\r
+ int result = 1;\r
+ result = prime * result\r
+ + ((moduleName == null) ? 0 : moduleName.hashCode());\r
+ result = prime * result\r
+ + ((revision == null) ? 0 : revision.hashCode());\r
+ result = prime * result\r
+ + ((prefix == null) ? 0 : prefix.hashCode());\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (this == obj) {\r
+ return true;\r
+ }\r
+ if (obj == null) {\r
+ return false;\r
+ }\r
+ if (getClass() != obj.getClass()) {\r
+ return false;\r
+ }\r
+ ModuleImport other = (ModuleImport) obj;\r
+ if (getModuleName() == null) {\r
+ if (other.getModuleName() != null) {\r
+ return false;\r
+ }\r
+ } else if (!getModuleName().equals(other.getModuleName())) {\r
+ return false;\r
+ }\r
+ if (getRevision() == null) {\r
+ if (other.getRevision() != null) {\r
+ return false;\r
+ }\r
+ } else if (!getRevision().equals(other.getRevision())) {\r
+ return false;\r
+ }\r
+ if (getPrefix() == null) {\r
+ if (other.getPrefix() != null) {\r
+ return false;\r
+ }\r
+ } else if (!getPrefix().equals(other.getPrefix())) {\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ return "ModuleImport[moduleName=" + moduleName + ", revision="\r
+ + revision + ", prefix=" + prefix + "]";\r
+ }\r
+ };\r
+ return moduleImport;\r
+ }\r
+\r
+ /**\r
+ * Traverse through given addedChilds and add only direct module childs.\r
+ * Direct module child path size is 2 (1. module name, 2. child name).\r
+ *\r
+ * @param addedChilds\r
+ * @return map of children, where key is child QName and value is child\r
+ * itself\r
+ */\r
+ private Map<QName, DataSchemaNode> buildModuleChildNodes(\r
+ Map<List<String>, DataSchemaNodeBuilder> addedChilds) {\r
+ final Map<QName, DataSchemaNode> childNodes = new HashMap<QName, DataSchemaNode>();\r
+ for (Map.Entry<List<String>, DataSchemaNodeBuilder> entry : addedChilds\r
+ .entrySet()) {\r
+ if (entry.getKey().size() == 2) {\r
+ DataSchemaNode node = entry.getValue().build();\r
+ QName qname = entry.getValue().getQName();\r
+ childNodes.put(qname, node);\r
+ }\r
+ }\r
+ return childNodes;\r
+ }\r
+\r
+ /**\r
+ * Traverse through given addedGroupings and add only direct module\r
+ * groupings. Direct module grouping path size is 2 (1. module name, 2.\r
+ * grouping name).\r
+ *\r
+ * @param addedGroupings\r
+ * @return set of built GroupingDefinition objects\r
+ */\r
+ private Set<GroupingDefinition> buildModuleGroupings(\r
+ Map<List<String>, GroupingBuilder> addedGroupings) {\r
+ final Set<GroupingDefinition> groupings = new HashSet<GroupingDefinition>();\r
+ for (Map.Entry<List<String>, GroupingBuilder> entry : addedGroupings\r
+ .entrySet()) {\r
+ if (entry.getKey().size() == 2) {\r
+ groupings.add(entry.getValue().build());\r
+ }\r
+ }\r
+ return groupings;\r
+ }\r
+\r
+ /**\r
+ * Traverse through given addedRpcs and build RpcDefinition objects.\r
+ *\r
+ * @param addedRpcs\r
+ * @return set of built RpcDefinition objects\r
+ */\r
+ private Set<RpcDefinition> buildModuleRpcs(\r
+ Map<List<String>, RpcDefinitionBuilder> addedRpcs) {\r
+ final Set<RpcDefinition> rpcs = new HashSet<RpcDefinition>();\r
+ RpcDefinitionBuilder builder;\r
+ for (Map.Entry<List<String>, RpcDefinitionBuilder> entry : addedRpcs\r
+ .entrySet()) {\r
+ builder = entry.getValue();\r
+ RpcDefinition rpc = builder.build();\r
+ rpcs.add(rpc);\r
+ }\r
+ return rpcs;\r
+ }\r
+\r
+ /**\r
+ * Traverse through given addedTypedefs and add only direct module typedef\r
+ * statements. Direct module typedef path size is 2 (1. module name, 2.\r
+ * typedef name).\r
+ *\r
+ * @param addedTypedefs\r
+ * @return set of built module typedef statements\r
+ */\r
+ private Set<TypeDefinition<?>> buildModuleTypedefs(\r
+ Map<List<String>, TypeDefinitionBuilder> addedTypedefs) {\r
+ Set<TypeDefinition<?>> typedefs = new HashSet<TypeDefinition<?>>();\r
+ for (Map.Entry<List<String>, TypeDefinitionBuilder> entry : addedTypedefs\r
+ .entrySet()) {\r
+ if (entry.getKey().size() == 2) {\r
+ TypeDefinition<? extends TypeDefinition<?>> node = entry\r
+ .getValue().build();\r
+ typedefs.add(node);\r
+ }\r
+ }\r
+ return typedefs;\r
+ }\r
+\r
+ /**\r
+ * Traverse through given addedUsesNodes and add only direct module uses\r
+ * nodes. Direct module uses node path size is 2 (1. module name, 2. uses\r
+ * name).\r
+ *\r
+ * @param addedUsesNodes\r
+ * @return set of built module uses nodes\r
+ */\r
+ private Set<UsesNode> buildUsesNodes(\r
+ Map<List<String>, UsesNodeBuilder> addedUsesNodes) {\r
+ final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();\r
+ for (Map.Entry<List<String>, UsesNodeBuilder> entry : addedUsesNodes\r
+ .entrySet()) {\r
+ if (entry.getKey().size() == 2) {\r
+ usesNodeDefinitions.add(entry.getValue().build());\r
+ }\r
+ }\r
+ return usesNodeDefinitions;\r
+ }\r
+\r
+ /**\r
+ * Traverse through given addedFeatures and add only direct module features.\r
+ * Direct module feature path size is 2 (1. module name, 2. feature name).\r
+ *\r
+ * @param addedFeatures\r
+ * @return set of built module features\r
+ */\r
+ private Set<FeatureDefinition> buildModuleFeatures(\r
+ Map<List<String>, FeatureBuilder> addedFeatures) {\r
+ Set<FeatureDefinition> features = new HashSet<FeatureDefinition>();\r
+ for (Map.Entry<List<String>, FeatureBuilder> entry : addedFeatures\r
+ .entrySet()) {\r
+ if (entry.getKey().size() == 2) {\r
+ features.add(entry.getValue().build());\r
+ }\r
+ }\r
+ return features;\r
+ }\r
+\r
+}\r