031d4cb87911c0eee8805356e7b4de65cb281db1
[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 java.util.HashMap;
11 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
12
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;
25 import java.net.URI;
26 import java.util.Collection;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.TreeMap;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
32
33 public class EffectiveSchemaContext extends AbstractEffectiveSchemaContext {
34
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;
39
40     private final ImmutableList<DeclaredStatement<?>> rootDeclaredStatements;
41     private final ImmutableList<EffectiveStatement<?, ?>> rootEffectiveStatements;
42
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);
50
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);
56             }
57         }
58
59         Module[] moduleArray = new Module[modulesInit.size()];
60         List<Module> sortedModuleList = ModuleDependencySort.sort(modulesInit.toArray(moduleArray));
61         this.modules = ImmutableSet.copyOf(sortedModuleList);
62
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<>();
68
69         for (Module m : modulesInit) {
70             nameMap.put(m.getName(), m);
71             nsMap.put(m.getNamespace(), m);
72             isMap.put(m, m.getSource());
73         }
74
75         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
76         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
77         identifiersToSources = ImmutableMap.copyOf(isMap);
78
79     }
80
81     public EffectiveSchemaContext(final Set<Module> modules, final Map<ModuleIdentifier, String> identifiersToSources) {
82         this.identifiersToSources = ImmutableMap.copyOf(identifiersToSources);
83
84          /*
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.
87          */
88         this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
89
90          /*
91          * The most common lookup is from Namespace->Module.
92          *
93          * RESTCONF performs lookups based on module name only, where it wants
94          * to receive the latest revision
95          *
96          * Invest some quality time in building up lookup tables for both.
97          */
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);
102
103         for (Module m : modules) {
104             nameMap.put(m.getName(), m);
105             nsMap.put(m.getNamespace(), m);
106         }
107
108         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
109         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
110
111         rootDeclaredStatements = null;
112         rootEffectiveStatements = null;
113     }
114
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());
119         }
120         return new EffectiveSchemaContext(modules, identifiersToSources);
121     }
122
123     public ImmutableList<DeclaredStatement<?>> getRootDeclaredStatements() {
124         return rootDeclaredStatements;
125     }
126
127     public ImmutableList<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
128         return rootEffectiveStatements;
129     }
130
131     @Override
132     protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
133
134         return identifiersToSources;
135     }
136
137     @Override
138     public Set<Module> getModules() {
139
140         return modules;
141     }
142
143     @Override
144     protected SetMultimap<URI, Module> getNamespaceToModules() {
145
146         return namespaceToModules;
147     }
148
149     @Override
150     protected SetMultimap<String, Module> getNameToModules() {
151
152         return nameToModules;
153     }
154
155     @Override
156     public String toString() {
157
158         return String.format("SchemaContextImpl{modules=%s}", modules);
159     }
160 }