* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.controller.cluster.datastore.config;
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.util.AbstractMap.SimpleEntry;
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
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<YangInstanceIdentifier, PrefixShardConfiguration> prefixConfigMap = Collections.emptyMap();
+ private volatile Map<DOMDataTreeIdentifier, PrefixShardConfiguration> prefixConfigMap = Collections.emptyMap();
// Look up maps to speed things up
this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values());
}
- private static Set<String> createAllShardNames(Iterable<ModuleConfig> moduleConfigs) {
+ private static Set<String> createAllShardNames(final Iterable<ModuleConfig> moduleConfigs) {
final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
for (ModuleConfig moduleConfig : moduleConfigs) {
builder.addAll(moduleConfig.getShardNames());
return builder.build();
}
- private static Map<String, String> createNamespaceToModuleName(Iterable<ModuleConfig> moduleConfigs) {
+ private static Map<String, String> createNamespaceToModuleName(final Iterable<ModuleConfig> moduleConfigs) {
final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
for (ModuleConfig moduleConfig : moduleConfigs) {
if (moduleConfig.getNamespace() != null) {
@Override
public Collection<String> getMemberShardNames(final MemberName memberName) {
- Preconditions.checkNotNull(memberName, "memberName should not be null");
+ requireNonNull(memberName, "memberName should not be null");
List<String> shards = new ArrayList<>();
for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
@Override
public String getModuleNameFromNameSpace(final String nameSpace) {
- Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
-
- return namespaceToModuleName.get(nameSpace);
+ return namespaceToModuleName.get(requireNonNull(nameSpace, "nameSpace should not be null"));
}
@Override
- public ShardStrategy getStrategyForModule(String moduleName) {
- Preconditions.checkNotNull(moduleName, "moduleName should not be null");
-
- ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
+ public ShardStrategy getStrategyForModule(final String moduleName) {
+ ModuleConfig moduleConfig = getModuleConfig(moduleName);
return moduleConfig != null ? moduleConfig.getShardStrategy() : null;
}
@Override
public String getShardNameForModule(final String moduleName) {
- Preconditions.checkNotNull(moduleName, "moduleName should not be null");
+ ModuleConfig moduleConfig = getModuleConfig(moduleName);
+ if (moduleConfig != null) {
+ Collection<ShardConfig> shardConfigs = moduleConfig.getShardConfigs();
+ if (!shardConfigs.isEmpty()) {
+ return shardConfigs.iterator().next().getName();
+ }
+ }
+ return null;
+ }
- ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
- Collection<ShardConfig> shardConfigs = moduleConfig != null ? moduleConfig.getShardConfigs() :
- Collections.<ShardConfig>emptySet();
- return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName() : null;
+ private ModuleConfig getModuleConfig(final String moduleName) {
+ return moduleConfigMap.get(requireNonNull(moduleName, "moduleName should not be null"));
}
- @Nullable
@Override
- public String getShardNameForPrefix(@Nonnull final YangInstanceIdentifier prefix) {
- Preconditions.checkNotNull(prefix, "prefix should not be null");
+ public String getShardNameForPrefix(final DOMDataTreeIdentifier prefix) {
+ requireNonNull(prefix, "prefix should not be null");
- Entry<YangInstanceIdentifier, PrefixShardConfiguration> bestMatchEntry =
- new SimpleEntry<>(YangInstanceIdentifier.EMPTY, null);
+ Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> bestMatchEntry = new SimpleEntry<>(
+ new DOMDataTreeIdentifier(prefix.getDatastoreType(), YangInstanceIdentifier.empty()), null);
- for (Entry<YangInstanceIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
- if (entry.getKey().contains(prefix) && entry.getKey().getPathArguments().size()
- > bestMatchEntry.getKey().getPathArguments().size()) {
+ 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.getValue().getPrefix().getRootIdentifier());
+ 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);
return Collections.emptyList();
}
+ private static void checkNotNullShardName(final String shardName) {
+ requireNonNull(shardName, "shardName should not be null");
+ }
+
@Override
public Set<String> getAllShardNames() {
return allShardNames;
}
@Override
- public synchronized void addModuleShardConfiguration(ModuleShardConfiguration config) {
- Preconditions.checkNotNull(config, "ModuleShardConfiguration should not be null");
+ public synchronized void addModuleShardConfiguration(final ModuleShardConfiguration config) {
+ requireNonNull(config, "ModuleShardConfiguration should not be null");
ModuleConfig moduleConfig = ModuleConfig.builder(config.getModuleName())
.nameSpace(config.getNamespace().toASCIIString())
}
@Override
- public void addPrefixShardConfiguration(@Nonnull final PrefixShardConfiguration config) {
- Preconditions.checkNotNull(config, "PrefixShardConfiguration cannot be null");
- updatePrefixConfigMap(config);
+ public void addPrefixShardConfiguration(final PrefixShardConfiguration config) {
+ addPrefixConfig(requireNonNull(config, "PrefixShardConfiguration cannot be null"));
allShardNames = ImmutableSet.<String>builder().addAll(allShardNames)
.add(ClusterUtils.getCleanShardName(config.getPrefix().getRootIdentifier())).build();
}
- private void updatePrefixConfigMap(final PrefixShardConfiguration config) {
- final Map<YangInstanceIdentifier, PrefixShardConfiguration> newPrefixConfigMap = new HashMap<>(prefixConfigMap);
- newPrefixConfigMap.put(config.getPrefix().getRootIdentifier(), config);
+ @Override
+ public void removePrefixShardConfiguration(final DOMDataTreeIdentifier prefix) {
+ removePrefixConfig(requireNonNull(prefix, "Prefix cannot be null"));
+
+ 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) {
+ private ShardStrategy createShardStrategy(final String moduleName, final String shardStrategyName) {
return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);
}
@Override
- public boolean isShardConfigured(String shardName) {
- Preconditions.checkNotNull(shardName, "shardName should not be null");
+ public boolean isShardConfigured(final String shardName) {
+ checkNotNullShardName(shardName);
return allShardNames.contains(shardName);
}
@Override
- public void addMemberReplicaForShard(String shardName, MemberName newMemberName) {
- Preconditions.checkNotNull(shardName, "shardName should not be null");
- Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
+ public void addMemberReplicaForShard(final String shardName, final MemberName newMemberName) {
+ checkNotNullShardName(shardName);
+ requireNonNull(newMemberName, "MemberName should not be null");
for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
}
@Override
- public void removeMemberReplicaForShard(String shardName, MemberName newMemberName) {
- Preconditions.checkNotNull(shardName, "shardName should not be null");
- Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
+ public void removeMemberReplicaForShard(final String shardName, final MemberName newMemberName) {
+ checkNotNullShardName(shardName);
+ requireNonNull(newMemberName, "MemberName should not be null");
for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
}
@Override
- public ShardStrategy getStrategyForPrefix(@Nonnull final YangInstanceIdentifier prefix) {
- Preconditions.checkNotNull(prefix, "Prefix cannot be null");
+ public ShardStrategy getStrategyForPrefix(final DOMDataTreeIdentifier prefix) {
+ requireNonNull(prefix, "Prefix cannot be null");
// FIXME using prefix tables like in mdsal will be better
- Entry<YangInstanceIdentifier, PrefixShardConfiguration> bestMatchEntry =
- new SimpleEntry<>(YangInstanceIdentifier.EMPTY, null);
+ Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> bestMatchEntry = new SimpleEntry<>(
+ new DOMDataTreeIdentifier(prefix.getDatastoreType(), YangInstanceIdentifier.empty()), null);
- for (Entry<YangInstanceIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
- if (entry.getKey().contains(prefix) && entry.getKey().getPathArguments().size()
- > bestMatchEntry.getKey().getPathArguments().size()) {
+ 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.getValue().getPrefix().getRootIdentifier()), this);
+ return new PrefixShardStrategy(ClusterUtils
+ .getCleanShardName(bestMatchEntry.getKey().getRootIdentifier()),
+ bestMatchEntry.getKey().getRootIdentifier());
}
private void updateModuleConfigMap(final ModuleConfig moduleConfig) {