Bug 6910: Fix anyxml node streaming
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / serialization / ValueType.java
index 8169c9e9ae4c13bf5a08bc790f5c272e468faf35..0015a06149ffe84f6a180f8971ed3bd461d30248 100644 (file)
@@ -8,29 +8,95 @@
 
 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;
 import java.math.BigInteger;
 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");
+        }
+    },
+    NULL_TYPE {
+        @Override
+        Object deserialize(final String str) {
+            return null;
+        }
+    };
 
     private static final Map<Class<?>, ValueType> TYPES;
 
@@ -43,7 +109,6 @@ public enum ValueType {
         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);
@@ -52,8 +117,12 @@ public enum ValueType {
         TYPES = b.build();
     }
 
+    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) {
@@ -63,6 +132,10 @@ public enum ValueType {
             return BITS_TYPE;
         }
 
+        if (node instanceof YangInstanceIdentifier) {
+            return YANG_IDENTIFIER_TYPE;
+        }
+
         throw new IllegalArgumentException("Unknown value type " + node.getClass().getSimpleName());
     }
 }