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 java.util.HashMap;
11 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
13 import com.google.common.collect.ImmutableMap;
14 import java.util.LinkedHashMap;
15 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
16 import java.util.HashSet;
17 import com.google.common.collect.ImmutableList;
18 import java.util.List;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import com.google.common.collect.ImmutableSet;
22 import com.google.common.collect.ImmutableSetMultimap;
23 import com.google.common.collect.Multimaps;
24 import com.google.common.collect.SetMultimap;
26 import java.util.Collection;
29 import java.util.TreeMap;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
33 public class EffectiveSchemaContext extends AbstractEffectiveSchemaContext {
35 private final Map<ModuleIdentifier, String> identifiersToSources;
36 private final SetMultimap<URI, Module> namespaceToModules;
37 private final SetMultimap<String, Module> nameToModules;
38 private final Set<Module> modules;
40 private final ImmutableList<DeclaredStatement<?>> rootDeclaredStatements;
41 private final ImmutableList<EffectiveStatement<?, ?>> rootEffectiveStatements;
43 public EffectiveSchemaContext(
44 List<DeclaredStatement<?>> rootDeclaredStatements,
45 List<EffectiveStatement<?, ?>> rootEffectiveStatements) {
46 this.rootDeclaredStatements = ImmutableList
47 .copyOf(rootDeclaredStatements);
48 this.rootEffectiveStatements = ImmutableList
49 .copyOf(rootEffectiveStatements);
51 Set<Module> modulesInit = new HashSet<>();
52 for (EffectiveStatement<?, ?> rootEffectiveStatement : rootEffectiveStatements) {
53 if (rootEffectiveStatement instanceof ModuleEffectiveStatementImpl) {
54 Module module = (Module) rootEffectiveStatement;
55 modulesInit.add(module);
59 Module[] moduleArray = new Module[modulesInit.size()];
60 List<Module> sortedModuleList = ModuleDependencySort.sort(modulesInit.toArray(moduleArray));
61 this.modules = ImmutableSet.copyOf(sortedModuleList);
63 final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
64 new TreeMap<URI, Collection<Module>>(), MODULE_SET_SUPPLIER);
65 final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
66 new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER);
67 final Map<ModuleIdentifier, String> isMap = new LinkedHashMap<>();
69 for (Module m : modulesInit) {
70 nameMap.put(m.getName(), m);
71 nsMap.put(m.getNamespace(), m);
72 isMap.put(m, m.getSource());
75 namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
76 nameToModules = ImmutableSetMultimap.copyOf(nameMap);
77 identifiersToSources = ImmutableMap.copyOf(isMap);
81 public EffectiveSchemaContext(final Set<Module> modules, final Map<ModuleIdentifier, String> identifiersToSources) {
82 this.identifiersToSources = ImmutableMap.copyOf(identifiersToSources);
85 * Instead of doing this on each invocation of getModules(), pre-compute
86 * it once and keep it around -- better than the set we got in.
88 this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
91 * The most common lookup is from Namespace->Module.
93 * RESTCONF performs lookups based on module name only, where it wants
94 * to receive the latest revision
96 * Invest some quality time in building up lookup tables for both.
98 final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
99 new TreeMap<URI, Collection<Module>>(), MODULE_SET_SUPPLIER);
100 final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
101 new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER);
103 for (Module m : modules) {
104 nameMap.put(m.getName(), m);
105 nsMap.put(m.getNamespace(), m);
108 namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
109 nameToModules = ImmutableSetMultimap.copyOf(nameMap);
111 rootDeclaredStatements = null;
112 rootEffectiveStatements = null;
115 public static SchemaContext resolveSchemaContext(final Set<Module> modules) {
116 Map<ModuleIdentifier, String> identifiersToSources = new HashMap<>();
117 for (Module module : modules) {
118 identifiersToSources.put(module, module.getSource());
120 return new EffectiveSchemaContext(modules, identifiersToSources);
123 public ImmutableList<DeclaredStatement<?>> getRootDeclaredStatements() {
124 return rootDeclaredStatements;
127 public ImmutableList<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
128 return rootEffectiveStatements;
132 protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
134 return identifiersToSources;
138 public Set<Module> getModules() {
144 protected SetMultimap<URI, Module> getNamespaceToModules() {
146 return namespaceToModules;
150 protected SetMultimap<String, Module> getNameToModules() {
152 return nameToModules;
156 public String toString() {
158 return String.format("SchemaContextImpl{modules=%s}", modules);