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 private static final String NULL_VALUE = "";
21 public static void serialize(NormalizedNodeMessages.Node.Builder builder,
22 QNameSerializationContext context, Object value) {
23 builder.setIntValueType(ValueType.getSerializableType(value).ordinal());
25 if (value instanceof YangInstanceIdentifier) {
26 builder.setInstanceIdentifierValue(
27 InstanceIdentifierUtils.toSerializable((YangInstanceIdentifier) value, context));
28 } else if (value instanceof Set) {
29 Set<?> set = (Set<?>) value;
31 for (Object o : set) {
32 if (o instanceof String) {
33 builder.addBitsValue(o.toString());
35 throw new IllegalArgumentException("Expected value type to be Bits but was : "
40 } else if (value instanceof byte[]) {
41 builder.setBytesValue(ByteString.copyFrom((byte[]) value));
42 } else if (value == null) {
43 builder.setValue(NULL_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());
54 if (value instanceof YangInstanceIdentifier) {
55 builder.setInstanceIdentifierValue(
56 InstanceIdentifierUtils.toSerializable((YangInstanceIdentifier) value, context));
57 } else if (value instanceof Set) {
58 Set<?> set = (Set<?>) value;
60 for (Object o : set) {
61 if (o instanceof String) {
62 builder.addBitsValue(o.toString());
64 throw new IllegalArgumentException("Expected value type to be Bits but was : "
69 } else if (value instanceof byte[]) {
70 builder.setBytesValue(ByteString.copyFrom((byte[]) value));
71 } else if (value == null) {
72 builder.setValue(NULL_VALUE);
74 builder.setValue(value.toString());
78 public static Object deSerialize(QNameDeSerializationContext context,
79 NormalizedNodeMessages.Node node) {
80 if (node.getIntValueType() == ValueType.YANG_IDENTIFIER_TYPE.ordinal()) {
81 return InstanceIdentifierUtils.fromSerializable(node.getInstanceIdentifierValue(), context);
82 } else if (node.getIntValueType() == ValueType.BITS_TYPE.ordinal()) {
83 return new HashSet<>(node.getBitsValueList());
84 } else if (node.getIntValueType() == ValueType.BINARY_TYPE.ordinal()) {
85 return node.getBytesValue().toByteArray();
87 return deSerializeBasicTypes(node.getIntValueType(), node.getValue());
90 public static Object deSerialize(QNameDeSerializationContext context,
91 NormalizedNodeMessages.PathArgumentAttribute attribute) {
93 if (attribute.getType() == ValueType.YANG_IDENTIFIER_TYPE.ordinal()) {
94 return InstanceIdentifierUtils.fromSerializable(attribute.getInstanceIdentifierValue(), context);
95 } else if (attribute.getType() == ValueType.BITS_TYPE.ordinal()) {
96 return new HashSet<>(attribute.getBitsValueList());
97 } else if (attribute.getType() == ValueType.BINARY_TYPE.ordinal()) {
98 return attribute.getBytesValue().toByteArray();
100 return deSerializeBasicTypes(attribute.getType(), attribute.getValue());
104 private static Object deSerializeBasicTypes(int valueType, String value) {
105 return ValueType.values()[valueType].deserialize(value);