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
8 package org.opendaylight.controller.cluster.datastore.config;
10 import static java.util.Objects.requireNonNull;
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(final 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(final 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 requireNonNull(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 return namespaceToModuleName.get(requireNonNull(nameSpace, "nameSpace should not be null"));
92 public ShardStrategy getStrategyForModule(final String moduleName) {
93 ModuleConfig moduleConfig = getModuleConfig(moduleName);
94 return moduleConfig != null ? moduleConfig.getShardStrategy() : null;
98 public String getShardNameForModule(final String moduleName) {
99 ModuleConfig moduleConfig = getModuleConfig(moduleName);
100 if (moduleConfig != null) {
101 Collection<ShardConfig> shardConfigs = moduleConfig.getShardConfigs();
102 if (!shardConfigs.isEmpty()) {
103 return shardConfigs.iterator().next().getName();
109 private ModuleConfig getModuleConfig(final String moduleName) {
110 return moduleConfigMap.get(requireNonNull(moduleName, "moduleName should not be null"));
114 public Collection<MemberName> getMembersFromShardName(final String shardName) {
115 checkNotNullShardName(shardName);
117 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
118 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
119 if (shardConfig != null) {
120 return shardConfig.getReplicas();
124 return Collections.emptyList();
127 private static void checkNotNullShardName(final String shardName) {
128 requireNonNull(shardName, "shardName should not be null");
132 public Set<String> getAllShardNames() {
133 return allShardNames;
137 public Collection<MemberName> getUniqueMemberNamesForAllShards() {
138 Set<MemberName> allNames = new HashSet<>();
139 for (String shardName: getAllShardNames()) {
140 allNames.addAll(getMembersFromShardName(shardName));
147 public synchronized void addModuleShardConfiguration(final ModuleShardConfiguration config) {
148 requireNonNull(config, "ModuleShardConfiguration should not be null");
150 ModuleConfig moduleConfig = ModuleConfig.builder(config.getModuleName())
151 .nameSpace(config.getNamespace().toString())
152 .shardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName()))
153 .shardConfig(config.getShardName(), config.getShardMemberNames()).build();
155 updateModuleConfigMap(moduleConfig);
157 namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName)
158 .put(moduleConfig.getNamespace(), moduleConfig.getName()).build();
159 allShardNames = ImmutableSet.<String>builder().addAll(allShardNames).add(config.getShardName()).build();
162 private ShardStrategy createShardStrategy(final String moduleName, final String shardStrategyName) {
163 return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);
167 public boolean isShardConfigured(final String shardName) {
168 checkNotNullShardName(shardName);
169 return allShardNames.contains(shardName);
173 public void addMemberReplicaForShard(final String shardName, final MemberName newMemberName) {
174 checkNotNullShardName(shardName);
175 requireNonNull(newMemberName, "MemberName should not be null");
177 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
178 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
179 if (shardConfig != null) {
180 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
181 replicas.add(newMemberName);
182 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
189 public void removeMemberReplicaForShard(final String shardName, final MemberName newMemberName) {
190 checkNotNullShardName(shardName);
191 requireNonNull(newMemberName, "MemberName should not be null");
193 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
194 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
195 if (shardConfig != null) {
196 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
197 replicas.remove(newMemberName);
198 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
204 private void updateModuleConfigMap(final ModuleConfig moduleConfig) {
205 final Map<String, ModuleConfig> newModuleConfigMap = new HashMap<>(moduleConfigMap);
206 newModuleConfigMap.put(moduleConfig.getName(), moduleConfig);
207 moduleConfigMap = ImmutableMap.copyOf(newModuleConfigMap);