71946b0a7abe476241e3f1775ffa8e110da1d559
[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     public static void serialize(NormalizedNodeMessages.Node.Builder builder,
20             QNameSerializationContext context, Object value) {
21         builder.setIntValueType(ValueType.getSerializableType(value).ordinal());
22
23         if(value instanceof YangInstanceIdentifier) {
24             builder.setInstanceIdentifierValue(
25                 InstanceIdentifierUtils.toSerializable((YangInstanceIdentifier) value, context));
26         } else if(value instanceof Set) {
27             Set<?> set = (Set<?>) value;
28             if (!set.isEmpty()) {
29                 for (Object o : set) {
30                     if (o instanceof String) {
31                         builder.addBitsValue(o.toString());
32                     } else {
33                         throw new IllegalArgumentException("Expected value type to be Bits but was : " +
34                                 value.toString());
35                     }
36                 }
37             }
38         } else if(value instanceof byte[]){
39             builder.setBytesValue(ByteString.copyFrom((byte[]) value));
40         } else {
41             builder.setValue(value.toString());
42         }
43     }
44
45     public static void serialize(NormalizedNodeMessages.PathArgumentAttribute.Builder builder,
46             QNameSerializationContext context, Object value){
47
48         builder.setType(ValueType.getSerializableType(value).ordinal());
49
50         if(value instanceof YangInstanceIdentifier) {
51             builder.setInstanceIdentifierValue(
52                     InstanceIdentifierUtils.toSerializable((YangInstanceIdentifier) value, context));
53         } else if(value instanceof Set) {
54             Set<?> set = (Set<?>) value;
55             if (!set.isEmpty()) {
56                 for (Object o : set) {
57                     if (o instanceof String) {
58                         builder.addBitsValue(o.toString());
59                     } else {
60                         throw new IllegalArgumentException("Expected value type to be Bits but was : " +
61                                 value.toString());
62                     }
63                 }
64             }
65         } else if(value instanceof byte[]){
66             builder.setBytesValue(ByteString.copyFrom((byte[]) value));
67         } else {
68             builder.setValue(value.toString());
69         }
70     }
71
72     public static Object deSerialize(QNameDeSerializationContext context,
73             NormalizedNodeMessages.Node node) {
74         if(node.getIntValueType() == ValueType.YANG_IDENTIFIER_TYPE.ordinal()){
75             return InstanceIdentifierUtils.fromSerializable(
76                     node.getInstanceIdentifierValue(), context);
77         } else if(node.getIntValueType() == ValueType.BITS_TYPE.ordinal()){
78             return new HashSet<>(node.getBitsValueList());
79         } else if(node.getIntValueType() == ValueType.BINARY_TYPE.ordinal()){
80             return node.getBytesValue().toByteArray();
81         }
82         return deSerializeBasicTypes(node.getIntValueType(), node.getValue());
83     }
84
85     public static Object deSerialize(QNameDeSerializationContext context,
86             NormalizedNodeMessages.PathArgumentAttribute attribute) {
87
88         if(attribute.getType() == ValueType.YANG_IDENTIFIER_TYPE.ordinal()){
89             return InstanceIdentifierUtils.fromSerializable(
90                     attribute.getInstanceIdentifierValue(), context);
91         } else if(attribute.getType() == ValueType.BITS_TYPE.ordinal()){
92             return new HashSet<>(attribute.getBitsValueList());
93         } else if(attribute.getType() == ValueType.BINARY_TYPE.ordinal()){
94             return attribute.getBytesValue().toByteArray();
95         }
96         return deSerializeBasicTypes(attribute.getType(), attribute.getValue());
97     }
98
99
100     private static Object deSerializeBasicTypes(int valueType, String value) {
101         return ValueType.values()[valueType].deserialize(value);
102     }
103
104 }