BUG-2138: Create DistributedShardFrontend
[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         Preconditions.checkNotNull(shardName, "shardName should not be null");
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     @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(@Nonnull 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(@Nonnull 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         Preconditions.checkNotNull(shardName, "shardName should not be null");
238         return allShardNames.contains(shardName);
239     }
240
241     @Override
242     public void addMemberReplicaForShard(String shardName, MemberName newMemberName) {
243         Preconditions.checkNotNull(shardName, "shardName should not be null");
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         Preconditions.checkNotNull(shardName, "shardName should not be null");
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(@Nonnull 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 }