BUG 2325 : Value type of byte[] not recognized by the NormalizedNodeSerializer
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / serialization / ValueType.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.datastore.node.utils.serialization;
10
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14
15 import java.math.BigDecimal;
16 import java.math.BigInteger;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
21 public enum ValueType {
22     SHORT_TYPE,
23     BYTE_TYPE,
24     INT_TYPE,
25     LONG_TYPE,
26     BOOL_TYPE,
27     QNAME_TYPE,
28     BITS_TYPE,
29     YANG_IDENTIFIER_TYPE,
30     STRING_TYPE,
31     BIG_INTEGER_TYPE,
32     BIG_DECIMAL_TYPE,
33     BINARY_TYPE;
34
35     private static Map<Class, ValueType> types = new HashMap<>();
36
37     static {
38         types.put(String.class, STRING_TYPE);
39         types.put(Byte.class, BYTE_TYPE);
40         types.put(Integer.class, INT_TYPE);
41         types.put(Long.class, LONG_TYPE);
42         types.put(Boolean.class, BOOL_TYPE);
43         types.put(QName.class, QNAME_TYPE);
44         types.put(Set.class, BITS_TYPE);
45         types.put(YangInstanceIdentifier.class, YANG_IDENTIFIER_TYPE);
46         types.put(Short.class,SHORT_TYPE);
47         types.put(BigInteger.class, BIG_INTEGER_TYPE);
48         types.put(BigDecimal.class, BIG_DECIMAL_TYPE);
49         types.put(byte[].class, BINARY_TYPE);
50     }
51
52     public static final ValueType getSerializableType(Object node){
53         Preconditions.checkNotNull(node, "node should not be null");
54
55         ValueType type = types.get(node.getClass());
56         if(type != null) {
57             return type;
58         } else if(node instanceof Set){
59             return BITS_TYPE;
60         }
61
62         throw new IllegalArgumentException("Unknown value type " + node.getClass().getSimpleName());
63     }
64 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.