BUG 2667 : Handle null value type 89/14789/1
authorMoiz Raja <moraja@cisco.com>
Tue, 3 Feb 2015 21:13:59 +0000 (13:13 -0800)
committerMoiz Raja <moraja@cisco.com>
Tue, 3 Feb 2015 21:13:59 +0000 (13:13 -0800)
Modified the code to handle null types in leaf and leafset entries since
they are allowed. This should help avoid atleast some of the issues seen
by GBP.

Change-Id: Id24e03738e9b5490c8221fbf31ec0734c58655bb
Signed-off-by: Moiz Raja <moraja@cisco.com>
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/NormalizedNodeInputStreamReader.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeOutputStreamWriter.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/ValueTypes.java
opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/serialization/ValueSerializerTest.java
opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/serialization/ValueTypeTest.java
opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamReaderWriterTest.java

index 71946b0a7abe476241e3f1775ffa8e110da1d559..9e3230a623855d4add4249d68af0c2c216f4d79f 100644 (file)
@@ -16,6 +16,8 @@ import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessa
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 
 public class ValueSerializer {
+    private static final String NULL_VALUE = "";
+
     public static void serialize(NormalizedNodeMessages.Node.Builder builder,
             QNameSerializationContext context, Object value) {
         builder.setIntValueType(ValueType.getSerializableType(value).ordinal());
@@ -35,8 +37,10 @@ public class ValueSerializer {
                     }
                 }
             }
-        } else if(value instanceof byte[]){
+        } else if(value instanceof byte[]) {
             builder.setBytesValue(ByteString.copyFrom((byte[]) value));
+        } else if(value == null){
+            builder.setValue(NULL_VALUE);
         } else {
             builder.setValue(value.toString());
         }
@@ -64,6 +68,8 @@ public class ValueSerializer {
             }
         } else if(value instanceof byte[]){
             builder.setBytesValue(ByteString.copyFrom((byte[]) value));
+        } else if(value == null){
+            builder.setValue(NULL_VALUE);
         } else {
             builder.setValue(value.toString());
         }
index b9e46a3a570fd159d1c8b9284d2efc2ec95c214b..9eec8a300a81f2fc304beb7d9978ec7aef26b331 100644 (file)
@@ -8,7 +8,6 @@
 
 package org.opendaylight.controller.cluster.datastore.node.utils.serialization;
 
-import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMap.Builder;
 import java.math.BigDecimal;
@@ -91,6 +90,12 @@ public enum ValueType {
         Object deserialize(final String str) {
             throw new UnsupportedOperationException("Should have been caught by caller");
         }
+    },
+    NULL_TYPE {
+        @Override
+        Object deserialize(final String str) {
+            return null;
+        }
     };
 
     private static final Map<Class<?>, ValueType> TYPES;
