5891c4f7616af75ffc93556aa4f7e8cdf1d9d7d1
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / shardstrategy / ShardStrategyFactory.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.shardstrategy;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
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;
17
18 public class ShardStrategyFactory {
19     private static final String UNKNOWN_MODULE_NAME = "unknown";
20
21     private final Configuration configuration;
22     private final LogicalDatastoreType logicalStoreType;
23
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);
28     }
29
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();
40             }
41             return strategyForPrefix;
42         }
43
44         return shardStrategy;
45     }
46
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);
51         }
52
53         return DefaultShardStrategy.getInstance();
54     }
55
56     private String getModuleName(final YangInstanceIdentifier path) {
57         if (path.isEmpty()) {
58             return UNKNOWN_MODULE_NAME;
59         }
60
61         String namespace = path.getPathArguments().get(0).getNodeType().getNamespace().toString();
62         String moduleName = configuration.getModuleNameFromNameSpace(namespace);
63         return moduleName != null ? moduleName : UNKNOWN_MODULE_NAME;
64     }
65 }