BUG 8566 Introduce a fallback for ChoiceSchemaNode lookup
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / EffectiveSchemaContext.java
1 /*
2  * Copyright (c) 2015 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.HashSet;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.TreeMap;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
29 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
30
31 public final class EffectiveSchemaContext extends AbstractEffectiveSchemaContext {
32
33     private final SetMultimap<URI, Module> namespaceToModules;
34     private final SetMultimap<String, Module> nameToModules;
35     private final Set<Module> modules;
36
37     private final List<DeclaredStatement<?>> rootDeclaredStatements;
38     private final List<EffectiveStatement<?, ?>> rootEffectiveStatements;
39     private final Set<ModuleIdentifier> moduleIdentifiers;
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<>(), MODULE_SET_SUPPLIER);
60         final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
61                 new TreeMap<>(), MODULE_SET_SUPPLIER);
62         Set<ModuleIdentifier> modIdBuilder = new HashSet<>();
63         for (Module m : modulesInit) {
64             nameMap.put(m.getName(), m);
65             nsMap.put(m.getNamespace(), m);
66             modIdBuilder.add(new ModuleIdentifierImpl(m.getName(), Optional.of(m.getNamespace()), Optional.of(m.getRevision())));
67             resolveSubmoduleIdentifiers(m.getSubmodules(), modIdBuilder);
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<>(), MODULE_SET_SUPPLIER);
93         final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
94                 new TreeMap<>(), 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             resolveSubmoduleIdentifiers(m.getSubmodules(), modIdBuilder);
102         }
103
104         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
105         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
106         moduleIdentifiers = ImmutableSet.copyOf(modIdBuilder);
107
108         rootDeclaredStatements = ImmutableList.of();
109         rootEffectiveStatements = ImmutableList.of();
110     }
111
112     public static SchemaContext resolveSchemaContext(final Set<Module> modules) {
113        return new EffectiveSchemaContext(modules);
114     }
115
116     private void resolveSubmoduleIdentifiers(final Set<Module> submodules, Set<ModuleIdentifier> modIdBuilder) {
117         for (Module submodule : submodules) {
118             modIdBuilder.add(new ModuleIdentifierImpl(submodule.getName(), Optional.of(
119                     submodule.getNamespace()), Optional.of(submodule.getRevision())));
120         }
121     }
122
123     public List<DeclaredStatement<?>> getRootDeclaredStatements() {
124         return rootDeclaredStatements;
125     }
126
127     public List<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
128         return rootEffectiveStatements;
129     }
130
131     @Override
132     protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
133         return ImmutableMap.of();
134     }
135
136     @Override
137     public Set<Module> getModules() {
138         return modules;
139     }
140
141     @Override
142     protected SetMultimap<URI, Module> getNamespaceToModules() {
143         return namespaceToModules;
144     }
145
146     @Override
147     protected SetMultimap<String, Module> getNameToModules() {
148         return nameToModules;
149     }
150
151     @Override
152     public Set<ModuleIdentifier> getAllModuleIdentifiers() {
153         return moduleIdentifiers;
154     }
155
156     @Override
157     public String toString() {
158         return String.format("EffectiveSchemaContext{modules=%s}", modules);
159     }
160 }