ab52385893a7fb43f00db90cdada2b464a980315
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / config / 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 package org.opendaylight.controller.cluster.datastore.config;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.AbstractMap.SimpleEntry;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.Set;
24 import org.opendaylight.controller.cluster.access.concepts.MemberName;
25 import org.opendaylight.controller.cluster.datastore.shardstrategy.PrefixShardStrategy;
26 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
27 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
28 import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31
32 // TODO clean this up once we get rid of module based configuration, prefix one should be alot simpler
33 public class ConfigurationImpl implements Configuration {
34     private volatile Map<String, ModuleConfig> moduleConfigMap;
35
36     // TODO should this be initialized with something? on restart we should restore the shards from configuration?
37     private volatile Map<DOMDataTreeIdentifier, PrefixShardConfiguration> prefixConfigMap = Collections.emptyMap();
38
39     // Look up maps to speed things up
40
41     private volatile Map<String, String> namespaceToModuleName;
42     private volatile Set<String> allShardNames;
43
44     public ConfigurationImpl(final String moduleShardsConfigPath, final String modulesConfigPath) {
45         this(new FileModuleShardConfigProvider(moduleShardsConfigPath, modulesConfigPath));
46     }
47
48     public ConfigurationImpl(final ModuleShardConfigProvider provider) {
49         ImmutableMap.Builder<String, ModuleConfig> mapBuilder = ImmutableMap.builder();
50         for (Map.Entry<String, ModuleConfig.Builder> e: provider.retrieveModuleConfigs(this).entrySet()) {
51             mapBuilder.put(e.getKey(), e.getValue().build());
52         }
53
54         this.moduleConfigMap = mapBuilder.build();
55
56         this.allShardNames = createAllShardNames(moduleConfigMap.values());
57         this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values());
58     }
59
60     private static Set<String> createAllShardNames(final Iterable<ModuleConfig> moduleConfigs) {
61         final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
62         for (ModuleConfig moduleConfig : moduleConfigs) {
63             builder.addAll(moduleConfig.getShardNames());
64         }
65
66         return builder.build();
67     }
68
69     private static Map<String, String> createNamespaceToModuleName(final Iterable<ModuleConfig> moduleConfigs) {
70         final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
71         for (ModuleConfig moduleConfig : moduleConfigs) {
72             if (moduleConfig.getNamespace() != null) {
73                 builder.put(moduleConfig.getNamespace(), moduleConfig.getName());
74             }
75         }
76
77         return builder.build();
78     }
79
80     @Override
81     public Collection<String> getMemberShardNames(final MemberName memberName) {
82         requireNonNull(memberName, "memberName should not be null");
83
84         List<String> shards = new ArrayList<>();
85         for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
86             for (ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
87                 if (shardConfig.getReplicas().contains(memberName)) {
88                     shards.add(shardConfig.getName());
89                 }
90             }
91         }
92
93         return shards;
94     }
95
96     @Override
97     public String getModuleNameFromNameSpace(final String nameSpace) {
98         return namespaceToModuleName.get(requireNonNull(nameSpace, "nameSpace should not be null"));
99     }
100
101     @Override
102     public ShardStrategy getStrategyForModule(final String moduleName) {
103         ModuleConfig moduleConfig = getModuleConfig(moduleName);
104         return moduleConfig != null ? moduleConfig.getShardStrategy() : null;
105     }
106
107     @Override
108     public String getShardNameForModule(final String moduleName) {
109         ModuleConfig moduleConfig = getModuleConfig(moduleName);
110         if (moduleConfig != null) {
111             Collection<ShardConfig> shardConfigs = moduleConfig.getShardConfigs();
112             if (!shardConfigs.isEmpty()) {
113                 return shardConfigs.iterator().next().getName();
114             }
115         }
116         return null;
117     }
118
119     private ModuleConfig getModuleConfig(final String moduleName) {
120         return moduleConfigMap.get(requireNonNull(moduleName, "moduleName should not be null"));
121     }
122
123     @Override
124     public String getShardNameForPrefix(final DOMDataTreeIdentifier prefix) {
125         requireNonNull(prefix, "prefix should not be null");
126
127         Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> bestMatchEntry = new SimpleEntry<>(
128                 new DOMDataTreeIdentifier(prefix.getDatastoreType(), YangInstanceIdentifier.empty()), null);
129
130         for (Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
131             if (entry.getKey().contains(prefix) && entry.getKey().getRootIdentifier().getPathArguments().size()
132                     > bestMatchEntry.getKey().getRootIdentifier().getPathArguments().size()) {
133                 bestMatchEntry = entry;
134             }
135         }
136
137         //TODO we really should have mapping based on prefix instead of Strings
138         return ClusterUtils.getCleanShardName(bestMatchEntry.getKey().getRootIdentifier());
139     }
140
141     @Override
142     public Collection<MemberName> getMembersFromShardName(final String shardName) {
143         checkNotNullShardName(shardName);
144
145         for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
146             ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
147             if (shardConfig != null) {
148                 return shardConfig.getReplicas();
149             }
150         }
151
152         for (final PrefixShardConfiguration prefixConfig : prefixConfigMap.values()) {
153             if (shardName.equals(ClusterUtils.getCleanShardName(prefixConfig.getPrefix().getRootIdentifier()))) {
154                 return prefixConfig.getShardMemberNames();
155             }
156         }
157
158         return Collections.emptyList();
159     }
160
161     private static void checkNotNullShardName(final String shardName) {
162         requireNonNull(shardName, "shardName should not be null");
163     }
164
165     @Override
166     public Set<String> getAllShardNames() {
167         return allShardNames;
168     }
169
170     @Override
171     public Collection<MemberName> getUniqueMemberNamesForAllShards() {
172         Set<MemberName> allNames = new HashSet<>();
173         for (String shardName: getAllShardNames()) {
174             allNames.addAll(getMembersFromShardName(shardName));
175         }
176
177         return allNames;
178     }
179
180     @Override
181     public synchronized void addModuleShardConfiguration(final ModuleShardConfiguration config) {
182         requireNonNull(config, "ModuleShardConfiguration should not be null");
183
184         ModuleConfig moduleConfig = ModuleConfig.builder(config.getModuleName())
185                 .nameSpace(config.getNamespace().toASCIIString())
186                 .shardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName()))
187                 .shardConfig(config.getShardName(), config.getShardMemberNames()).build();
188
189         updateModuleConfigMap(moduleConfig);
190
191         namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName)
192                 .put(moduleConfig.getNamespace(), moduleConfig.getName()).build();
193         allShardNames = ImmutableSet.<String>builder().addAll(allShardNames).add(config.getShardName()).build();
194     }
195
196     @Override
197     public void addPrefixShardConfiguration(final PrefixShardConfiguration config) {
198         addPrefixConfig(requireNonNull(config, "PrefixShardConfiguration cannot be null"));
199         allShardNames = ImmutableSet.<String>builder().addAll(allShardNames)
200                 .add(ClusterUtils.getCleanShardName(config.getPrefix().getRootIdentifier())).build();
201     }
202
203     @Override
204     public void removePrefixShardConfiguration(final DOMDataTreeIdentifier prefix) {
205         removePrefixConfig(requireNonNull(prefix, "Prefix cannot be null"));
206
207         final HashSet<String> temp = new HashSet<>(allShardNames);
208         temp.remove(ClusterUtils.getCleanShardName(prefix.getRootIdentifier()));
209
210         allShardNames = ImmutableSet.copyOf(temp);
211     }
212
213     @Override
214     public Map<DOMDataTreeIdentifier, PrefixShardConfiguration> getAllPrefixShardConfigurations() {
215         return ImmutableMap.copyOf(prefixConfigMap);
216     }
217
218     private void addPrefixConfig(final PrefixShardConfiguration config) {
219         final Map<DOMDataTreeIdentifier, PrefixShardConfiguration> newPrefixConfigMap = new HashMap<>(prefixConfigMap);
220         newPrefixConfigMap.put(config.getPrefix(), config);
221         prefixConfigMap = ImmutableMap.copyOf(newPrefixConfigMap);
222     }
223
224     private void removePrefixConfig(final DOMDataTreeIdentifier prefix) {
225         final Map<DOMDataTreeIdentifier, PrefixShardConfiguration> newPrefixConfigMap = new HashMap<>(prefixConfigMap);
226         newPrefixConfigMap.remove(prefix);
227         prefixConfigMap = ImmutableMap.copyOf(newPrefixConfigMap);
228     }
229
230     private ShardStrategy createShardStrategy(final String moduleName, final String shardStrategyName) {
231         return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);
232     }
233
234     @Override
235     public boolean isShardConfigured(final String shardName) {
236         checkNotNullShardName(shardName);
237         return allShardNames.contains(shardName);
238     }
239
240     @Override
241     public void addMemberReplicaForShard(final String shardName, final MemberName newMemberName) {
242         checkNotNullShardName(shardName);
243         requireNonNull(newMemberName, "MemberName should not be null");
244
245         for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
246             ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
247             if (shardConfig != null) {
248                 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
249                 replicas.add(newMemberName);
250                 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
251                 return;
252             }
253         }
254     }
255
256     @Override
257     public void removeMemberReplicaForShard(final String shardName, final MemberName newMemberName) {
258         checkNotNullShardName(shardName);
259         requireNonNull(newMemberName, "MemberName should not be null");
260
261         for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
262             ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
263             if (shardConfig != null) {
264                 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
265                 replicas.remove(newMemberName);
266                 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
267                 return;
268             }
269         }
270     }
271
272     @Override
273     public ShardStrategy getStrategyForPrefix(final DOMDataTreeIdentifier prefix) {
274         requireNonNull(prefix, "Prefix cannot be null");
275         // FIXME using prefix tables like in mdsal will be better
276         Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> bestMatchEntry = new SimpleEntry<>(
277                 new DOMDataTreeIdentifier(prefix.getDatastoreType(), YangInstanceIdentifier.empty()), null);
278
279         for (Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
280             if (entry.getKey().contains(prefix) && entry.getKey().getRootIdentifier().getPathArguments().size()
281                     > bestMatchEntry.getKey().getRootIdentifier().getPathArguments().size()) {
282                 bestMatchEntry = entry;
283             }
284         }
285
286         if (bestMatchEntry.getValue() == null) {
287             return null;
288         }
289         return new PrefixShardStrategy(ClusterUtils
290                 .getCleanShardName(bestMatchEntry.getKey().getRootIdentifier()),
291                 bestMatchEntry.getKey().getRootIdentifier());
292     }
293
294     private void updateModuleConfigMap(final ModuleConfig moduleConfig) {
295         final Map<String, ModuleConfig> newModuleConfigMap = new HashMap<>(moduleConfigMap);
296         newModuleConfigMap.put(moduleConfig.getName(), moduleConfig);
297         moduleConfigMap = ImmutableMap.copyOf(newModuleConfigMap);
298     }
299 }