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.base.Optional;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.collect.ImmutableSetMultimap;
15 import com.google.common.collect.Multimaps;
16 import com.google.common.collect.SetMultimap;
18 import java.util.Collection;
19 import java.util.HashSet;
20 import java.util.List;
23 import java.util.TreeMap;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
29 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
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;
40 private final Set<ModuleIdentifier> moduleIdentifiers;
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 Set<ModuleIdentifier> modIdBuilder = new HashSet<>();
64 for (Module m : modulesInit) {
65 nameMap.put(m.getName(), m);
66 nsMap.put(m.getNamespace(), m);
67 modIdBuilder.add(new ModuleIdentifierImpl(m.getName(), Optional.of(m.getNamespace()), Optional.of(m.getRevision())));
70 namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
71 nameToModules = ImmutableSetMultimap.copyOf(nameMap);
72 moduleIdentifiers = ImmutableSet.copyOf(modIdBuilder);
75 public EffectiveSchemaContext(final Set<Module> modules) {
78 * Instead of doing this on each invocation of getModules(), pre-compute
79 * it once and keep it around -- better than the set we got in.
81 this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
84 * The most common lookup is from Namespace->Module.
86 * RESTCONF performs lookups based on module name only, where it wants
87 * to receive the latest revision
89 * Invest some quality time in building up lookup tables for both.
91 final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
92 new TreeMap<URI, Collection<Module>>(), MODULE_SET_SUPPLIER);
93 final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
94 new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER);
96 Set<ModuleIdentifier> modIdBuilder = new HashSet<>();
97 for (Module m : modules) {
98 nameMap.put(m.getName(), m);
99 nsMap.put(m.getNamespace(), m);
100 modIdBuilder.add(new ModuleIdentifierImpl(m.getName(), Optional.of(m.getNamespace()), Optional.of(m.getRevision())));
103 namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
104 nameToModules = ImmutableSetMultimap.copyOf(nameMap);
105 moduleIdentifiers = ImmutableSet.copyOf(modIdBuilder);
107 rootDeclaredStatements = ImmutableList.of();
108 rootEffectiveStatements = ImmutableList.of();
111 public static SchemaContext resolveSchemaContext(final Set<Module> modules) {
112 return new EffectiveSchemaContext(modules);
115 public ImmutableList<DeclaredStatement<?>> getRootDeclaredStatements() {
116 return rootDeclaredStatements;
119 public ImmutableList<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
120 return rootEffectiveStatements;
124 protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
125 return ImmutableMap.of();
129 public Set<Module> getModules() {
134 protected SetMultimap<URI, Module> getNamespaceToModules() {
135 return namespaceToModules;
139 protected SetMultimap<String, Module> getNameToModules() {
140 return nameToModules;
144 public Set<ModuleIdentifier> getAllModuleIdentifiers() {
145 return moduleIdentifiers;
149 public String toString() {
150 return String.format("SchemaContextImpl{modules=%s}", modules);