X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=yang%2Fyang-data-codec-gson%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fcodec%2Fgson%2FJSONCodecFactory.java;h=c5840cff986b359c5f4271a29c8a5fa6829e0eb6;hb=9e5540fb30986583d69e51f9290dbb651338f4b0;hp=8ee9517ec2db90f976aa64e5b26a883abe3a7c4e;hpb=9a6e147af64a81b7987a6e9d0dac6d9d228295e2;p=yangtools.git diff --git a/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONCodecFactory.java b/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONCodecFactory.java index 8ee9517ec2..c5840cff98 100644 --- a/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONCodecFactory.java +++ b/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONCodecFactory.java @@ -12,13 +12,18 @@ import com.google.common.base.Preconditions; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; - +import com.google.gson.stream.JsonWriter; +import java.io.IOException; import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; +import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; import org.opendaylight.yangtools.yang.model.util.IdentityrefType; import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType; +import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,7 +34,6 @@ import org.slf4j.LoggerFactory; @Beta public final class JSONCodecFactory { private static final Logger LOG = LoggerFactory.getLogger(JSONCodecFactory.class); - private static final JSONCodec LEAFREF_DEFAULT_CODEC = new JSONLeafrefCodec(); private static final JSONCodec NULL_CODEC = new JSONCodec() { @Override public Object deserialize(final String input) { @@ -45,40 +49,27 @@ public final class JSONCodecFactory { public boolean needQuotes() { return false; } - }; - private static TypeDefinition resolveBaseTypeFrom(final TypeDefinition type) { - TypeDefinition superType = type; - while (superType.getBaseType() != null) { - superType = superType.getBaseType(); + @Override + public void serializeToWriter(JsonWriter writer, Object value) throws IOException { + // NOOP since codec is unkwown. + LOG.warn("Call of the serializeToWriter method on JSONCodecFactory.NULL_CODEC object. No operation performed."); } - return superType; - } + }; - private final LoadingCache, JSONCodec> codecs = - CacheBuilder.newBuilder().softValues().build(new CacheLoader, JSONCodec>() { - @SuppressWarnings("unchecked") + private final LoadingCache> codecs = + CacheBuilder.newBuilder().softValues().build(new CacheLoader>() { @Override - public JSONCodec load(final TypeDefinition key) throws Exception { - final TypeDefinition type = resolveBaseTypeFrom(key); - - if (type instanceof InstanceIdentifierType) { - return (JSONCodec) iidCodec; - } - if (type instanceof IdentityrefType) { - return (JSONCodec) idrefCodec; + public JSONCodec load(final DataSchemaNode key) throws Exception { + final TypeDefinition type; + if (key instanceof LeafSchemaNode) { + type = ((LeafSchemaNode) key).getType(); + } else if (key instanceof LeafListSchemaNode) { + type = ((LeafListSchemaNode) key).getType(); + } else { + throw new IllegalArgumentException("Not supported node type " + key.getClass().getName()); } - if (type instanceof LeafrefTypeDefinition) { - return LEAFREF_DEFAULT_CODEC; - } - - final TypeDefinitionAwareCodec> codec = TypeDefinitionAwareCodec.from(type); - if (codec == null) { - LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName().getLocalName()); - return NULL_CODEC; - } - - return AbstractJSONCodec.create(codec); + return createCodec(key,type); } }); @@ -102,11 +93,55 @@ public final class JSONCodecFactory { return new JSONCodecFactory(context); } + private static TypeDefinition resolveBaseTypeFrom(final TypeDefinition type) { + TypeDefinition superType = type; + while (superType.getBaseType() != null) { + superType = superType.getBaseType(); + } + return superType; + } + + private JSONCodec createCodec(DataSchemaNode key, TypeDefinition type) { + TypeDefinition baseType = resolveBaseTypeFrom(type); + if (baseType instanceof LeafrefTypeDefinition) { + return createReferencedTypeCodec(key, (LeafrefTypeDefinition) baseType); + } + return createFromSimpleType(type); + } + + private JSONCodec createReferencedTypeCodec(DataSchemaNode schema, + LeafrefTypeDefinition type) { + // FIXME: Verify if this does indeed support leafref of leafref + TypeDefinition referencedType = + SchemaContextUtil.getBaseTypeForLeafRef(type, getSchemaContext(), schema); + return createFromSimpleType(referencedType); + } + + @SuppressWarnings("unchecked") + private JSONCodec createFromSimpleType(TypeDefinition type) { + final TypeDefinition baseType = resolveBaseTypeFrom(type); + if (baseType instanceof InstanceIdentifierType) { + return (JSONCodec) iidCodec; + } + if (baseType instanceof IdentityrefType) { + return (JSONCodec) idrefCodec; + } + + final TypeDefinitionAwareCodec codec = TypeDefinitionAwareCodec.from(type); + if (codec == null) { + LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName() + .getLocalName()); + return NULL_CODEC; + } + return (JSONCodec) AbstractJSONCodec.create(codec); + } + SchemaContext getSchemaContext() { return schemaContext; } - JSONCodec codecFor(final TypeDefinition typeDefinition) { - return codecs.getUnchecked(typeDefinition); + JSONCodec codecFor(DataSchemaNode schema) { + return codecs.getUnchecked(schema); } + }