Merge "Simplify method isMutualExclusive in Subnet. Remove redundant 'if' statements."
[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.from(type);
51                     if (typeAwarecodec != null) {
52                         return typeAwarecodec.deserialize(String.valueOf(input));
53                     } else {
54                         logger.debug("Codec for type \"" + type.getQName().getLocalName() + "\" is not implemented yet.");
55                         return null;
56                     }
57                 }
58             } catch (ClassCastException e) { // TODO remove this catch when everyone use codecs
59                 logger.error("ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input), e);
60                 return input;
61             }
62         }
63
64         @SuppressWarnings("unchecked")
65         @Override
66         public Object serialize(Object input) {
67             try {
68                 if (type instanceof IdentityrefTypeDefinition) {
69                     return IDENTITYREF_DEFAULT_CODEC.serialize(input);
70                 } else if (type instanceof LeafrefTypeDefinition) {
71                     return LEAFREF_DEFAULT_CODEC.serialize(input);
72                 } else {
73                     TypeDefinitionAwareCodec<Object,? extends TypeDefinition<?>> typeAwarecodec = TypeDefinitionAwareCodec.from(type);
74                     if (typeAwarecodec != null) {
75                         return typeAwarecodec.serialize(input);
76                     } else {
77                         logger.debug("Codec for type \"" + type.getQName().getLocalName() + "\" is not implemented yet.");
78                         return null;
79                     }
80                 }
81             } catch (ClassCastException e) { // TODO remove this catch when everyone use codecs
82                 logger.error("ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input), e);
83                 return input;
84             }
85         }
86         
87     }
88     
89     public static class IdentityrefCodecImpl implements IdentityrefCodec<IdentityValuesDTO> {
90
91         @Override
92         public IdentityValuesDTO serialize(QName data) {
93             return new IdentityValuesDTO(data.getNamespace().toString(), data.getLocalName(), data.getPrefix());
94         }
95
96         @Override
97         public QName deserialize(IdentityValuesDTO data) {
98             IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
99             String namespace = valueWithNamespace.getNamespace();
100             URI validNamespace = ControllerContext.getInstance().findNamespaceByModule(namespace);
101             if (validNamespace == null) {
102                 validNamespace = URI.create(namespace);
103             }
104             return QName.create(validNamespace, null, valueWithNamespace.getValue());
105         }
106
107     }
108     
109     public static class LeafrefCodecImpl implements LeafrefCodec<String> {
110
111         @Override
112         public String serialize(Object data) {
113             return String.valueOf(data);
114         }
115
116         @Override
117         public Object deserialize(String data) {
118             return data;
119         }
120         
121     }
122     
123 }