Added tests for yang.model.util
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / helpers / ObjectCodec.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.codec.gson.helpers;
9
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;
18
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;
26
27     ObjectCodec(final SchemaContextUtils schema, final TypeDefinition<?> typeDefinition) {
28         super(schema);
29         type = RestUtil.resolveBaseTypeFrom(typeDefinition);
30         if (type instanceof IdentityrefTypeDefinition) {
31             identityrefCodec = new IdentityrefCodecImpl(schema);
32         } else {
33             identityrefCodec = null;
34         }
35         if (type instanceof InstanceIdentifierTypeDefinition) {
36             instanceIdentifier = new InstanceIdentifierCodecImpl(schema);
37         } else {
38             instanceIdentifier = null;
39         }
40     }
41
42     @SuppressWarnings("unchecked")
43     @Override
44     public Object deserialize(final Object input) {
45         try {
46             if (type instanceof IdentityrefTypeDefinition) {
47                 if (input instanceof IdentityValuesDTO) {
48                     return identityrefCodec.deserialize(input);
49                 }
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));
52                 return null;
53             } else if (type instanceof LeafrefTypeDefinition) {
54                 if (input instanceof IdentityValuesDTO) {
55                     return LEAFREF_DEFAULT_CODEC.deserialize(((IdentityValuesDTO) input).getOriginValue());
56                 }
57                 return LEAFREF_DEFAULT_CODEC.deserialize(input);
58             } else if (type instanceof InstanceIdentifierTypeDefinition) {
59                 if (input instanceof IdentityValuesDTO) {
60                     return instanceIdentifier.deserialize(input);
61                 }
62                 LOG.info(
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));
65                 return null;
66             } else {
67                 TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec = TypeDefinitionAwareCodec
68                         .from(type);
69                 if (typeAwarecodec != null) {
70                     if (input instanceof IdentityValuesDTO) {
71                         return typeAwarecodec.deserialize(((IdentityValuesDTO) input).getOriginValue());
72                     }
73                     return typeAwarecodec.deserialize(String.valueOf(input));
74                 } else {
75                     LOG.debug("Codec for type \"" + type.getQName().getLocalName()
76                             + "\" is not implemented yet.");
77                     return null;
78                 }
79             }
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),
83                     e);
84             return null;
85         }
86     }
87
88     @SuppressWarnings("unchecked")
89     @Override
90     public Object serialize(final Object input) {
91         try {
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);
98             } else {
99                 TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec = TypeDefinitionAwareCodec
100                         .from(type);
101                 if (typeAwarecodec != null) {
102                     return typeAwarecodec.serialize(input);
103                 } else {
104                     LOG.debug("Codec for type \"" + type.getQName().getLocalName()
105                             + "\" is not implemented yet.");
106                     return null;
107                 }
108             }
109         } catch (ClassCastException e) { // TODO remove this catch when everyone use codecs
110             LOG.error(
111                     "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input),
112                     e);
113             return input;
114         }
115     }
116
117 }