X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fnode%2Futils%2Fserialization%2FValueSerializer.java;h=71946b0a7abe476241e3f1775ffa8e110da1d559;hb=4ed6793fcad89fcb4a3c0f7ec230753cb7ed31a9;hp=04c95d61ceb20854a8f19308df74481998b4c776;hpb=f722659d5c1f6893105d1eb016a2859c08846717;p=controller.git diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/serialization/ValueSerializer.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/serialization/ValueSerializer.java index 04c95d61ce..71946b0a7a 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/serialization/ValueSerializer.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/serialization/ValueSerializer.java @@ -8,100 +8,97 @@ package org.opendaylight.controller.cluster.datastore.node.utils.serialization; -import com.google.common.base.Preconditions; -import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory; +import com.google.protobuf.ByteString; +import java.util.HashSet; +import java.util.Set; import org.opendaylight.controller.cluster.datastore.util.InstanceIdentifierUtils; import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.HashSet; -import java.util.Set; - public class ValueSerializer { public static void serialize(NormalizedNodeMessages.Node.Builder builder, - NormalizedNodeSerializationContext context, Object value){ + QNameSerializationContext context, Object value) { builder.setIntValueType(ValueType.getSerializableType(value).ordinal()); if(value instanceof YangInstanceIdentifier) { builder.setInstanceIdentifierValue( - InstanceIdentifierUtils.toSerializable((YangInstanceIdentifier) value)); + InstanceIdentifierUtils.toSerializable((YangInstanceIdentifier) value, context)); } else if(value instanceof Set) { - Set set = (Set) value; - if(!set.isEmpty()){ - for(Object o : set){ - if(o instanceof String){ + Set set = (Set) value; + if (!set.isEmpty()) { + for (Object o : set) { + if (o instanceof String) { builder.addBitsValue(o.toString()); } else { throw new IllegalArgumentException("Expected value type to be Bits but was : " + - value.toString()); + value.toString()); } } } + } else if(value instanceof byte[]){ + builder.setBytesValue(ByteString.copyFrom((byte[]) value)); } else { builder.setValue(value.toString()); } } public static void serialize(NormalizedNodeMessages.PathArgumentAttribute.Builder builder, - NormalizedNodeSerializationContext context, Object value){ + QNameSerializationContext context, Object value){ builder.setType(ValueType.getSerializableType(value).ordinal()); - builder.setValue(value.toString()); + + if(value instanceof YangInstanceIdentifier) { + builder.setInstanceIdentifierValue( + InstanceIdentifierUtils.toSerializable((YangInstanceIdentifier) value, context)); + } else if(value instanceof Set) { + Set set = (Set) value; + if (!set.isEmpty()) { + for (Object o : set) { + if (o instanceof String) { + builder.addBitsValue(o.toString()); + } else { + throw new IllegalArgumentException("Expected value type to be Bits but was : " + + value.toString()); + } + } + } + } else if(value instanceof byte[]){ + builder.setBytesValue(ByteString.copyFrom((byte[]) value)); + } else { + builder.setValue(value.toString()); + } } - public static Object deSerialize( - NormalizedNodeDeSerializationContext context, NormalizedNodeMessages.Node node) { + public static Object deSerialize(QNameDeSerializationContext context, + NormalizedNodeMessages.Node node) { if(node.getIntValueType() == ValueType.YANG_IDENTIFIER_TYPE.ordinal()){ return InstanceIdentifierUtils.fromSerializable( - node.getInstanceIdentifierValue()); + node.getInstanceIdentifierValue(), context); } else if(node.getIntValueType() == ValueType.BITS_TYPE.ordinal()){ - return new HashSet(node.getBitsValueList()); + return new HashSet<>(node.getBitsValueList()); + } else if(node.getIntValueType() == ValueType.BINARY_TYPE.ordinal()){ + return node.getBytesValue().toByteArray(); } return deSerializeBasicTypes(node.getIntValueType(), node.getValue()); } - public static Object deSerialize( - NormalizedNodeDeSerializationContext context, - NormalizedNodeMessages.PathArgumentAttribute attribute) { + public static Object deSerialize(QNameDeSerializationContext context, + NormalizedNodeMessages.PathArgumentAttribute attribute) { + + if(attribute.getType() == ValueType.YANG_IDENTIFIER_TYPE.ordinal()){ + return InstanceIdentifierUtils.fromSerializable( + attribute.getInstanceIdentifierValue(), context); + } else if(attribute.getType() == ValueType.BITS_TYPE.ordinal()){ + return new HashSet<>(attribute.getBitsValueList()); + } else if(attribute.getType() == ValueType.BINARY_TYPE.ordinal()){ + return attribute.getBytesValue().toByteArray(); + } return deSerializeBasicTypes(attribute.getType(), attribute.getValue()); } private static Object deSerializeBasicTypes(int valueType, String value) { - Preconditions.checkArgument(valueType >= 0 && valueType < ValueType.values().length, - "Illegal value type " + valueType ); - - switch(ValueType.values()[valueType]){ - case SHORT_TYPE: { - return Short.valueOf(value); - } - case BOOL_TYPE: { - return Boolean.valueOf(value); - } - case BYTE_TYPE: { - return Byte.valueOf(value); - } - case INT_TYPE : { - return Integer.valueOf(value); - } - case LONG_TYPE: { - return Long.valueOf(value); - } - case QNAME_TYPE: { - return QNameFactory.create(value); - } - case BIG_INTEGER_TYPE: { - return new BigInteger(value); - } - case BIG_DECIMAL_TYPE: { - return new BigDecimal(value); - } - default: { - return value; - } - } + return ValueType.values()[valueType].deserialize(value); } }