Use Objects.equals() in effective statements
[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.HashMap;
19 import java.util.HashSet;
20 import java.util.LinkedHashMap;
21 import java.util.List;
22 import java.util.Map;
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.parser.util.ModuleDependencySort;
31
32 public class EffectiveSchemaContext extends AbstractEffectiveSchemaContext {
33
34     private final Map<ModuleIdentifier, String> identifiersToSources;
35     private final SetMultimap<URI, Module> namespaceToModules;
36     private final SetMultimap<String, Module> nameToModules;
37     private final Set<Module> modules;
38
39     private final ImmutableList<DeclaredStatement<?>> rootDeclaredStatements;
40     private final ImmutableList<EffectiveStatement<?, ?>> rootEffectiveStatements;
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         final Map<ModuleIdentifier, String> isMap = new LinkedHashMap<>();
64
65         for (Module m : modulesInit) {
66             nameMap.put(m.getName(), m);
67             nsMap.put(m.getNamespace(), m);
68             isMap.put(m, m.getSource());
69         }
70
71         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
72         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
73         identifiersToSources = ImmutableMap.copyOf(isMap);
74
75     }
76
77     public EffectiveSchemaContext(final Set<Module> modules, final Map<ModuleIdentifier, String> identifiersToSources) {
78         this.identifiersToSources = ImmutableMap.copyOf(identifiersToSources);
79
80          /*
81          * Instead of doing this on each invocation of getModules(), pre-compute
82          * it once and keep it around -- better than the set we got in.
83          */
84         this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
85
86          /*
87          * The most common lookup is from Namespace->Module.
88          *
89          * RESTCONF performs lookups based on module name only, where it wants
90          * to receive the latest revision
91          *
92          * Invest some quality time in building up lookup tables for both.
93          */
94         final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
95                 new TreeMap<URI, Collection<Module>>(), MODULE_SET_SUPPLIER);
96         final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
97                 new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER);
98
99         for (Module m : modules) {
100             nameMap.put(m.getName(), m);
101             nsMap.put(m.getNamespace(), m);
102         }
103
104         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
105         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
106
107         rootDeclaredStatements = null;
108         rootEffectiveStatements = null;
109     }
110
111     public static SchemaContext resolveSchemaContext(final Set<Module> modules) {
112         Map<ModuleIdentifier, String> identifiersToSources = new HashMap<>();
113         for (Module module : modules) {
114             identifiersToSources.put(module, module.getSource());
115         }
116         return new EffectiveSchemaContext(modules, identifiersToSources);
117     }
118
119     public ImmutableList<DeclaredStatement<?>> getRootDeclaredStatements() {
120         return rootDeclaredStatements;
121     }
122
123     public ImmutableList<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
124         return rootEffectiveStatements;
125     }
126
127     @Override
128     protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
129         return identifiersToSources;
130     }
131
132     @Override
133     public Set<Module> getModules() {
134         return modules;
135     }
136
137     @Override
138     protected SetMultimap<URI, Module> getNamespaceToModules() {
139         return namespaceToModules;
140     }
141
142     @Override
143     protected SetMultimap<String, Module> getNameToModules() {
144         return nameToModules;
145     }
146
147     @Override
148     public String toString() {
149         return String.format("SchemaContextImpl{modules=%s}", modules);
150     }
151 }