X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FConfigurationImpl.java;h=0541e3a48bb1aab6cef1843e429ff700949d7688;hp=061e1ab448f97ce9ad08541be9b0b1b6d4e73c30;hb=c6c9b43923bbe8bc6d586ce09649324949e6b092;hpb=b5686c35b3e903b46dd0f9da55f18f7606c5152f diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ConfigurationImpl.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ConfigurationImpl.java index 061e1ab448..0541e3a48b 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ConfigurationImpl.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ConfigurationImpl.java @@ -10,6 +10,12 @@ package org.opendaylight.controller.cluster.datastore; import com.google.common.base.Optional; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; +import com.google.common.collect.ImmutableListMultimap; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ListMultimap; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; import com.typesafe.config.ConfigObject; @@ -17,7 +23,6 @@ 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; @@ -29,9 +34,9 @@ import org.slf4j.LoggerFactory; public class ConfigurationImpl implements Configuration { - private final List moduleShards = new ArrayList<>(); + private final List moduleShards; - private final List modules = new ArrayList<>(); + private final List modules; private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class); @@ -44,6 +49,10 @@ public class ConfigurationImpl implements Configuration { // key = shardName, value = list of replicaNames (replicaNames are the same as memberNames) private final Map> shardReplicaNames = new HashMap<>(); + private final ListMultimap moduleNameToShardName; + private final Map moduleNameToStrategy; + private final Map namespaceToModuleName; + private final Set allShardNames; public ConfigurationImpl(final String moduleShardsConfigPath, @@ -74,9 +83,51 @@ public class ConfigurationImpl implements Configuration { modulesConfig = ConfigFactory.load(modulesConfigPath); } - readModuleShards(moduleShardsConfig); + this.moduleShards = readModuleShards(moduleShardsConfig); + this.modules = readModules(modulesConfig); - readModules(modulesConfig); + this.allShardNames = createAllShardNames(moduleShards); + this.moduleNameToShardName = createModuleNameToShardName(moduleShards); + this.moduleNameToStrategy = createModuleNameToStrategy(modules); + this.namespaceToModuleName = createNamespaceToModuleName(modules); + } + + private static Set createAllShardNames(Iterable moduleShards) { + final com.google.common.collect.ImmutableSet.Builder b = ImmutableSet.builder(); + for(ModuleShard ms : moduleShards){ + for(Shard s : ms.getShards()) { + b.add(s.getName()); + } + } + return b.build(); + } + + private static Map createModuleNameToStrategy(Iterable modules) { + final com.google.common.collect.ImmutableMap.Builder b = ImmutableMap.builder(); + for (Module m : modules) { + b.put(m.getName(), m.getShardStrategy()); + } + return b.build(); + } + + private static Map createNamespaceToModuleName(Iterable modules) { + final com.google.common.collect.ImmutableMap.Builder b = ImmutableMap.builder(); + for (Module m : modules) { + b.put(m.getNameSpace(), m.getName()); + } + return b.build(); + } + + private static ListMultimap createModuleNameToShardName(Iterable moduleShards) { + final com.google.common.collect.ImmutableListMultimap.Builder b = ImmutableListMultimap.builder(); + + for (ModuleShard m : moduleShards) { + for (Shard s : m.getShards()) { + b.put(m.getModuleName(), s.getName()); + } + } + + return b.build(); } @Override public List getMemberShardNames(final String memberName){ @@ -104,41 +155,21 @@ public class ConfigurationImpl implements Configuration { } - @Override public Optional getModuleNameFromNameSpace(final String nameSpace) { - + @Override + public Optional getModuleNameFromNameSpace(final String nameSpace) { Preconditions.checkNotNull(nameSpace, "nameSpace should not be null"); - - for(Module m : modules){ - if(m.getNameSpace().equals(nameSpace)){ - return Optional.of(m.getName()); - } - } - return Optional.absent(); + return Optional.fromNullable(namespaceToModuleName.get(nameSpace)); } - @Override public Map getModuleNameToShardStrategyMap() { - Map map = new HashMap<>(); - for(Module m : modules){ - map.put(m.getName(), m.getShardStrategy()); - } - return map; + @Override + public Map getModuleNameToShardStrategyMap() { + return moduleNameToStrategy; } - @Override public List getShardNamesFromModuleName(final String moduleName) { - + @Override + public List getShardNamesFromModuleName(final String moduleName) { Preconditions.checkNotNull(moduleName, "moduleName should not be null"); - - for(ModuleShard m : moduleShards){ - if(m.getModuleName().equals(moduleName)){ - List l = new ArrayList<>(); - for(Shard s : m.getShards()){ - l.add(s.getName()); - } - return l; - } - } - - return Collections.emptyList(); + return moduleNameToShardName.get(moduleName); } @Override public List getMembersFromShardName(final String shardName) { @@ -162,33 +193,30 @@ public class ConfigurationImpl implements Configuration { return Collections.emptyList(); } - @Override public Set getAllShardNames() { - Set shardNames = new LinkedHashSet<>(); - for(ModuleShard ms : moduleShards){ - for(Shard s : ms.getShards()) { - shardNames.add(s.getName()); - } - } - return shardNames; + @Override + public Set getAllShardNames() { + return allShardNames; } - - - private void readModules(final Config modulesConfig) { + private List readModules(final Config modulesConfig) { List modulesConfigObjectList = modulesConfig.getObjectList("modules"); + final Builder b = ImmutableList.builder(); for(ConfigObject o : modulesConfigObjectList){ ConfigObjectWrapper w = new ConfigObjectWrapper(o); - modules.add(new Module(w.stringValue("name"), w.stringValue( + b.add(new Module(w.stringValue("name"), w.stringValue( "namespace"), w.stringValue("shard-strategy"))); } + + return b.build(); } - private void readModuleShards(final Config moduleShardsConfig) { + private static List readModuleShards(final Config moduleShardsConfig) { List moduleShardsConfigObjectList = moduleShardsConfig.getObjectList("module-shards"); + final Builder b = ImmutableList.builder(); for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){ String moduleName = moduleShardConfigObject.get("name").unwrapped().toString(); @@ -204,12 +232,13 @@ public class ConfigurationImpl implements Configuration { shards.add(new Shard(shardName, replicas)); } - this.moduleShards.add(new ModuleShard(moduleName, shards)); + b.add(new ModuleShard(moduleName, shards)); } - } + return b.build(); + } - private class ModuleShard { + private static class ModuleShard { private final String moduleName; private final List shards; @@ -227,7 +256,7 @@ public class ConfigurationImpl implements Configuration { } } - private class Shard { + private static class Shard { private final String name; private final List replicas; @@ -257,7 +286,7 @@ public class ConfigurationImpl implements Configuration { if(ModuleShardStrategy.NAME.equals(shardStrategy)){ this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this); } else { - this.shardStrategy = new DefaultShardStrategy(); + this.shardStrategy = DefaultShardStrategy.getInstance(); } }