2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore.config;
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;
22 import java.util.Map.Entry;
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.yangtools.yang.data.api.YangInstanceIdentifier;
33 // TODO clean this up once we get rid of module based configuration, prefix one should be alot simpler
34 public class ConfigurationImpl implements Configuration {
35 private volatile Map<String, ModuleConfig> moduleConfigMap;
37 // TODO should this be initialized with something? on restart we should restore the shards from configuration?
38 private volatile Map<YangInstanceIdentifier, PrefixShardConfiguration> prefixConfigMap = Collections.emptyMap();
40 // Look up maps to speed things up
42 private volatile Map<String, String> namespaceToModuleName;
43 private volatile Set<String> allShardNames;
45 public ConfigurationImpl(final String moduleShardsConfigPath, final String modulesConfigPath) {
46 this(new FileModuleShardConfigProvider(moduleShardsConfigPath, modulesConfigPath));
49 public ConfigurationImpl(final ModuleShardConfigProvider provider) {
50 ImmutableMap.Builder<String, ModuleConfig> mapBuilder = ImmutableMap.builder();
51 for (Map.Entry<String, ModuleConfig.Builder> e: provider.retrieveModuleConfigs(this).entrySet()) {
52 mapBuilder.put(e.getKey(), e.getValue().build());
55 this.moduleConfigMap = mapBuilder.build();
57 this.allShardNames = createAllShardNames(moduleConfigMap.values());
58 this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values());
61 private static Set<String> createAllShardNames(Iterable<ModuleConfig> moduleConfigs) {
62 final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
63 for (ModuleConfig moduleConfig : moduleConfigs) {
64 builder.addAll(moduleConfig.getShardNames());
67 return builder.build();
70 private static Map<String, String> createNamespaceToModuleName(Iterable<ModuleConfig> moduleConfigs) {
71 final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
72 for (ModuleConfig moduleConfig : moduleConfigs) {
73 if (moduleConfig.getNamespace() != null) {
74 builder.put(moduleConfig.getNamespace(), moduleConfig.getName());
78 return builder.build();
82 public Collection<String> getMemberShardNames(final MemberName memberName) {
83 Preconditions.checkNotNull(memberName, "memberName should not be null");
85 List<String> shards = new ArrayList<>();
86 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
87 for (ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
88 if (shardConfig.getReplicas().contains(memberName)) {
89 shards.add(shardConfig.getName());
98 public String getModuleNameFromNameSpace(final String nameSpace) {
99 Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
101 return namespaceToModuleName.get(nameSpace);
105 public ShardStrategy getStrategyForModule(String moduleName) {
106 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
108 ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
109 return moduleConfig != null ? moduleConfig.getShardStrategy() : null;
113 public String getShardNameForModule(final String moduleName) {
114 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
116 ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
117 Collection<ShardConfig> shardConfigs = moduleConfig != null ? moduleConfig.getShardConfigs() :
118 Collections.<ShardConfig>emptySet();
119 return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName() : null;
124 public String getShardNameForPrefix(@Nonnull final YangInstanceIdentifier prefix) {
125 Preconditions.checkNotNull(prefix, "prefix should not be null");
127 Entry<YangInstanceIdentifier, PrefixShardConfiguration> bestMatchEntry =
128 new SimpleEntry<>(YangInstanceIdentifier.EMPTY, null);
130 for (Entry<YangInstanceIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
131 if (entry.getKey().contains(prefix) && entry.getKey().getPathArguments().size()
132 > bestMatchEntry.getKey().getPathArguments().size()) {
133 bestMatchEntry = entry;
137 //TODO we really should have mapping based on prefix instead of Strings
138 return ClusterUtils.getCleanShardName(bestMatchEntry.getValue().getPrefix().getRootIdentifier());
142 public Collection<MemberName> getMembersFromShardName(final String shardName) {
143 Preconditions.checkNotNull(shardName, "shardName should not be null");
145 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
146 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
147 if (shardConfig != null) {
148 return shardConfig.getReplicas();
152 for (final PrefixShardConfiguration prefixConfig : prefixConfigMap.values()) {
153 if (shardName.equals(ClusterUtils.getCleanShardName(prefixConfig.getPrefix().getRootIdentifier()))) {
154 return prefixConfig.getShardMemberNames();
158 return Collections.emptyList();
162 public Set<String> getAllShardNames() {
163 return allShardNames;
167 public Collection<MemberName> getUniqueMemberNamesForAllShards() {
168 Set<MemberName> allNames = new HashSet<>();
169 for (String shardName: getAllShardNames()) {
170 allNames.addAll(getMembersFromShardName(shardName));
177 public synchronized void addModuleShardConfiguration(ModuleShardConfiguration config) {
178 Preconditions.checkNotNull(config, "ModuleShardConfiguration should not be null");
180 ModuleConfig moduleConfig = ModuleConfig.builder(config.getModuleName())
181 .nameSpace(config.getNamespace().toASCIIString())
182 .shardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName()))
183 .shardConfig(config.getShardName(), config.getShardMemberNames()).build();
185 updateModuleConfigMap(moduleConfig);
187 namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName)
188 .put(moduleConfig.getNamespace(), moduleConfig.getName()).build();
189 allShardNames = ImmutableSet.<String>builder().addAll(allShardNames).add(config.getShardName()).build();
193 public void addPrefixShardConfiguration(@Nonnull final PrefixShardConfiguration config) {
194 Preconditions.checkNotNull(config, "PrefixShardConfiguration cannot be null");
195 updatePrefixConfigMap(config);
196 allShardNames = ImmutableSet.<String>builder().addAll(allShardNames)
197 .add(ClusterUtils.getCleanShardName(config.getPrefix().getRootIdentifier())).build();
200 private void updatePrefixConfigMap(final PrefixShardConfiguration config) {
201 final Map<YangInstanceIdentifier, PrefixShardConfiguration> newPrefixConfigMap = new HashMap<>(prefixConfigMap);
202 newPrefixConfigMap.put(config.getPrefix().getRootIdentifier(), config);
203 prefixConfigMap = ImmutableMap.copyOf(newPrefixConfigMap);
206 private ShardStrategy createShardStrategy(String moduleName, String shardStrategyName) {
207 return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);
211 public boolean isShardConfigured(String shardName) {
212 Preconditions.checkNotNull(shardName, "shardName should not be null");
213 return allShardNames.contains(shardName);
217 public void addMemberReplicaForShard(String shardName, MemberName newMemberName) {
218 Preconditions.checkNotNull(shardName, "shardName should not be null");
219 Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
221 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
222 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
223 if (shardConfig != null) {
224 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
225 replicas.add(newMemberName);
226 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
233 public void removeMemberReplicaForShard(String shardName, MemberName newMemberName) {
234 Preconditions.checkNotNull(shardName, "shardName should not be null");
235 Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
237 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
238 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
239 if (shardConfig != null) {
240 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
241 replicas.remove(newMemberName);
242 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
249 public ShardStrategy getStrategyForPrefix(@Nonnull final YangInstanceIdentifier prefix) {
250 Preconditions.checkNotNull(prefix, "Prefix cannot be null");
251 // FIXME using prefix tables like in mdsal will be better
252 Entry<YangInstanceIdentifier, PrefixShardConfiguration> bestMatchEntry =
253 new SimpleEntry<>(YangInstanceIdentifier.EMPTY, null);
255 for (Entry<YangInstanceIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
256 if (entry.getKey().contains(prefix) && entry.getKey().getPathArguments().size()
257 > bestMatchEntry.getKey().getPathArguments().size()) {
258 bestMatchEntry = entry;
262 if (bestMatchEntry.getValue() == null) {
265 return new PrefixShardStrategy(
266 ClusterUtils.getCleanShardName(bestMatchEntry.getValue().getPrefix().getRootIdentifier()), this);
269 private void updateModuleConfigMap(final ModuleConfig moduleConfig) {
270 final Map<String, ModuleConfig> newModuleConfigMap = new HashMap<>(moduleConfigMap);
271 newModuleConfigMap.put(moduleConfig.getName(), moduleConfig);
272 moduleConfigMap = ImmutableMap.copyOf(newModuleConfigMap);