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%2Fstmt%2Frfc6020%2Feffective%2FEffectiveSchemaContext.java;h=c76b86f82809415f99e632ddbc5798d118c2c9a0;hb=e99bf7da4cf4f715e6d899a8c41a8df2853e3055;hp=4cedda12fa6c55db907185ad5b05d864776a86d3;hpb=c4dc5b33e7d24670b59cc81b65e15b37a3268608;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveSchemaContext.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveSchemaContext.java index 4cedda12fa..c76b86f828 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveSchemaContext.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveSchemaContext.java @@ -7,105 +7,146 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; -import java.util.HashSet; +import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; -import java.util.List; -import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement; -import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +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.Collection; +import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement; +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl; +import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort; -public class EffectiveSchemaContext extends AbstractEffectiveSchemaContext { +public final class EffectiveSchemaContext extends AbstractEffectiveSchemaContext { - private final Map identifiersToSources; private final SetMultimap namespaceToModules; private final SetMultimap nameToModules; private final Set modules; - private final ImmutableList> rootDeclaredStatements; - private final ImmutableList> rootEffectiveStatements; + private final List> rootDeclaredStatements; + private final List> rootEffectiveStatements; + private final Set moduleIdentifiers; - public EffectiveSchemaContext( - List> rootDeclaredStatements, - List> rootEffectiveStatements) { - this.rootDeclaredStatements = ImmutableList - .copyOf(rootDeclaredStatements); - this.rootEffectiveStatements = ImmutableList - .copyOf(rootEffectiveStatements); + public EffectiveSchemaContext(final List> rootDeclaredStatements, + final List> rootEffectiveStatements) { + this.rootDeclaredStatements = ImmutableList.copyOf(rootDeclaredStatements); + this.rootEffectiveStatements = ImmutableList.copyOf(rootEffectiveStatements); Set modulesInit = new HashSet<>(); for (EffectiveStatement rootEffectiveStatement : rootEffectiveStatements) { - if (rootEffectiveStatement instanceof Module) { + if (rootEffectiveStatement instanceof ModuleEffectiveStatementImpl) { Module module = (Module) rootEffectiveStatement; modulesInit.add(module); } } - this.modules = ImmutableSet.copyOf(modulesInit); + + Module[] moduleArray = new Module[modulesInit.size()]; + List sortedModuleList = ModuleDependencySort.sort(modulesInit.toArray(moduleArray)); + this.modules = ImmutableSet.copyOf(sortedModuleList); final SetMultimap nsMap = Multimaps.newSetMultimap( new TreeMap>(), MODULE_SET_SUPPLIER); final SetMultimap nameMap = Multimaps.newSetMultimap( new TreeMap>(), MODULE_SET_SUPPLIER); - + Set modIdBuilder = new HashSet<>(); for (Module m : modulesInit) { nameMap.put(m.getName(), m); nsMap.put(m.getNamespace(), m); + modIdBuilder.add(new ModuleIdentifierImpl(m.getName(), Optional.of(m.getNamespace()), Optional.of(m.getRevision()))); + } + + namespaceToModules = ImmutableSetMultimap.copyOf(nsMap); + nameToModules = ImmutableSetMultimap.copyOf(nameMap); + moduleIdentifiers = ImmutableSet.copyOf(modIdBuilder); + } + + public EffectiveSchemaContext(final Set modules) { + + /* + * 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); + + Set modIdBuilder = new HashSet<>(); + for (Module m : modules) { + nameMap.put(m.getName(), m); + nsMap.put(m.getNamespace(), m); + modIdBuilder.add(new ModuleIdentifierImpl(m.getName(), Optional.of(m.getNamespace()), Optional.of(m.getRevision()))); } namespaceToModules = ImmutableSetMultimap.copyOf(nsMap); nameToModules = ImmutableSetMultimap.copyOf(nameMap); + moduleIdentifiers = ImmutableSet.copyOf(modIdBuilder); - // :TODO - // this.identifiersToSources = - // ImmutableMap.copyOf(identifiersToSources); - this.identifiersToSources = null; + rootDeclaredStatements = ImmutableList.of(); + rootEffectiveStatements = ImmutableList.of(); + } + public static SchemaContext resolveSchemaContext(final Set modules) { + return new EffectiveSchemaContext(modules); } - public ImmutableList> getRootDeclaredStatements() { + public List> getRootDeclaredStatements() { return rootDeclaredStatements; } - public ImmutableList> getRootEffectiveStatements() { + public List> getRootEffectiveStatements() { return rootEffectiveStatements; } @Override protected Map getIdentifiersToSources() { - - return identifiersToSources; + return ImmutableMap.of(); } @Override public Set getModules() { - return modules; } @Override protected SetMultimap getNamespaceToModules() { - return namespaceToModules; } @Override protected SetMultimap getNameToModules() { - return nameToModules; } @Override - public String toString() { + public Set getAllModuleIdentifiers() { + return moduleIdentifiers; + } + @Override + public String toString() { return String.format("SchemaContextImpl{modules=%s}", modules); } }