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.List;
29 public class ConfigurationImpl implements Configuration {
31 private final List<ModuleShard> moduleShards = new ArrayList<>();
33 private final List<Module> modules = new ArrayList<>();
35 private static final Logger
36 LOG = LoggerFactory.getLogger(DistributedDataStore.class);
38 // Look up maps to speed things up
40 // key = memberName, value = list of shardNames
41 private Map<String, List<String>> memberShardNames = new HashMap<>();
43 // key = shardName, value = list of replicaNames (replicaNames are the same as memberNames)
44 private Map<String, List<String>> shardReplicaNames = new HashMap<>();
47 public ConfigurationImpl(String moduleShardsConfigPath,
49 String modulesConfigPath){
51 Preconditions.checkNotNull(moduleShardsConfigPath, "moduleShardsConfigPath should not be null");
52 Preconditions.checkNotNull(modulesConfigPath, "modulesConfigPath should not be null");
55 File moduleShardsFile = new File("./configuration/initial/" + moduleShardsConfigPath);
56 File modulesFile = new File("./configuration/initial/" + modulesConfigPath);
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);
63 LOG.warn("module shards configuration read from resource");
64 moduleShardsConfig = ConfigFactory.load(moduleShardsConfigPath);
67 Config modulesConfig = null;
68 if(modulesFile.exists()) {
69 LOG.info("modules config file exists - reading config from it");
70 modulesConfig = ConfigFactory.parseFile(modulesFile);
72 LOG.warn("modules configuration read from resource");
73 modulesConfig = ConfigFactory.load(modulesConfigPath);
76 readModuleShards(moduleShardsConfig);
78 readModules(modulesConfig);
81 @Override public List<String> getMemberShardNames(String memberName){
83 Preconditions.checkNotNull(memberName, "memberName should not be null");
85 if(memberShardNames.containsKey(memberName)){
86 return memberShardNames.get(memberName);
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());
100 memberShardNames.put(memberName, shards);
106 @Override public Optional<String> getModuleNameFromNameSpace(String nameSpace) {
108 Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
110 for(Module m : modules){
111 if(m.getNameSpace().equals(nameSpace)){
112 return Optional.of(m.getName());
115 return Optional.absent();
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());
126 @Override public List<String> getShardNamesFromModuleName(String moduleName) {
128 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
130 for(ModuleShard m : moduleShards){
131 if(m.getModuleName().equals(moduleName)){
132 List<String> l = new ArrayList<>();
133 for(Shard s : m.getShards()){
140 return Collections.EMPTY_LIST;
143 @Override public List<String> getMembersFromShardName(String shardName) {
145 Preconditions.checkNotNull(shardName, "shardName should not be null");
147 if(shardReplicaNames.containsKey(shardName)){
148 return shardReplicaNames.get(shardName);
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);
160 shardReplicaNames.put(shardName, Collections.EMPTY_LIST);
161 return Collections.EMPTY_LIST;
166 private void readModules(Config modulesConfig) {
167 List<? extends ConfigObject> modulesConfigObjectList =
168 modulesConfig.getObjectList("modules");
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")));
177 private void readModuleShards(Config moduleShardsConfig) {
178 List<? extends ConfigObject> moduleShardsConfigObjectList =
179 moduleShardsConfig.getObjectList("module-shards");
181 for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){
183 String moduleName = moduleShardConfigObject.get("name").unwrapped().toString();
185 List<? extends ConfigObject> shardsConfigObjectList =
186 moduleShardConfigObject.toConfig().getObjectList("shards");
188 List<Shard> shards = new ArrayList<>();
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));
196 this.moduleShards.add(new ModuleShard(moduleName, shards));
201 private class ModuleShard {
202 private final String moduleName;
203 private final List<Shard> shards;
205 public ModuleShard(String moduleName, List<Shard> shards) {
206 this.moduleName = moduleName;
207 this.shards = shards;
210 public String getModuleName() {
214 public List<Shard> getShards() {
219 private class Shard {
220 private final String name;
221 private final List<String> replicas;
223 Shard(String name, List<String> replicas) {
225 this.replicas = replicas;
228 public String getName() {
232 public List<String> getReplicas() {
237 private class Module {
239 private final String name;
240 private final String nameSpace;
241 private final ShardStrategy shardStrategy;
243 Module(String name, String nameSpace, String shardStrategy) {
245 this.nameSpace = nameSpace;
246 if(ModuleShardStrategy.NAME.equals(shardStrategy)){
247 this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this);
249 this.shardStrategy = new DefaultShardStrategy();
253 public String getName() {
257 public String getNameSpace() {
261 public ShardStrategy getShardStrategy() {
262 return shardStrategy;
267 private static class ConfigObjectWrapper{
269 private final ConfigObject configObject;
271 ConfigObjectWrapper(ConfigObject configObject){
272 this.configObject = configObject;
275 public String stringValue(String name){
276 return configObject.get(name).unwrapped().toString();