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.config;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.List;
21 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
22 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
24 public class ConfigurationImpl implements Configuration {
25 private volatile Map<String, ModuleConfig> moduleConfigMap;
27 // Look up maps to speed things up
29 private volatile Map<String, String> namespaceToModuleName;
30 private volatile Set<String> allShardNames;
32 public ConfigurationImpl(final String moduleShardsConfigPath, final String modulesConfigPath) {
33 this(new FileModuleShardConfigProvider(moduleShardsConfigPath, modulesConfigPath));
36 public ConfigurationImpl(final ModuleShardConfigProvider provider) {
37 this.moduleConfigMap = ImmutableMap.copyOf(provider.retrieveModuleConfigs(this));
39 this.allShardNames = createAllShardNames(moduleConfigMap.values());
40 this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values());
43 private static Set<String> createAllShardNames(Iterable<ModuleConfig> moduleConfigs) {
44 final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
45 for(ModuleConfig moduleConfig : moduleConfigs) {
46 builder.addAll(moduleConfig.getShardNames());
49 return builder.build();
52 private static Map<String, String> createNamespaceToModuleName(Iterable<ModuleConfig> moduleConfigs) {
53 final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
54 for(ModuleConfig moduleConfig : moduleConfigs) {
55 if(moduleConfig.getNameSpace() != null) {
56 builder.put(moduleConfig.getNameSpace(), moduleConfig.getName());
60 return builder.build();
64 public Collection<String> getMemberShardNames(final String memberName){
65 Preconditions.checkNotNull(memberName, "memberName should not be null");
67 List<String> shards = new ArrayList<>();
68 for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
69 for(ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
70 if(shardConfig.getReplicas().contains(memberName)) {
71 shards.add(shardConfig.getName());
80 public String getModuleNameFromNameSpace(final String nameSpace) {
81 Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
83 return namespaceToModuleName.get(nameSpace);
87 public ShardStrategy getStrategyForModule(String moduleName) {
88 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
90 ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
91 return moduleConfig != null ? moduleConfig.getShardStrategy(): null;
95 public String getShardNameForModule(final String moduleName) {
96 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
98 ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
99 Collection<ShardConfig> shardConfigs = moduleConfig != null ? moduleConfig.getShardConfigs() :
100 Collections.<ShardConfig>emptySet();
101 return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName(): null;
105 public Collection<String> getMembersFromShardName(final String shardName) {
106 Preconditions.checkNotNull(shardName, "shardName should not be null");
108 for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
109 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
110 if(shardConfig != null) {
111 return shardConfig.getReplicas();
115 return Collections.emptyList();
119 public Set<String> getAllShardNames() {
120 return allShardNames;
124 public Collection<String> getUniqueMemberNamesForAllShards() {
125 Set<String> allNames = new HashSet<>();
126 for(String shardName: getAllShardNames()) {
127 allNames.addAll(getMembersFromShardName(shardName));
134 public synchronized void addModuleShardConfiguration(ModuleShardConfiguration config) {
135 Preconditions.checkNotNull(config, "ModuleShardConfiguration should not be null");
137 ModuleConfig moduleConfig = new ModuleConfig(config.getModuleName());
138 moduleConfig.setNameSpace(config.getNamespace().toASCIIString());
139 moduleConfig.setShardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName()));
141 moduleConfig.addShardConfig(config.getShardName(), ImmutableSet.copyOf(config.getShardMemberNames()));
143 moduleConfigMap = ImmutableMap.<String, ModuleConfig>builder().putAll(moduleConfigMap).
144 put(config.getModuleName(), moduleConfig).build();
146 namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName).
147 put(moduleConfig.getNameSpace(), moduleConfig.getName()).build();
148 allShardNames = ImmutableSet.<String>builder().addAll(allShardNames).add(config.getShardName()).build();
151 private ShardStrategy createShardStrategy(String moduleName, String shardStrategyName) {
152 return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);