2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore.node.utils.serialization;
11 import com.google.protobuf.ByteString;
12 import java.math.BigDecimal;
13 import java.math.BigInteger;
14 import java.util.HashSet;
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;
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());
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;
32 for (Object o : set) {
33 if (o instanceof String) {
34 builder.addBitsValue(o.toString());
36 throw new IllegalArgumentException("Expected value type to be Bits but was : " +
41 } else if(value instanceof byte[]){
42 builder.setBytesValue(ByteString.copyFrom((byte[]) value));
44 builder.setValue(value.toString());
48 public static void serialize(NormalizedNodeMessages.PathArgumentAttribute.Builder builder,
49 QNameSerializationContext context, Object value){
51 builder.setType(ValueType.getSerializableType(value).ordinal());
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;
59 for (Object o : set) {
60 if (o instanceof String) {
61 builder.addBitsValue(o.toString());
63 throw new IllegalArgumentException("Expected value type to be Bits but was : " +
68 } else if(value instanceof byte[]){
69 builder.setBytesValue(ByteString.copyFrom((byte[]) value));
71 builder.setValue(value.toString());
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();
85 return deSerializeBasicTypes(node.getIntValueType(), node.getValue());
88 public static Object deSerialize(QNameDeSerializationContext context,
89 NormalizedNodeMessages.PathArgumentAttribute attribute) {
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();
99 return deSerializeBasicTypes(attribute.getType(), attribute.getValue());
103 private static Object deSerializeBasicTypes(int valueType, String value) {
104 switch(ValueType.values()[valueType]){
106 return Short.valueOf(value);
109 return Boolean.valueOf(value);
112 return Byte.valueOf(value);
115 return Integer.valueOf(value);
118 return Long.valueOf(value);
121 return QNameFactory.create(value);
123 case BIG_INTEGER_TYPE: {
124 return new BigInteger(value);
126 case BIG_DECIMAL_TYPE: {
127 return new BigDecimal(value);