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 / ValueSerializer.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.protobuf.ByteString;
12 import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory;
13 import org.opendaylight.controller.cluster.datastore.util.InstanceIdentifierUtils;
14 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16
17 import java.math.BigDecimal;
18 import java.math.BigInteger;
19 import java.util.HashSet;
20 import java.util.Set;
21
22 public class ValueSerializer {
23     public static void serialize(NormalizedNodeMessages.Node.Builder builder,
24             QNameSerializationContext context, Object value) {
25         builder.setIntValueType(ValueType.getSerializableType(value).ordinal());
26
27         if(value instanceof YangInstanceIdentifier) {
28             builder.setInstanceIdentifierValue(
29                 InstanceIdentifierUtils.toSerializable((YangInstanceIdentifier) value, context));
30         } else if(value instanceof Set) {
31             Set set = (Set) value;
32             if (!set.isEmpty()) {
33                 for (Object o : set) {
34                     if (o instanceof String) {
35                         builder.addBitsValue(o.toString());
36                     } else {
37                         throw new IllegalArgumentException("Expected value type to be Bits but was : " +
38                                 value.toString());
39                     }
40                 }
41             }
42         } else if(value instanceof byte[]){
43             builder.setBytesValue(ByteString.copyFrom((byte[]) value));
44         } else {
45             builder.setValue(value.toString());
46         }
47     }
48
49     public static void serialize(NormalizedNodeMessages.PathArgumentAttribute.Builder builder,
50             QNameSerializationContext context, Object value){
51
52         builder.setType(ValueType.getSerializableType(value).ordinal());
53         builder.setValue(value.toString());
54     }
55
56     public static Object deSerialize(QNameDeSerializationContext context,
57             NormalizedNodeMessages.Node node) {
58         if(node.getIntValueType() == ValueType.YANG_IDENTIFIER_TYPE.ordinal()){
59             return InstanceIdentifierUtils.fromSerializable(
60                     node.getInstanceIdentifierValue(), context);
61         } else if(node.getIntValueType() == ValueType.BITS_TYPE.ordinal()){
62             return new HashSet(node.getBitsValueList());
63         } else if(node.getIntValueType() == ValueType.BINARY_TYPE.ordinal()){
64             return node.getBytesValue().toByteArray();
65         }
66         return deSerializeBasicTypes(node.getIntValueType(), node.getValue());
67     }
68
69     public static Object deSerialize(QNameDeSerializationContext context,
70             NormalizedNodeMessages.PathArgumentAttribute attribute) {
71         return deSerializeBasicTypes(attribute.getType(), attribute.getValue());
72     }
73
74
75     private static Object deSerializeBasicTypes(int valueType, String value) {
76         switch(ValueType.values()[valueType]){
77            case SHORT_TYPE: {
78                return Short.valueOf(value);
79            }
80            case BOOL_TYPE: {
81                return Boolean.valueOf(value);
82            }
83            case BYTE_TYPE: {
84                return Byte.valueOf(value);
85            }
86            case INT_TYPE : {
87                 return Integer.valueOf(value);
88            }
89            case LONG_TYPE: {
90                return Long.valueOf(value);
91            }
92            case QNAME_TYPE: {
93                return QNameFactory.create(value);
94            }
95            case BIG_INTEGER_TYPE: {
96                return new BigInteger(value);
97            }
98            case BIG_DECIMAL_TYPE: {
99                return new BigDecimal(value);
100            }
101            default: {
102                return value;
103            }
104         }
105     }
106
107 }