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 this.moduleConfigMap = ImmutableMap.copyOf(provider.retrieveModuleConfigs(this));
40 this.allShardNames = createAllShardNames(moduleConfigMap.values());
41 this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values());
44 private static Set<String> createAllShardNames(Iterable<ModuleConfig> moduleConfigs) {
45 final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
46 for(ModuleConfig moduleConfig : moduleConfigs) {
47 builder.addAll(moduleConfig.getShardNames());
50 return builder.build();
53 private static Map<String, String> createNamespaceToModuleName(Iterable<ModuleConfig> moduleConfigs) {
54 final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
55 for(ModuleConfig moduleConfig : moduleConfigs) {
56 if(moduleConfig.getNameSpace() != null) {
57 builder.put(moduleConfig.getNameSpace(), moduleConfig.getName());
61 return builder.build();
65 public Collection<String> getMemberShardNames(final String memberName){
66 Preconditions.checkNotNull(memberName, "memberName should not be null");
68 List<String> shards = new ArrayList<>();
69 for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
70 for(ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
71 if(shardConfig.getReplicas().contains(memberName)) {
72 shards.add(shardConfig.getName());
81 public String getModuleNameFromNameSpace(final String nameSpace) {
82 Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
84 return namespaceToModuleName.get(nameSpace);
88 public ShardStrategy getStrategyForModule(String moduleName) {
89 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
91 ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
92 return moduleConfig != null ? moduleConfig.getShardStrategy(): null;
96 public String getShardNameForModule(final String moduleName) {
97 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
99 ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
100 Collection<ShardConfig> shardConfigs = moduleConfig != null ? moduleConfig.getShardConfigs() :
101 Collections.<ShardConfig>emptySet();
102 return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName(): null;
106 public Collection<String> getMembersFromShardName(final String shardName) {
107 Preconditions.checkNotNull(shardName, "shardName should not be null");
109 for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
110 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
111 if(shardConfig != null) {
112 return shardConfig.getReplicas();
116 return Collections.emptyList();
120 public Set<String> getAllShardNames() {
121 return allShardNames;
125 public Collection<String> getUniqueMemberNamesForAllShards() {
126 Set<String> allNames = new HashSet<>();
127 for(String shardName: getAllShardNames()) {
128 allNames.addAll(getMembersFromShardName(shardName));
135 public synchronized void addModuleShardConfiguration(ModuleShardConfiguration config) {
136 Preconditions.checkNotNull(config, "ModuleShardConfiguration should not be null");
138 ModuleConfig moduleConfig = new ModuleConfig(config.getModuleName());
139 moduleConfig.setNameSpace(config.getNamespace().toASCIIString());
140 moduleConfig.setShardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName()));
142 moduleConfig.addShardConfig(config.getShardName(), ImmutableSet.copyOf(config.getShardMemberNames()));
144 moduleConfigMap = ImmutableMap.<String, ModuleConfig>builder().putAll(moduleConfigMap).
145 put(config.getModuleName(), moduleConfig).build();
147 namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName).
148 put(moduleConfig.getNameSpace(), moduleConfig.getName()).build();
149 allShardNames = ImmutableSet.<String>builder().addAll(allShardNames).add(config.getShardName()).build();
152 private ShardStrategy createShardStrategy(String moduleName, String shardStrategyName) {
153 return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);
157 public boolean isShardConfigured(String shardName) {
158 Preconditions.checkNotNull(shardName, "shardName should not be null");
159 return allShardNames.contains(shardName);
163 public void addMemberReplicaForShard (String shardName, String newMemberName) {
164 Preconditions.checkNotNull(shardName, "shardName should not be null");
165 Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
167 for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
168 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
169 if(shardConfig != null) {
170 ModuleConfig newModuleConfig = new ModuleConfig(moduleConfig);
171 Set<String> replica = new HashSet<>(shardConfig.getReplicas());
172 replica.add(newMemberName);
173 newModuleConfig.addShardConfig(shardName, ImmutableSet.copyOf(replica));
174 updateModuleConfigMap(newModuleConfig);
181 public void removeMemberReplicaForShard (String shardName, String newMemberName) {
182 Preconditions.checkNotNull(shardName, "shardName should not be null");
183 Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
185 for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
186 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
187 if(shardConfig != null) {
188 ModuleConfig newModuleConfig = new ModuleConfig(moduleConfig);
189 Set<String> replica = new HashSet<>(shardConfig.getReplicas());
190 replica.remove(newMemberName);
191 newModuleConfig.addShardConfig(shardName, ImmutableSet.copyOf(replica));
192 updateModuleConfigMap(newModuleConfig);
198 private void updateModuleConfigMap(ModuleConfig moduleConfig) {
199 HashMap<String, ModuleConfig> newModuleConfigMap = new HashMap<>(moduleConfigMap);
200 newModuleConfigMap.put(moduleConfig.getName(), moduleConfig);
201 moduleConfigMap = ImmutableMap.<String, ModuleConfig>builder().putAll(newModuleConfigMap).build();