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=319f189043f4743daedc326834caeea4c2e12d1d;hb=6b512ccc24393302a472d89d55a71fcda40dd81b;hp=459ea32cdd4a1ca91ceaf698a1902ca1f1219a8f;hpb=9a591c38809720f0edc438f1f6bab857d1bef9a2;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 459ea32cdd..319f189043 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,10 +7,19 @@ */ package org.opendaylight.yangtools.yang.parser.impl; +import com.google.common.base.Optional; +import com.google.common.base.Supplier; +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.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.Date; import java.util.HashSet; import java.util.LinkedHashSet; @@ -18,6 +27,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; +import java.util.TreeSet; import javax.annotation.concurrent.Immutable; @@ -39,26 +49,30 @@ 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.base.Optional; -import com.google.common.base.Supplier; -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; - @Immutable final class SchemaContextImpl implements SchemaContext { - private static final Supplier> URI_SET_SUPPLIER = new Supplier>() { + private static final Comparator REVISION_COMPARATOR = new Comparator() { + @Override + public int compare(final Module o1, final Module o2) { + if (o2.getRevision() == null) { + return -1; + } + + return o2.getRevision().compareTo(o1.getRevision()); + } + }; + + private static final Supplier> MODULE_SET_SUPPLIER = new Supplier>() { @Override - public HashSet get() { - return new HashSet<>(); + public TreeSet get() { + return new TreeSet<>(REVISION_COMPARATOR); } }; - private final ImmutableMap identifiersToSources; - private final ImmutableSetMultimap namespaceToModules; - private final ImmutableSet modules; + 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); @@ -70,16 +84,24 @@ final class SchemaContextImpl implements SchemaContext { this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()]))); /* - * The most common lookup is from Namespace->Module. Invest some quality time in - * building that up. + * 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 multimap = Multimaps.newSetMultimap( - new TreeMap>(), URI_SET_SUPPLIER); + final SetMultimap nsMap = Multimaps.newSetMultimap( + new TreeMap>(), MODULE_SET_SUPPLIER); + final SetMultimap nameMap = Multimaps.newSetMultimap( + new TreeMap>(), MODULE_SET_SUPPLIER); for (Module m : modules) { - multimap.put(m.getNamespace(), m); + nameMap.put(m.getName(), m); + nsMap.put(m.getNamespace(), m); } - namespaceToModules = ImmutableSetMultimap.copyOf(multimap); + namespaceToModules = ImmutableSetMultimap.copyOf(nsMap); + nameToModules = ImmutableSetMultimap.copyOf(nameMap); } @Override @@ -125,17 +147,12 @@ final class SchemaContextImpl implements SchemaContext { @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; - } + for (final Module module : nameToModules.get(name)) { + if (revision == null || revision.equals(module.getRevision())) { + return module; } } + return null; } @@ -150,26 +167,9 @@ final class SchemaContextImpl implements SchemaContext { if (namespace == null) { return null; } - final Set modules = findModuleByNamespace(namespace); - if (modules.isEmpty()) { - return null; - } - - if (revision == null) { - // FIXME: The ordering of modules in Multimap could just guarantee this... - 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); - } + for (Module module : findModuleByNamespace(namespace)) { + if (revision == null || revision.equals(module.getRevision())) { + return module; } } return null; @@ -258,26 +258,24 @@ final class SchemaContextImpl implements SchemaContext { @Override public DataSchemaNode getDataChildByName(final QName name) { - DataSchemaNode result = null; for (Module module : modules) { - result = module.getDataChildByName(name); + final DataSchemaNode result = module.getDataChildByName(name); if (result != null) { - break; + return result; } } - return result; + return null; } @Override public DataSchemaNode getDataChildByName(final String name) { - DataSchemaNode result = null; for (Module module : modules) { - result = module.getDataChildByName(name); + final DataSchemaNode result = module.getDataChildByName(name); if (result != null) { - break; + return result; } } - return result; + return null; } @Override