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=f4ab8fab6f768e84bd48c15ebc83e862315e09eb;hp=210537925b151a0b0c140433d4d1f009fa90acf6;hb=c46e223995956f1f759c551163c212947c1e2fb7;hpb=9d50e2ca76cf1a5eb0636d4a5a704b49ec4a8a25 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..f4ab8fab6f 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 @@ -8,41 +8,52 @@ package org.opendaylight.controller.cluster.datastore.shardstrategy; +import com.google.common.base.Optional; import com.google.common.base.Preconditions; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; +import org.opendaylight.controller.cluster.datastore.Configuration; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class ShardStrategyFactory { - private static final Map moduleNameToStrategyMap = new ConcurrentHashMap(); + private static Map moduleNameToStrategyMap = + new ConcurrentHashMap(); - private static final String UNKNOWN_MODULE_NAME = "unknown"; + private static final String UNKNOWN_MODULE_NAME = "unknown"; + private static Configuration configuration; - public static ShardStrategy getStrategy(InstanceIdentifier path){ - Preconditions.checkNotNull(path, "path should not be null"); - String moduleName = getModuleName(path); - ShardStrategy shardStrategy = moduleNameToStrategyMap.get(moduleName); - if(shardStrategy == null){ - return new DefaultShardStrategy(); + public static void setConfiguration(Configuration configuration){ + ShardStrategyFactory.configuration = configuration; + moduleNameToStrategyMap = configuration.getModuleNameToShardStrategyMap(); } - return shardStrategy; - } + public static ShardStrategy getStrategy(YangInstanceIdentifier path) { + Preconditions.checkState(configuration != null, "configuration should not be missing"); + Preconditions.checkNotNull(path, "path should not be null"); - private static String getModuleName(InstanceIdentifier path){ - return UNKNOWN_MODULE_NAME; - } + String moduleName = getModuleName(path); + ShardStrategy shardStrategy = moduleNameToStrategyMap.get(moduleName); + if (shardStrategy == null) { + return new DefaultShardStrategy(); + } - /** - * 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"); - } + return shardStrategy; + } + + + private static String getModuleName(YangInstanceIdentifier path) { + String namespace = path.getPathArguments().iterator().next().getNodeType().getNamespace().toASCIIString(); + + Optional optional = + configuration.getModuleNameFromNameSpace(namespace); + + if(!optional.isPresent()){ + return UNKNOWN_MODULE_NAME; + } + + return optional.get(); + } }