Merge "Bug 2347: Minor fixes to correct log output"
[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 java.math.BigDecimal;
13 import java.math.BigInteger;
14 import java.util.HashSet;
15 import java.util.Set;
16 import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory;
17 import org.opendaylight.controller.cluster.datastore.util.InstanceIdentifierUtils;
18 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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 if(value instanceof byte[]){
42             builder.setBytesValue(ByteString.copyFrom((byte[]) value));
43         } else {
44             builder.setValue(value.toString());
45         }
46     }
47
48     public static void serialize(NormalizedNodeMessages.PathArgumentAttribute.Builder builder,
49             QNameSerializationContext context, Object value){
50
51         builder.setType(ValueType.getSerializableType(value).ordinal());
52
53         if(value instanceof YangInstanceIdentifier) {
54             builder.setInstanceIdentifierValue(
55                     InstanceIdentifierUtils.toSerializable((YangInstanceIdentifier) value, context));
56         } else if(value instanceof Set) {
57             Set<?> set = (Set<?>) value;
58             if (!set.isEmpty()) {
59                 for (Object o : set) {
60                     if (o instanceof String) {
61                         builder.addBitsValue(o.toString());
62                     } else {
63                         throw new IllegalArgumentException("Expected value type to be Bits but was : " +
64                                 value.toString());
65                     }
66                 }
67             }
68         } else if(value instanceof byte[]){
69             builder.setBytesValue(ByteString.copyFrom((byte[]) value));
70         } else {
71             builder.setValue(value.toString());
72         }
73     }
74
75     public static Object deSerialize(QNameDeSerializationContext context,
76             NormalizedNodeMessages.Node node) {
77         if(node.getIntValueType() == ValueType.YANG_IDENTIFIER_TYPE.ordinal()){
78             return InstanceIdentifierUtils.fromSerializable(
79                     node.getInstanceIdentifierValue(), context);
80         } else if(node.getIntValueType() == ValueType.BITS_TYPE.ordinal()){
81             return new HashSet<>(node.getBitsValueList());
82         } else if(node.getIntValueType() == ValueType.BINARY_TYPE.ordinal()){
83             return node.getBytesValue().toByteArray();
84         }
85         return deSerializeBasicTypes(node.getIntValueType(), node.getValue());
86     }
87
88     public static Object deSerialize(QNameDeSerializationContext context,
89             NormalizedNodeMessages.PathArgumentAttribute attribute) {
90
91         if(attribute.getType() == ValueType.YANG_IDENTIFIER_TYPE.ordinal()){
92             return InstanceIdentifierUtils.fromSerializable(
93                     attribute.getInstanceIdentifierValue(), context);
94         } else if(attribute.getType() == ValueType.BITS_TYPE.ordinal()){
95             return new HashSet<>(attribute.getBitsValueList());
96         } else if(attribute.getType() == ValueType.BINARY_TYPE.ordinal()){
97             return attribute.getBytesValue().toByteArray();
98         }
99         return deSerializeBasicTypes(attribute.getType(), attribute.getValue());
100     }
101
102
103     private static Object deSerializeBasicTypes(int valueType, String value) {
104         switch(ValueType.values()[valueType]){
105            case SHORT_TYPE: {
106                return Short.valueOf(value);
107            }
108            case BOOL_TYPE: {
109                return Boolean.valueOf(value);
110            }
111            case BYTE_TYPE: {
112                return Byte.valueOf(value);
113            }
114            case INT_TYPE : {
115                 return Integer.valueOf(value);
116            }
117            case LONG_TYPE: {
118                return Long.valueOf(value);
119            }
120            case QNAME_TYPE: {
121                return QNameFactory.create(value);
122            }
123            case BIG_INTEGER_TYPE: {
124                return new BigInteger(value);
125            }
126            case BIG_DECIMAL_TYPE: {
127                return new BigDecimal(value);
128            }
129            default: {
130                return value;
131            }
132         }
133     }
134
135 }