bf8463df0c4615ede2948b957c1b5631e228ca5b
[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.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.HashSet;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Optional;
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.model.util.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(ModuleIdentifierImpl.create(m.getName(), Optional.of(m.getNamespace()),
67                 Optional.of(m.getRevision())));
68             resolveSubmoduleIdentifiers(m.getSubmodules(), modIdBuilder);
69         }
70
71         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
72         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
73         moduleIdentifiers = ImmutableSet.copyOf(modIdBuilder);
74     }
75
76     public EffectiveSchemaContext(final Set<Module> modules) {
77
78          /*
79          * Instead of doing this on each invocation of getModules(), pre-compute
80          * it once and keep it around -- better than the set we got in.
81          */
82         this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
83
84          /*
85          * The most common lookup is from Namespace->Module.
86          *
87          * RESTCONF performs lookups based on module name only, where it wants
88          * to receive the latest revision
89          *
90          * Invest some quality time in building up lookup tables for both.
91          */
92         final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
93                 new TreeMap<>(), MODULE_SET_SUPPLIER);
94         final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
95                 new TreeMap<>(), MODULE_SET_SUPPLIER);
96
97         Set<ModuleIdentifier> modIdBuilder = new HashSet<>();
98         for (Module m : modules) {
99             nameMap.put(m.getName(), m);
100             nsMap.put(m.getNamespace(), m);
101             modIdBuilder.add(ModuleIdentifierImpl.create(m.getName(), Optional.of(m.getNamespace()),
102                 Optional.of(m.getRevision())));
103             resolveSubmoduleIdentifiers(m.getSubmodules(), modIdBuilder);
104         }
105
106         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
107         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
108         moduleIdentifiers = ImmutableSet.copyOf(modIdBuilder);
109
110         rootDeclaredStatements = ImmutableList.of();
111         rootEffectiveStatements = ImmutableList.of();
112     }
113
114     public static SchemaContext resolveSchemaContext(final Set<Module> modules) {
115        return new EffectiveSchemaContext(modules);
116     }
117
118     private static void resolveSubmoduleIdentifiers(final Set<Module> submodules, final Set<ModuleIdentifier> modIdBuilder) {
119         for (Module submodule : submodules) {
120             modIdBuilder.add(ModuleIdentifierImpl.create(submodule.getName(),
121                 Optional.of(submodule.getNamespace()), Optional.of(submodule.getRevision())));
122         }
123     }
124
125     public List<DeclaredStatement<?>> getRootDeclaredStatements() {
126         return rootDeclaredStatements;
127     }
128
129     public List<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
130         return rootEffectiveStatements;
131     }
132
133     @Override
134     protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
135         return ImmutableMap.of();
136     }
137
138     @Override
139     public Set<Module> getModules() {
140         return modules;
141     }
142
143     @Override
144     protected SetMultimap<URI, Module> getNamespaceToModules() {
145         return namespaceToModules;
146     }
147
148     @Override
149     protected SetMultimap<String, Module> getNameToModules() {
150         return nameToModules;
151     }
152
153     @Override
154     public Set<ModuleIdentifier> getAllModuleIdentifiers() {
155         return moduleIdentifiers;
156     }
157
158     @Override
159     public String toString() {
160         return String.format("EffectiveSchemaContext{modules=%s}", modules);
161     }
162 }