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.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
22 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
23 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
25 public class ConfigurationImpl implements Configuration {
26 private volatile Map<String, ModuleConfig> moduleConfigMap;
28 // Look up maps to speed things up
30 private volatile Map<String, String> namespaceToModuleName;
31 private volatile Set<String> allShardNames;
33 public ConfigurationImpl(final String moduleShardsConfigPath, final String modulesConfigPath) {
34 this(new FileModuleShardConfigProvider(moduleShardsConfigPath, modulesConfigPath));
37 public ConfigurationImpl(final ModuleShardConfigProvider provider) {
38 ImmutableMap.Builder<String, ModuleConfig> mapBuilder = ImmutableMap.builder();
39 for(Map.Entry<String, ModuleConfig.Builder> e: provider.retrieveModuleConfigs(this).entrySet()) {
40 mapBuilder.put(e.getKey(), e.getValue().build());
43 this.moduleConfigMap = mapBuilder.build();
45 this.allShardNames = createAllShardNames(moduleConfigMap.values());
46 this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values());
49 private static Set<String> createAllShardNames(Iterable<ModuleConfig> moduleConfigs) {
50 final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
51 for(ModuleConfig moduleConfig : moduleConfigs) {
52 builder.addAll(moduleConfig.getShardNames());
55 return builder.build();
58 private static Map<String, String> createNamespaceToModuleName(Iterable<ModuleConfig> moduleConfigs) {
59 final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
60 for(ModuleConfig moduleConfig : moduleConfigs) {
61 if(moduleConfig.getNameSpace() != null) {
62 builder.put(moduleConfig.getNameSpace(), moduleConfig.getName());
66 return builder.build();
70 public Collection<String> getMemberShardNames(final String memberName){
71 Preconditions.checkNotNull(memberName, "memberName should not be null");
73 List<String> shards = new ArrayList<>();
74 for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
75 for(ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
76 if(shardConfig.getReplicas().contains(memberName)) {
77 shards.add(shardConfig.getName());
86 public String getModuleNameFromNameSpace(final String nameSpace) {
87 Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
89 return namespaceToModuleName.get(nameSpace);
93 public ShardStrategy getStrategyForModule(String moduleName) {
94 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
96 ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
97 return moduleConfig != null ? moduleConfig.getShardStrategy(): null;
101 public String getShardNameForModule(final String moduleName) {
102 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
104 ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
105 Collection<ShardConfig> shardConfigs = moduleConfig != null ? moduleConfig.getShardConfigs() :
106 Collections.<ShardConfig>emptySet();
107 return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName(): null;
111 public Collection<String> getMembersFromShardName(final String shardName) {
112 Preconditions.checkNotNull(shardName, "shardName should not be null");
114 for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
115 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
116 if(shardConfig != null) {
117 return shardConfig.getReplicas();
121 return Collections.emptyList();
125 public Set<String> getAllShardNames() {
126 return allShardNames;
130 public Collection<String> getUniqueMemberNamesForAllShards() {
131 Set<String> allNames = new HashSet<>();
132 for(String shardName: getAllShardNames()) {
133 allNames.addAll(getMembersFromShardName(shardName));
140 public synchronized void addModuleShardConfiguration(ModuleShardConfiguration config) {
141 Preconditions.checkNotNull(config, "ModuleShardConfiguration should not be null");
143 ModuleConfig moduleConfig = ModuleConfig.builder(config.getModuleName()).
144 nameSpace(config.getNamespace().toASCIIString()).
145 shardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName())).
146 shardConfig(config.getShardName(), config.getShardMemberNames()).build();
148 updateModuleConfigMap(moduleConfig);
150 namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName).
151 put(moduleConfig.getNameSpace(), moduleConfig.getName()).build();
152 allShardNames = ImmutableSet.<String>builder().addAll(allShardNames).add(config.getShardName()).build();
155 private ShardStrategy createShardStrategy(String moduleName, String shardStrategyName) {
156 return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);
160 public boolean isShardConfigured(String shardName) {
161 Preconditions.checkNotNull(shardName, "shardName should not be null");
162 return allShardNames.contains(shardName);
166 public void addMemberReplicaForShard (String shardName, String newMemberName) {
167 Preconditions.checkNotNull(shardName, "shardName should not be null");
168 Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
170 for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
171 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
172 if(shardConfig != null) {
173 Set<String> replicas = new HashSet<>(shardConfig.getReplicas());
174 replicas.add(newMemberName);
175 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
182 public void removeMemberReplicaForShard (String shardName, String newMemberName) {
183 Preconditions.checkNotNull(shardName, "shardName should not be null");
184 Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
186 for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
187 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
188 if(shardConfig != null) {
189 Set<String> replicas = new HashSet<>(shardConfig.getReplicas());
190 replicas.remove(newMemberName);
191 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
197 private void updateModuleConfigMap(ModuleConfig moduleConfig) {
198 Map<String, ModuleConfig> newModuleConfigMap = new HashMap<>(moduleConfigMap);
199 newModuleConfigMap.put(moduleConfig.getName(), moduleConfig);
200 moduleConfigMap = ImmutableMap.copyOf(newModuleConfigMap);