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 org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
12 import java.util.HashSet;
13 import com.google.common.collect.ImmutableList;
14 import java.util.List;
15 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import com.google.common.collect.ImmutableSet;
18 import com.google.common.collect.ImmutableSetMultimap;
19 import com.google.common.collect.Multimaps;
20 import com.google.common.collect.SetMultimap;
22 import java.util.Collection;
25 import java.util.TreeMap;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
29 public class EffectiveSchemaContext extends AbstractEffectiveSchemaContext {
31 private final Map<ModuleIdentifier, String> identifiersToSources;
32 private final SetMultimap<URI, Module> namespaceToModules;
33 private final SetMultimap<String, Module> nameToModules;
34 private final Set<Module> modules;
36 private final ImmutableList<DeclaredStatement<?>> rootDeclaredStatements;
37 private final ImmutableList<EffectiveStatement<?, ?>> rootEffectiveStatements;
39 public EffectiveSchemaContext(
40 List<DeclaredStatement<?>> rootDeclaredStatements,
41 List<EffectiveStatement<?, ?>> rootEffectiveStatements) {
42 this.rootDeclaredStatements = ImmutableList
43 .copyOf(rootDeclaredStatements);
44 this.rootEffectiveStatements = ImmutableList
45 .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);
64 for (Module m : modulesInit) {
65 nameMap.put(m.getName(), m);
66 nsMap.put(m.getNamespace(), m);
69 namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
70 nameToModules = ImmutableSetMultimap.copyOf(nameMap);
72 // :TODO init identifiersToSources
73 this.identifiersToSources = null;
77 public ImmutableList<DeclaredStatement<?>> getRootDeclaredStatements() {
78 return rootDeclaredStatements;
81 public ImmutableList<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
82 return rootEffectiveStatements;
86 protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
88 return identifiersToSources;
92 public Set<Module> getModules() {
98 protected SetMultimap<URI, Module> getNamespaceToModules() {
100 return namespaceToModules;
104 protected SetMultimap<String, Module> getNameToModules() {
106 return nameToModules;
110 public String toString() {
112 return String.format("SchemaContextImpl{modules=%s}", modules);