package org.opendaylight.yangtools.yang.data.impl.codec; import static org.opendaylight.yangtools.yang.model.util.BaseTypes.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Set; import javax.xml.bind.DatatypeConverter; import org.opendaylight.yangtools.yang.data.api.codec.*; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.*; import com.google.common.base.*; import com.google.common.collect.ImmutableSet; import com.google.common.io.BaseEncoding; public abstract class TypeDefinitionAwareCodec> implements DataStringCodec { private final Optional typeDefinition; private final Class inputClass; public static final BinaryCodecStringImpl BINARY_DEFAULT_CODEC = new BinaryCodecStringImpl( Optional. absent()); public static final BitsCodecStringImpl BITS_DEFAULT_CODEC = new BitsCodecStringImpl( Optional. absent()); public static final BooleanCodecStringImpl BOOLEAN_DEFAULT_CODEC = new BooleanCodecStringImpl( Optional. absent()); public static final DecimalCodecStringImpl DECIMAL64_DEFAULT_CODEC = new DecimalCodecStringImpl( Optional. absent()); public static final EmptyCodecStringImpl EMPTY_DEFAULT_CODEC = new EmptyCodecStringImpl( Optional. absent()); public static final EnumCodecStringImpl ENUMERATION_DEFAULT_CODEC = new EnumCodecStringImpl( Optional. absent()); public static final Int8CodecStringImpl INT8_DEFAULT_CODEC = new Int8CodecStringImpl( Optional. absent()); public static final Int16CodecStringImpl INT16_DEFAULT_CODEC = new Int16CodecStringImpl( Optional. absent()); public static final Int32CodecStringImpl INT32_DEFAULT_CODEC = new Int32CodecStringImpl( Optional. absent()); public static final Int64CodecStringImpl INT64_DEFAULT_CODEC = new Int64CodecStringImpl( Optional. absent()); public static final StringCodecStringImpl STRING_DEFAULT_CODEC = new StringCodecStringImpl( Optional. absent()); public static final Uint8CodecStringImpl UINT8_DEFAULT_CODEC = new Uint8CodecStringImpl( Optional. absent()); public static final Uint16CodecStringImpl UINT16_DEFAULT_CODEC = new Uint16CodecStringImpl( Optional. absent()); public static final Uint32CodecStringImpl UINT32_DEFAULT_CODEC = new Uint32CodecStringImpl( Optional. absent()); public static final Uint64CodecStringImpl UINT64_DEFAULT_CODEC = new Uint64CodecStringImpl( Optional. absent()); public Class getInputClass() { return inputClass; } protected TypeDefinitionAwareCodec(Optional typeDefinition, Class outputClass) { Preconditions.checkArgument(outputClass != null, "Output class must be specified."); this.typeDefinition = typeDefinition; this.inputClass = outputClass; } public Optional getTypeDefinition() { return typeDefinition; } @SuppressWarnings({ "rawtypes", "unchecked" }) public static final TypeDefinitionAwareCodec> from(TypeDefinition typeDefinition) { final TypeDefinitionAwareCodec codec = fromType(typeDefinition); return (TypeDefinitionAwareCodec>) codec; } public static final > TypeDefinitionAwareCodec fromType(T typeDefinition) { T superType = typeDefinition; while (superType.getBaseType() != null) { superType = superType.getBaseType(); } @SuppressWarnings("rawtypes") TypeDefinitionAwareCodec codec = null; if (superType instanceof BinaryTypeDefinition) { codec = BINARY_DEFAULT_CODEC; } else if (superType instanceof BitsTypeDefinition) { codec = BITS_DEFAULT_CODEC; } else if (superType instanceof BooleanTypeDefinition) { codec = BOOLEAN_DEFAULT_CODEC; } else if (superType instanceof DecimalTypeDefinition) { codec = DECIMAL64_DEFAULT_CODEC; } else if (superType instanceof EmptyTypeDefinition) { codec = EMPTY_DEFAULT_CODEC; } else if (superType instanceof EnumTypeDefinition) { codec = ENUMERATION_DEFAULT_CODEC; } else if (superType instanceof IntegerTypeDefinition) { if (INT8_QNAME.equals(superType.getQName())) { codec = INT8_DEFAULT_CODEC; } else if (INT16_QNAME.equals(superType.getQName())) { codec = INT16_DEFAULT_CODEC; } else if (INT32_QNAME.equals(superType.getQName())) { codec = INT32_DEFAULT_CODEC; } else if (INT64_QNAME.equals(superType.getQName())) { codec = INT64_DEFAULT_CODEC; } } else if (superType instanceof StringTypeDefinition) { codec = STRING_DEFAULT_CODEC; } else if (superType instanceof UnsignedIntegerTypeDefinition) { if (UINT8_QNAME.equals(superType.getQName())) { codec = UINT8_DEFAULT_CODEC; } if (UINT16_QNAME.equals(superType.getQName())) { codec = UINT16_DEFAULT_CODEC; } if (UINT32_QNAME.equals(superType.getQName())) { codec = UINT32_DEFAULT_CODEC; } if (UINT64_QNAME.equals(superType.getQName())) { codec = UINT64_DEFAULT_CODEC; } } return (TypeDefinitionAwareCodec) codec; } public static class BooleanCodecStringImpl extends TypeDefinitionAwareCodec implements BooleanCodec { protected BooleanCodecStringImpl(Optional typeDef) { super(typeDef, Boolean.class); } @Override public String serialize(Boolean data) { return data.toString(); } @Override public Boolean deserialize(String stringRepresentation) { return Boolean.parseBoolean(stringRepresentation); } }; public static class Uint8CodecStringImpl extends TypeDefinitionAwareCodec implements Uint8Codec { protected Uint8CodecStringImpl(Optional typeDef) { super(typeDef, Short.class); } @Override public String serialize(Short data) { return data.toString(); } @Override public Short deserialize(String stringRepresentation) { return Short.parseShort(stringRepresentation); } }; public static class Uint16CodecStringImpl extends TypeDefinitionAwareCodec implements Uint16Codec { protected Uint16CodecStringImpl(Optional typeDef) { super(typeDef, Integer.class); } @Override public Integer deserialize(String stringRepresentation) { return Integer.parseInt(stringRepresentation); } @Override public String serialize(Integer data) { return data.toString(); } }; public static class Uint32CodecStringImpl extends TypeDefinitionAwareCodec implements Uint32Codec { protected Uint32CodecStringImpl(Optional typeDef) { super(typeDef, Long.class); } @Override public Long deserialize(String stringRepresentation) { return Long.parseLong(stringRepresentation); } @Override public String serialize(Long data) { return data.toString(); } }; public static class Uint64CodecStringImpl extends TypeDefinitionAwareCodec implements Uint64Codec { protected Uint64CodecStringImpl(Optional typeDef) { super(typeDef, BigInteger.class); } @Override public BigInteger deserialize(String stringRepresentation) { // FIXME: Implement codec correctly return BigInteger.valueOf(Long.valueOf(stringRepresentation)); } @Override public String serialize(BigInteger data) { return data.toString(); } }; public static class StringCodecStringImpl extends TypeDefinitionAwareCodec implements StringCodec { protected StringCodecStringImpl(Optional typeDef) { super(typeDef, String.class); } @Override public String deserialize(String stringRepresentation) { return stringRepresentation; } @Override public String serialize(String data) { return data.toString(); } }; public static class Int16CodecStringImpl extends TypeDefinitionAwareCodec implements Int16Codec { protected Int16CodecStringImpl(Optional typeDef) { super(typeDef, Short.class); } @Override public Short deserialize(String stringRepresentation) { return Short.valueOf(stringRepresentation); } @Override public String serialize(Short data) { return data.toString(); } }; public static class Int32CodecStringImpl extends TypeDefinitionAwareCodec implements Int32Codec { protected Int32CodecStringImpl(Optional typeDef) { super(typeDef, Integer.class); } @Override public Integer deserialize(String stringRepresentation) { return Integer.valueOf(stringRepresentation); } @Override public String serialize(Integer data) { return data.toString(); } }; public static class Int64CodecStringImpl extends TypeDefinitionAwareCodec implements Int64Codec { protected Int64CodecStringImpl(Optional typeDef) { super(typeDef, Long.class); } @Override public Long deserialize(String stringRepresentation) { return Long.parseLong(stringRepresentation); } @Override public String serialize(Long data) { return data.toString(); } }; public static class Int8CodecStringImpl extends TypeDefinitionAwareCodec implements Int8Codec { protected Int8CodecStringImpl(Optional typeDef) { super(typeDef, Byte.class); } @Override public Byte deserialize(String stringRepresentation) { return Byte.parseByte(stringRepresentation); } @Override public String serialize(Byte data) { return data.toString(); } }; public static class EmptyCodecStringImpl extends TypeDefinitionAwareCodec implements EmptyCodec { protected EmptyCodecStringImpl(Optional typeDef) { super(typeDef, Void.class); } @Override public String serialize(Void data) { return ""; } @Override public Void deserialize(String stringRepresentation) { return null; } }; public static final class BinaryCodecStringImpl extends TypeDefinitionAwareCodec implements BinaryCodec { protected BinaryCodecStringImpl(Optional typeDef) { super(typeDef, byte[].class); } @Override public String serialize(byte[] data) { return BaseEncoding.base64().encode(data); } @Override public byte[] deserialize(String stringRepresentation) { return BaseEncoding.base64().decode(stringRepresentation); } }; public static final class BitsCodecStringImpl extends TypeDefinitionAwareCodec, BitsTypeDefinition> implements BitsCodec { @SuppressWarnings("unchecked") protected BitsCodecStringImpl(Optional typeDef) { super(typeDef, (Class>) ((Class) Set.class)); } @Override public String serialize(Set data) { return Joiner.on(" ").join(data).toString(); } @Override public Set deserialize(String stringRepresentation) { String[] strings = stringRepresentation.split(" "); return ImmutableSet.copyOf(strings); } }; public static class EnumCodecStringImpl extends TypeDefinitionAwareCodec implements EnumCodec { protected EnumCodecStringImpl(Optional typeDef) { super(typeDef, String.class); } @Override public String deserialize(String stringRepresentation) { return stringRepresentation; } @Override public String serialize(String data) { return data.toString(); } }; public static class DecimalCodecStringImpl extends TypeDefinitionAwareCodec implements DecimalCodec { protected DecimalCodecStringImpl(Optional typeDef) { super(typeDef, BigDecimal.class); } @Override public String serialize(BigDecimal data) { return data.toString(); } @Override public BigDecimal deserialize(String stringRepresentation) { return new BigDecimal(stringRepresentation); } }; }