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 org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory;
13 import org.opendaylight.controller.cluster.datastore.util.InstanceIdentifierUtils;
14 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import java.math.BigDecimal;
18 import java.math.BigInteger;
19 import java.util.HashSet;
22 public class ValueSerializer {
23 public static void serialize(NormalizedNodeMessages.Node.Builder builder,
24 QNameSerializationContext context, Object value) {
25 builder.setIntValueType(ValueType.getSerializableType(value).ordinal());
27 if(value instanceof YangInstanceIdentifier) {
28 builder.setInstanceIdentifierValue(
29 InstanceIdentifierUtils.toSerializable((YangInstanceIdentifier) value, context));
30 } else if(value instanceof Set) {
31 Set<?> set = (Set<?>) value;
33 for (Object o : set) {
34 if (o instanceof String) {
35 builder.addBitsValue(o.toString());
37 throw new IllegalArgumentException("Expected value type to be Bits but was : " +
42 } else if(value instanceof byte[]){
43 builder.setBytesValue(ByteString.copyFrom((byte[]) value));
45 builder.setValue(value.toString());
49 public static void serialize(NormalizedNodeMessages.PathArgumentAttribute.Builder builder,
50 QNameSerializationContext context, Object value){
52 builder.setType(ValueType.getSerializableType(value).ordinal());
53 builder.setValue(value.toString());
56 public static Object deSerialize(QNameDeSerializationContext context,
57 NormalizedNodeMessages.Node node) {
58 if(node.getIntValueType() == ValueType.YANG_IDENTIFIER_TYPE.ordinal()){
59 return InstanceIdentifierUtils.fromSerializable(
60 node.getInstanceIdentifierValue(), context);
61 } else if(node.getIntValueType() == ValueType.BITS_TYPE.ordinal()){
62 return new HashSet<>(node.getBitsValueList());
63 } else if(node.getIntValueType() == ValueType.BINARY_TYPE.ordinal()){
64 return node.getBytesValue().toByteArray();
66 return deSerializeBasicTypes(node.getIntValueType(), node.getValue());
69 public static Object deSerialize(QNameDeSerializationContext context,
70 NormalizedNodeMessages.PathArgumentAttribute attribute) {
71 return deSerializeBasicTypes(attribute.getType(), attribute.getValue());
75 private static Object deSerializeBasicTypes(int valueType, String value) {
76 switch(ValueType.values()[valueType]){
78 return Short.valueOf(value);
81 return Boolean.valueOf(value);
84 return Byte.valueOf(value);
87 return Integer.valueOf(value);
90 return Long.valueOf(value);
93 return QNameFactory.create(value);
95 case BIG_INTEGER_TYPE: {
96 return new BigInteger(value);
98 case BIG_DECIMAL_TYPE: {
99 return new BigDecimal(value);