Merge "Bug 1569 - [DEV] Too small variable for OF-Port-Number"
[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.common.base.Preconditions;
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         NormalizedNodeSerializationContext context, Object value){
25         builder.setIntValueType(ValueType.getSerializableType(value).ordinal());
26
27         if(value instanceof YangInstanceIdentifier) {
28             builder.setInstanceIdentifierValue(
29                 InstanceIdentifierUtils.toSerializable((YangInstanceIdentifier) value));
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 {
43             builder.setValue(value.toString());
44         }
45     }
46
47     public static void serialize(NormalizedNodeMessages.PathArgumentAttribute.Builder builder,
48         NormalizedNodeSerializationContext context, Object value){
49
50         builder.setType(ValueType.getSerializableType(value).ordinal());
51         builder.setValue(value.toString());
52     }
53
54     public static Object deSerialize(
55         NormalizedNodeDeSerializationContext context, NormalizedNodeMessages.Node node) {
56         if(node.getIntValueType() == ValueType.YANG_IDENTIFIER_TYPE.ordinal()){
57             return InstanceIdentifierUtils.fromSerializable(
58                 node.getInstanceIdentifierValue());
59         } else if(node.getIntValueType() == ValueType.BITS_TYPE.ordinal()){
60             return new HashSet(node.getBitsValueList());
61         }
62         return deSerializeBasicTypes(node.getIntValueType(), node.getValue());
63     }
64
65     public static Object deSerialize(
66         NormalizedNodeDeSerializationContext context,
67         NormalizedNodeMessages.PathArgumentAttribute attribute) {
68         return deSerializeBasicTypes(attribute.getType(), attribute.getValue());
69     }
70
71
72     private static Object deSerializeBasicTypes(int valueType, String value) {
73         Preconditions.checkArgument(valueType >= 0 && valueType < ValueType.values().length,
74             "Illegal value type " + valueType );
75
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 }