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.shardstrategy;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.controller.cluster.datastore.Configuration;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import java.util.concurrent.ConcurrentHashMap;
19 public class ShardStrategyFactory {
20 private static Map<String, ShardStrategy> moduleNameToStrategyMap =
21 new ConcurrentHashMap();
23 private static final String UNKNOWN_MODULE_NAME = "unknown";
24 private static Configuration configuration;
27 public static void setConfiguration(Configuration configuration){
28 ShardStrategyFactory.configuration = configuration;
29 moduleNameToStrategyMap = configuration.getModuleNameToShardStrategyMap();
32 public static ShardStrategy getStrategy(YangInstanceIdentifier path) {
33 Preconditions.checkState(configuration != null, "configuration should not be missing");
34 Preconditions.checkNotNull(path, "path should not be null");
37 String moduleName = getModuleName(path);
38 ShardStrategy shardStrategy = moduleNameToStrategyMap.get(moduleName);
39 if (shardStrategy == null) {
40 return new DefaultShardStrategy();
47 private static String getModuleName(YangInstanceIdentifier path) {
48 String namespace = path.getPathArguments().iterator().next().getNodeType().getNamespace().toASCIIString();
50 Optional<String> optional =
51 configuration.getModuleNameFromNameSpace(namespace);
53 if(!optional.isPresent()){
54 return UNKNOWN_MODULE_NAME;
57 return optional.get();