2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore;
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;
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;
31 public class ConfigurationImpl implements Configuration {
33 private final List<ModuleShard> moduleShards = new ArrayList<>();
35 private final List<Module> modules = new ArrayList<>();
37 private static final Logger
38 LOG = LoggerFactory.getLogger(DistributedDataStore.class);
40 // Look up maps to speed things up
42 // key = memberName, value = list of shardNames
43 private Map<String, List<String>> memberShardNames = new HashMap<>();
45 // key = shardName, value = list of replicaNames (replicaNames are the same as memberNames)
46 private Map<String, List<String>> shardReplicaNames = new HashMap<>();
49 public ConfigurationImpl(String moduleShardsConfigPath,
51 String modulesConfigPath){
53 Preconditions.checkNotNull(moduleShardsConfigPath, "moduleShardsConfigPath should not be null");
54 Preconditions.checkNotNull(modulesConfigPath, "modulesConfigPath should not be null");
57 File moduleShardsFile = new File("./configuration/initial/" + moduleShardsConfigPath);
58 File modulesFile = new File("./configuration/initial/" + modulesConfigPath);
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);
65 LOG.warn("module shards configuration read from resource");
66 moduleShardsConfig = ConfigFactory.load(moduleShardsConfigPath);
69 Config modulesConfig = null;
70 if(modulesFile.exists()) {
71 LOG.info("modules config file exists - reading config from it");
72 modulesConfig = ConfigFactory.parseFile(modulesFile);
74 LOG.warn("modules configuration read from resource");
75 modulesConfig = ConfigFactory.load(modulesConfigPath);
78 readModuleShards(moduleShardsConfig);
80 readModules(modulesConfig);
83 @Override public List<String> getMemberShardNames(String memberName){
85 Preconditions.checkNotNull(memberName, "memberName should not be null");
87 if(memberShardNames.containsKey(memberName)){
88 return memberShardNames.get(memberName);
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());
102 memberShardNames.put(memberName, shards);
108 @Override public Optional<String> getModuleNameFromNameSpace(String nameSpace) {
110 Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
112 for(Module m : modules){
113 if(m.getNameSpace().equals(nameSpace)){
114 return Optional.of(m.getName());
117 return Optional.absent();
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());
128 @Override public List<String> getShardNamesFromModuleName(String moduleName) {
130 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
132 for(ModuleShard m : moduleShards){
133 if(m.getModuleName().equals(moduleName)){
134 List<String> l = new ArrayList<>();
135 for(Shard s : m.getShards()){
142 return Collections.EMPTY_LIST;
145 @Override public List<String> getMembersFromShardName(String shardName) {
147 Preconditions.checkNotNull(shardName, "shardName should not be null");
149 if(shardReplicaNames.containsKey(shardName)){
150 return shardReplicaNames.get(shardName);
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);
162 shardReplicaNames.put(shardName, Collections.EMPTY_LIST);
163 return Collections.EMPTY_LIST;
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());
178 private void readModules(Config modulesConfig) {
179 List<? extends ConfigObject> modulesConfigObjectList =
180 modulesConfig.getObjectList("modules");
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")));
189 private void readModuleShards(Config moduleShardsConfig) {
190 List<? extends ConfigObject> moduleShardsConfigObjectList =
191 moduleShardsConfig.getObjectList("module-shards");
193 for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){
195 String moduleName = moduleShardConfigObject.get("name").unwrapped().toString();
197 List<? extends ConfigObject> shardsConfigObjectList =
198 moduleShardConfigObject.toConfig().getObjectList("shards");
200 List<Shard> shards = new ArrayList<>();
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));
208 this.moduleShards.add(new ModuleShard(moduleName, shards));
213 private class ModuleShard {
214 private final String moduleName;
215 private final List<Shard> shards;
217 public ModuleShard(String moduleName, List<Shard> shards) {
218 this.moduleName = moduleName;
219 this.shards = shards;
222 public String getModuleName() {
226 public List<Shard> getShards() {
231 private class Shard {
232 private final String name;
233 private final List<String> replicas;
235 Shard(String name, List<String> replicas) {
237 this.replicas = replicas;
240 public String getName() {
244 public List<String> getReplicas() {
249 private class Module {
251 private final String name;
252 private final String nameSpace;
253 private final ShardStrategy shardStrategy;
255 Module(String name, String nameSpace, String shardStrategy) {
257 this.nameSpace = nameSpace;
258 if(ModuleShardStrategy.NAME.equals(shardStrategy)){
259 this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this);
261 this.shardStrategy = new DefaultShardStrategy();
265 public String getName() {
269 public String getNameSpace() {
273 public ShardStrategy getShardStrategy() {
274 return shardStrategy;
279 private static class ConfigObjectWrapper{
281 private final ConfigObject configObject;
283 ConfigObjectWrapper(ConfigObject configObject){
284 this.configObject = configObject;
287 public String stringValue(String name){
288 return configObject.get(name).unwrapped().toString();