Handle empty type in NormalizedNode streaming
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / ValueTypes.java
index 1afc73a3c8deb04a471eee01b437ffd06ed769f3..51ff8d35006f5fba38c6b047038965105a6408e8 100644 (file)
@@ -13,7 +13,9 @@ import com.google.common.collect.ImmutableMap.Builder;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
+import org.opendaylight.yangtools.yang.common.Empty;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 
@@ -33,24 +35,29 @@ 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;
+    // Leaf nodes no longer allow null values. The "empty" type is now represented as
+    // org.opendaylight.yangtools.yang.common.Empty. This is kept for backwards compatibility.
+    @Deprecated
     public static final byte NULL_TYPE = 13;
     public static final byte STRING_BYTES_TYPE = 14;
+    public static final byte EMPTY_TYPE = 15;
 
     private static final Map<Class<?>, Byte> TYPES;
 
     static {
         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(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));
+        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(Short.class, SHORT_TYPE);
+        b.put(BigInteger.class, BIG_INTEGER_TYPE);
+        b.put(BigDecimal.class, BIG_DECIMAL_TYPE);
+        b.put(byte[].class, BINARY_TYPE);
+        b.put(Empty.class, EMPTY_TYPE);
 
         TYPES = b.build();
     }
@@ -59,14 +66,12 @@ final class ValueTypes {
         throw new UnsupportedOperationException("Utility class");
     }
 
-    public static final byte getSerializableType(Object node) {
-        if(node == null){
-            return NULL_TYPE;
-        }
+    public static byte getSerializableType(Object node) {
+        Objects.requireNonNull(node);
 
         final Byte type = TYPES.get(node.getClass());
         if (type != null) {
-            if(type == STRING_TYPE && ((String) node).length() >= STRING_BYTES_LENGTH_THRESHOLD ){
+            if (type == STRING_TYPE && ((String) node).length() >= STRING_BYTES_LENGTH_THRESHOLD) {
                 return STRING_BYTES_TYPE;
             }
             return type;