Merge "Modify the FindPrimary implementation so that it works correctly with a config...
[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
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 public class ConfigurationImpl implements Configuration {
26
27     private final List<ModuleShard> moduleShards = new ArrayList<>();
28
29     private final List<Module> modules = new ArrayList<>();
30
31
32     public ConfigurationImpl(String moduleShardsConfigPath,
33         String modulesConfigPath){
34         Config moduleShardsConfig = ConfigFactory.load(moduleShardsConfigPath);
35         Config modulesConfig = ConfigFactory.load(modulesConfigPath);
36
37         readModuleShards(moduleShardsConfig);
38
39         readModules(modulesConfig);
40     }
41
42     @Override public List<String> getMemberShardNames(String memberName){
43         List<String> shards = new ArrayList();
44         for(ModuleShard ms : moduleShards){
45             for(Shard s : ms.getShards()){
46                 for(String m : s.getReplicas()){
47                     if(memberName.equals(m)){
48                         shards.add(s.getName());
49                     }
50                 }
51             }
52         }
53         return shards;
54
55     }
56
57     @Override public Optional<String> getModuleNameFromNameSpace(String nameSpace) {
58         for(Module m : modules){
59             if(m.getNameSpace().equals(nameSpace)){
60                 return Optional.of(m.getName());
61             }
62         }
63         return Optional.absent();
64     }
65
66     @Override public Map<String, ShardStrategy> getModuleNameToShardStrategyMap() {
67         Map<String, ShardStrategy> map = new HashMap<>();
68         for(Module m : modules){
69             map.put(m.getName(), m.getShardStrategy());
70         }
71         return map;
72     }
73
74     @Override public List<String> getShardNamesFromModuleName(String moduleName) {
75         for(ModuleShard m : moduleShards){
76             if(m.getModuleName().equals(moduleName)){
77                 List<String> l = new ArrayList<>();
78                 for(Shard s : m.getShards()){
79                     l.add(s.getName());
80                 }
81                 return l;
82             }
83         }
84
85         return Collections.EMPTY_LIST;
86     }
87
88     @Override public List<String> getMembersFromShardName(String shardName) {
89         List<String> shards = new ArrayList();
90         for(ModuleShard ms : moduleShards){
91             for(Shard s : ms.getShards()) {
92                 if(s.getName().equals(shardName)){
93                     return s.getReplicas();
94                 }
95             }
96         }
97         return Collections.EMPTY_LIST;
98     }
99
100
101
102     private void readModules(Config modulesConfig) {
103         List<? extends ConfigObject> modulesConfigObjectList =
104             modulesConfig.getObjectList("modules");
105
106         for(ConfigObject o : modulesConfigObjectList){
107             ConfigObjectWrapper w = new ConfigObjectWrapper(o);
108             modules.add(new Module(w.stringValue("name"), w.stringValue(
109                 "namespace"), w.stringValue("shard-strategy")));
110         }
111     }
112
113     private void readModuleShards(Config moduleShardsConfig) {
114         List<? extends ConfigObject> moduleShardsConfigObjectList =
115             moduleShardsConfig.getObjectList("module-shards");
116
117         for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){
118
119             String moduleName = moduleShardConfigObject.get("name").unwrapped().toString();
120
121             List<? extends ConfigObject> shardsConfigObjectList =
122                 moduleShardConfigObject.toConfig().getObjectList("shards");
123
124             List<Shard> shards = new ArrayList<>();
125
126             for(ConfigObject shard : shardsConfigObjectList){
127                 String shardName = shard.get("name").unwrapped().toString();
128                 List<String> replicas = shard.toConfig().getStringList("replicas");
129                 shards.add(new Shard(shardName, replicas));
130             }
131
132             this.moduleShards.add(new ModuleShard(moduleName, shards));
133         }
134     }
135
136
137     private class ModuleShard {
138         private final String moduleName;
139         private final List<Shard> shards;
140
141         public ModuleShard(String moduleName, List<Shard> shards) {
142             this.moduleName = moduleName;
143             this.shards = shards;
144         }
145
146         public String getModuleName() {
147             return moduleName;
148         }
149
150         public List<Shard> getShards() {
151             return shards;
152         }
153     }
154
155     private class Shard {
156         private final String name;
157         private final List<String> replicas;
158
159         Shard(String name, List<String> replicas) {
160             this.name = name;
161             this.replicas = replicas;
162         }
163
164         public String getName() {
165             return name;
166         }
167
168         public List<String> getReplicas() {
169             return replicas;
170         }
171     }
172
173     private class Module {
174
175         private final String name;
176         private final String nameSpace;
177         private final ShardStrategy shardStrategy;
178
179         Module(String name, String nameSpace, String shardStrategy) {
180             this.name = name;
181             this.nameSpace = nameSpace;
182             if(ModuleShardStrategy.NAME.equals(shardStrategy)){
183                 this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this);
184             } else {
185                 this.shardStrategy = new DefaultShardStrategy();
186             }
187         }
188
189         public String getName() {
190             return name;
191         }
192
193         public String getNameSpace() {
194             return nameSpace;
195         }
196
197         public ShardStrategy getShardStrategy() {
198             return shardStrategy;
199         }
200     }
201
202
203     private static class ConfigObjectWrapper{
204
205         private final ConfigObject configObject;
206
207         ConfigObjectWrapper(ConfigObject configObject){
208             this.configObject = configObject;
209         }
210
211         public String stringValue(String name){
212             return configObject.get(name).unwrapped().toString();
213         }
214     }
215 }