@@ -116,7 +121,9 @@ public enum ValueType {
     abstract Object deserialize(String str);
 
     public static final ValueType getSerializableType(Object node) {
-        Preconditions.checkNotNull(node, "node should not be null");
+        if(node == null){
+            return NULL_TYPE;
+        }
 
         final ValueType type = TYPES.get(node.getClass());
         if (type != null) {
index 2aa0027e55756b8d81cfe65f29b12044f7bf5c91..cde338179ba727903e35d4513195cbbddd9b1b1c 100644 (file)
@@ -284,7 +284,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
                 return bytes;
 
             case ValueTypes.YANG_IDENTIFIER_TYPE :
-            return readYangInstanceIdentifier();
+                return readYangInstanceIdentifier();
 
             default :
                 return null;
index 05858a87a12a20630c6bafb531844367e01e1907..fc381c19a98053161fbc93c3525ca8219d006720 100644 (file)
@@ -349,6 +349,8 @@ public class NormalizedNodeOutputStreamWriter implements NormalizedNodeStreamWri
             case ValueTypes.YANG_IDENTIFIER_TYPE:
                 writeYangInstanceIdentifier((YangInstanceIdentifier) value);
                 break;
+            case ValueTypes.NULL_TYPE :
+                break;
             default:
                 output.writeUTF(value.toString());
                 break;
index 3a2d2b49b3ac90c49a7855de4f64d3502d89c2e2..83099f8a5bd656b9c776d60108fd9b3e000e359b 100644 (file)
@@ -8,7 +8,6 @@
 
 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;
@@ -31,6 +30,7 @@ final class ValueTypes {
     public static final byte BIG_INTEGER_TYPE = 10;
     public static final byte BIG_DECIMAL_TYPE = 11;
     public static final byte BINARY_TYPE = 12;
+    public static final byte NULL_TYPE = 13;
 
     private static final Map<Class<?>, Byte> TYPES;
 
@@ -57,7 +57,9 @@ final class ValueTypes {
     }
 
     public static final byte getSerializableType(Object node) {
-        Preconditions.checkNotNull(node, "node should not be null");
+        if(node == null){
+            return NULL_TYPE;
+        }
 
         final Byte type = TYPES.get(node.getClass());
         if (type != null) {
index d9b7a18fdae51ab11899eda96da95a5ec2a81667..f5eecf33d90aa0df9afe164c3d72fc3663c2b14d 100644 (file)
@@ -291,6 +291,24 @@ public class ValueSerializerTest{
 
     }
 
+    @Test
+    public void testSerializeNull(){
+        NormalizedNodeMessages.Node.Builder builder = NormalizedNodeMessages.Node.newBuilder();
+        Object none = null;
+        ValueSerializer.serialize(builder, mock(QNameSerializationContext.class),none);
+
+        assertEquals(ValueType.NULL_TYPE.ordinal(), builder.getIntValueType());
+        assertEquals("", builder.getValue());
+
+        NormalizedNodeMessages.PathArgumentAttribute.Builder builder1 = NormalizedNodeMessages.PathArgumentAttribute.newBuilder();
+
+        ValueSerializer.serialize(builder1, mock(QNameSerializationContext.class),none);
+
+        assertEquals(ValueType.NULL_TYPE.ordinal(), builder1.getType());
+        assertEquals("", builder.getValue());
+
+    }
+
 
     @Test
     public void testDeSerializeShort(){
@@ -519,5 +537,31 @@ public class ValueSerializerTest{
 
     }
 
+    @Test
+    public void testDeSerializeNullType(){
+        NormalizedNodeMessages.Node.Builder nodeBuilder = NormalizedNodeMessages.Node.newBuilder();
+        nodeBuilder.setIntValueType(ValueType.NULL_TYPE.ordinal());
+        nodeBuilder.setValue("");
+
+        Object o = ValueSerializer
+                .deSerialize(mock(QNameDeSerializationContext.class),
+                        nodeBuilder.build());
+
+        assertEquals(null, o);
+
+        NormalizedNodeMessages.PathArgumentAttribute.Builder argumentBuilder
+                = NormalizedNodeMessages.PathArgumentAttribute.newBuilder();
+
+        argumentBuilder.setType(ValueType.NULL_TYPE.ordinal());
+        argumentBuilder.setValue("");
+
+        o = ValueSerializer
+                .deSerialize(mock(QNameDeSerializationContext.class),
+                        argumentBuilder.build());
+
+        assertEquals(null, o);
+
+    }
+
 
 }
index 8fe0633b6e3fbdaf6f4dba52d945cc6b6d014c78..1280559c00c1ecd26d2ce621ddb64e64479f6263 100644 (file)
@@ -1,8 +1,7 @@
 package org.opendaylight.controller.cluster.datastore.node.utils.serialization;
 
-import org.junit.Test;
-
 import static org.junit.Assert.assertEquals;
+import org.junit.Test;
 
 public class ValueTypeTest {
 
@@ -15,4 +14,12 @@ public class ValueTypeTest {
         ValueType serializableType = ValueType.getSerializableType(b);
         assertEquals(ValueType.BINARY_TYPE, serializableType);
     }
+
+    @Test
+    public void testNullType(){
+        ValueType serializableType = ValueType.getSerializableType(null);
+        assertEquals(ValueType.NULL_TYPE, serializableType);
+
+        assertEquals(null, ValueType.NULL_TYPE.deserialize(""));
+    }
 }
\ No newline at end of file
index a67e887a15cf7c9727ca7e9771acb231fdd4a6b1..6528f2e4d2e6524f2f08acb750aae764c86dbc0b 100644 (file)
@@ -59,10 +59,15 @@ public class NormalizedNodeStreamReaderWriterTest {
                 new YangInstanceIdentifier.NodeWithValue(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).
                 withValue(bytes2).build();
 
+        LeafSetEntryNode<Object> entry3 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
+                new YangInstanceIdentifier.NodeWithValue(TestModel.BINARY_LEAF_LIST_QNAME, null)).
+                withValue(null).build();
+
+
         return TestModel.createBaseTestContainerBuilder().
                 withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
                         new YangInstanceIdentifier.NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME)).
-                        withChild(entry1).withChild(entry2).build()).
+                        withChild(entry1).withChild(entry2).withChild(entry3).build()).
                 withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1,2,3,4})).
                 withChild(Builders.orderedMapBuilder().
                       withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.ORDERED_LIST_QNAME)).