BUG-7052: Move ModuleDependencySort to yang-model-util
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / 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.reactor;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Verify;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.ImmutableSetMultimap;
16 import com.google.common.collect.Multimaps;
17 import com.google.common.collect.SetMultimap;
18 import java.net.URI;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Optional;
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.model.api.stmt.ModuleStatement;
31 import org.opendaylight.yangtools.yang.model.util.AbstractSchemaContext;
32 import org.opendaylight.yangtools.yang.model.util.ModuleDependencySort;
33 import org.opendaylight.yangtools.yang.model.util.ModuleIdentifierImpl;
34
35 @VisibleForTesting
36 public final class EffectiveSchemaContext extends AbstractSchemaContext {
37
38     private final SetMultimap<URI, Module> namespaceToModules;
39     private final SetMultimap<String, Module> nameToModules;
40     private final Set<Module> modules;
41
42     private final List<DeclaredStatement<?>> rootDeclaredStatements;
43     private final List<EffectiveStatement<?, ?>> rootEffectiveStatements;
44     private final Set<ModuleIdentifier> moduleIdentifiers;
45
46     EffectiveSchemaContext(final List<DeclaredStatement<?>> rootDeclaredStatements,
47             final List<EffectiveStatement<?, ?>> rootEffectiveStatements) {
48         this.rootDeclaredStatements = ImmutableList.copyOf(rootDeclaredStatements);
49         this.rootEffectiveStatements = ImmutableList.copyOf(rootEffectiveStatements);
50
51         final Set<Module> modulesInit = new HashSet<>();
52         for (EffectiveStatement<?, ?> stmt : rootEffectiveStatements) {
53             if (stmt.getDeclared() instanceof ModuleStatement) {
54                 Verify.verify(stmt instanceof Module);
55                 modulesInit.add((Module) stmt);
56             }
57         }
58         this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modulesInit));
59
60         final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(new TreeMap<>(), MODULE_SET_SUPPLIER);
61         final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(new TreeMap<>(), MODULE_SET_SUPPLIER);
62         final 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     private 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));
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     @VisibleForTesting
126     public List<DeclaredStatement<?>> getRootDeclaredStatements() {
127         return rootDeclaredStatements;
128     }
129
130     @VisibleForTesting
131     public List<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
132         return rootEffectiveStatements;
133     }
134
135     @Override
136     protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
137         return ImmutableMap.of();
138     }
139
140     @Override
141     public Set<Module> getModules() {
142         return modules;
143     }
144
145     @Override
146     protected SetMultimap<URI, Module> getNamespaceToModules() {
147         return namespaceToModules;
148     }
149
150     @Override
151     protected SetMultimap<String, Module> getNameToModules() {
152         return nameToModules;
153     }
154
155     @Override
156     public Set<ModuleIdentifier> getAllModuleIdentifiers() {
157         return moduleIdentifiers;
158     }
159
160     @Override
161     public String toString() {
162         return String.format("EffectiveSchemaContext{modules=%s}", modules);
163     }
164 }