Optimize use of YangInstanceIdentifier.getPathArguments()
[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.mdsal.common.api.LogicalDatastoreType;
14 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16
17 public class ShardStrategyFactory {
18     private static final String UNKNOWN_MODULE_NAME = "unknown";
19
20     private final Configuration configuration;
21     private final LogicalDatastoreType logicalStoreType;
22
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);
27     }
28
29     public ShardStrategy getStrategy(final YangInstanceIdentifier path) {
30         Preconditions.checkNotNull(path, "path should not be null");
31
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();
41             }
42             return strategyForPrefix;
43         }
44
45         return shardStrategy;
46     }
47
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);
52         }
53
54         return DefaultShardStrategy.getInstance();
55     }
56
57     private String getModuleName(final YangInstanceIdentifier path) {
58         if (path.isEmpty()) {
59             return UNKNOWN_MODULE_NAME;
60         }
61
62         String namespace = path.getPathArguments().get(0).getNodeType().getNamespace().toASCIIString();
63         String moduleName = configuration.getModuleNameFromNameSpace(namespace);
64         return moduleName != null ? moduleName : UNKNOWN_MODULE_NAME;
65     }
66 }