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
8 package org.opendaylight.yangtools.yang.data.codec.gson.helpers;
10 import org.opendaylight.yangtools.concepts.Codec;
11 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
12 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
13 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
14 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
15 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 @SuppressWarnings("rawtypes")
20 final class ObjectCodec extends AbstractCodecImpl implements Codec<Object, Object> {
21 public static final Codec LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl();
22 private static final Logger LOG = LoggerFactory.getLogger(RestCodecFactory.class);
23 private final Codec instanceIdentifier;
24 private final Codec identityrefCodec;
25 private final TypeDefinition<?> type;
27 ObjectCodec(final SchemaContextUtils schema, final TypeDefinition<?> typeDefinition) {
29 type = RestUtil.resolveBaseTypeFrom(typeDefinition);
30 if (type instanceof IdentityrefTypeDefinition) {
31 identityrefCodec = new IdentityrefCodecImpl(schema);
33 identityrefCodec = null;
35 if (type instanceof InstanceIdentifierTypeDefinition) {
36 instanceIdentifier = new InstanceIdentifierCodecImpl(schema);
38 instanceIdentifier = null;
42 @SuppressWarnings("unchecked")
44 public Object deserialize(final Object input) {
46 if (type instanceof IdentityrefTypeDefinition) {
47 if (input instanceof IdentityValuesDTO) {
48 return identityrefCodec.deserialize(input);
50 LOG.debug("Value is not instance of IdentityrefTypeDefinition but is {}. Therefore NULL is used as translation of - {}",
51 input == null ? "null" : input.getClass(), String.valueOf(input));
53 } else if (type instanceof LeafrefTypeDefinition) {
54 if (input instanceof IdentityValuesDTO) {
55 return LEAFREF_DEFAULT_CODEC.deserialize(((IdentityValuesDTO) input).getOriginValue());
57 return LEAFREF_DEFAULT_CODEC.deserialize(input);
58 } else if (type instanceof InstanceIdentifierTypeDefinition) {
59 if (input instanceof IdentityValuesDTO) {
60 return instanceIdentifier.deserialize(input);
63 "Value is not instance of InstanceIdentifierTypeDefinition but is {}. Therefore NULL is used as translation of - {}",
64 input == null ? "null" : input.getClass(), String.valueOf(input));
67 TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec = TypeDefinitionAwareCodec
69 if (typeAwarecodec != null) {
70 if (input instanceof IdentityValuesDTO) {
71 return typeAwarecodec.deserialize(((IdentityValuesDTO) input).getOriginValue());
73 return typeAwarecodec.deserialize(String.valueOf(input));
75 LOG.debug("Codec for type \"" + type.getQName().getLocalName()
76 + "\" is not implemented yet.");
80 } catch (ClassCastException e) {
81 // TODO remove this catch when everyone use codecs
82 LOG.error("ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input),
88 @SuppressWarnings("unchecked")
90 public Object serialize(final Object input) {
92 if (type instanceof IdentityrefTypeDefinition) {
93 return identityrefCodec.serialize(input);
94 } else if (type instanceof LeafrefTypeDefinition) {
95 return LEAFREF_DEFAULT_CODEC.serialize(input);
96 } else if (type instanceof InstanceIdentifierTypeDefinition) {
97 return instanceIdentifier.serialize(input);
99 TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec = TypeDefinitionAwareCodec
101 if (typeAwarecodec != null) {
102 return typeAwarecodec.serialize(input);
104 LOG.debug("Codec for type \"" + type.getQName().getLocalName()
105 + "\" is not implemented yet.");
109 } catch (ClassCastException e) { // TODO remove this catch when everyone use codecs
111 "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input),