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