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