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.Preconditions;
12 import org.opendaylight.controller.cluster.datastore.config.Configuration;
13 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
14 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 public class ShardStrategyFactory {
18 private static final String UNKNOWN_MODULE_NAME = "unknown";
20 private final Configuration configuration;
21 private final LogicalDatastoreType logicalStoreType;
23 public ShardStrategyFactory(final Configuration configuration, final LogicalDatastoreType logicalStoreType) {
24 Preconditions.checkState(configuration != null, "configuration should not be missing");
25 this.configuration = configuration;
26 this.logicalStoreType = Preconditions.checkNotNull(logicalStoreType);
29 public ShardStrategy getStrategy(final YangInstanceIdentifier path) {
30 Preconditions.checkNotNull(path, "path should not be null");
32 // try with the legacy module based shard mapping
33 final String moduleName = getModuleName(path);
34 final ShardStrategy shardStrategy = configuration.getStrategyForModule(moduleName);
35 if (shardStrategy == null) {
36 // retry with prefix based sharding
37 final ShardStrategy strategyForPrefix =
38 configuration.getStrategyForPrefix(new DOMDataTreeIdentifier(logicalStoreType, path));
39 if (strategyForPrefix == null) {
40 return DefaultShardStrategy.getInstance();
42 return strategyForPrefix;
48 public static ShardStrategy newShardStrategyInstance(final String moduleName, final String strategyName,
49 final Configuration configuration) {
50 if (ModuleShardStrategy.NAME.equals(strategyName)) {
51 return new ModuleShardStrategy(moduleName, configuration);
54 return DefaultShardStrategy.getInstance();
57 private String getModuleName(final YangInstanceIdentifier path) {
59 return UNKNOWN_MODULE_NAME;
62 String namespace = path.getPathArguments().get(0).getNodeType().getNamespace().toASCIIString();
63 String moduleName = configuration.getModuleNameFromNameSpace(namespace);
64 return moduleName != null ? moduleName : UNKNOWN_MODULE_NAME;