Merge "Implementation of ModuleShardStrategy"
[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
89
90     private void readModules(Config modulesConfig) {
91         List<? extends ConfigObject> modulesConfigObjectList =
92             modulesConfig.getObjectList("modules");
93
94         for(ConfigObject o : modulesConfigObjectList){
95             ConfigObjectWrapper w = new ConfigObjectWrapper(o);
96             modules.add(new Module(w.stringValue("name"), w.stringValue(
97                 "namespace"), w.stringValue("shard-strategy")));
98         }
99     }
100
101     private void readModuleShards(Config moduleShardsConfig) {
102         List<? extends ConfigObject> moduleShardsConfigObjectList =
103             moduleShardsConfig.getObjectList("module-shards");
104
105         for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){
106
107             String moduleName = moduleShardConfigObject.get("name").unwrapped().toString();
108
109             List<? extends ConfigObject> shardsConfigObjectList =
110                 moduleShardConfigObject.toConfig().getObjectList("shards");
111
112             List<Shard> shards = new ArrayList<>();
113
114             for(ConfigObject shard : shardsConfigObjectList){
115                 String shardName = shard.get("name").unwrapped().toString();
116                 List<String> replicas = shard.toConfig().getStringList("replicas");
117                 shards.add(new Shard(shardName, replicas));
118             }
119
120             this.moduleShards.add(new ModuleShard(moduleName, shards));
121         }
122     }
123
124
125     private class ModuleShard {
126         private final String moduleName;
127         private final List<Shard> shards;
128
129         public ModuleShard(String moduleName, List<Shard> shards) {
130             this.moduleName = moduleName;
131             this.shards = shards;
132         }
133
134         public String getModuleName() {
135             return moduleName;
136         }
137
138         public List<Shard> getShards() {
139             return shards;
140         }
141     }
142
143     private class Shard {
144         private final String name;
145         private final List<String> replicas;
146
147         Shard(String name, List<String> replicas) {
148             this.name = name;
149             this.replicas = replicas;
150         }
151
152         public String getName() {
153             return name;
154         }
155
156         public List<String> getReplicas() {
157             return replicas;
158         }
159     }
160
161     private class Module {
162
163         private final String name;
164         private final String nameSpace;
165         private final ShardStrategy shardStrategy;
166
167         Module(String name, String nameSpace, String shardStrategy) {
168             this.name = name;
169             this.nameSpace = nameSpace;
170             if(ModuleShardStrategy.NAME.equals(shardStrategy)){
171                 this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this);
172             } else {
173                 this.shardStrategy = new DefaultShardStrategy();
174             }
175         }
176
177         public String getName() {
178             return name;
179         }
180
181         public String getNameSpace() {
182             return nameSpace;
183         }
184
185         public ShardStrategy getShardStrategy() {
186             return shardStrategy;
187         }
188     }
189
190
191     private static class ConfigObjectWrapper{
192
193         private final ConfigObject configObject;
194
195         ConfigObjectWrapper(ConfigObject configObject){
196             this.configObject = configObject;
197         }
198
199         public String stringValue(String name){
200             return configObject.get(name).unwrapped().toString();
201         }
202     }
203 }