2 * Copyright (c) 2013 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.Collection;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.LinkedHashMap;
21 import java.util.List;
24 import java.util.TreeMap;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
29 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
30 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
32 public class EffectiveSchemaContext extends AbstractEffectiveSchemaContext {
34 private final Map<ModuleIdentifier, String> identifiersToSources;
35 private final SetMultimap<URI, Module> namespaceToModules;
36 private final SetMultimap<String, Module> nameToModules;
37 private final Set<Module> modules;
39 private final ImmutableList<DeclaredStatement<?>> rootDeclaredStatements;
40 private final ImmutableList<EffectiveStatement<?, ?>> rootEffectiveStatements;
42 public EffectiveSchemaContext(final List<DeclaredStatement<?>> rootDeclaredStatements,
43 final List<EffectiveStatement<?, ?>> rootEffectiveStatements) {
44 this.rootDeclaredStatements = ImmutableList.copyOf(rootDeclaredStatements);
45 this.rootEffectiveStatements = ImmutableList.copyOf(rootEffectiveStatements);
47 Set<Module> modulesInit = new HashSet<>();
48 for (EffectiveStatement<?, ?> rootEffectiveStatement : rootEffectiveStatements) {
49 if (rootEffectiveStatement instanceof ModuleEffectiveStatementImpl) {
50 Module module = (Module) rootEffectiveStatement;
51 modulesInit.add(module);
55 Module[] moduleArray = new Module[modulesInit.size()];
56 List<Module> sortedModuleList = ModuleDependencySort.sort(modulesInit.toArray(moduleArray));
57 this.modules = ImmutableSet.copyOf(sortedModuleList);
59 final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
60 new TreeMap<URI, Collection<Module>>(), MODULE_SET_SUPPLIER);
61 final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
62 new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER);
63 final Map<ModuleIdentifier, String> isMap = new LinkedHashMap<>();
65 for (Module m : modulesInit) {
66 nameMap.put(m.getName(), m);
67 nsMap.put(m.getNamespace(), m);
68 isMap.put(m, m.getSource());
71 namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
72 nameToModules = ImmutableSetMultimap.copyOf(nameMap);
73 identifiersToSources = ImmutableMap.copyOf(isMap);
77 public EffectiveSchemaContext(final Set<Module> modules, final Map<ModuleIdentifier, String> identifiersToSources) {
78 this.identifiersToSources = ImmutableMap.copyOf(identifiersToSources);
81 * Instead of doing this on each invocation of getModules(), pre-compute
82 * it once and keep it around -- better than the set we got in.
84 this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
87 * The most common lookup is from Namespace->Module.
89 * RESTCONF performs lookups based on module name only, where it wants
90 * to receive the latest revision
92 * Invest some quality time in building up lookup tables for both.
94 final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
95 new TreeMap<URI, Collection<Module>>(), MODULE_SET_SUPPLIER);
96 final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
97 new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER);
99 for (Module m : modules) {
100 nameMap.put(m.getName(), m);
101 nsMap.put(m.getNamespace(), m);
104 namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
105 nameToModules = ImmutableSetMultimap.copyOf(nameMap);
107 rootDeclaredStatements = null;
108 rootEffectiveStatements = null;
111 public static SchemaContext resolveSchemaContext(final Set<Module> modules) {
112 Map<ModuleIdentifier, String> identifiersToSources = new HashMap<>();
113 for (Module module : modules) {
114 identifiersToSources.put(module, module.getSource());
116 return new EffectiveSchemaContext(modules, identifiersToSources);
119 public ImmutableList<DeclaredStatement<?>> getRootDeclaredStatements() {
120 return rootDeclaredStatements;
123 public ImmutableList<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
124 return rootEffectiveStatements;
128 protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
129 return identifiersToSources;
133 public Set<Module> getModules() {
138 protected SetMultimap<URI, Module> getNamespaceToModules() {
139 return namespaceToModules;
143 protected SetMultimap<String, Module> getNameToModules() {
144 return nameToModules;
148 public String toString() {
149 return String.format("SchemaContextImpl{modules=%s}", modules);