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