Cleanup imports
[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.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.Collection;
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.util.ModuleDependencySort;
29
30 public class EffectiveSchemaContext extends AbstractEffectiveSchemaContext {
31
32     private final SetMultimap<URI, Module> namespaceToModules;
33     private final SetMultimap<String, Module> nameToModules;
34     private final Set<Module> modules;
35
36     private final ImmutableList<DeclaredStatement<?>> rootDeclaredStatements;
37     private final ImmutableList<EffectiveStatement<?, ?>> rootEffectiveStatements;
38
39     public EffectiveSchemaContext(final List<DeclaredStatement<?>> rootDeclaredStatements,
40             final List<EffectiveStatement<?, ?>> rootEffectiveStatements) {
41         this.rootDeclaredStatements = ImmutableList.copyOf(rootDeclaredStatements);
42         this.rootEffectiveStatements = ImmutableList.copyOf(rootEffectiveStatements);
43
44         Set<Module> modulesInit = new HashSet<>();
45         for (EffectiveStatement<?, ?> rootEffectiveStatement : rootEffectiveStatements) {
46             if (rootEffectiveStatement instanceof ModuleEffectiveStatementImpl) {
47                 Module module = (Module) rootEffectiveStatement;
48                 modulesInit.add(module);
49             }
50         }
51
52         Module[] moduleArray = new Module[modulesInit.size()];
53         List<Module> sortedModuleList = ModuleDependencySort.sort(modulesInit.toArray(moduleArray));
54         this.modules = ImmutableSet.copyOf(sortedModuleList);
55
56         final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
57                 new TreeMap<URI, Collection<Module>>(), MODULE_SET_SUPPLIER);
58         final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
59                 new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER);
60
61         for (Module m : modulesInit) {
62             nameMap.put(m.getName(), m);
63             nsMap.put(m.getNamespace(), m);
64         }
65
66         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
67         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
68     }
69
70     public EffectiveSchemaContext(final Set<Module> modules) {
71
72          /*
73          * Instead of doing this on each invocation of getModules(), pre-compute
74          * it once and keep it around -- better than the set we got in.
75          */
76         this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
77
78          /*
79          * The most common lookup is from Namespace->Module.
80          *
81          * RESTCONF performs lookups based on module name only, where it wants
82          * to receive the latest revision
83          *
84          * Invest some quality time in building up lookup tables for both.
85          */
86         final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
87                 new TreeMap<URI, Collection<Module>>(), MODULE_SET_SUPPLIER);
88         final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
89                 new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER);
90
91         for (Module m : modules) {
92             nameMap.put(m.getName(), m);
93             nsMap.put(m.getNamespace(), m);
94         }
95
96         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
97         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
98
99         rootDeclaredStatements = null;
100         rootEffectiveStatements = null;
101     }
102
103     public static SchemaContext resolveSchemaContext(final Set<Module> modules) {
104        return new EffectiveSchemaContext(modules);
105     }
106
107     public ImmutableList<DeclaredStatement<?>> getRootDeclaredStatements() {
108         return rootDeclaredStatements;
109     }
110
111     public ImmutableList<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
112         return rootEffectiveStatements;
113     }
114
115     @Override
116     protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
117         return ImmutableMap.of();
118     }
119
120     @Override
121     public Set<Module> getModules() {
122         return modules;
123     }
124
125     @Override
126     protected SetMultimap<URI, Module> getNamespaceToModules() {
127         return namespaceToModules;
128     }
129
130     @Override
131     protected SetMultimap<String, Module> getNameToModules() {
132         return nameToModules;
133     }
134
135     @Override
136     public String toString() {
137         return String.format("SchemaContextImpl{modules=%s}", modules);
138     }
139 }