Merge "Fixed Karaf Distribution Archetype add dependent bundles"
[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.LinkedHashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30
31 public class ConfigurationImpl implements Configuration {
32
33     private final List<ModuleShard> moduleShards = new ArrayList<>();
34
35     private final List<Module> modules = new ArrayList<>();
36
37     private static final Logger
38         LOG = LoggerFactory.getLogger(DistributedDataStore.class);
39
40     // Look up maps to speed things up
41
42     // key = memberName, value = list of shardNames
43     private Map<String, List<String>> memberShardNames = new HashMap<>();
44
45     // key = shardName, value = list of replicaNames (replicaNames are the same as memberNames)
46     private Map<String, List<String>> shardReplicaNames = new HashMap<>();
47
48
49     public ConfigurationImpl(String moduleShardsConfigPath,
50
51         String modulesConfigPath){
52
53         Preconditions.checkNotNull(moduleShardsConfigPath, "moduleShardsConfigPath should not be null");
54         Preconditions.checkNotNull(modulesConfigPath, "modulesConfigPath should not be null");
55
56
57         File moduleShardsFile = new File("./configuration/initial/" + moduleShardsConfigPath);
58         File modulesFile = new File("./configuration/initial/" + modulesConfigPath);
59
60         Config moduleShardsConfig = null;
61         if(moduleShardsFile.exists()) {
62             LOG.info("module shards config file exists - reading config from it");
63             moduleShardsConfig = ConfigFactory.parseFile(moduleShardsFile);
64         } else {
65             LOG.warn("module shards configuration read from resource");
66             moduleShardsConfig = ConfigFactory.load(moduleShardsConfigPath);
67         }
68
69         Config modulesConfig = null;
70         if(modulesFile.exists()) {
71             LOG.info("modules config file exists - reading config from it");
72             modulesConfig = ConfigFactory.parseFile(modulesFile);
73         } else {
74             LOG.warn("modules configuration read from resource");
75             modulesConfig = ConfigFactory.load(modulesConfigPath);
76         }
77
78         readModuleShards(moduleShardsConfig);
79
80         readModules(modulesConfig);
81     }
82
83     @Override public List<String> getMemberShardNames(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(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(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.EMPTY_LIST;
143     }
144
145     @Override public List<String> getMembersFromShardName(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.EMPTY_LIST);
163         return Collections.EMPTY_LIST;
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 void readModules(Config modulesConfig) {
179         List<? extends ConfigObject> modulesConfigObjectList =
180             modulesConfig.getObjectList("modules");
181
182         for(ConfigObject o : modulesConfigObjectList){
183             ConfigObjectWrapper w = new ConfigObjectWrapper(o);
184             modules.add(new Module(w.stringValue("name"), w.stringValue(
185                 "namespace"), w.stringValue("shard-strategy")));
186         }
187     }
188
189     private void readModuleShards(Config moduleShardsConfig) {
190         List<? extends ConfigObject> moduleShardsConfigObjectList =
191             moduleShardsConfig.getObjectList("module-shards");
192
193         for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){
194
195             String moduleName = moduleShardConfigObject.get("name").unwrapped().toString();
196
197             List<? extends ConfigObject> shardsConfigObjectList =
198                 moduleShardConfigObject.toConfig().getObjectList("shards");
199
200             List<Shard> shards = new ArrayList<>();
201
202             for(ConfigObject shard : shardsConfigObjectList){
203                 String shardName = shard.get("name").unwrapped().toString();
204                 List<String> replicas = shard.toConfig().getStringList("replicas");
205                 shards.add(new Shard(shardName, replicas));
206             }
207
208             this.moduleShards.add(new ModuleShard(moduleName, shards));
209         }
210     }
211
212
213     private class ModuleShard {
214         private final String moduleName;
215         private final List<Shard> shards;
216
217         public ModuleShard(String moduleName, List<Shard> shards) {
218             this.moduleName = moduleName;
219             this.shards = shards;
220         }
221
222         public String getModuleName() {
223             return moduleName;
224         }
225
226         public List<Shard> getShards() {
227             return shards;
228         }
229     }
230
231     private class Shard {
232         private final String name;
233         private final List<String> replicas;
234
235         Shard(String name, List<String> replicas) {
236             this.name = name;
237             this.replicas = replicas;
238         }
239
240         public String getName() {
241             return name;
242         }
243
244         public List<String> getReplicas() {
245             return replicas;
246         }
247     }
248
249     private class Module {
250
251         private final String name;
252         private final String nameSpace;
253         private final ShardStrategy shardStrategy;
254
255         Module(String name, String nameSpace, String shardStrategy) {
256             this.name = name;
257             this.nameSpace = nameSpace;
258             if(ModuleShardStrategy.NAME.equals(shardStrategy)){
259                 this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this);
260             } else {
261                 this.shardStrategy = new DefaultShardStrategy();
262             }
263         }
264
265         public String getName() {
266             return name;
267         }
268
269         public String getNameSpace() {
270             return nameSpace;
271         }
272
273         public ShardStrategy getShardStrategy() {
274             return shardStrategy;
275         }
276     }
277
278
279     private static class ConfigObjectWrapper{
280
281         private final ConfigObject configObject;
282
283         ConfigObjectWrapper(ConfigObject configObject){
284             this.configObject = configObject;
285         }
286
287         public String stringValue(String name){
288             return configObject.get(name).unwrapped().toString();
289         }
290     }
291 }