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.common.base.Preconditions;
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 NormalizedNodeSerializationContext context, Object value){
25 builder.setIntValueType(ValueType.getSerializableType(value).ordinal());
27 if(value instanceof YangInstanceIdentifier) {
28 builder.setInstanceIdentifierValue(
29 InstanceIdentifierUtils.toSerializable((YangInstanceIdentifier) value));
30 } else if(value instanceof Set) {
31 Set set = (Set) value;
34 if(o instanceof String){
35 builder.addBitsValue(o.toString());
37 throw new IllegalArgumentException("Expected value type to be Bits but was : " +
43 builder.setValue(value.toString());
47 public static void serialize(NormalizedNodeMessages.PathArgumentAttribute.Builder builder,
48 NormalizedNodeSerializationContext context, Object value){
50 builder.setType(ValueType.getSerializableType(value).ordinal());
51 builder.setValue(value.toString());
54 public static Object deSerialize(
55 NormalizedNodeDeSerializationContext context, NormalizedNodeMessages.Node node) {
56 if(node.getIntValueType() == ValueType.YANG_IDENTIFIER_TYPE.ordinal()){
57 return InstanceIdentifierUtils.fromSerializable(
58 node.getInstanceIdentifierValue());
59 } else if(node.getIntValueType() == ValueType.BITS_TYPE.ordinal()){
60 return new HashSet(node.getBitsValueList());
62 return deSerializeBasicTypes(node.getIntValueType(), node.getValue());
65 public static Object deSerialize(
66 NormalizedNodeDeSerializationContext context,
67 NormalizedNodeMessages.PathArgumentAttribute attribute) {
68 return deSerializeBasicTypes(attribute.getType(), attribute.getValue());
72 private static Object deSerializeBasicTypes(int valueType, String value) {
73 Preconditions.checkArgument(valueType >= 0 && valueType < ValueType.values().length,
74 "Illegal value type " + valueType );
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);