2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.collect.ImmutableSetMultimap;
14 import com.google.common.collect.Multimaps;
15 import com.google.common.collect.SetMultimap;
17 import java.util.HashSet;
18 import java.util.List;
20 import java.util.Optional;
22 import java.util.TreeMap;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.util.ModuleIdentifierImpl;
29 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
31 public final class EffectiveSchemaContext extends AbstractEffectiveSchemaContext {
33 private final SetMultimap<URI, Module> namespaceToModules;
34 private final SetMultimap<String, Module> nameToModules;
35 private final Set<Module> modules;
37 private final List<DeclaredStatement<?>> rootDeclaredStatements;
38 private final List<EffectiveStatement<?, ?>> rootEffectiveStatements;
39 private final Set<ModuleIdentifier> moduleIdentifiers;
41 public EffectiveSchemaContext(final List<DeclaredStatement<?>> rootDeclaredStatements,
42 final List<EffectiveStatement<?, ?>> rootEffectiveStatements) {
43 this.rootDeclaredStatements = ImmutableList.copyOf(rootDeclaredStatements);
44 this.rootEffectiveStatements = ImmutableList.copyOf(rootEffectiveStatements);
46 Set<Module> modulesInit = new HashSet<>();
47 for (EffectiveStatement<?, ?> rootEffectiveStatement : rootEffectiveStatements) {
48 if (rootEffectiveStatement instanceof ModuleEffectiveStatementImpl) {
49 Module module = (Module) rootEffectiveStatement;
50 modulesInit.add(module);
54 Module[] moduleArray = new Module[modulesInit.size()];
55 List<Module> sortedModuleList = ModuleDependencySort.sort(modulesInit.toArray(moduleArray));
56 this.modules = ImmutableSet.copyOf(sortedModuleList);
58 final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
59 new TreeMap<>(), MODULE_SET_SUPPLIER);
60 final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
61 new TreeMap<>(), MODULE_SET_SUPPLIER);
62 Set<ModuleIdentifier> modIdBuilder = new HashSet<>();
63 for (Module m : modulesInit) {
64 nameMap.put(m.getName(), m);
65 nsMap.put(m.getNamespace(), m);
66 modIdBuilder.add(ModuleIdentifierImpl.create(m.getName(), Optional.of(m.getNamespace()),
67 Optional.of(m.getRevision())));
68 resolveSubmoduleIdentifiers(m.getSubmodules(), modIdBuilder);
71 namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
72 nameToModules = ImmutableSetMultimap.copyOf(nameMap);
73 moduleIdentifiers = ImmutableSet.copyOf(modIdBuilder);
76 public EffectiveSchemaContext(final Set<Module> modules) {
79 * Instead of doing this on each invocation of getModules(), pre-compute
80 * it once and keep it around -- better than the set we got in.
82 this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
85 * The most common lookup is from Namespace->Module.
87 * RESTCONF performs lookups based on module name only, where it wants
88 * to receive the latest revision
90 * Invest some quality time in building up lookup tables for both.
92 final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
93 new TreeMap<>(), MODULE_SET_SUPPLIER);
94 final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
95 new TreeMap<>(), MODULE_SET_SUPPLIER);
97 Set<ModuleIdentifier> modIdBuilder = new HashSet<>();
98 for (Module m : modules) {
99 nameMap.put(m.getName(), m);
100 nsMap.put(m.getNamespace(), m);
101 modIdBuilder.add(ModuleIdentifierImpl.create(m.getName(), Optional.of(m.getNamespace()),
102 Optional.of(m.getRevision())));
103 resolveSubmoduleIdentifiers(m.getSubmodules(), modIdBuilder);
106 namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
107 nameToModules = ImmutableSetMultimap.copyOf(nameMap);
108 moduleIdentifiers = ImmutableSet.copyOf(modIdBuilder);
110 rootDeclaredStatements = ImmutableList.of();
111 rootEffectiveStatements = ImmutableList.of();
114 public static SchemaContext resolveSchemaContext(final Set<Module> modules) {
115 return new EffectiveSchemaContext(modules);
118 private static void resolveSubmoduleIdentifiers(final Set<Module> submodules, final Set<ModuleIdentifier> modIdBuilder) {
119 for (Module submodule : submodules) {
120 modIdBuilder.add(ModuleIdentifierImpl.create(submodule.getName(),
121 Optional.of(submodule.getNamespace()), Optional.of(submodule.getRevision())));
125 public List<DeclaredStatement<?>> getRootDeclaredStatements() {
126 return rootDeclaredStatements;
129 public List<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
130 return rootEffectiveStatements;
134 protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
135 return ImmutableMap.of();
139 public Set<Module> getModules() {
144 protected SetMultimap<URI, Module> getNamespaceToModules() {
145 return namespaceToModules;
149 protected SetMultimap<String, Module> getNameToModules() {
150 return nameToModules;
154 public Set<ModuleIdentifier> getAllModuleIdentifiers() {
155 return moduleIdentifiers;
159 public String toString() {
160 return String.format("EffectiveSchemaContext{modules=%s}", modules);