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.typesafe.config.Config;
13 import com.typesafe.config.ConfigFactory;
14 import com.typesafe.config.ConfigObject;
15 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
16 import org.opendaylight.controller.cluster.datastore.shardstrategy.ModuleShardStrategy;
17 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
28 public class ConfigurationImpl implements Configuration {
30 private final List<ModuleShard> moduleShards = new ArrayList<>();
32 private final List<Module> modules = new ArrayList<>();
34 private static final Logger
35 LOG = LoggerFactory.getLogger(DistributedDataStore.class);
38 public ConfigurationImpl(String moduleShardsConfigPath,
40 String modulesConfigPath){
42 File moduleShardsFile = new File("./configuration/initial/" + moduleShardsConfigPath);
43 File modulesFile = new File("./configuration/initial/" + modulesConfigPath);
45 Config moduleShardsConfig = null;
46 if(moduleShardsFile.exists()) {
47 LOG.info("module shards config file exists - reading config from it");
48 moduleShardsConfig = ConfigFactory.parseFile(moduleShardsFile);
50 LOG.warn("module shards configuration read from resource");
51 moduleShardsConfig = ConfigFactory.load(moduleShardsConfigPath);
54 Config modulesConfig = null;
55 if(modulesFile.exists()) {
56 LOG.info("modules config file exists - reading config from it");
57 modulesConfig = ConfigFactory.parseFile(modulesFile);
59 LOG.warn("modules configuration read from resource");
60 modulesConfig = ConfigFactory.load(modulesConfigPath);
63 readModuleShards(moduleShardsConfig);
65 readModules(modulesConfig);
68 @Override public List<String> getMemberShardNames(String memberName){
69 List<String> shards = new ArrayList();
70 for(ModuleShard ms : moduleShards){
71 for(Shard s : ms.getShards()){
72 for(String m : s.getReplicas()){
73 if(memberName.equals(m)){
74 shards.add(s.getName());
83 @Override public Optional<String> getModuleNameFromNameSpace(String nameSpace) {
84 for(Module m : modules){
85 if(m.getNameSpace().equals(nameSpace)){
86 return Optional.of(m.getName());
89 return Optional.absent();
92 @Override public Map<String, ShardStrategy> getModuleNameToShardStrategyMap() {
93 Map<String, ShardStrategy> map = new HashMap<>();
94 for(Module m : modules){
95 map.put(m.getName(), m.getShardStrategy());
100 @Override public List<String> getShardNamesFromModuleName(String moduleName) {
101 for(ModuleShard m : moduleShards){
102 if(m.getModuleName().equals(moduleName)){
103 List<String> l = new ArrayList<>();
104 for(Shard s : m.getShards()){
111 return Collections.EMPTY_LIST;
114 @Override public List<String> getMembersFromShardName(String shardName) {
115 List<String> shards = new ArrayList();
116 for(ModuleShard ms : moduleShards){
117 for(Shard s : ms.getShards()) {
118 if(s.getName().equals(shardName)){
119 return s.getReplicas();
123 return Collections.EMPTY_LIST;
128 private void readModules(Config modulesConfig) {
129 List<? extends ConfigObject> modulesConfigObjectList =
130 modulesConfig.getObjectList("modules");
132 for(ConfigObject o : modulesConfigObjectList){
133 ConfigObjectWrapper w = new ConfigObjectWrapper(o);
134 modules.add(new Module(w.stringValue("name"), w.stringValue(
135 "namespace"), w.stringValue("shard-strategy")));
139 private void readModuleShards(Config moduleShardsConfig) {
140 List<? extends ConfigObject> moduleShardsConfigObjectList =
141 moduleShardsConfig.getObjectList("module-shards");
143 for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){
145 String moduleName = moduleShardConfigObject.get("name").unwrapped().toString();
147 List<? extends ConfigObject> shardsConfigObjectList =
148 moduleShardConfigObject.toConfig().getObjectList("shards");
150 List<Shard> shards = new ArrayList<>();
152 for(ConfigObject shard : shardsConfigObjectList){
153 String shardName = shard.get("name").unwrapped().toString();
154 List<String> replicas = shard.toConfig().getStringList("replicas");
155 shards.add(new Shard(shardName, replicas));
158 this.moduleShards.add(new ModuleShard(moduleName, shards));
163 private class ModuleShard {
164 private final String moduleName;
165 private final List<Shard> shards;
167 public ModuleShard(String moduleName, List<Shard> shards) {
168 this.moduleName = moduleName;
169 this.shards = shards;
172 public String getModuleName() {
176 public List<Shard> getShards() {
181 private class Shard {
182 private final String name;
183 private final List<String> replicas;
185 Shard(String name, List<String> replicas) {
187 this.replicas = replicas;
190 public String getName() {
194 public List<String> getReplicas() {
199 private class Module {
201 private final String name;
202 private final String nameSpace;
203 private final ShardStrategy shardStrategy;
205 Module(String name, String nameSpace, String shardStrategy) {
207 this.nameSpace = nameSpace;
208 if(ModuleShardStrategy.NAME.equals(shardStrategy)){
209 this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this);
211 this.shardStrategy = new DefaultShardStrategy();
215 public String getName() {
219 public String getNameSpace() {
223 public ShardStrategy getShardStrategy() {
224 return shardStrategy;
229 private static class ConfigObjectWrapper{
231 private final ConfigObject configObject;
233 ConfigObjectWrapper(ConfigObject configObject){
234 this.configObject = configObject;
237 public String stringValue(String name){
238 return configObject.get(name).unwrapped().toString();