X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2FRestCodec.java;h=40fba88356e89f924e6c2903e7460102a74d818c;hb=79eba117e59f10c8bff34d0dd6bbb67b8ccc3e10;hp=6452b72f1e972bd9013b986e4c3734172a69683f;hpb=d468a5db1e6fe2c3949d3f6227c0645c6777ecb5;p=controller.git diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestCodec.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestCodec.java index 6452b72f1e..40fba88356 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestCodec.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestCodec.java @@ -2,56 +2,100 @@ package org.opendaylight.controller.sal.restconf.impl; import java.net.URI; +import org.opendaylight.controller.sal.rest.impl.RestUtil; import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO.IdentityValue; import org.opendaylight.yangtools.concepts.Codec; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.codec.IdentityrefCodec; +import org.opendaylight.yangtools.yang.data.api.codec.LeafrefCodec; import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class RestCodec { - - @SuppressWarnings("rawtypes") - public static final Codec IDENTITYREF_DEFAULT_CODEC = new IdentityrefCodecImpl(); - + private RestCodec() { } - + public static final Codec from(TypeDefinition typeDefinition) { return new ObjectCodec(typeDefinition); } - + + @SuppressWarnings("rawtypes") public static final class ObjectCodec implements Codec { + private final Logger logger = LoggerFactory.getLogger(RestCodec.class); + + public static final Codec IDENTITYREF_DEFAULT_CODEC = new IdentityrefCodecImpl(); + public static final Codec LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl(); + private TypeDefinition type; - + private ObjectCodec(TypeDefinition typeDefinition) { - type = typeDefinition; + type = RestUtil.resolveBaseTypeFrom(typeDefinition); } - + @SuppressWarnings("unchecked") @Override public Object deserialize(Object input) { - if (type instanceof IdentityrefTypeDefinition) { - return IDENTITYREF_DEFAULT_CODEC.deserialize(input); - } else { - return TypeDefinitionAwareCodec.from(type).deserialize(String.valueOf(input)); + try { + if (type instanceof IdentityrefTypeDefinition) { + return IDENTITYREF_DEFAULT_CODEC.deserialize(input); + } else if (type instanceof LeafrefTypeDefinition) { + return LEAFREF_DEFAULT_CODEC.deserialize(input); + } else { + TypeDefinitionAwareCodec> typeAwarecodec = TypeDefinitionAwareCodec + .from(type); + if (typeAwarecodec != null) { + return typeAwarecodec.deserialize(String.valueOf(input)); + } else { + logger.debug("Codec for type \"" + type.getQName().getLocalName() + + "\" is not implemented yet."); + return null; + } + } + } catch (ClassCastException e) { // TODO remove this catch when + // everyone use codecs + logger.error( + "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input), + e); + return input; } } @SuppressWarnings("unchecked") @Override public Object serialize(Object input) { - if (type instanceof IdentityrefTypeDefinition) { - return IDENTITYREF_DEFAULT_CODEC.serialize(input); - } else { - return TypeDefinitionAwareCodec.from(type).serialize(input); + try { + if (type instanceof IdentityrefTypeDefinition) { + return IDENTITYREF_DEFAULT_CODEC.serialize(input); + } else if (type instanceof LeafrefTypeDefinition) { + return LEAFREF_DEFAULT_CODEC.serialize(input); + } else { + TypeDefinitionAwareCodec> typeAwarecodec = TypeDefinitionAwareCodec + .from(type); + if (typeAwarecodec != null) { + return typeAwarecodec.serialize(input); + } else { + logger.debug("Codec for type \"" + type.getQName().getLocalName() + + "\" is not implemented yet."); + return null; + } + } + } catch (ClassCastException e) { // TODO remove this catch when + // everyone use codecs + logger.error( + "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input), + e); + return input; } } - + } - + public static class IdentityrefCodecImpl implements IdentityrefCodec { @Override @@ -71,5 +115,19 @@ public class RestCodec { } } - + + public static class LeafrefCodecImpl implements LeafrefCodec { + + @Override + public String serialize(Object data) { + return String.valueOf(data); + } + + @Override + public Object deserialize(String data) { + return data; + } + + } + }