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.util.HashSet;
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;
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());
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;
29 for (Object o : set) {
30 if (o instanceof String) {
31 builder.addBitsValue(o.toString());
33 throw new IllegalArgumentException("Expected value type to be Bits but was : " +
38 } else if(value instanceof byte[]){
39 builder.setBytesValue(ByteString.copyFrom((byte[]) value));
41 builder.setValue(value.toString());
45 public static void serialize(NormalizedNodeMessages.PathArgumentAttribute.Builder builder,
46 QNameSerializationContext context, Object value){
48 builder.setType(ValueType.getSerializableType(value).ordinal());
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;
56 for (Object o : set) {
57 if (o instanceof String) {
58 builder.addBitsValue(o.toString());
60 throw new IllegalArgumentException("Expected value type to be Bits but was : " +
65 } else if(value instanceof byte[]){
66 builder.setBytesValue(ByteString.copyFrom((byte[]) value));
68 builder.setValue(value.toString());
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();
82 return deSerializeBasicTypes(node.getIntValueType(), node.getValue());
85 public static Object deSerialize(QNameDeSerializationContext context,
86 NormalizedNodeMessages.PathArgumentAttribute attribute) {
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();
96 return deSerializeBasicTypes(attribute.getType(), attribute.getValue());
100 private static Object deSerializeBasicTypes(int valueType, String value) {
101 return ValueType.values()[valueType].deserialize(value);