a97adc27308fdbc265d06e925a50a982f2fd22b7
[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
9 package org.opendaylight.controller.cluster.datastore.shardstrategy;
10
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.controller.cluster.datastore.config.Configuration;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14
15 public class ShardStrategyFactory {
16     private static final String UNKNOWN_MODULE_NAME = "unknown";
17
18     private final Configuration configuration;
19
20     public ShardStrategyFactory(final Configuration configuration) {
21         Preconditions.checkState(configuration != null, "configuration should not be missing");
22         this.configuration = configuration;
23     }
24
25     public ShardStrategy getStrategy(final YangInstanceIdentifier path) {
26         Preconditions.checkNotNull(path, "path should not be null");
27
28         // try with the legacy module based shard mapping
29         final String moduleName = getModuleName(path);
30         final ShardStrategy shardStrategy = configuration.getStrategyForModule(moduleName);
31         if (shardStrategy == null) {
32             // retry with prefix based sharding
33             final ShardStrategy strategyForPrefix = configuration.getStrategyForPrefix(path);
34             if (strategyForPrefix == null) {
35                 return DefaultShardStrategy.getInstance();
36             }
37             return strategyForPrefix;
38         }
39
40         return shardStrategy;
41     }
42
43     public static ShardStrategy newShardStrategyInstance(String moduleName, String strategyName,
44             Configuration configuration) {
45         if (ModuleShardStrategy.NAME.equals(strategyName)) {
46             return new ModuleShardStrategy(moduleName, configuration);
47         }
48
49         return DefaultShardStrategy.getInstance();
50     }
51
52     private String getModuleName(final YangInstanceIdentifier path) {
53         if (path.isEmpty()) {
54             return UNKNOWN_MODULE_NAME;
55         }
56
57         String namespace = path.getPathArguments().iterator().next().getNodeType().getNamespace().toASCIIString();
58         String moduleName = configuration.getModuleNameFromNameSpace(namespace);
59         return moduleName != null ? moduleName : UNKNOWN_MODULE_NAME;
60     }
61 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.