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