Merge "Fixed for bug 1197"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ConfigurationImpl.java
1 /*
2  * Copyright (c) 2014 Cisco 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
9 package org.opendaylight.controller.cluster.datastore;
10
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;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 public class ConfigurationImpl implements Configuration {
29
30     private final List<ModuleShard> moduleShards = new ArrayList<>();
31
32     private final List<Module> modules = new ArrayList<>();
33
34     private static final Logger
35         LOG = LoggerFactory.getLogger(DistributedDataStore.class);
36
37
38     public ConfigurationImpl(String moduleShardsConfigPath,
39
40         String modulesConfigPath){
41
42         File moduleShardsFile = new File("./configuration/initial/" + moduleShardsConfigPath);
43         File modulesFile = new File("./configuration/initial/" + modulesConfigPath);
44
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);
49         } else {
50             LOG.warn("module shards configuration read from resource");
51             moduleShardsConfig = ConfigFactory.load(moduleShardsConfigPath);
52         }
53
54         Config modulesConfig = null;
55         if(modulesFile.exists()) {
56             LOG.info("modules config file exists - reading config from it");
57             modulesConfig = ConfigFactory.parseFile(modulesFile);
58         } else {
59             LOG.warn("modules configuration read from resource");
60             modulesConfig = ConfigFactory.load(modulesConfigPath);
61         }
62
63         readModuleShards(moduleShardsConfig);
64
65         readModules(modulesConfig);
66     }
67
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());
75                     }
76                 }
77             }
78         }
79         return shards;
80
81     }
82
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());
87             }
88         }
89         return Optional.absent();
90     }
91
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());
96         }
97         return map;
98     }
99
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()){
105                     l.add(s.getName());
106                 }
107                 return l;
108             }
109         }
110
111         return Collections.EMPTY_LIST;
112     }
113
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();
120                 }
121             }
122         }
123         return Collections.EMPTY_LIST;
124     }
125
126
127
128     private void readModules(Config modulesConfig) {
129         List<? extends ConfigObject> modulesConfigObjectList =
130             modulesConfig.getObjectList("modules");
131
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")));
136         }
137     }
138
139     private void readModuleShards(Config moduleShardsConfig) {
140         List<? extends ConfigObject> moduleShardsConfigObjectList =
141             moduleShardsConfig.getObjectList("module-shards");
142
143         for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){
144
145             String moduleName = moduleShardConfigObject.get("name").unwrapped().toString();
146
147             List<? extends ConfigObject> shardsConfigObjectList =
148                 moduleShardConfigObject.toConfig().getObjectList("shards");
149
150             List<Shard> shards = new ArrayList<>();
151
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));
156             }
157
158             this.moduleShards.add(new ModuleShard(moduleName, shards));
159         }
160     }
161
162
163     private class ModuleShard {
164         private final String moduleName;
165         private final List<Shard> shards;
166
167         public ModuleShard(String moduleName, List<Shard> shards) {
168             this.moduleName = moduleName;
169             this.shards = shards;
170         }
171
172         public String getModuleName() {
173             return moduleName;
174         }
175
176         public List<Shard> getShards() {
177             return shards;
178         }
179     }
180
181     private class Shard {
182         private final String name;
183         private final List<String> replicas;
184
185         Shard(String name, List<String> replicas) {
186             this.name = name;
187             this.replicas = replicas;
188         }
189
190         public String getName() {
191             return name;
192         }
193
194         public List<String> getReplicas() {
195             return replicas;
196         }
197     }
198
199     private class Module {
200
201         private final String name;
202         private final String nameSpace;
203         private final ShardStrategy shardStrategy;
204
205         Module(String name, String nameSpace, String shardStrategy) {
206             this.name = name;
207             this.nameSpace = nameSpace;
208             if(ModuleShardStrategy.NAME.equals(shardStrategy)){
209                 this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this);
210             } else {
211                 this.shardStrategy = new DefaultShardStrategy();
212             }
213         }
214
215         public String getName() {
216             return name;
217         }
218
219         public String getNameSpace() {
220             return nameSpace;
221         }
222
223         public ShardStrategy getShardStrategy() {
224             return shardStrategy;
225         }
226     }
227
228
229     private static class ConfigObjectWrapper{
230
231         private final ConfigObject configObject;
232
233         ConfigObjectWrapper(ConfigObject configObject){
234             this.configObject = configObject;
235         }
236
237         public String stringValue(String name){
238             return configObject.get(name).unwrapped().toString();
239         }
240     }
241 }