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.google.common.collect.ImmutableMap;
16 import com.typesafe.config.Config;
17 import com.typesafe.config.ConfigFactory;
18 import com.typesafe.config.ConfigObject;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.LinkedHashSet;
24 import java.util.List;
27 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
28 import org.opendaylight.controller.cluster.datastore.shardstrategy.ModuleShardStrategy;
29 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class ConfigurationImpl implements Configuration {
35 private final List<ModuleShard> moduleShards;
37 private final List<Module> modules;
39 private static final Logger
40 LOG = LoggerFactory.getLogger(DistributedDataStore.class);
42 // Look up maps to speed things up
44 // key = memberName, value = list of shardNames
45 private final Map<String, List<String>> memberShardNames = new HashMap<>();
47 // key = shardName, value = list of replicaNames (replicaNames are the same as memberNames)
48 private final Map<String, List<String>> shardReplicaNames = new HashMap<>();
50 private final Map<String, String> namespaceToModuleName;
52 public ConfigurationImpl(final String moduleShardsConfigPath,
54 final String modulesConfigPath){
56 Preconditions.checkNotNull(moduleShardsConfigPath, "moduleShardsConfigPath should not be null");
57 Preconditions.checkNotNull(modulesConfigPath, "modulesConfigPath should not be null");
60 File moduleShardsFile = new File("./configuration/initial/" + moduleShardsConfigPath);
61 File modulesFile = new File("./configuration/initial/" + modulesConfigPath);
63 Config moduleShardsConfig = null;
64 if(moduleShardsFile.exists()) {
65 LOG.info("module shards config file exists - reading config from it");
66 moduleShardsConfig = ConfigFactory.parseFile(moduleShardsFile);
68 LOG.warn("module shards configuration read from resource");
69 moduleShardsConfig = ConfigFactory.load(moduleShardsConfigPath);
72 Config modulesConfig = null;
73 if(modulesFile.exists()) {
74 LOG.info("modules config file exists - reading config from it");
75 modulesConfig = ConfigFactory.parseFile(modulesFile);
77 LOG.warn("modules configuration read from resource");
78 modulesConfig = ConfigFactory.load(modulesConfigPath);
81 this.moduleShards = readModuleShards(moduleShardsConfig);
82 this.modules = readModules(modulesConfig);
84 namespaceToModuleName = createNamespaceToModuleName(modules);
87 private static Map<String, String> createNamespaceToModuleName(Iterable<Module> modules) {
88 final com.google.common.collect.ImmutableMap.Builder<String, String> b = ImmutableMap.builder();
89 for (Module m : modules) {
90 b.put(m.getNameSpace(), m.getName());
95 @Override public List<String> getMemberShardNames(final String memberName){
97 Preconditions.checkNotNull(memberName, "memberName should not be null");
99 if(memberShardNames.containsKey(memberName)){
100 return memberShardNames.get(memberName);
103 List<String> shards = new ArrayList<>();
104 for(ModuleShard ms : moduleShards){
105 for(Shard s : ms.getShards()){
106 for(String m : s.getReplicas()){
107 if(memberName.equals(m)){
108 shards.add(s.getName());
114 memberShardNames.put(memberName, shards);
121 public Optional<String> getModuleNameFromNameSpace(final String nameSpace) {
122 Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
123 return Optional.fromNullable(namespaceToModuleName.get(nameSpace));
126 @Override public Map<String, ShardStrategy> getModuleNameToShardStrategyMap() {
127 // FIXME: can be constant view of modules
128 Map<String, ShardStrategy> map = new HashMap<>();
129 for(Module m : modules){
130 map.put(m.getName(), m.getShardStrategy());
135 @Override public List<String> getShardNamesFromModuleName(final String moduleName) {
137 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
139 // FIXME: can be constant view of moduleShards
140 for(ModuleShard m : moduleShards){
141 if(m.getModuleName().equals(moduleName)){
142 List<String> l = new ArrayList<>();
143 for(Shard s : m.getShards()){
150 return Collections.emptyList();
153 @Override public List<String> getMembersFromShardName(final String shardName) {
155 Preconditions.checkNotNull(shardName, "shardName should not be null");
157 if(shardReplicaNames.containsKey(shardName)){
158 return shardReplicaNames.get(shardName);
161 for(ModuleShard ms : moduleShards){
162 for(Shard s : ms.getShards()) {
163 if(s.getName().equals(shardName)){
164 List<String> replicas = s.getReplicas();
165 shardReplicaNames.put(shardName, replicas);
170 shardReplicaNames.put(shardName, Collections.<String>emptyList());
171 return Collections.emptyList();
174 @Override public Set<String> getAllShardNames() {
175 Set<String> shardNames = new LinkedHashSet<>();
176 for(ModuleShard ms : moduleShards){
177 for(Shard s : ms.getShards()) {
178 shardNames.add(s.getName());
186 private List<Module> readModules(final Config modulesConfig) {
187 List<? extends ConfigObject> modulesConfigObjectList =
188 modulesConfig.getObjectList("modules");
190 final Builder<Module> b = ImmutableList.builder();
191 for(ConfigObject o : modulesConfigObjectList){
192 ConfigObjectWrapper w = new ConfigObjectWrapper(o);
193 b.add(new Module(w.stringValue("name"), w.stringValue(
194 "namespace"), w.stringValue("shard-strategy")));
200 private static List<ModuleShard> readModuleShards(final Config moduleShardsConfig) {
201 List<? extends ConfigObject> moduleShardsConfigObjectList =
202 moduleShardsConfig.getObjectList("module-shards");
204 final Builder<ModuleShard> b = ImmutableList.builder();
205 for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){
207 String moduleName = moduleShardConfigObject.get("name").unwrapped().toString();
209 List<? extends ConfigObject> shardsConfigObjectList =
210 moduleShardConfigObject.toConfig().getObjectList("shards");
212 List<Shard> shards = new ArrayList<>();
214 for(ConfigObject shard : shardsConfigObjectList){
215 String shardName = shard.get("name").unwrapped().toString();
216 List<String> replicas = shard.toConfig().getStringList("replicas");
217 shards.add(new Shard(shardName, replicas));
220 b.add(new ModuleShard(moduleName, shards));
227 private static class ModuleShard {
228 private final String moduleName;
229 private final List<Shard> shards;
231 public ModuleShard(final String moduleName, final List<Shard> shards) {
232 this.moduleName = moduleName;
233 this.shards = shards;
236 public String getModuleName() {
240 public List<Shard> getShards() {
245 private static class Shard {
246 private final String name;
247 private final List<String> replicas;
249 Shard(final String name, final List<String> replicas) {
251 this.replicas = replicas;
254 public String getName() {
258 public List<String> getReplicas() {
263 private class Module {
265 private final String name;
266 private final String nameSpace;
267 private final ShardStrategy shardStrategy;
269 Module(final String name, final String nameSpace, final String shardStrategy) {
271 this.nameSpace = nameSpace;
272 if(ModuleShardStrategy.NAME.equals(shardStrategy)){
273 this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this);
275 this.shardStrategy = new DefaultShardStrategy();
279 public String getName() {
283 public String getNameSpace() {
287 public ShardStrategy getShardStrategy() {
288 return shardStrategy;
293 private static class ConfigObjectWrapper{
295 private final ConfigObject configObject;
297 ConfigObjectWrapper(final ConfigObject configObject){
298 this.configObject = configObject;
301 public String stringValue(final String name){
302 return configObject.get(name).unwrapped().toString();