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 SetMultimap<URI, Module> namespaceToModules;
35 private final SetMultimap<String, Module> nameToModules;
36 private final Set<Module> modules;
38 private final ImmutableList<DeclaredStatement<?>> rootDeclaredStatements;
39 private final ImmutableList<EffectiveStatement<?, ?>> rootEffectiveStatements;
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<URI, Collection<Module>>(), MODULE_SET_SUPPLIER);
60 final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
61 new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER);
62 final Map<ModuleIdentifier, String> isMap = new LinkedHashMap<>();
64 for (Module m : modulesInit) {
65 nameMap.put(m.getName(), m);
66 nsMap.put(m.getNamespace(), m);
67 isMap.put(m, m.getSource());
70 namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
71 nameToModules = ImmutableSetMultimap.copyOf(nameMap);
74 public EffectiveSchemaContext(final Set<Module> modules) {
77 * Instead of doing this on each invocation of getModules(), pre-compute
78 * it once and keep it around -- better than the set we got in.
80 this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
83 * The most common lookup is from Namespace->Module.
85 * RESTCONF performs lookups based on module name only, where it wants
86 * to receive the latest revision
88 * Invest some quality time in building up lookup tables for both.
90 final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
91 new TreeMap<URI, Collection<Module>>(), MODULE_SET_SUPPLIER);
92 final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
93 new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER);
95 for (Module m : modules) {
96 nameMap.put(m.getName(), m);
97 nsMap.put(m.getNamespace(), m);
100 namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
101 nameToModules = ImmutableSetMultimap.copyOf(nameMap);
103 rootDeclaredStatements = null;
104 rootEffectiveStatements = null;
107 public static SchemaContext resolveSchemaContext(final Set<Module> modules) {
108 return new EffectiveSchemaContext(modules);
111 public ImmutableList<DeclaredStatement<?>> getRootDeclaredStatements() {
112 return rootDeclaredStatements;
115 public ImmutableList<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
116 return rootEffectiveStatements;
120 protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
121 return ImmutableMap.of();
125 public Set<Module> getModules() {
130 protected SetMultimap<URI, Module> getNamespaceToModules() {
131 return namespaceToModules;
135 protected SetMultimap<String, Module> getNameToModules() {
136 return nameToModules;
140 public String toString() {
141 return String.format("SchemaContextImpl{modules=%s}", modules);