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 org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory;
12 import org.opendaylight.controller.cluster.datastore.util.InstanceIdentifierUtils;
13 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import java.math.BigDecimal;
17 import java.math.BigInteger;
18 import java.util.HashSet;
21 public class ValueSerializer {
22 public static void serialize(NormalizedNodeMessages.Node.Builder builder,
23 NormalizedNodeSerializationContext context, Object value){
24 builder.setIntValueType(ValueType.getSerializableType(value).ordinal());
26 if(value instanceof YangInstanceIdentifier) {
27 builder.setInstanceIdentifierValue(
28 InstanceIdentifierUtils.toSerializable((YangInstanceIdentifier) value));
29 } else if(value instanceof Set) {
30 Set set = (Set) value;
33 if(o instanceof String){
34 builder.addBitsValue(o.toString());
36 throw new IllegalArgumentException("Expected value type to be Bits but was : " +
42 builder.setValue(value.toString());
46 public static void serialize(NormalizedNodeMessages.PathArgumentAttribute.Builder builder,
47 NormalizedNodeSerializationContext context, Object value){
49 builder.setType(ValueType.getSerializableType(value).ordinal());
50 builder.setValue(value.toString());
53 public static Object deSerialize(
54 NormalizedNodeDeSerializationContext context, NormalizedNodeMessages.Node node) {
55 if(node.getIntValueType() == ValueType.YANG_IDENTIFIER_TYPE.ordinal()){
56 return InstanceIdentifierUtils.fromSerializable(
57 node.getInstanceIdentifierValue());
58 } else if(node.getIntValueType() == ValueType.BITS_TYPE.ordinal()){
59 return new HashSet(node.getBitsValueList());
61 return deSerializeBasicTypes(node.getIntValueType(), node.getValue());
64 public static Object deSerialize(
65 NormalizedNodeDeSerializationContext context,
66 NormalizedNodeMessages.PathArgumentAttribute attribute) {
67 return deSerializeBasicTypes(attribute.getType(), attribute.getValue());
71 private static Object deSerializeBasicTypes(int valueType, String value) {
72 switch(ValueType.values()[valueType]){
74 return Short.valueOf(value);
77 return Boolean.valueOf(value);
80 return Byte.valueOf(value);
83 return Integer.valueOf(value);
86 return Long.valueOf(value);
89 return QNameFactory.create(value);
91 case BIG_INTEGER_TYPE: {
92 return new BigInteger(value);
94 case BIG_DECIMAL_TYPE: {
95 return new BigDecimal(value);