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.access.concepts.MemberName;
23 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
24 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
26 public class ConfigurationImpl implements Configuration {
27 private volatile Map<String, ModuleConfig> moduleConfigMap;
29 // Look up maps to speed things up
31 private volatile Map<String, String> namespaceToModuleName;
32 private volatile Set<String> allShardNames;
34 public ConfigurationImpl(final String moduleShardsConfigPath, final String modulesConfigPath) {
35 this(new FileModuleShardConfigProvider(moduleShardsConfigPath, modulesConfigPath));
38 public ConfigurationImpl(final ModuleShardConfigProvider provider) {
39 ImmutableMap.Builder<String, ModuleConfig> mapBuilder = ImmutableMap.builder();
40 for (Map.Entry<String, ModuleConfig.Builder> e: provider.retrieveModuleConfigs(this).entrySet()) {
41 mapBuilder.put(e.getKey(), e.getValue().build());
44 this.moduleConfigMap = mapBuilder.build();
46 this.allShardNames = createAllShardNames(moduleConfigMap.values());
47 this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values());
50 private static Set<String> createAllShardNames(Iterable<ModuleConfig> moduleConfigs) {
51 final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
52 for (ModuleConfig moduleConfig : moduleConfigs) {
53 builder.addAll(moduleConfig.getShardNames());
56 return builder.build();
59 private static Map<String, String> createNamespaceToModuleName(Iterable<ModuleConfig> moduleConfigs) {
60 final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
61 for (ModuleConfig moduleConfig : moduleConfigs) {
62 if (moduleConfig.getNamespace() != null) {
63 builder.put(moduleConfig.getNamespace(), moduleConfig.getName());
67 return builder.build();
71 public Collection<String> getMemberShardNames(final MemberName memberName) {
72 Preconditions.checkNotNull(memberName, "memberName should not be null");
74 List<String> shards = new ArrayList<>();
75 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
76 for (ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
77 if (shardConfig.getReplicas().contains(memberName)) {
78 shards.add(shardConfig.getName());
87 public String getModuleNameFromNameSpace(final String nameSpace) {
88 Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
90 return namespaceToModuleName.get(nameSpace);
94 public ShardStrategy getStrategyForModule(String moduleName) {
95 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
97 ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
98 return moduleConfig != null ? moduleConfig.getShardStrategy() : null;
102 public String getShardNameForModule(final String moduleName) {
103 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
105 ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
106 Collection<ShardConfig> shardConfigs = moduleConfig != null ? moduleConfig.getShardConfigs() :
107 Collections.<ShardConfig>emptySet();
108 return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName() : null;
112 public Collection<MemberName> getMembersFromShardName(final String shardName) {
113 Preconditions.checkNotNull(shardName, "shardName should not be null");
115 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
116 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
117 if (shardConfig != null) {
118 return shardConfig.getReplicas();
122 return Collections.emptyList();
126 public Set<String> getAllShardNames() {
127 return allShardNames;
131 public Collection<MemberName> getUniqueMemberNamesForAllShards() {
132 Set<MemberName> allNames = new HashSet<>();
133 for (String shardName: getAllShardNames()) {
134 allNames.addAll(getMembersFromShardName(shardName));
141 public synchronized void addModuleShardConfiguration(ModuleShardConfiguration config) {
142 Preconditions.checkNotNull(config, "ModuleShardConfiguration should not be null");
144 ModuleConfig moduleConfig = ModuleConfig.builder(config.getModuleName())
145 .nameSpace(config.getNamespace().toASCIIString())
146 .shardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName()))
147 .shardConfig(config.getShardName(), config.getShardMemberNames()).build();
149 updateModuleConfigMap(moduleConfig);
151 namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName)
152 .put(moduleConfig.getNamespace(), moduleConfig.getName()).build();
153 allShardNames = ImmutableSet.<String>builder().addAll(allShardNames).add(config.getShardName()).build();
156 private ShardStrategy createShardStrategy(String moduleName, String shardStrategyName) {
157 return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);
161 public boolean isShardConfigured(String shardName) {
162 Preconditions.checkNotNull(shardName, "shardName should not be null");
163 return allShardNames.contains(shardName);
167 public void addMemberReplicaForShard(String shardName, MemberName newMemberName) {
168 Preconditions.checkNotNull(shardName, "shardName should not be null");
169 Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
171 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
172 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
173 if (shardConfig != null) {
174 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
175 replicas.add(newMemberName);
176 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
183 public void removeMemberReplicaForShard(String shardName, MemberName newMemberName) {
184 Preconditions.checkNotNull(shardName, "shardName should not be null");
185 Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
187 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
188 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
189 if (shardConfig != null) {
190 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
191 replicas.remove(newMemberName);
192 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
198 private void updateModuleConfigMap(ModuleConfig moduleConfig) {
199 Map<String, ModuleConfig> newModuleConfigMap = new HashMap<>(moduleConfigMap);
200 newModuleConfigMap.put(moduleConfig.getName(), moduleConfig);
201 moduleConfigMap = ImmutableMap.copyOf(newModuleConfigMap);