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