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
8 package org.opendaylight.controller.cluster.datastore.shardstrategy;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import org.opendaylight.controller.cluster.datastore.config.Configuration;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 public class ShardStrategyFactory {
19 private static final String UNKNOWN_MODULE_NAME = "unknown";
21 private final Configuration configuration;
22 private final LogicalDatastoreType logicalStoreType;
24 public ShardStrategyFactory(final Configuration configuration, final LogicalDatastoreType logicalStoreType) {
25 checkState(configuration != null, "configuration should not be missing");
26 this.configuration = configuration;
27 this.logicalStoreType = requireNonNull(logicalStoreType);
30 public ShardStrategy getStrategy(final YangInstanceIdentifier path) {
31 // try with the legacy module based shard mapping
32 final String moduleName = getModuleName(requireNonNull(path, "path should not be null"));
33 final ShardStrategy shardStrategy = configuration.getStrategyForModule(moduleName);
34 if (shardStrategy == null) {
35 // retry with prefix based sharding
36 final ShardStrategy strategyForPrefix =
37 configuration.getStrategyForPrefix(new DOMDataTreeIdentifier(logicalStoreType, path));
38 if (strategyForPrefix == null) {
39 return DefaultShardStrategy.getInstance();
41 return strategyForPrefix;
47 public static ShardStrategy newShardStrategyInstance(final String moduleName, final String strategyName,
48 final Configuration configuration) {
49 if (ModuleShardStrategy.NAME.equals(strategyName)) {
50 return new ModuleShardStrategy(moduleName, configuration);
53 return DefaultShardStrategy.getInstance();
56 private String getModuleName(final YangInstanceIdentifier path) {
58 return UNKNOWN_MODULE_NAME;
61 String namespace = path.getPathArguments().get(0).getNodeType().getNamespace().toASCIIString();
62 String moduleName = configuration.getModuleNameFromNameSpace(namespace);
63 return moduleName != null ? moduleName : UNKNOWN_MODULE_NAME;