Optimize getModuleNameFromNameSpace
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ConfigurationImpl.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.cluster.datastore;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableList.Builder;
15 import com.google.common.collect.ImmutableMap;
16 import com.typesafe.config.Config;
17 import com.typesafe.config.ConfigFactory;
18 import com.typesafe.config.ConfigObject;
19 import java.io.File;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.LinkedHashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
28 import org.opendaylight.controller.cluster.datastore.shardstrategy.ModuleShardStrategy;
29 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class ConfigurationImpl implements Configuration {
34
35     private final List<ModuleShard> moduleShards;
36
37     private final List<Module> modules;
38
39     private static final Logger
40         LOG = LoggerFactory.getLogger(DistributedDataStore.class);
41
42     // Look up maps to speed things up
43
44     // key = memberName, value = list of shardNames
45     private final Map<String, List<String>> memberShardNames = new HashMap<>();
46
47     // key = shardName, value = list of replicaNames (replicaNames are the same as memberNames)
48     private final Map<String, List<String>> shardReplicaNames = new HashMap<>();
49
50     private final Map<String, String> namespaceToModuleName;
51
52     public ConfigurationImpl(final String moduleShardsConfigPath,
53
54         final String modulesConfigPath){
55
56         Preconditions.checkNotNull(moduleShardsConfigPath, "moduleShardsConfigPath should not be null");
57         Preconditions.checkNotNull(modulesConfigPath, "modulesConfigPath should not be null");
58
59
60         File moduleShardsFile = new File("./configuration/initial/" + moduleShardsConfigPath);
61         File modulesFile = new File("./configuration/initial/" + modulesConfigPath);
62
63         Config moduleShardsConfig = null;
64         if(moduleShardsFile.exists()) {
65             LOG.info("module shards config file exists - reading config from it");
66             moduleShardsConfig = ConfigFactory.parseFile(moduleShardsFile);
67         } else {
68             LOG.warn("module shards configuration read from resource");
69             moduleShardsConfig = ConfigFactory.load(moduleShardsConfigPath);
70         }
71
72         Config modulesConfig = null;
73         if(modulesFile.exists()) {
74             LOG.info("modules config file exists - reading config from it");
75             modulesConfig = ConfigFactory.parseFile(modulesFile);
76         } else {
77             LOG.warn("modules configuration read from resource");
78             modulesConfig = ConfigFactory.load(modulesConfigPath);
79         }
80
81         this.moduleShards = readModuleShards(moduleShardsConfig);
82         this.modules = readModules(modulesConfig);
83
84         namespaceToModuleName = createNamespaceToModuleName(modules);
85     }
86
87     private static Map<String, String> createNamespaceToModuleName(Iterable<Module> modules) {
88         final com.google.common.collect.ImmutableMap.Builder<String, String> b = ImmutableMap.builder();
89         for (Module m : modules) {
90             b.put(m.getNameSpace(), m.getName());
91         }
92         return b.build();
93     }
94
95     @Override public List<String> getMemberShardNames(final String memberName){
96
97         Preconditions.checkNotNull(memberName, "memberName should not be null");
98
99         if(memberShardNames.containsKey(memberName)){
100             return memberShardNames.get(memberName);
101         }
102
103         List<String> shards = new ArrayList<>();
104         for(ModuleShard ms : moduleShards){
105             for(Shard s : ms.getShards()){
106                 for(String m : s.getReplicas()){
107                     if(memberName.equals(m)){
108                         shards.add(s.getName());
109                     }
110                 }
111             }
112         }
113
114         memberShardNames.put(memberName, shards);
115
116         return shards;
117
118     }
119
120     @Override
121     public Optional<String> getModuleNameFromNameSpace(final String nameSpace) {
122         Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
123         return Optional.fromNullable(namespaceToModuleName.get(nameSpace));
124     }
125
126     @Override public Map<String, ShardStrategy> getModuleNameToShardStrategyMap() {
127         // FIXME: can be constant view of modules
128         Map<String, ShardStrategy> map = new HashMap<>();
129         for(Module m : modules){
130             map.put(m.getName(), m.getShardStrategy());
131         }
132         return map;
133     }
134
135     @Override public List<String> getShardNamesFromModuleName(final String moduleName) {
136
137         Preconditions.checkNotNull(moduleName, "moduleName should not be null");
138
139         // FIXME: can be constant view of moduleShards
140         for(ModuleShard m : moduleShards){
141             if(m.getModuleName().equals(moduleName)){
142                 List<String> l = new ArrayList<>();
143                 for(Shard s : m.getShards()){
144                     l.add(s.getName());
145                 }
146                 return l;
147             }
148         }
149
150         return Collections.emptyList();
151     }
152
153     @Override public List<String> getMembersFromShardName(final String shardName) {
154
155         Preconditions.checkNotNull(shardName, "shardName should not be null");
156
157         if(shardReplicaNames.containsKey(shardName)){
158             return shardReplicaNames.get(shardName);
159         }
160
161         for(ModuleShard ms : moduleShards){
162             for(Shard s : ms.getShards()) {
163                 if(s.getName().equals(shardName)){
164                     List<String> replicas = s.getReplicas();
165                     shardReplicaNames.put(shardName, replicas);
166                     return replicas;
167                 }
168             }
169         }
170         shardReplicaNames.put(shardName, Collections.<String>emptyList());
171         return Collections.emptyList();
172     }
173
174     @Override public Set<String> getAllShardNames() {
175         Set<String> shardNames = new LinkedHashSet<>();
176         for(ModuleShard ms : moduleShards){
177             for(Shard s : ms.getShards()) {
178                 shardNames.add(s.getName());
179             }
180         }
181         return shardNames;
182     }
183
184
185
186     private List<Module> readModules(final Config modulesConfig) {
187         List<? extends ConfigObject> modulesConfigObjectList =
188             modulesConfig.getObjectList("modules");
189
190         final Builder<Module> b = ImmutableList.builder();
191         for(ConfigObject o : modulesConfigObjectList){
192             ConfigObjectWrapper w = new ConfigObjectWrapper(o);
193             b.add(new Module(w.stringValue("name"), w.stringValue(
194                 "namespace"), w.stringValue("shard-strategy")));
195         }
196
197         return b.build();
198     }
199
200     private static List<ModuleShard> readModuleShards(final Config moduleShardsConfig) {
201         List<? extends ConfigObject> moduleShardsConfigObjectList =
202             moduleShardsConfig.getObjectList("module-shards");
203
204         final Builder<ModuleShard> b = ImmutableList.builder();
205         for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){
206
207             String moduleName = moduleShardConfigObject.get("name").unwrapped().toString();
208
209             List<? extends ConfigObject> shardsConfigObjectList =
210                 moduleShardConfigObject.toConfig().getObjectList("shards");
211
212             List<Shard> shards = new ArrayList<>();
213
214             for(ConfigObject shard : shardsConfigObjectList){
215                 String shardName = shard.get("name").unwrapped().toString();
216                 List<String> replicas = shard.toConfig().getStringList("replicas");
217                 shards.add(new Shard(shardName, replicas));
218             }
219
220             b.add(new ModuleShard(moduleName, shards));
221         }
222
223         return b.build();
224     }
225
226
227     private static class ModuleShard {
228         private final String moduleName;
229         private final List<Shard> shards;
230
231         public ModuleShard(final String moduleName, final List<Shard> shards) {
232             this.moduleName = moduleName;
233             this.shards = shards;
234         }
235
236         public String getModuleName() {
237             return moduleName;
238         }
239
240         public List<Shard> getShards() {
241             return shards;
242         }
243     }
244
245     private static class Shard {
246         private final String name;
247         private final List<String> replicas;
248
249         Shard(final String name, final List<String> replicas) {
250             this.name = name;
251             this.replicas = replicas;
252         }
253
254         public String getName() {
255             return name;
256         }
257
258         public List<String> getReplicas() {
259             return replicas;
260         }
261     }
262
263     private class Module {
264
265         private final String name;
266         private final String nameSpace;
267         private final ShardStrategy shardStrategy;
268
269         Module(final String name, final String nameSpace, final String shardStrategy) {
270             this.name = name;
271             this.nameSpace = nameSpace;
272             if(ModuleShardStrategy.NAME.equals(shardStrategy)){
273                 this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this);
274             } else {
275                 this.shardStrategy = new DefaultShardStrategy();
276             }
277         }
278
279         public String getName() {
280             return name;
281         }
282
283         public String getNameSpace() {
284             return nameSpace;
285         }
286
287         public ShardStrategy getShardStrategy() {
288             return shardStrategy;
289         }
290     }
291
292
293     private static class ConfigObjectWrapper{
294
295         private final ConfigObject configObject;
296
297         ConfigObjectWrapper(final ConfigObject configObject){
298             this.configObject = configObject;
299         }
300
301         public String stringValue(final String name){
302             return configObject.get(name).unwrapped().toString();
303         }
304     }
305 }