Merge "Add support for metadata to the Match/Action classes"
[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             QNameSerializationContext context, Object value) {
24         builder.setIntValueType(ValueType.getSerializableType(value).ordinal());
25
26         if(value instanceof YangInstanceIdentifier) {
27             builder.setInstanceIdentifierValue(
28                 InstanceIdentifierUtils.toSerializable((YangInstanceIdentifier) value, context));
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             QNameSerializationContext context, Object value){
48
49         builder.setType(ValueType.getSerializableType(value).ordinal());
50         builder.setValue(value.toString());
51     }
52
53     public static Object deSerialize(QNameDeSerializationContext context,
54             NormalizedNodeMessages.Node node) {
55         if(node.getIntValueType() == ValueType.YANG_IDENTIFIER_TYPE.ordinal()){
56             return InstanceIdentifierUtils.fromSerializable(
57                     node.getInstanceIdentifierValue(), context);
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(QNameDeSerializationContext context,
65             NormalizedNodeMessages.PathArgumentAttribute attribute) {
66         return deSerializeBasicTypes(attribute.getType(), attribute.getValue());
67     }
68
69
70     private static Object deSerializeBasicTypes(int valueType, String value) {
71         switch(ValueType.values()[valueType]){
72            case SHORT_TYPE: {
73                return Short.valueOf(value);
74            }
75            case BOOL_TYPE: {
76                return Boolean.valueOf(value);
77            }
78            case BYTE_TYPE: {
79                return Byte.valueOf(value);
80            }
81            case INT_TYPE : {
82                 return Integer.valueOf(value);
83            }
84            case LONG_TYPE: {
85                return Long.valueOf(value);
86            }
87            case QNAME_TYPE: {
88                return QNameFactory.create(value);
89            }
90            case BIG_INTEGER_TYPE: {
91                return new BigInteger(value);
92            }
93            case BIG_DECIMAL_TYPE: {
94                return new BigDecimal(value);
95            }
96            default: {
97                return value;
98            }
99         }
100     }
101
102 }