X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fimpl%2FSchemaContextImpl.java;h=652011a2250d681bc0e81b22650dd143a93a021d;hb=3d283ec6184505ad5e7eefb173044ff383222e9f;hp=6f81e7adaf66bfa951ccda2a25f2ab4eec886ea8;hpb=3583a98815919107c1c23025957e94b31efcbe30;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/SchemaContextImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/SchemaContextImpl.java index 6f81e7adaf..652011a225 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/SchemaContextImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/SchemaContextImpl.java @@ -7,270 +7,88 @@ */ package org.opendaylight.yangtools.yang.parser.impl; -import com.google.common.base.Optional; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; -import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition; -import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; -import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition; -import org.opendaylight.yangtools.yang.model.api.GroupingDefinition; -import org.opendaylight.yangtools.yang.model.api.Module; -import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier; -import org.opendaylight.yangtools.yang.model.api.NotificationDefinition; -import org.opendaylight.yangtools.yang.model.api.RpcDefinition; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.Status; -import org.opendaylight.yangtools.yang.model.api.TypeDefinition; -import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; -import org.opendaylight.yangtools.yang.model.api.UsesNode; -import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort; - +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableSetMultimap; +import com.google.common.collect.Multimaps; +import com.google.common.collect.SetMultimap; 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.List; +import java.util.Collection; import java.util.Map; import java.util.Set; import java.util.TreeMap; +import javax.annotation.concurrent.Immutable; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier; +import org.opendaylight.yangtools.yang.model.util.AbstractSchemaContext; +import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort; -final class SchemaContextImpl implements SchemaContext { - private final Set modules; - private final Map identifiersToSources; - - SchemaContextImpl(final Set modules, Map identifiersToSources) { - this.modules = modules; - this.identifiersToSources = identifiersToSources; - } - - @Override - public Set getDataDefinitions() { - final Set dataDefs = new HashSet(); - for (Module m : modules) { - dataDefs.addAll(m.getChildNodes()); - } - return dataDefs; - } - - @Override - public Set getModules() { - List sorted = ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])); - return new LinkedHashSet(sorted); - } - - @Override - public Set getNotifications() { - final Set notifications = new HashSet(); - for (Module m : modules) { - notifications.addAll(m.getNotifications()); - } - return notifications; - } - - @Override - public Set getOperations() { - final Set rpcs = new HashSet(); - for (Module m : modules) { - rpcs.addAll(m.getRpcs()); - } - return rpcs; - } +@Immutable +final class SchemaContextImpl extends AbstractSchemaContext { + + private final Map identifiersToSources; + private final SetMultimap namespaceToModules; + private final SetMultimap nameToModules; + private final Set modules; + + SchemaContextImpl(final Set modules, final Map identifiersToSources) { + this.identifiersToSources = ImmutableMap.copyOf(identifiersToSources); + + /* + * Instead of doing this on each invocation of getModules(), pre-compute + * it once and keep it around -- better than the set we got in. + */ + this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()]))); + + /* + * The most common lookup is from Namespace->Module. + * + * RESTCONF performs lookups based on module name only, where it wants + * to receive the latest revision + * + * Invest some quality time in building up lookup tables for both. + */ + final SetMultimap nsMap = Multimaps.newSetMultimap( + new TreeMap>(), MODULE_SET_SUPPLIER); + final SetMultimap nameMap = Multimaps.newSetMultimap( + new TreeMap>(), MODULE_SET_SUPPLIER); - @Override - public Set getExtensions() { - final Set extensions = new HashSet(); for (Module m : modules) { - extensions.addAll(m.getExtensionSchemaNodes()); + nameMap.put(m.getName(), m); + nsMap.put(m.getNamespace(), m); } - return extensions; - } - - @Override - public Module findModuleByName(final String name, final Date revision) { - if (name != null) { - for (final Module module : modules) { - if (revision == null) { - if (module.getName().equals(name)) { - return module; - } - } else if (module.getName().equals(name) && module.getRevision().equals(revision)) { - return module; - } - } - } - return null; - } - - @Override - public Set findModuleByNamespace(final URI namespace) { - final Set ret = new HashSet(); - if (namespace != null) { - for (final Module module : modules) { - if (module.getNamespace().equals(namespace)) { - ret.add(module); - } - } - } - return ret; - } - - @Override - public Module findModuleByNamespaceAndRevision(URI namespace, Date revision) { - if (namespace != null) { - Set modules = findModuleByNamespace(namespace); - - if (revision == null) { - TreeMap map = new TreeMap(); - for (Module module : modules) { - map.put(module.getRevision(), module); - } - if (map.isEmpty()) { - return null; - } - return map.lastEntry().getValue(); - } else { - for (Module module : modules) { - if (module.getRevision().equals(revision)) { - return(module); - } - } - } - } - return null; - } - - @Override - public boolean isAugmenting() { - return false; - } - @Override - public boolean isAddedByUses() { - return false; + namespaceToModules = ImmutableSetMultimap.copyOf(nsMap); + nameToModules = ImmutableSetMultimap.copyOf(nameMap); } @Override - public boolean isConfiguration() { - return false; - } + protected Map getIdentifiersToSources(){ - @Override - public ConstraintDefinition getConstraints() { - return null; - } - - @Override - public QName getQName() { - return SchemaContext.NAME; - } - - @Override - public SchemaPath getPath() { - return null; + return identifiersToSources; } @Override - public String getDescription() { - return null; - } + public Set getModules(){ - @Override - public String getReference() { - return null; + return modules; } @Override - public Status getStatus() { - return Status.CURRENT; - } + protected SetMultimap getNamespaceToModules() { - @Override - public List getUnknownSchemaNodes() { - final List result = new ArrayList<>(); - for (Module module : modules) { - result.addAll(module.getUnknownSchemaNodes()); - } - return Collections.unmodifiableList(result); + return namespaceToModules; } @Override - public Set> getTypeDefinitions() { - final Set> result = new LinkedHashSet<>(); - for (Module module : modules) { - result.addAll(module.getTypeDefinitions()); - } - return Collections.unmodifiableSet(result); - } + protected SetMultimap getNameToModules() { - @Override - public Set getChildNodes() { - final Set result = new LinkedHashSet<>(); - for (Module module : modules) { - result.addAll(module.getChildNodes()); - } - return Collections.unmodifiableSet(result); + return nameToModules; } @Override - public Set getGroupings() { - final Set result = new LinkedHashSet<>(); - for (Module module : modules) { - result.addAll(module.getGroupings()); - } - return Collections.unmodifiableSet(result); - } + public String toString() { - @Override - public DataSchemaNode getDataChildByName(QName name) { - DataSchemaNode result = null; - for (Module module : modules) { - result = module.getDataChildByName(name); - if (result != null) { - break; - } - } - return result; - } - - @Override - public DataSchemaNode getDataChildByName(String name) { - DataSchemaNode result = null; - for (Module module : modules) { - result = module.getDataChildByName(name); - if (result != null) { - break; - } - } - return result; - } - - @Override - public Set getUses() { - return Collections.emptySet(); - } - - @Override - public boolean isPresenceContainer() { - return false; - } - - @Override - public Set getAvailableAugmentations() { - return Collections.emptySet(); - } - - //FIXME: should work for submodules too - @Override - public Set getAllModuleIdentifiers() { - return identifiersToSources.keySet(); - } - - @Override - public Optional getModuleSource(ModuleIdentifier moduleIdentifier) { - String maybeSource = identifiersToSources.get(moduleIdentifier); - return Optional.fromNullable(maybeSource); + return String.format("SchemaContextImpl{modules=%s}", modules); } }