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