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