80fc09c97a1a57ddadbe242c41c3dc3cb46e3ea0
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / config / FileModuleShardConfigProvider.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.config;
9
10 import com.typesafe.config.Config;
11 import com.typesafe.config.ConfigFactory;
12 import com.typesafe.config.ConfigObject;
13 import java.io.File;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Implementation of ModuleShardConfigProvider that reads the module and shard configuration from files.
23  *
24  * @author Thomas Pantelis
25  */
26 public class FileModuleShardConfigProvider implements ModuleShardConfigProvider {
27     private static final Logger LOG = LoggerFactory.getLogger(FileModuleShardConfigProvider.class);
28
29     private final String moduleShardsConfigPath;
30     private final String modulesConfigPath;
31
32     public FileModuleShardConfigProvider(String moduleShardsConfigPath, String modulesConfigPath) {
33         this.moduleShardsConfigPath = moduleShardsConfigPath;
34         this.modulesConfigPath = modulesConfigPath;
35     }
36
37     @Override
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);
41
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);
46         } else {
47             LOG.warn("module shards configuration read from resource");
48             moduleShardsConfig = ConfigFactory.load(moduleShardsConfigPath);
49         }
50
51         Config modulesConfig = null;
52         if(modulesFile.exists()) {
53             LOG.info("modules config file exists - reading config from it");
54             modulesConfig = ConfigFactory.parseFile(modulesFile);
55         } else {
56             LOG.warn("modules configuration read from resource");
57             modulesConfig = ConfigFactory.load(modulesConfigPath);
58         }
59
60         Map<String, ModuleConfig.Builder> moduleConfigMap = readModuleShardsConfig(moduleShardsConfig);
61         readModulesConfig(modulesConfig, moduleConfigMap, configuration);
62
63         return moduleConfigMap;
64     }
65
66     private static void readModulesConfig(final Config modulesConfig, Map<String, ModuleConfig.Builder> moduleConfigMap,
67             Configuration configuration) {
68         List<? extends ConfigObject> modulesConfigObjectList = modulesConfig.getObjectList("modules");
69
70         for(ConfigObject o : modulesConfigObjectList){
71             ConfigObjectWrapper w = new ConfigObjectWrapper(o);
72
73             String moduleName = w.stringValue("name");
74             ModuleConfig.Builder builder = moduleConfigMap.get(moduleName);
75             if(builder == null) {
76                 builder = ModuleConfig.builder(moduleName);
77                 moduleConfigMap.put(moduleName, builder);
78             }
79
80             builder.nameSpace(w.stringValue("namespace"));
81             builder.shardStrategy(ShardStrategyFactory.newShardStrategyInstance(moduleName,
82                     w.stringValue("shard-strategy"), configuration));
83         }
84     }
85
86     private static Map<String, ModuleConfig.Builder> readModuleShardsConfig(final Config moduleShardsConfig) {
87         List<? extends ConfigObject> moduleShardsConfigObjectList =
88             moduleShardsConfig.getObjectList("module-shards");
89
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);
94
95             List<? extends ConfigObject> shardsConfigObjectList =
96                 moduleShardConfigObject.toConfig().getObjectList("shards");
97
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);
102             }
103
104             moduleConfigMap.put(moduleName, builder);
105         }
106
107         return moduleConfigMap;
108     }
109
110     private static class ConfigObjectWrapper{
111
112         private final ConfigObject configObject;
113
114         ConfigObjectWrapper(final ConfigObject configObject){
115             this.configObject = configObject;
116         }
117
118         public String stringValue(final String name){
119             return configObject.get(name).unwrapped().toString();
120         }
121     }
122 }