</dependencies>
</dependencyManagement>
<dependencies>
+ <dependency>
+ <groupId>org.opendaylight.yangtools</groupId>
+ <artifactId>features-yangtools</artifactId>
+ <classifier>features</classifier>
+ <version>${yangtools.version}</version>
+ <type>xml</type>
+ <scope>runtime</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.opendaylight.controller</groupId>
+ <artifactId>features-mdsal</artifactId>
+ <classifier>features</classifier>
+ <version>${mdsal.version}</version>
+ <type>xml</type>
+ <scope>runtime</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.opendaylight.controller</groupId>
+ <artifactId>features-restconf</artifactId>
+ <classifier>features</classifier>
+ <version>${mdsal.version}</version>
+ <type>xml</type>
+ <scope>runtime</scope>
+ </dependency>
<dependency>
<groupId>${symbol_dollar}{groupId}</groupId>
<artifactId>${artifactId}-impl</artifactId>
<properties>
<config.version>0.3.0-SNAPSHOT</config.version>
<mdsal.version>1.2.0-SNAPSHOT</mdsal.version>
+ <yangtools.version>0.7.0-SNAPSHOT</yangtools.version>
<jmxGeneratorPath>src/main/yang-gen-config</jmxGeneratorPath>
<config.file>src/main/config/default-config.xml</config.file>
</properties>
<type>pom</type>
<scope>import</scope>
</dependency>
+ <dependency>
+ <groupId>org.opendaylight.yangtools</groupId>
+ <artifactId>yangtools-artifacts</artifactId>
+ <version>${yangtools.version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
</dependencies>
</dependencyManagement>
<dependencies>
+ <dependency>
+ <groupId>org.opendaylight.yangtools</groupId>
+ <artifactId>yang-common</artifactId>
+ </dependency>
<dependency>
<groupId>org.opendaylight.controller</groupId>
<artifactId>config-api</artifactId>
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
+import java.util.Map;
+import java.util.WeakHashMap;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;
+import javax.annotation.concurrent.GuardedBy;
import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter;
import org.opendaylight.controller.sal.binding.codegen.RpcIsNotRoutedException;
import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory;
import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
+import org.opendaylight.yangtools.util.ClassLoaderUtils;
import org.opendaylight.yangtools.yang.binding.BindingMapping;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.binding.NotificationListener;
import org.opendaylight.yangtools.yang.binding.RpcService;
import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
-import org.opendaylight.yangtools.yang.binding.util.ClassLoaderUtils;
-
-import javax.annotation.concurrent.GuardedBy;
-import java.util.Map;
-import java.util.WeakHashMap;
abstract class AbstractRuntimeCodeGenerator implements org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator, NotificationInvokerFactory {
@GuardedBy("this")
*/
Thread.currentThread().getContextClassLoader().loadClass(routingPair.getInputType().getName());
} else {
- throw new RpcIsNotRoutedException("RPC " + method.getName() + " from "+ iface.getName() +" is not routed");
+ throw new RpcIsNotRoutedException(String.format("RPC %s from %s is not routed", method.getName(), iface.getName()));
}
}
}
try {
return getRpcMetadata(utils.asCtClass(serviceType));
} catch (ClassNotFoundException | NotFoundException e) {
- throw new IllegalStateException(String.format("Failed to load metadata for class {}", serviceType), e);
+ throw new IllegalStateException(String.format("Failed to load metadata for class %s", serviceType), e);
}
}
});
package org.opendaylight.controller.cluster.datastore.node.utils;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-
+import com.google.common.base.Splitter;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
public class AugmentationIdentifierGenerator {
+ private static final Pattern PATTERN = Pattern.compile("AugmentationIdentifier\\Q{\\EchildNames=\\Q[\\E(.*)\\Q]}\\E");
+ private static final Splitter COMMA_SPLITTER = Splitter.on(',').trimResults();
+
private final String id;
- private static final Pattern pattern = Pattern.compile("AugmentationIdentifier\\Q{\\EchildNames=\\Q[\\E(.*)\\Q]}\\E");
private final Matcher matcher;
private final boolean doesMatch;
- public AugmentationIdentifierGenerator(String id){
+ public AugmentationIdentifierGenerator(String id) {
this.id = id;
- matcher = pattern.matcher(this.id);
+ matcher = PATTERN.matcher(this.id);
doesMatch = matcher.matches();
}
- public boolean matches(){
+ public boolean matches() {
return doesMatch;
}
- public YangInstanceIdentifier.AugmentationIdentifier getPathArgument(){
- Set<QName> childNames = new HashSet<QName>();
+ public YangInstanceIdentifier.AugmentationIdentifier getPathArgument() {
final String childQNames = matcher.group(1);
- final String[] splitChildQNames = childQNames.split(",");
-
- for(String name : splitChildQNames){
- childNames.add(
- org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory
- .create(name.trim()));
+ final Set<QName> childNames = new HashSet<>();
+ for (String name : COMMA_SPLITTER.split(childQNames)) {
+ childNames.add(QNameFactory.create(name));
}
return new YangInstanceIdentifier.AugmentationIdentifier(childNames);
package org.opendaylight.controller.cluster.datastore.node.utils;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-
+import com.google.common.base.Splitter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
public class PathUtils {
+ private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings();
/**
* Given a YangInstanceIdentifier return a serialized version of the same
* @param path
* @return
*/
- public static String toString(YangInstanceIdentifier path){
- StringBuilder sb = new StringBuilder();
- Iterator<YangInstanceIdentifier.PathArgument> iterator =
+ public static String toString(YangInstanceIdentifier path) {
+ final Iterator<YangInstanceIdentifier.PathArgument> it =
path.getPathArguments().iterator();
+ if (!it.hasNext()) {
+ return "";
+ }
- while(iterator.hasNext()){
- sb.append(toString(iterator.next()));
- if(iterator.hasNext()){
- sb.append("/");
+ final StringBuilder sb = new StringBuilder();
+ for (;;) {
+ sb.append(toString(it.next()));
+ if (!it.hasNext()) {
+ break;
}
+ sb.append('/');
}
+
return sb.toString();
}
* @return
*/
public static YangInstanceIdentifier toYangInstanceIdentifier(String path){
- String[] segments = path.split("/");
-
List<YangInstanceIdentifier.PathArgument> pathArguments = new ArrayList<>();
- for (String segment : segments) {
- if (!"".equals(segment)) {
- pathArguments.add(NodeIdentifierFactory.getArgument(segment));
- }
+ for (String segment : SLASH_SPLITTER.split(path)) {
+ pathArguments.add(NodeIdentifierFactory.getArgument(segment));
}
return YangInstanceIdentifier.create(pathArguments);
}
private static final int MAX_QNAME_CACHE_SIZE = 10000;
- private static LoadingCache<String, QName> cache = CacheBuilder.newBuilder()
+ private static final LoadingCache<String, QName> CACHE = CacheBuilder.newBuilder()
.maximumSize(MAX_QNAME_CACHE_SIZE)
.softValues()
.build(
new CacheLoader<String, QName>() {
+ @Override
public QName load(String key) {
return QName.create(key);
}
public static QName create(String name){
- return cache.getUnchecked(name);
+ return CACHE.getUnchecked(name);
}
}
package org.opendaylight.controller.cluster.datastore.node.utils.serialization;
import com.google.protobuf.ByteString;
-import java.math.BigDecimal;
-import java.math.BigInteger;
import java.util.HashSet;
import java.util.Set;
-import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory;
import org.opendaylight.controller.cluster.datastore.util.InstanceIdentifierUtils;
import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
private static Object deSerializeBasicTypes(int valueType, String value) {
- switch(ValueType.values()[valueType]){
- case SHORT_TYPE: {
- return Short.valueOf(value);
- }
- case BOOL_TYPE: {
- return Boolean.valueOf(value);
- }
- case BYTE_TYPE: {
- return Byte.valueOf(value);
- }
- case INT_TYPE : {
- return Integer.valueOf(value);
- }
- case LONG_TYPE: {
- return Long.valueOf(value);
- }
- case QNAME_TYPE: {
- return QNameFactory.create(value);
- }
- case BIG_INTEGER_TYPE: {
- return new BigInteger(value);
- }
- case BIG_DECIMAL_TYPE: {
- return new BigDecimal(value);
- }
- default: {
- return value;
- }
- }
+ return ValueType.values()[valueType].deserialize(value);
}
}
package org.opendaylight.controller.cluster.datastore.node.utils.serialization;
import com.google.common.base.Preconditions;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableMap.Builder;
import java.math.BigDecimal;
import java.math.BigInteger;
-import java.util.HashMap;
import java.util.Map;
import java.util.Set;
+import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
public enum ValueType {
- SHORT_TYPE,
- BYTE_TYPE,
- INT_TYPE,
- LONG_TYPE,
- BOOL_TYPE,
- QNAME_TYPE,
- BITS_TYPE,
- YANG_IDENTIFIER_TYPE,
- STRING_TYPE,
- BIG_INTEGER_TYPE,
- BIG_DECIMAL_TYPE,
- BINARY_TYPE;
+ SHORT_TYPE {
+ @Override
+ Object deserialize(final String str) {
+ return Short.valueOf(str);
+ }
+ },
+ BYTE_TYPE {
+ @Override
+ Object deserialize(final String str) {
+ return Byte.valueOf(str);
+ }
+ },
+ INT_TYPE {
+ @Override
+ Object deserialize(final String str) {
+ return Integer.valueOf(str);
+ }
+ },
+ LONG_TYPE {
+ @Override
+ Object deserialize(final String str) {
+ return Long.valueOf(str);
+ }
+ },
+ BOOL_TYPE {
+ @Override
+ Object deserialize(final String str) {
+ return Boolean.valueOf(str);
+ }
+ },
+ QNAME_TYPE {
+ @Override
+ Object deserialize(final String str) {
+ return QNameFactory.create(str);
+ }
+ },
+ BITS_TYPE {
+ @Override
+ Object deserialize(final String str) {
+ throw new UnsupportedOperationException("Should have been caught by caller");
+ }
+ },
+ YANG_IDENTIFIER_TYPE {
+ @Override
+ Object deserialize(final String str) {
+ throw new UnsupportedOperationException("Should have been caught by caller");
+ }
+ },
+ STRING_TYPE {
+ @Override
+ Object deserialize(final String str) {
+ return str;
+ }
+ },
+ BIG_INTEGER_TYPE {
+ @Override
+ Object deserialize(final String str) {
+ return new BigInteger(str);
+ }
+ },
+ BIG_DECIMAL_TYPE {
+ @Override
+ Object deserialize(final String str) {
+ return new BigDecimal(str);
+ }
+ },
+ BINARY_TYPE {
+ @Override
+ Object deserialize(final String str) {
+ throw new UnsupportedOperationException("Should have been caught by caller");
+ }
+ };
- private static Map<Class<?>, ValueType> types = new HashMap<>();
+ private static final Map<Class<?>, ValueType> TYPES;
static {
- types.put(String.class, STRING_TYPE);
- types.put(Byte.class, BYTE_TYPE);
- types.put(Integer.class, INT_TYPE);
- types.put(Long.class, LONG_TYPE);
- types.put(Boolean.class, BOOL_TYPE);
- types.put(QName.class, QNAME_TYPE);
- types.put(Set.class, BITS_TYPE);
- types.put(YangInstanceIdentifier.class, YANG_IDENTIFIER_TYPE);
- types.put(Short.class,SHORT_TYPE);
- types.put(BigInteger.class, BIG_INTEGER_TYPE);
- types.put(BigDecimal.class, BIG_DECIMAL_TYPE);
- types.put(byte[].class, BINARY_TYPE);
+ final Builder<Class<?>, ValueType> b = ImmutableMap.builder();
+
+ b.put(String.class, STRING_TYPE);
+ b.put(Byte.class, BYTE_TYPE);
+ b.put(Integer.class, INT_TYPE);
+ b.put(Long.class, LONG_TYPE);
+ b.put(Boolean.class, BOOL_TYPE);
+ b.put(QName.class, QNAME_TYPE);
+ b.put(YangInstanceIdentifier.class, YANG_IDENTIFIER_TYPE);
+ b.put(Short.class,SHORT_TYPE);
+ b.put(BigInteger.class, BIG_INTEGER_TYPE);
+ b.put(BigDecimal.class, BIG_DECIMAL_TYPE);
+ b.put(byte[].class, BINARY_TYPE);
+
+ TYPES = b.build();
}
- public static final ValueType getSerializableType(Object node){
+ abstract Object deserialize(String str);
+
+ public static final ValueType getSerializableType(Object node) {
Preconditions.checkNotNull(node, "node should not be null");
- ValueType type = types.get(node.getClass());
- if(type != null) {
+ final ValueType type = TYPES.get(node.getClass());
+ if (type != null) {
return type;
- } else if(node instanceof Set){
+ }
+ if (node instanceof Set) {
return BITS_TYPE;
}
package org.opendaylight.controller.cluster.datastore.node.utils.stream;
import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableMap.Builder;
import java.math.BigDecimal;
import java.math.BigInteger;
-import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-public class ValueTypes {
+final class ValueTypes {
public static final byte SHORT_TYPE = 1;
public static final byte BYTE_TYPE = 2;
public static final byte INT_TYPE = 3;
public static final byte BIG_DECIMAL_TYPE = 11;
public static final byte BINARY_TYPE = 12;
- private static Map<Class<?>, Byte> types = new HashMap<>();
+ private static final Map<Class<?>, Byte> TYPES;
static {
- types.put(String.class, Byte.valueOf(STRING_TYPE));
- types.put(Byte.class, Byte.valueOf(BYTE_TYPE));
- types.put(Integer.class, Byte.valueOf(INT_TYPE));
- types.put(Long.class, Byte.valueOf(LONG_TYPE));
- types.put(Boolean.class, Byte.valueOf(BOOL_TYPE));
- types.put(QName.class, Byte.valueOf(QNAME_TYPE));
- types.put(Set.class, Byte.valueOf(BITS_TYPE));
- types.put(YangInstanceIdentifier.class, Byte.valueOf(YANG_IDENTIFIER_TYPE));
- types.put(Short.class, Byte.valueOf(SHORT_TYPE));
- types.put(BigInteger.class, Byte.valueOf(BIG_INTEGER_TYPE));
- types.put(BigDecimal.class, Byte.valueOf(BIG_DECIMAL_TYPE));
- types.put(byte[].class, Byte.valueOf(BINARY_TYPE));
+ final Builder<Class<?>, Byte> b = ImmutableMap.builder();
+
+ b.put(String.class, Byte.valueOf(STRING_TYPE));
+ b.put(Byte.class, Byte.valueOf(BYTE_TYPE));
+ b.put(Integer.class, Byte.valueOf(INT_TYPE));
+ b.put(Long.class, Byte.valueOf(LONG_TYPE));
+ b.put(Boolean.class, Byte.valueOf(BOOL_TYPE));
+ b.put(QName.class, Byte.valueOf(QNAME_TYPE));
+ b.put(YangInstanceIdentifier.class, Byte.valueOf(YANG_IDENTIFIER_TYPE));
+ b.put(Short.class, Byte.valueOf(SHORT_TYPE));
+ b.put(BigInteger.class, Byte.valueOf(BIG_INTEGER_TYPE));
+ b.put(BigDecimal.class, Byte.valueOf(BIG_DECIMAL_TYPE));
+ b.put(byte[].class, Byte.valueOf(BINARY_TYPE));
+
+ TYPES = b.build();
}
- public static final byte getSerializableType(Object node){
+ private ValueTypes() {
+ throw new UnsupportedOperationException("Utility class");
+ }
+
+ public static final byte getSerializableType(Object node) {
Preconditions.checkNotNull(node, "node should not be null");
- Byte type = types.get(node.getClass());
- if(type != null) {
+ final Byte type = TYPES.get(node.getClass());
+ if (type != null) {
return type;
- } else if(node instanceof Set){
+ }
+ if (node instanceof Set) {
return BITS_TYPE;
}
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;
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 {
- private final List<ModuleShard> moduleShards = new ArrayList<>();
+ private final List<ModuleShard> moduleShards;
- private final List<Module> modules = new ArrayList<>();
+ private final List<Module> modules;
private static final Logger
LOG = LoggerFactory.getLogger(DistributedDataStore.class);
// key = shardName, value = list of replicaNames (replicaNames are the same as memberNames)
private final Map<String, List<String>> shardReplicaNames = new HashMap<>();
+ private final ListMultimap<String, String> moduleNameToShardName;
+ private final Map<String, ShardStrategy> moduleNameToStrategy;
+ private final Map<String, String> namespaceToModuleName;
+ private final Set<String> allShardNames;
public ConfigurationImpl(final String moduleShardsConfigPath,
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<String> createAllShardNames(Iterable<ModuleShard> moduleShards) {
+ final com.google.common.collect.ImmutableSet.Builder<String> b = ImmutableSet.builder();
+ for(ModuleShard ms : moduleShards){
+ for(Shard s : ms.getShards()) {
+ b.add(s.getName());
+ }
+ }
+ return b.build();
+ }
+
+ private static Map<String, ShardStrategy> createModuleNameToStrategy(Iterable<Module> modules) {
+ final com.google.common.collect.ImmutableMap.Builder<String, ShardStrategy> b = ImmutableMap.builder();
+ for (Module m : modules) {
+ b.put(m.getName(), m.getShardStrategy());
+ }
+ return b.build();
+ }
+
+ private static Map<String, String> createNamespaceToModuleName(Iterable<Module> modules) {
+ final com.google.common.collect.ImmutableMap.Builder<String, String> b = ImmutableMap.builder();
+ for (Module m : modules) {
+ b.put(m.getNameSpace(), m.getName());
+ }
+ return b.build();
+ }
+
+ private static ListMultimap<String, String> createModuleNameToShardName(Iterable<ModuleShard> moduleShards) {
+ final com.google.common.collect.ImmutableListMultimap.Builder<String, String> b = ImmutableListMultimap.builder();
+
+ for (ModuleShard m : moduleShards) {
+ for (Shard s : m.getShards()) {
+ b.put(m.getModuleName(), s.getName());
+ }
+ }
+
+ return b.build();
}
@Override public List<String> getMemberShardNames(final String memberName){
}
- @Override public Optional<String> getModuleNameFromNameSpace(final String nameSpace) {
-
+ @Override
+ public Optional<String> 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<String, ShardStrategy> getModuleNameToShardStrategyMap() {
- Map<String, ShardStrategy> map = new HashMap<>();
- for(Module m : modules){
- map.put(m.getName(), m.getShardStrategy());
- }
- return map;
+ @Override
+ public Map<String, ShardStrategy> getModuleNameToShardStrategyMap() {
+ return moduleNameToStrategy;
}
- @Override public List<String> getShardNamesFromModuleName(final String moduleName) {
-
+ @Override
+ public List<String> getShardNamesFromModuleName(final String moduleName) {
Preconditions.checkNotNull(moduleName, "moduleName should not be null");
-
- for(ModuleShard m : moduleShards){
- if(m.getModuleName().equals(moduleName)){
- List<String> l = new ArrayList<>();
- for(Shard s : m.getShards()){
- l.add(s.getName());
- }
- return l;
- }
- }
-
- return Collections.emptyList();
+ return moduleNameToShardName.get(moduleName);
}
@Override public List<String> getMembersFromShardName(final String shardName) {
return Collections.emptyList();
}
- @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;
+ @Override
+ public Set<String> getAllShardNames() {
+ return allShardNames;
}
-
-
- private void readModules(final Config modulesConfig) {
+ private List<Module> readModules(final Config modulesConfig) {
List<? extends ConfigObject> modulesConfigObjectList =
modulesConfig.getObjectList("modules");
+ final Builder<Module> 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<ModuleShard> readModuleShards(final Config moduleShardsConfig) {
List<? extends ConfigObject> moduleShardsConfigObjectList =
moduleShardsConfig.getObjectList("module-shards");
+ final Builder<ModuleShard> b = ImmutableList.builder();
for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){
String moduleName = moduleShardConfigObject.get("name").unwrapped().toString();
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<Shard> shards;
}
}
- private class Shard {
+ private static class Shard {
private final String name;
private final List<String> replicas;
if(ModuleShardStrategy.NAME.equals(shardStrategy)){
this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this);
} else {
- this.shardStrategy = new DefaultShardStrategy();
+ this.shardStrategy = DefaultShardStrategy.getInstance();
}
}
public static final CanCommitTransactionReply YES = new CanCommitTransactionReply(true);
public static final CanCommitTransactionReply NO = new CanCommitTransactionReply(false);
+ private static final ThreePhaseCommitCohortMessages.CanCommitTransactionReply YES_SERIALIZED =
+ ThreePhaseCommitCohortMessages.CanCommitTransactionReply.newBuilder().setCanCommit(true).build();
+
+ private static final ThreePhaseCommitCohortMessages.CanCommitTransactionReply NO_SERIALIZED =
+ ThreePhaseCommitCohortMessages.CanCommitTransactionReply.newBuilder().setCanCommit(false).build();
+
private final boolean canCommit;
- private final Object serializedMessage;
private CanCommitTransactionReply(final boolean canCommit) {
this.canCommit = canCommit;
- this.serializedMessage = ThreePhaseCommitCohortMessages.CanCommitTransactionReply.newBuilder().
- setCanCommit(canCommit).build();
}
public boolean getCanCommit() {
@Override
public Object toSerializable() {
- return serializedMessage;
+ return canCommit ? YES_SERIALIZED : NO_SERIALIZED;
}
public static CanCommitTransactionReply fromSerializable(final Object message) {
}
public static boolean isSerializedType(Object message) {
- return SERIALIZABLE_CLASS.isAssignableFrom(message.getClass()) ||
+ return SERIALIZABLE_CLASS.isInstance(message) ||
message instanceof ShardTransactionMessages.MergeData;
}
}
}
public static boolean isSerializedType(Object message) {
- return SERIALIZABLE_CLASS.isAssignableFrom(message.getClass()) ||
+ return SERIALIZABLE_CLASS.isInstance(message) ||
message instanceof ShardTransactionMessages.WriteData;
}
}
* The default shard stores data for all modules for which a specific set of shards has not been configured
* </p>
*/
-public class DefaultShardStrategy implements ShardStrategy{
+public final class DefaultShardStrategy implements ShardStrategy {
+ public static final String NAME = "default";
+ public static final String DEFAULT_SHARD = "default";
+ private static final DefaultShardStrategy INSTANCE = new DefaultShardStrategy();
- public static final String NAME = "default";
- public static final String DEFAULT_SHARD = "default";
+ private DefaultShardStrategy() {
+ // Hidden to force a singleton instnace
+ }
- @Override
- public String findShard(YangInstanceIdentifier path) {
- return DEFAULT_SHARD;
- }
+ public static DefaultShardStrategy getInstance() {
+ return INSTANCE;
+ }
+
+ @Override
+ public String findShard(YangInstanceIdentifier path) {
+ return DEFAULT_SHARD;
+ }
}
package org.opendaylight.controller.cluster.datastore.shardstrategy;
+import java.util.List;
import org.opendaylight.controller.cluster.datastore.Configuration;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import java.util.List;
-
public class ModuleShardStrategy implements ShardStrategy {
public static final String NAME = "module";
this.configuration = configuration;
}
- @Override public String findShard(YangInstanceIdentifier path) {
+ @Override
+ public String findShard(YangInstanceIdentifier path) {
List<String> shardNames =
configuration.getShardNamesFromModuleName(moduleName);
- if(shardNames.size() == 0){
+ if (shardNames.isEmpty()) {
return DefaultShardStrategy.DEFAULT_SHARD;
}
return shardNames.get(0);
String moduleName = getModuleName(path);
ShardStrategy shardStrategy = moduleNameToStrategyMap.get(moduleName);
if (shardStrategy == null) {
- return new DefaultShardStrategy();
+ return DefaultShardStrategy.getInstance();
}
return shardStrategy;
import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
public class DefaultShardStrategyTest {
-
- @Test
- public void testFindShard() throws Exception {
- String shard = new DefaultShardStrategy().findShard(TestModel.TEST_PATH);
- Assert.assertEquals(DefaultShardStrategy.DEFAULT_SHARD, shard);
- }
+ @Test
+ public void testFindShard() throws Exception {
+ String shard = DefaultShardStrategy.getInstance().findShard(TestModel.TEST_PATH);
+ Assert.assertEquals(DefaultShardStrategy.DEFAULT_SHARD, shard);
+ }
}
\ No newline at end of file