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.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;
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;
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;
32 public class ConfigurationImpl implements Configuration {
34 private final List<ModuleShard> moduleShards;
36 private final List<Module> modules;
38 private static final Logger
39 LOG = LoggerFactory.getLogger(DistributedDataStore.class);
41 // Look up maps to speed things up
43 // key = memberName, value = list of shardNames
44 private final Map<String, List<String>> memberShardNames = new HashMap<>();
46 // key = shardName, value = list of replicaNames (replicaNames are the same as memberNames)
47 private final Map<String, List<String>> shardReplicaNames = new HashMap<>();
50 public ConfigurationImpl(final String moduleShardsConfigPath,
52 final String modulesConfigPath){
54 Preconditions.checkNotNull(moduleShardsConfigPath, "moduleShardsConfigPath should not be null");
55 Preconditions.checkNotNull(modulesConfigPath, "modulesConfigPath should not be null");
58 File moduleShardsFile = new File("./configuration/initial/" + moduleShardsConfigPath);
59 File modulesFile = new File("./configuration/initial/" + modulesConfigPath);
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);
66 LOG.warn("module shards configuration read from resource");
67 moduleShardsConfig = ConfigFactory.load(moduleShardsConfigPath);
70 Config modulesConfig = null;
71 if(modulesFile.exists()) {
72 LOG.info("modules config file exists - reading config from it");
73 modulesConfig = ConfigFactory.parseFile(modulesFile);
75 LOG.warn("modules configuration read from resource");
76 modulesConfig = ConfigFactory.load(modulesConfigPath);
79 this.moduleShards = readModuleShards(moduleShardsConfig);
80 this.modules = readModules(modulesConfig);
83 @Override public List<String> getMemberShardNames(final 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(final 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(final 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.emptyList();
145 @Override public List<String> getMembersFromShardName(final 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.<String>emptyList());
163 return Collections.emptyList();
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 List<Module> readModules(final Config modulesConfig) {
179 List<? extends ConfigObject> modulesConfigObjectList =
180 modulesConfig.getObjectList("modules");
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")));
192 private static List<ModuleShard> readModuleShards(final Config moduleShardsConfig) {
193 List<? extends ConfigObject> moduleShardsConfigObjectList =
194 moduleShardsConfig.getObjectList("module-shards");
196 final Builder<ModuleShard> b = ImmutableList.builder();
197 for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){
199 String moduleName = moduleShardConfigObject.get("name").unwrapped().toString();
201 List<? extends ConfigObject> shardsConfigObjectList =
202 moduleShardConfigObject.toConfig().getObjectList("shards");
204 List<Shard> shards = new ArrayList<>();
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));
212 b.add(new ModuleShard(moduleName, shards));
219 private static class ModuleShard {
220 private final String moduleName;
221 private final List<Shard> shards;
223 public ModuleShard(final String moduleName, final List<Shard> shards) {
224 this.moduleName = moduleName;
225 this.shards = shards;
228 public String getModuleName() {
232 public List<Shard> getShards() {
237 private static class Shard {
238 private final String name;
239 private final List<String> replicas;
241 Shard(final String name, final List<String> replicas) {
243 this.replicas = replicas;
246 public String getName() {
250 public List<String> getReplicas() {
255 private class Module {
257 private final String name;
258 private final String nameSpace;
259 private final ShardStrategy shardStrategy;
261 Module(final String name, final String nameSpace, final String shardStrategy) {
263 this.nameSpace = nameSpace;
264 if(ModuleShardStrategy.NAME.equals(shardStrategy)){
265 this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this);
267 this.shardStrategy = new DefaultShardStrategy();
271 public String getName() {
275 public String getNameSpace() {
279 public ShardStrategy getShardStrategy() {
280 return shardStrategy;
285 private static class ConfigObjectWrapper{
287 private final ConfigObject configObject;
289 ConfigObjectWrapper(final ConfigObject configObject){
290 this.configObject = configObject;
293 public String stringValue(final String name){
294 return configObject.get(name).unwrapped().toString();