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.ImmutableListMultimap;
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.collect.ImmutableSet;
18 import com.google.common.collect.ListMultimap;
19 import com.typesafe.config.Config;
20 import com.typesafe.config.ConfigFactory;
21 import com.typesafe.config.ConfigObject;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
29 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
30 import org.opendaylight.controller.cluster.datastore.shardstrategy.ModuleShardStrategy;
31 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class ConfigurationImpl implements Configuration {
37 private final List<ModuleShard> moduleShards;
39 private final List<Module> modules;
41 private static final Logger
42 LOG = LoggerFactory.getLogger(DistributedDataStore.class);
44 // Look up maps to speed things up
46 // key = memberName, value = list of shardNames
47 private final Map<String, List<String>> memberShardNames = new HashMap<>();
49 // key = shardName, value = list of replicaNames (replicaNames are the same as memberNames)
50 private final Map<String, List<String>> shardReplicaNames = new HashMap<>();
52 private final ListMultimap<String, String> moduleNameToShardName;
53 private final Map<String, ShardStrategy> moduleNameToStrategy;
54 private final Map<String, String> namespaceToModuleName;
55 private final Set<String> allShardNames;
57 public ConfigurationImpl(final String moduleShardsConfigPath,
59 final String modulesConfigPath){
61 Preconditions.checkNotNull(moduleShardsConfigPath, "moduleShardsConfigPath should not be null");
62 Preconditions.checkNotNull(modulesConfigPath, "modulesConfigPath should not be null");
65 File moduleShardsFile = new File("./configuration/initial/" + moduleShardsConfigPath);
66 File modulesFile = new File("./configuration/initial/" + modulesConfigPath);
68 Config moduleShardsConfig = null;
69 if(moduleShardsFile.exists()) {
70 LOG.info("module shards config file exists - reading config from it");
71 moduleShardsConfig = ConfigFactory.parseFile(moduleShardsFile);
73 LOG.warn("module shards configuration read from resource");
74 moduleShardsConfig = ConfigFactory.load(moduleShardsConfigPath);
77 Config modulesConfig = null;
78 if(modulesFile.exists()) {
79 LOG.info("modules config file exists - reading config from it");
80 modulesConfig = ConfigFactory.parseFile(modulesFile);
82 LOG.warn("modules configuration read from resource");
83 modulesConfig = ConfigFactory.load(modulesConfigPath);
86 this.moduleShards = readModuleShards(moduleShardsConfig);
87 this.modules = readModules(modulesConfig);
89 this.allShardNames = createAllShardNames(moduleShards);
90 this.moduleNameToShardName = createModuleNameToShardName(moduleShards);
91 this.moduleNameToStrategy = createModuleNameToStrategy(modules);
92 this.namespaceToModuleName = createNamespaceToModuleName(modules);
95 private static Set<String> createAllShardNames(Iterable<ModuleShard> moduleShards) {
96 final com.google.common.collect.ImmutableSet.Builder<String> b = ImmutableSet.builder();
97 for(ModuleShard ms : moduleShards){
98 for(Shard s : ms.getShards()) {
105 private static Map<String, ShardStrategy> createModuleNameToStrategy(Iterable<Module> modules) {
106 final com.google.common.collect.ImmutableMap.Builder<String, ShardStrategy> b = ImmutableMap.builder();
107 for (Module m : modules) {
108 b.put(m.getName(), m.getShardStrategy());
113 private static Map<String, String> createNamespaceToModuleName(Iterable<Module> modules) {
114 final com.google.common.collect.ImmutableMap.Builder<String, String> b = ImmutableMap.builder();
115 for (Module m : modules) {
116 b.put(m.getNameSpace(), m.getName());
121 private static ListMultimap<String, String> createModuleNameToShardName(Iterable<ModuleShard> moduleShards) {
122 final com.google.common.collect.ImmutableListMultimap.Builder<String, String> b = ImmutableListMultimap.builder();
124 for (ModuleShard m : moduleShards) {
125 for (Shard s : m.getShards()) {
126 b.put(m.getModuleName(), s.getName());
133 @Override public List<String> getMemberShardNames(final String memberName){
135 Preconditions.checkNotNull(memberName, "memberName should not be null");
137 if(memberShardNames.containsKey(memberName)){
138 return memberShardNames.get(memberName);
141 List<String> shards = new ArrayList<>();
142 for(ModuleShard ms : moduleShards){
143 for(Shard s : ms.getShards()){
144 for(String m : s.getReplicas()){
145 if(memberName.equals(m)){
146 shards.add(s.getName());
152 memberShardNames.put(memberName, shards);
159 public Optional<String> getModuleNameFromNameSpace(final String nameSpace) {
160 Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
161 return Optional.fromNullable(namespaceToModuleName.get(nameSpace));
165 public Map<String, ShardStrategy> getModuleNameToShardStrategyMap() {
166 return moduleNameToStrategy;
170 public List<String> getShardNamesFromModuleName(final String moduleName) {
171 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
172 return moduleNameToShardName.get(moduleName);
175 @Override public List<String> getMembersFromShardName(final String shardName) {
177 Preconditions.checkNotNull(shardName, "shardName should not be null");
179 if(shardReplicaNames.containsKey(shardName)){
180 return shardReplicaNames.get(shardName);
183 for(ModuleShard ms : moduleShards){
184 for(Shard s : ms.getShards()) {
185 if(s.getName().equals(shardName)){
186 List<String> replicas = s.getReplicas();
187 shardReplicaNames.put(shardName, replicas);
192 shardReplicaNames.put(shardName, Collections.<String>emptyList());
193 return Collections.emptyList();
197 public Set<String> getAllShardNames() {
198 return allShardNames;
201 private List<Module> readModules(final Config modulesConfig) {
202 List<? extends ConfigObject> modulesConfigObjectList =
203 modulesConfig.getObjectList("modules");
205 final Builder<Module> b = ImmutableList.builder();
206 for(ConfigObject o : modulesConfigObjectList){
207 ConfigObjectWrapper w = new ConfigObjectWrapper(o);
208 b.add(new Module(w.stringValue("name"), w.stringValue(
209 "namespace"), w.stringValue("shard-strategy")));
215 private static List<ModuleShard> readModuleShards(final Config moduleShardsConfig) {
216 List<? extends ConfigObject> moduleShardsConfigObjectList =
217 moduleShardsConfig.getObjectList("module-shards");
219 final Builder<ModuleShard> b = ImmutableList.builder();
220 for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){
222 String moduleName = moduleShardConfigObject.get("name").unwrapped().toString();
224 List<? extends ConfigObject> shardsConfigObjectList =
225 moduleShardConfigObject.toConfig().getObjectList("shards");
227 List<Shard> shards = new ArrayList<>();
229 for(ConfigObject shard : shardsConfigObjectList){
230 String shardName = shard.get("name").unwrapped().toString();
231 List<String> replicas = shard.toConfig().getStringList("replicas");
232 shards.add(new Shard(shardName, replicas));
235 b.add(new ModuleShard(moduleName, shards));
241 private static class ModuleShard {
242 private final String moduleName;
243 private final List<Shard> shards;
245 public ModuleShard(final String moduleName, final List<Shard> shards) {
246 this.moduleName = moduleName;
247 this.shards = shards;
250 public String getModuleName() {
254 public List<Shard> getShards() {
259 private static class Shard {
260 private final String name;
261 private final List<String> replicas;
263 Shard(final String name, final List<String> replicas) {
265 this.replicas = replicas;
268 public String getName() {
272 public List<String> getReplicas() {
277 private class Module {
279 private final String name;
280 private final String nameSpace;
281 private final ShardStrategy shardStrategy;
283 Module(final String name, final String nameSpace, final String shardStrategy) {
285 this.nameSpace = nameSpace;
286 if(ModuleShardStrategy.NAME.equals(shardStrategy)){
287 this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this);
289 this.shardStrategy = DefaultShardStrategy.getInstance();
293 public String getName() {
297 public String getNameSpace() {
301 public ShardStrategy getShardStrategy() {
302 return shardStrategy;
307 private static class ConfigObjectWrapper{
309 private final ConfigObject configObject;
311 ConfigObjectWrapper(final ConfigObject configObject){
312 this.configObject = configObject;
315 public String stringValue(final String name){
316 return configObject.get(name).unwrapped().toString();