2 * Copyright (c) 2015 Brocade Communications 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
8 package org.opendaylight.controller.cluster.datastore.config;
10 import com.typesafe.config.Config;
11 import com.typesafe.config.ConfigFactory;
12 import com.typesafe.config.ConfigObject;
14 import java.util.HashMap;
15 import java.util.List;
17 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Implementation of ModuleShardConfigProvider that reads the module and shard configuration from files.
24 * @author Thomas Pantelis
26 public class FileModuleShardConfigProvider implements ModuleShardConfigProvider {
27 private static final Logger LOG = LoggerFactory.getLogger(FileModuleShardConfigProvider.class);
29 private final String moduleShardsConfigPath;
30 private final String modulesConfigPath;
32 public FileModuleShardConfigProvider(String moduleShardsConfigPath, String modulesConfigPath) {
33 this.moduleShardsConfigPath = moduleShardsConfigPath;
34 this.modulesConfigPath = modulesConfigPath;
38 public Map<String, ModuleConfig.Builder> retrieveModuleConfigs(Configuration configuration) {
39 File moduleShardsFile = new File("./configuration/initial/" + moduleShardsConfigPath);
40 File modulesFile = new File("./configuration/initial/" + modulesConfigPath);
42 Config moduleShardsConfig = null;
43 if(moduleShardsFile.exists()) {
44 LOG.info("module shards config file exists - reading config from it");
45 moduleShardsConfig = ConfigFactory.parseFile(moduleShardsFile);
47 LOG.warn("module shards configuration read from resource");
48 moduleShardsConfig = ConfigFactory.load(moduleShardsConfigPath);
51 Config modulesConfig = null;
52 if(modulesFile.exists()) {
53 LOG.info("modules config file exists - reading config from it");
54 modulesConfig = ConfigFactory.parseFile(modulesFile);
56 LOG.warn("modules configuration read from resource");
57 modulesConfig = ConfigFactory.load(modulesConfigPath);
60 Map<String, ModuleConfig.Builder> moduleConfigMap = readModuleShardsConfig(moduleShardsConfig);
61 readModulesConfig(modulesConfig, moduleConfigMap, configuration);
63 return moduleConfigMap;
66 private static void readModulesConfig(final Config modulesConfig, Map<String, ModuleConfig.Builder> moduleConfigMap,
67 Configuration configuration) {
68 List<? extends ConfigObject> modulesConfigObjectList = modulesConfig.getObjectList("modules");
70 for(ConfigObject o : modulesConfigObjectList){
71 ConfigObjectWrapper w = new ConfigObjectWrapper(o);
73 String moduleName = w.stringValue("name");
74 ModuleConfig.Builder builder = moduleConfigMap.get(moduleName);
76 builder = ModuleConfig.builder(moduleName);
77 moduleConfigMap.put(moduleName, builder);
80 builder.nameSpace(w.stringValue("namespace"));
81 builder.shardStrategy(ShardStrategyFactory.newShardStrategyInstance(moduleName,
82 w.stringValue("shard-strategy"), configuration));
86 private static Map<String, ModuleConfig.Builder> readModuleShardsConfig(final Config moduleShardsConfig) {
87 List<? extends ConfigObject> moduleShardsConfigObjectList =
88 moduleShardsConfig.getObjectList("module-shards");
90 Map<String, ModuleConfig.Builder> moduleConfigMap = new HashMap<>();
91 for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){
92 String moduleName = moduleShardConfigObject.get("name").unwrapped().toString();
93 ModuleConfig.Builder builder = ModuleConfig.builder(moduleName);
95 List<? extends ConfigObject> shardsConfigObjectList =
96 moduleShardConfigObject.toConfig().getObjectList("shards");
98 for(ConfigObject shard : shardsConfigObjectList){
99 String shardName = shard.get("name").unwrapped().toString();
100 List<String> replicas = shard.toConfig().getStringList("replicas");
101 builder.shardConfig(shardName, replicas);
104 moduleConfigMap.put(moduleName, builder);
107 return moduleConfigMap;
110 private static class ConfigObjectWrapper{
112 private final ConfigObject configObject;
114 ConfigObjectWrapper(final ConfigObject configObject){
115 this.configObject = configObject;
118 public String stringValue(final String name){
119 return configObject.get(name).unwrapped().toString();