import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
+import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Set;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import org.opendaylight.controller.cluster.access.concepts.MemberName;
+import org.opendaylight.controller.cluster.datastore.shardstrategy.PrefixShardStrategy;
import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
+import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+// TODO clean this up once we get rid of module based configuration, prefix one should be alot simpler
public class ConfigurationImpl implements Configuration {
private volatile Map<String, ModuleConfig> moduleConfigMap;
+ // TODO should this be initialized with something? on restart we should restore the shards from configuration?
+ private volatile Map<DOMDataTreeIdentifier, PrefixShardConfiguration> prefixConfigMap = Collections.emptyMap();
+
// Look up maps to speed things up
private volatile Map<String, String> namespaceToModuleName;
private static Map<String, String> createNamespaceToModuleName(Iterable<ModuleConfig> moduleConfigs) {
final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
for (ModuleConfig moduleConfig : moduleConfigs) {
- if (moduleConfig.getNameSpace() != null) {
- builder.put(moduleConfig.getNameSpace(), moduleConfig.getName());
+ if (moduleConfig.getNamespace() != null) {
+ builder.put(moduleConfig.getNamespace(), moduleConfig.getName());
}
}
return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName() : null;
}
+ @Nullable
+ @Override
+ public String getShardNameForPrefix(@Nonnull final DOMDataTreeIdentifier prefix) {
+ Preconditions.checkNotNull(prefix, "prefix should not be null");
+
+ Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> bestMatchEntry =
+ new SimpleEntry<>(
+ new DOMDataTreeIdentifier(prefix.getDatastoreType(), YangInstanceIdentifier.EMPTY), null);
+
+ for (Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
+ if (entry.getKey().contains(prefix) && entry.getKey().getRootIdentifier().getPathArguments().size()
+ > bestMatchEntry.getKey().getRootIdentifier().getPathArguments().size()) {
+ bestMatchEntry = entry;
+ }
+ }
+
+ //TODO we really should have mapping based on prefix instead of Strings
+ return ClusterUtils.getCleanShardName(bestMatchEntry.getKey().getRootIdentifier());
+ }
+
@Override
public Collection<MemberName> getMembersFromShardName(final String shardName) {
- Preconditions.checkNotNull(shardName, "shardName should not be null");
+ checkNotNullShardName(shardName);
for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
}
}
+ for (final PrefixShardConfiguration prefixConfig : prefixConfigMap.values()) {
+ if (shardName.equals(ClusterUtils.getCleanShardName(prefixConfig.getPrefix().getRootIdentifier()))) {
+ return prefixConfig.getShardMemberNames();
+ }
+ }
+
return Collections.emptyList();
}
+ private static void checkNotNullShardName(final String shardName) {
+ Preconditions.checkNotNull(shardName, "shardName should not be null");
+ }
+
@Override
public Set<String> getAllShardNames() {
return allShardNames;
updateModuleConfigMap(moduleConfig);
namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName)
- .put(moduleConfig.getNameSpace(), moduleConfig.getName()).build();
+ .put(moduleConfig.getNamespace(), moduleConfig.getName()).build();
allShardNames = ImmutableSet.<String>builder().addAll(allShardNames).add(config.getShardName()).build();
}
+ @Override
+ public void addPrefixShardConfiguration(@Nonnull final PrefixShardConfiguration config) {
+ Preconditions.checkNotNull(config, "PrefixShardConfiguration cannot be null");
+ addPrefixConfig(config);
+ allShardNames = ImmutableSet.<String>builder().addAll(allShardNames)
+ .add(ClusterUtils.getCleanShardName(config.getPrefix().getRootIdentifier())).build();
+ }
+
+ @Override
+ public void removePrefixShardConfiguration(@Nonnull final DOMDataTreeIdentifier prefix) {
+ Preconditions.checkNotNull(prefix, "Prefix cannot be null");
+
+ removePrefixConfig(prefix);
+
+ final HashSet<String> temp = new HashSet<>(allShardNames);
+ temp.remove(ClusterUtils.getCleanShardName(prefix.getRootIdentifier()));
+
+ allShardNames = ImmutableSet.copyOf(temp);
+ }
+
+ @Override
+ public Map<DOMDataTreeIdentifier, PrefixShardConfiguration> getAllPrefixShardConfigurations() {
+ return ImmutableMap.copyOf(prefixConfigMap);
+ }
+
+ private void addPrefixConfig(final PrefixShardConfiguration config) {
+ final Map<DOMDataTreeIdentifier, PrefixShardConfiguration> newPrefixConfigMap = new HashMap<>(prefixConfigMap);
+ newPrefixConfigMap.put(config.getPrefix(), config);
+ prefixConfigMap = ImmutableMap.copyOf(newPrefixConfigMap);
+ }
+
+ private void removePrefixConfig(final DOMDataTreeIdentifier prefix) {
+ final Map<DOMDataTreeIdentifier, PrefixShardConfiguration> newPrefixConfigMap = new HashMap<>(prefixConfigMap);
+ newPrefixConfigMap.remove(prefix);
+ prefixConfigMap = ImmutableMap.copyOf(newPrefixConfigMap);
+ }
+
private ShardStrategy createShardStrategy(String moduleName, String shardStrategyName) {
return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);
}
@Override
public boolean isShardConfigured(String shardName) {
- Preconditions.checkNotNull(shardName, "shardName should not be null");
+ checkNotNullShardName(shardName);
return allShardNames.contains(shardName);
}
@Override
public void addMemberReplicaForShard(String shardName, MemberName newMemberName) {
- Preconditions.checkNotNull(shardName, "shardName should not be null");
+ checkNotNullShardName(shardName);
Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
@Override
public void removeMemberReplicaForShard(String shardName, MemberName newMemberName) {
- Preconditions.checkNotNull(shardName, "shardName should not be null");
+ checkNotNullShardName(shardName);
Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
}
}
- private void updateModuleConfigMap(ModuleConfig moduleConfig) {
- Map<String, ModuleConfig> newModuleConfigMap = new HashMap<>(moduleConfigMap);
+ @Override
+ public ShardStrategy getStrategyForPrefix(@Nonnull final DOMDataTreeIdentifier prefix) {
+ Preconditions.checkNotNull(prefix, "Prefix cannot be null");
+ // FIXME using prefix tables like in mdsal will be better
+ Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> bestMatchEntry =
+ new SimpleEntry<>(
+ new DOMDataTreeIdentifier(prefix.getDatastoreType(), YangInstanceIdentifier.EMPTY), null);
+
+ for (Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
+ if (entry.getKey().contains(prefix) && entry.getKey().getRootIdentifier().getPathArguments().size()
+ > bestMatchEntry.getKey().getRootIdentifier().getPathArguments().size()) {
+ bestMatchEntry = entry;
+ }
+ }
+
+ if (bestMatchEntry.getValue() == null) {
+ return null;
+ }
+ return new PrefixShardStrategy(ClusterUtils
+ .getCleanShardName(bestMatchEntry.getKey().getRootIdentifier()),
+ bestMatchEntry.getKey().getRootIdentifier());
+ }
+
+ private void updateModuleConfigMap(final ModuleConfig moduleConfig) {
+ final Map<String, ModuleConfig> newModuleConfigMap = new HashMap<>(moduleConfigMap);
newModuleConfigMap.put(moduleConfig.getName(), moduleConfig);
moduleConfigMap = ImmutableMap.copyOf(newModuleConfigMap);
}