Merge "Remove redundant expression true:false"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / RestCodec.java
1 package org.opendaylight.controller.sal.restconf.impl;
2
3 import java.net.URI;
4
5 import org.opendaylight.controller.sal.rest.impl.RestUtil;
6 import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO.IdentityValue;
7 import org.opendaylight.yangtools.concepts.Codec;
8 import org.opendaylight.yangtools.yang.common.QName;
9 import org.opendaylight.yangtools.yang.data.api.codec.IdentityrefCodec;
10 import org.opendaylight.yangtools.yang.data.api.codec.LeafrefCodec;
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.LeafrefTypeDefinition;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public class RestCodec {
19
20     private RestCodec() {
21     }
22
23     public static final Codec<Object, Object> from(TypeDefinition<?> typeDefinition) {
24         return new ObjectCodec(typeDefinition);
25     }
26
27     @SuppressWarnings("rawtypes")
28     public static final class ObjectCodec implements Codec<Object, Object> {
29
30         private final Logger logger = LoggerFactory.getLogger(RestCodec.class);
31
32         public static final Codec IDENTITYREF_DEFAULT_CODEC = new IdentityrefCodecImpl();
33         public static final Codec LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl();
34
35         private TypeDefinition<?> type;
36
37         private ObjectCodec(TypeDefinition<?> typeDefinition) {
38             type = RestUtil.resolveBaseTypeFrom(typeDefinition);
39         }
40
41         @SuppressWarnings("unchecked")
42         @Override
43         public Object deserialize(Object input) {
44             try {
45                 if (type instanceof IdentityrefTypeDefinition) {
46                     return IDENTITYREF_DEFAULT_CODEC.deserialize(input);
47                 } else if (type instanceof LeafrefTypeDefinition) {
48                     return LEAFREF_DEFAULT_CODEC.deserialize(input);
49                 } else {
50                     TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec = TypeDefinitionAwareCodec
51                             .from(type);
52                     if (typeAwarecodec != null) {
53                         return typeAwarecodec.deserialize(String.valueOf(input));
54                     } else {
55                         logger.debug("Codec for type \"" + type.getQName().getLocalName()
56                                 + "\" is not implemented yet.");
57                         return null;
58                     }
59                 }
60             } catch (ClassCastException e) { // TODO remove this catch when
61                                              // everyone use codecs
62                 logger.error(
63                         "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input),
64                         e);
65                 return input;
66             }
67         }
68
69         @SuppressWarnings("unchecked")
70         @Override
71         public Object serialize(Object input) {
72             try {
73                 if (type instanceof IdentityrefTypeDefinition) {
74                     return IDENTITYREF_DEFAULT_CODEC.serialize(input);
75                 } else if (type instanceof LeafrefTypeDefinition) {
76                     return LEAFREF_DEFAULT_CODEC.serialize(input);
77                 } else {
78                     TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec = TypeDefinitionAwareCodec
79                             .from(type);
80                     if (typeAwarecodec != null) {
81                         return typeAwarecodec.serialize(input);
82                     } else {
83                         logger.debug("Codec for type \"" + type.getQName().getLocalName()
84                                 + "\" is not implemented yet.");
85                         return null;
86                     }
87                 }
88             } catch (ClassCastException e) { // TODO remove this catch when
89                                              // everyone use codecs
90                 logger.error(
91                         "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input),
92                         e);
93                 return input;
94             }
95         }
96
97     }
98
99     public static class IdentityrefCodecImpl implements IdentityrefCodec<IdentityValuesDTO> {
100
101         @Override
102         public IdentityValuesDTO serialize(QName data) {
103             return new IdentityValuesDTO(data.getNamespace().toString(), data.getLocalName(), data.getPrefix());
104         }
105
106         @Override
107         public QName deserialize(IdentityValuesDTO data) {
108             IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
109             String namespace = valueWithNamespace.getNamespace();
110             URI validNamespace = ControllerContext.getInstance().findNamespaceByModule(namespace);
111             if (validNamespace == null) {
112                 validNamespace = URI.create(namespace);
113             }
114             return QName.create(validNamespace, null, valueWithNamespace.getValue());
115         }
116
117     }
118
119     public static class LeafrefCodecImpl implements LeafrefCodec<String> {
120
121         @Override
122         public String serialize(Object data) {
123             return String.valueOf(data);
124         }
125
126         @Override
127         public Object deserialize(String data) {
128             return data;
129         }
130
131     }
132
133 }