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;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.LinkedHashSet;
21 import java.util.List;
24 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
25 import org.opendaylight.controller.cluster.datastore.shardstrategy.ModuleShardStrategy;
26 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class ConfigurationImpl implements Configuration {
32 private final List<ModuleShard> moduleShards = new ArrayList<>();
34 private final List<Module> modules = new ArrayList<>();
36 private static final Logger
37 LOG = LoggerFactory.getLogger(DistributedDataStore.class);
39 // Look up maps to speed things up
41 // key = memberName, value = list of shardNames
42 private final Map<String, List<String>> memberShardNames = new HashMap<>();
44 // key = shardName, value = list of replicaNames (replicaNames are the same as memberNames)
45 private final Map<String, List<String>> shardReplicaNames = new HashMap<>();
48 public ConfigurationImpl(final String moduleShardsConfigPath,
50 final String modulesConfigPath){
52 Preconditions.checkNotNull(moduleShardsConfigPath, "moduleShardsConfigPath should not be null");
53 Preconditions.checkNotNull(modulesConfigPath, "modulesConfigPath should not be null");
56 File moduleShardsFile = new File("./configuration/initial/" + moduleShardsConfigPath);
57 File modulesFile = new File("./configuration/initial/" + modulesConfigPath);
59 Config moduleShardsConfig = null;
60 if(moduleShardsFile.exists()) {
61 LOG.info("module shards config file exists - reading config from it");
62 moduleShardsConfig = ConfigFactory.parseFile(moduleShardsFile);
64 LOG.warn("module shards configuration read from resource");
65 moduleShardsConfig = ConfigFactory.load(moduleShardsConfigPath);
68 Config modulesConfig = null;
69 if(modulesFile.exists()) {
70 LOG.info("modules config file exists - reading config from it");
71 modulesConfig = ConfigFactory.parseFile(modulesFile);
73 LOG.warn("modules configuration read from resource");
74 modulesConfig = ConfigFactory.load(modulesConfigPath);
77 readModuleShards(moduleShardsConfig);
79 readModules(modulesConfig);
82 @Override public List<String> getMemberShardNames(final String memberName){
84 Preconditions.checkNotNull(memberName, "memberName should not be null");
86 if(memberShardNames.containsKey(memberName)){
87 return memberShardNames.get(memberName);
90 List<String> shards = new ArrayList<>();
91 for(ModuleShard ms : moduleShards){
92 for(Shard s : ms.getShards()){
93 for(String m : s.getReplicas()){
94 if(memberName.equals(m)){
95 shards.add(s.getName());
101 memberShardNames.put(memberName, shards);
107 @Override public Optional<String> getModuleNameFromNameSpace(final String nameSpace) {
109 Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
111 for(Module m : modules){
112 if(m.getNameSpace().equals(nameSpace)){
113 return Optional.of(m.getName());
116 return Optional.absent();
119 @Override public Map<String, ShardStrategy> getModuleNameToShardStrategyMap() {
120 Map<String, ShardStrategy> map = new HashMap<>();
121 for(Module m : modules){
122 map.put(m.getName(), m.getShardStrategy());
127 @Override public List<String> getShardNamesFromModuleName(final String moduleName) {
129 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
131 for(ModuleShard m : moduleShards){
132 if(m.getModuleName().equals(moduleName)){
133 List<String> l = new ArrayList<>();
134 for(Shard s : m.getShards()){
141 return Collections.emptyList();
144 @Override public List<String> getMembersFromShardName(final String shardName) {
146 Preconditions.checkNotNull(shardName, "shardName should not be null");
148 if(shardReplicaNames.containsKey(shardName)){
149 return shardReplicaNames.get(shardName);
152 for(ModuleShard ms : moduleShards){
153 for(Shard s : ms.getShards()) {
154 if(s.getName().equals(shardName)){
155 List<String> replicas = s.getReplicas();
156 shardReplicaNames.put(shardName, replicas);
161 shardReplicaNames.put(shardName, Collections.<String>emptyList());
162 return Collections.emptyList();
165 @Override public Set<String> getAllShardNames() {
166 Set<String> shardNames = new LinkedHashSet<>();
167 for(ModuleShard ms : moduleShards){
168 for(Shard s : ms.getShards()) {
169 shardNames.add(s.getName());
177 private void readModules(final Config modulesConfig) {
178 List<? extends ConfigObject> modulesConfigObjectList =
179 modulesConfig.getObjectList("modules");
181 for(ConfigObject o : modulesConfigObjectList){
182 ConfigObjectWrapper w = new ConfigObjectWrapper(o);
183 modules.add(new Module(w.stringValue("name"), w.stringValue(
184 "namespace"), w.stringValue("shard-strategy")));
188 private void readModuleShards(final Config moduleShardsConfig) {
189 List<? extends ConfigObject> moduleShardsConfigObjectList =
190 moduleShardsConfig.getObjectList("module-shards");
192 for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){
194 String moduleName = moduleShardConfigObject.get("name").unwrapped().toString();
196 List<? extends ConfigObject> shardsConfigObjectList =
197 moduleShardConfigObject.toConfig().getObjectList("shards");
199 List<Shard> shards = new ArrayList<>();
201 for(ConfigObject shard : shardsConfigObjectList){
202 String shardName = shard.get("name").unwrapped().toString();
203 List<String> replicas = shard.toConfig().getStringList("replicas");
204 shards.add(new Shard(shardName, replicas));
207 this.moduleShards.add(new ModuleShard(moduleName, shards));
212 private class ModuleShard {
213 private final String moduleName;
214 private final List<Shard> shards;
216 public ModuleShard(final String moduleName, final List<Shard> shards) {
217 this.moduleName = moduleName;
218 this.shards = shards;
221 public String getModuleName() {
225 public List<Shard> getShards() {
230 private class Shard {
231 private final String name;
232 private final List<String> replicas;
234 Shard(final String name, final List<String> replicas) {
236 this.replicas = replicas;
239 public String getName() {
243 public List<String> getReplicas() {
248 private class Module {
250 private final String name;
251 private final String nameSpace;
252 private final ShardStrategy shardStrategy;
254 Module(final String name, final String nameSpace, final String shardStrategy) {
256 this.nameSpace = nameSpace;
257 if(ModuleShardStrategy.NAME.equals(shardStrategy)){
258 this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this);
260 this.shardStrategy = new DefaultShardStrategy();
264 public String getName() {
268 public String getNameSpace() {
272 public ShardStrategy getShardStrategy() {
273 return shardStrategy;
278 private static class ConfigObjectWrapper{
280 private final ConfigObject configObject;
282 ConfigObjectWrapper(final ConfigObject configObject){
283 this.configObject = configObject;
286 public String stringValue(final String name){
287 return configObject.get(name).unwrapped().toString();