Merge "Bug 2265: Use streaming for DeleteData message"
authorMoiz Raja <moraja@cisco.com>
Wed, 28 Jan 2015 15:29:00 +0000 (15:29 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 28 Jan 2015 15:29:00 +0000 (15:29 +0000)
17 files changed:
opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/__artifactId__-features/pom.xml
opendaylight/config/config-parent/pom.xml
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/impl/AbstractRuntimeCodeGenerator.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/AugmentationIdentifierGenerator.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/PathUtils.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/QNameFactory.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/serialization/ValueSerializer.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/serialization/ValueType.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/ValueTypes.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ConfigurationImpl.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/CanCommitTransactionReply.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/MergeData.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/WriteData.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/DefaultShardStrategy.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ModuleShardStrategy.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactory.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/shardstrategy/DefaultShardStrategyTest.java

index 880e2dc9d59543db777e11c76af628b1adef7fbe..1ee28b8d5a2ed48b7540f09d41f1172fc2a2cb08 100644 (file)
@@ -39,6 +39,30 @@ and is available at http://www.eclipse.org/legal/epl-v10.html INTERNAL
     </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>
index 10c1824ebece2a24af7cc299dba79d88b03d18c3..af39b63447cdf6429371a40a8155de53272cba1d 100644 (file)
@@ -24,6 +24,7 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
   <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>
@@ -45,10 +46,21 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
         <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>
index 677b6aa65ecd8220726e384e318968f9cfe1b116..6c02001fec78baf5a2790990d9bb9419bcc9ba00 100644 (file)
@@ -9,24 +9,23 @@ package org.opendaylight.controller.sal.binding.codegen.impl;
 
 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")
@@ -79,7 +78,7 @@ abstract class AbstractRuntimeCodeGenerator implements org.opendaylight.controll
                      */
                     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()));
                 }
             }
         }
@@ -164,7 +163,7 @@ abstract class AbstractRuntimeCodeGenerator implements org.opendaylight.controll
                 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);
                 }
             }
         });
index 7f7cdc650ee70c5038a7159fb4d7ea4a81a80679..56ef8ea6fc2042c45d0558eb59e8fe2e22dc2f7c 100644 (file)
 
 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);
index 6cdddfd2716c1cf0e008f47853877174ba61d4b3..930dbceab44b83f98aa54d8a8613ec80d73304f9 100644 (file)
 
 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
@@ -27,17 +28,22 @@ public class PathUtils {
      * @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();
     }
 
@@ -70,13 +76,9 @@ public class PathUtils {
      * @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);
     }
index 5a8f5228619e9f80a091da123aa734fb59dcd85f..c63266ff08e218fc6180fdf1cae61e990677eace 100644 (file)
@@ -19,11 +19,12 @@ public class QNameFactory {
 
     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);
                 }
@@ -32,6 +33,6 @@ public class QNameFactory {
 
 
     public static QName create(String name){
-        return cache.getUnchecked(name);
+        return CACHE.getUnchecked(name);
     }
 }
index f562d8b35dd09995ac03d9ce2b3f4436518c8c6f..71946b0a7abe476241e3f1775ffa8e110da1d559 100644 (file)
@@ -9,11 +9,8 @@
 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;
@@ -101,35 +98,7 @@ public class ValueSerializer {
 
 
     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);
     }
 
 }
index 2007544b7edd96d3498d303e3ad0ffd8469ada36..b9e46a3a570fd159d1c8b9284d2efc2ec95c214b 100644 (file)
 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;
         }
 
index e75a454d394294795c633a1d57d83ccc5661ce98..3a2d2b49b3ac90c49a7855de4f64d3502d89c2e2 100644 (file)
@@ -9,15 +9,16 @@
 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;
@@ -31,30 +32,38 @@ public class ValueTypes {
     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;
         }
 
index 061e1ab448f97ce9ad08541be9b0b1b6d4e73c30..0541e3a48bb1aab6cef1843e429ff700949d7688 100644 (file)
@@ -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<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);
@@ -44,6 +49,10 @@ public class ConfigurationImpl implements Configuration {
     // 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,
 
@@ -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<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){
@@ -104,41 +155,21 @@ public class ConfigurationImpl implements Configuration {
 
     }
 
-    @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) {
@@ -162,33 +193,30 @@ public class ConfigurationImpl implements Configuration {
         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();
@@ -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<Shard> shards;
 
@@ -227,7 +256,7 @@ public class ConfigurationImpl implements Configuration {
         }
     }
 
-    private class Shard {
+    private static class Shard {
         private final String name;
         private final List<String> 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();
             }
         }
 
index 7db4846ef4ff69c4c318ab459a07cc09b7943a47..7e8cd436d2ce0133d612eaa0ec2ad31ec57b54c9 100644 (file)
@@ -17,13 +17,16 @@ public class CanCommitTransactionReply implements SerializableMessage {
     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() {
@@ -32,7 +35,7 @@ public class CanCommitTransactionReply implements SerializableMessage {
 
     @Override
     public Object toSerializable() {
-        return serializedMessage;
+        return canCommit ? YES_SERIALIZED : NO_SERIALIZED;
     }
 
     public static CanCommitTransactionReply fromSerializable(final Object message) {
index 9234385b3536bfbe8ccaf528075efcdedc1be792..ae0d630cf267aa75b0ef403b8df7e7733d06e082 100644 (file)
@@ -55,7 +55,7 @@ public class MergeData extends ModifyData implements VersionedSerializableMessag
     }
 
     public static boolean isSerializedType(Object message) {
-        return SERIALIZABLE_CLASS.isAssignableFrom(message.getClass()) ||
+        return SERIALIZABLE_CLASS.isInstance(message) ||
                message instanceof ShardTransactionMessages.MergeData;
     }
 }
index c5e3a6b05966c9c30f12c5f6f7dab93a78fbebf7..989949c88fb0f0c5400ea903119435a6d4ed0940 100644 (file)
@@ -55,7 +55,7 @@ public class WriteData extends ModifyData implements VersionedSerializableMessag
     }
 
     public static boolean isSerializedType(Object message) {
-        return SERIALIZABLE_CLASS.isAssignableFrom(message.getClass()) ||
+        return SERIALIZABLE_CLASS.isInstance(message) ||
                message instanceof ShardTransactionMessages.WriteData;
     }
 }
index 55c682b86000187e10e2376b3f69e94a87772d73..6a3de4256b0b968345b8c01764f15146923e0f9a 100644 (file)
@@ -16,13 +16,21 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
  *   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;
+    }
 }
index fc7ebd94dd481c909ba6e4d65d205e41cbfdebbf..e9ecf7eac30f3e64851e2225ad4a8838d02060e6 100644 (file)
@@ -8,11 +8,10 @@
 
 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";
@@ -26,10 +25,11 @@ public class ModuleShardStrategy implements ShardStrategy {
         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);
index 62fb65482b09d366289107f93df14050340688c4..fd786c903e87e4bc44136bf629977316250f79a3 100644 (file)
@@ -36,7 +36,7 @@ public class ShardStrategyFactory {
         String moduleName = getModuleName(path);
         ShardStrategy shardStrategy = moduleNameToStrategyMap.get(moduleName);
         if (shardStrategy == null) {
-            return new DefaultShardStrategy();
+            return DefaultShardStrategy.getInstance();
         }
 
         return shardStrategy;
index c065782af5056c8e0f57b973f8a099e761d5a322..5842d566ab2bbca843214288e6450d3850694778 100644 (file)
@@ -5,10 +5,9 @@ import org.junit.Test;
 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