X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fshardstrategy%2FShardStrategyFactory.java;h=724499477ebde6a5b03fbba4a72d3c14564a6886;hp=210537925b151a0b0c140433d4d1f009fa90acf6;hb=c1336f9b497bc6867536a24f629c3f0b002ccb2f;hpb=81bbe76bd26399118d028663d08e464ce6b7d040 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactory.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactory.java index 210537925b..724499477e 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactory.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactory.java @@ -9,40 +9,58 @@ package org.opendaylight.controller.cluster.datastore.shardstrategy; import com.google.common.base.Preconditions; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import org.opendaylight.controller.cluster.datastore.config.Configuration; +import org.opendaylight.mdsal.common.api.LogicalDatastoreType; +import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; public class ShardStrategyFactory { - private static final Map moduleNameToStrategyMap = new ConcurrentHashMap(); + private static final String UNKNOWN_MODULE_NAME = "unknown"; + + private final Configuration configuration; + private final LogicalDatastoreType logicalStoreType; + + public ShardStrategyFactory(final Configuration configuration, LogicalDatastoreType logicalStoreType) { + Preconditions.checkState(configuration != null, "configuration should not be missing"); + this.configuration = configuration; + this.logicalStoreType = Preconditions.checkNotNull(logicalStoreType); + } - private static final String UNKNOWN_MODULE_NAME = "unknown"; + public ShardStrategy getStrategy(final YangInstanceIdentifier path) { + Preconditions.checkNotNull(path, "path should not be null"); - public static ShardStrategy getStrategy(InstanceIdentifier path){ - Preconditions.checkNotNull(path, "path should not be null"); + // try with the legacy module based shard mapping + final String moduleName = getModuleName(path); + final ShardStrategy shardStrategy = configuration.getStrategyForModule(moduleName); + if (shardStrategy == null) { + // retry with prefix based sharding + final ShardStrategy strategyForPrefix = + configuration.getStrategyForPrefix(new DOMDataTreeIdentifier(logicalStoreType, path)); + if (strategyForPrefix == null) { + return DefaultShardStrategy.getInstance(); + } + return strategyForPrefix; + } - String moduleName = getModuleName(path); - ShardStrategy shardStrategy = moduleNameToStrategyMap.get(moduleName); - if(shardStrategy == null){ - return new DefaultShardStrategy(); + return shardStrategy; } - return shardStrategy; - } + public static ShardStrategy newShardStrategyInstance(String moduleName, String strategyName, + Configuration configuration) { + if (ModuleShardStrategy.NAME.equals(strategyName)) { + return new ModuleShardStrategy(moduleName, configuration); + } + return DefaultShardStrategy.getInstance(); + } - private static String getModuleName(InstanceIdentifier path){ - return UNKNOWN_MODULE_NAME; - } + private String getModuleName(final YangInstanceIdentifier path) { + if (path.isEmpty()) { + return UNKNOWN_MODULE_NAME; + } - /** - * This is to be used in the future to register a custom shard strategy - * - * @param moduleName - * @param shardStrategy - */ - public static void registerShardStrategy(String moduleName, ShardStrategy shardStrategy){ - throw new UnsupportedOperationException("registering a custom shard strategy not supported yet"); - } + String namespace = path.getPathArguments().iterator().next().getNodeType().getNamespace().toASCIIString(); + String moduleName = configuration.getModuleNameFromNameSpace(namespace); + return moduleName != null ? moduleName : UNKNOWN_MODULE_NAME; + } }