BUG-5280: use MemberName instead of String
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / config / ConfigurationImpl.java
index d553c66dbe69d072b93617b38937d8e213395e61..1af8ed63e03936c7b4962c4b190cb432e633379a 100644 (file)
@@ -14,10 +14,12 @@ import com.google.common.collect.ImmutableSet;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import org.opendaylight.controller.cluster.access.concepts.MemberName;
 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
 
@@ -34,7 +36,12 @@ public class ConfigurationImpl implements Configuration {
     }
 
     public ConfigurationImpl(final ModuleShardConfigProvider provider) {
-        this.moduleConfigMap = ImmutableMap.copyOf(provider.retrieveModuleConfigs(this));
+        ImmutableMap.Builder<String, ModuleConfig> mapBuilder = ImmutableMap.builder();
+        for(Map.Entry<String, ModuleConfig.Builder> e: provider.retrieveModuleConfigs(this).entrySet()) {
+            mapBuilder.put(e.getKey(), e.getValue().build());
+        }
+
+        this.moduleConfigMap = mapBuilder.build();
 
         this.allShardNames = createAllShardNames(moduleConfigMap.values());
         this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values());
@@ -61,12 +68,12 @@ public class ConfigurationImpl implements Configuration {
     }
 
     @Override
-    public Collection<String> getMemberShardNames(final String memberName){
+    public Collection<String> getMemberShardNames(final MemberName memberName){
         Preconditions.checkNotNull(memberName, "memberName should not be null");
 
         List<String> shards = new ArrayList<>();
-        for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
-            for(ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
+        for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
+            for (ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
                 if(shardConfig.getReplicas().contains(memberName)) {
                     shards.add(shardConfig.getName());
                 }
@@ -102,7 +109,7 @@ public class ConfigurationImpl implements Configuration {
     }
 
     @Override
-    public Collection<String> getMembersFromShardName(final String shardName) {
+    public Collection<MemberName> getMembersFromShardName(final String shardName) {
         Preconditions.checkNotNull(shardName, "shardName should not be null");
 
         for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
@@ -121,8 +128,8 @@ public class ConfigurationImpl implements Configuration {
     }
 
     @Override
-    public Collection<String> getUniqueMemberNamesForAllShards() {
-        Set<String> allNames = new HashSet<>();
+    public Collection<MemberName> getUniqueMemberNamesForAllShards() {
+        Set<MemberName> allNames = new HashSet<>();
         for(String shardName: getAllShardNames()) {
             allNames.addAll(getMembersFromShardName(shardName));
         }
@@ -134,14 +141,12 @@ public class ConfigurationImpl implements Configuration {
     public synchronized void addModuleShardConfiguration(ModuleShardConfiguration config) {
         Preconditions.checkNotNull(config, "ModuleShardConfiguration should not be null");
 
-        ModuleConfig moduleConfig = new ModuleConfig(config.getModuleName());
-        moduleConfig.setNameSpace(config.getNamespace().toASCIIString());
-        moduleConfig.setShardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName()));
-
-        moduleConfig.addShardConfig(config.getShardName(), ImmutableSet.copyOf(config.getShardMemberNames()));
+        ModuleConfig moduleConfig = ModuleConfig.builder(config.getModuleName()).
+                nameSpace(config.getNamespace().toASCIIString()).
+                shardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName())).
+                shardConfig(config.getShardName(), config.getShardMemberNames()).build();
 
-        moduleConfigMap = ImmutableMap.<String, ModuleConfig>builder().putAll(moduleConfigMap).
-                put(config.getModuleName(), moduleConfig).build();
+        updateModuleConfigMap(moduleConfig);
 
         namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName).
                 put(moduleConfig.getNameSpace(), moduleConfig.getName()).build();
@@ -157,4 +162,42 @@ public class ConfigurationImpl implements Configuration {
         Preconditions.checkNotNull(shardName, "shardName should not be null");
         return allShardNames.contains(shardName);
     }
+
+    @Override
+    public void addMemberReplicaForShard (String shardName, MemberName newMemberName) {
+        Preconditions.checkNotNull(shardName, "shardName should not be null");
+        Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
+
+        for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
+            ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
+            if(shardConfig != null) {
+                Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
+                replicas.add(newMemberName);
+                updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
+                return;
+            }
+        }
+    }
+
+    @Override
+    public void removeMemberReplicaForShard (String shardName, MemberName newMemberName) {
+        Preconditions.checkNotNull(shardName, "shardName should not be null");
+        Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
+
+        for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
+            ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
+            if(shardConfig != null) {
+                Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
+                replicas.remove(newMemberName);
+                updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
+                return;
+            }
+        }
+    }
+
+    private void updateModuleConfigMap(ModuleConfig moduleConfig) {
+        Map<String, ModuleConfig> newModuleConfigMap = new HashMap<>(moduleConfigMap);
+        newModuleConfigMap.put(moduleConfig.getName(), moduleConfig);
+        moduleConfigMap = ImmutableMap.copyOf(newModuleConfigMap);
+    }
 }