Merge "BUG 1839 - HTTP delete of non existing data"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ConfigurationImpl.java
index 34590025d59ab78d3e0095e81addaa8ed874935f..1a0a5dd6591c9a653ae2079b938d6448e59c036f 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.controller.cluster.datastore;
 
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 import com.typesafe.config.Config;
 import com.typesafe.config.ConfigFactory;
 import com.typesafe.config.ConfigObject;
@@ -22,8 +23,10 @@ import java.io.File;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 public class ConfigurationImpl implements Configuration {
 
@@ -34,11 +37,23 @@ public class ConfigurationImpl implements Configuration {
     private static final Logger
         LOG = LoggerFactory.getLogger(DistributedDataStore.class);
 
+    // Look up maps to speed things up
+
+    // key = memberName, value = list of shardNames
+    private Map<String, List<String>> memberShardNames = new HashMap<>();
+
+    // key = shardName, value = list of replicaNames (replicaNames are the same as memberNames)
+    private Map<String, List<String>> shardReplicaNames = new HashMap<>();
+
 
     public ConfigurationImpl(String moduleShardsConfigPath,
 
         String modulesConfigPath){
 
+        Preconditions.checkNotNull(moduleShardsConfigPath, "moduleShardsConfigPath should not be null");
+        Preconditions.checkNotNull(modulesConfigPath, "modulesConfigPath should not be null");
+
+
         File moduleShardsFile = new File("./configuration/initial/" + moduleShardsConfigPath);
         File modulesFile = new File("./configuration/initial/" + modulesConfigPath);
 
@@ -66,6 +81,13 @@ public class ConfigurationImpl implements Configuration {
     }
 
     @Override public List<String> getMemberShardNames(String memberName){
+
+        Preconditions.checkNotNull(memberName, "memberName should not be null");
+
+        if(memberShardNames.containsKey(memberName)){
+            return memberShardNames.get(memberName);
+        }
+
         List<String> shards = new ArrayList();
         for(ModuleShard ms : moduleShards){
             for(Shard s : ms.getShards()){
@@ -76,11 +98,17 @@ public class ConfigurationImpl implements Configuration {
                 }
             }
         }
+
+        memberShardNames.put(memberName, shards);
+
         return shards;
 
     }
 
     @Override public Optional<String> getModuleNameFromNameSpace(String nameSpace) {
+
+        Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
+
         for(Module m : modules){
             if(m.getNameSpace().equals(nameSpace)){
                 return Optional.of(m.getName());
@@ -98,6 +126,9 @@ public class ConfigurationImpl implements Configuration {
     }
 
     @Override public List<String> getShardNamesFromModuleName(String moduleName) {
+
+        Preconditions.checkNotNull(moduleName, "moduleName should not be null");
+
         for(ModuleShard m : moduleShards){
             if(m.getModuleName().equals(moduleName)){
                 List<String> l = new ArrayList<>();
@@ -112,17 +143,36 @@ public class ConfigurationImpl implements Configuration {
     }
 
     @Override public List<String> getMembersFromShardName(String shardName) {
-        List<String> shards = new ArrayList();
+
+        Preconditions.checkNotNull(shardName, "shardName should not be null");
+
+        if(shardReplicaNames.containsKey(shardName)){
+            return shardReplicaNames.get(shardName);
+        }
+
         for(ModuleShard ms : moduleShards){
             for(Shard s : ms.getShards()) {
                 if(s.getName().equals(shardName)){
-                    return s.getReplicas();
+                    List<String> replicas = s.getReplicas();
+                    shardReplicaNames.put(shardName, replicas);
+                    return replicas;
                 }
             }
         }
+        shardReplicaNames.put(shardName, Collections.EMPTY_LIST);
         return Collections.EMPTY_LIST;
     }
 
+    @Override public Set<String> getAllShardNames() {
+        Set<String> shardNames = new LinkedHashSet<>();
+        for(ModuleShard ms : moduleShards){
+            for(Shard s : ms.getShards()) {
+                shardNames.add(s.getName());
+            }
+        }
+        return shardNames;
+    }
+
 
 
     private void readModules(Config modulesConfig) {