Fixed allModuleIdentifiers
[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.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;
17 import java.net.URI;
18 import java.util.Collection;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
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;
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     private final Set<ModuleIdentifier> moduleIdentifiers;
41
42     public EffectiveSchemaContext(final List<DeclaredStatement<?>> rootDeclaredStatements,
43             final List<EffectiveStatement<?, ?>> rootEffectiveStatements) {
44         this.rootDeclaredStatements = ImmutableList.copyOf(rootDeclaredStatements);
45         this.rootEffectiveStatements = ImmutableList.copyOf(rootEffectiveStatements);
46
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);
52             }
53         }
54
55         Module[] moduleArray = new Module[modulesInit.size()];
56         List<Module> sortedModuleList = ModuleDependencySort.sort(modulesInit.toArray(moduleArray));
57         this.modules = ImmutableSet.copyOf(sortedModuleList);
58
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())));
68         }
69
70         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
71         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
72         moduleIdentifiers = ImmutableSet.copyOf(modIdBuilder);
73     }
74
75     public EffectiveSchemaContext(final Set<Module> modules) {
76
77          /*
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.
80          */
81         this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
82
83          /*
84          * The most common lookup is from Namespace->Module.
85          *
86          * RESTCONF performs lookups based on module name only, where it wants
87          * to receive the latest revision
88          *
89          * Invest some quality time in building up lookup tables for both.
90          */
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);
95
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())));
101         }
102
103         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
104         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
105         moduleIdentifiers = ImmutableSet.copyOf(modIdBuilder);
106
107         rootDeclaredStatements = ImmutableList.of();
108         rootEffectiveStatements = ImmutableList.of();
109     }
110
111     public static SchemaContext resolveSchemaContext(final Set<Module> modules) {
112        return new EffectiveSchemaContext(modules);
113     }
114
115     public ImmutableList<DeclaredStatement<?>> getRootDeclaredStatements() {
116         return rootDeclaredStatements;
117     }
118
119     public ImmutableList<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
120         return rootEffectiveStatements;
121     }
122
123     @Override
124     protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
125         return ImmutableMap.of();
126     }
127
128     @Override
129     public Set<Module> getModules() {
130         return modules;
131     }
132
133     @Override
134     protected SetMultimap<URI, Module> getNamespaceToModules() {
135         return namespaceToModules;
136     }
137
138     @Override
139     protected SetMultimap<String, Module> getNameToModules() {
140         return nameToModules;
141     }
142
143     @Override
144     public Set<ModuleIdentifier> getAllModuleIdentifiers() {
145         return moduleIdentifiers;
146     }
147
148     @Override
149     public String toString() {
150         return String.format("SchemaContextImpl{modules=%s}", modules);
151     }
152 }