5acbf5959a2260a1fc37edc3d8f2202c5fc1d565
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / EffectiveSchemaContext.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
9
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;
16 import java.net.URI;
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;
22 import java.util.Map;
23 import java.util.Set;
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;
31
32 public class EffectiveSchemaContext extends AbstractEffectiveSchemaContext {
33
34     private final SetMultimap<URI, Module> namespaceToModules;
35     private final SetMultimap<String, Module> nameToModules;
36     private final Set<Module> modules;
37
38     private final ImmutableList<DeclaredStatement<?>> rootDeclaredStatements;
39     private final ImmutableList<EffectiveStatement<?, ?>> rootEffectiveStatements;
40
41     public EffectiveSchemaContext(final List<DeclaredStatement<?>> rootDeclaredStatements,
42             final List<EffectiveStatement<?, ?>> rootEffectiveStatements) {
43         this.rootDeclaredStatements = ImmutableList.copyOf(rootDeclaredStatements);
44         this.rootEffectiveStatements = ImmutableList.copyOf(rootEffectiveStatements);
45
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);
51             }
52         }
53
54         Module[] moduleArray = new Module[modulesInit.size()];
55         List<Module> sortedModuleList = ModuleDependencySort.sort(modulesInit.toArray(moduleArray));
56         this.modules = ImmutableSet.copyOf(sortedModuleList);
57
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<>();
63
64         for (Module m : modulesInit) {
65             nameMap.put(m.getName(), m);
66             nsMap.put(m.getNamespace(), m);
67             isMap.put(m, m.getSource());
68         }
69
70         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
71         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
72     }
73
74     public EffectiveSchemaContext(final Set<Module> modules) {
75         
76          /*
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.
79          */
80         this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
81
82          /*
83          * The most common lookup is from Namespace->Module.
84          *
85          * RESTCONF performs lookups based on module name only, where it wants
86          * to receive the latest revision
87          *
88          * Invest some quality time in building up lookup tables for both.
89          */
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);
94
95         for (Module m : modules) {
96             nameMap.put(m.getName(), m);
97             nsMap.put(m.getNamespace(), m);
98         }
99
100         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
101         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
102
103         rootDeclaredStatements = null;
104         rootEffectiveStatements = null;
105     }
106
107     public static SchemaContext resolveSchemaContext(final Set<Module> modules) {
108        return new EffectiveSchemaContext(modules);
109     }
110
111     public ImmutableList<DeclaredStatement<?>> getRootDeclaredStatements() {
112         return rootDeclaredStatements;
113     }
114
115     public ImmutableList<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
116         return rootEffectiveStatements;
117     }
118
119     @Override
120     protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
121         return ImmutableMap.of();
122     }
123
124     @Override
125     public Set<Module> getModules() {
126         return modules;
127     }
128
129     @Override
130     protected SetMultimap<URI, Module> getNamespaceToModules() {
131         return namespaceToModules;
132     }
133
134     @Override
135     protected SetMultimap<String, Module> getNameToModules() {
136         return nameToModules;
137     }
138
139     @Override
140     public String toString() {
141         return String.format("SchemaContextImpl{modules=%s}", modules);
142     }
143 }