1 package org.opendaylight.controller.sal.restconf.impl;
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;
18 public class RestCodec {
23 public static final Codec<Object, Object> from(TypeDefinition<?> typeDefinition) {
24 return new ObjectCodec(typeDefinition);
27 @SuppressWarnings("rawtypes")
28 public static final class ObjectCodec implements Codec<Object, Object> {
30 private final Logger logger = LoggerFactory.getLogger(RestCodec.class);
32 public static final Codec IDENTITYREF_DEFAULT_CODEC = new IdentityrefCodecImpl();
33 public static final Codec LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl();
35 private TypeDefinition<?> type;
37 private ObjectCodec(TypeDefinition<?> typeDefinition) {
38 type = RestUtil.resolveBaseTypeFrom(typeDefinition);
41 @SuppressWarnings("unchecked")
43 public Object deserialize(Object input) {
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);
50 TypeDefinitionAwareCodec<Object,? extends TypeDefinition<?>> typeAwarecodec = TypeDefinitionAwareCodec.from(type);
51 if (typeAwarecodec != null) {
52 return typeAwarecodec.deserialize(String.valueOf(input));
54 logger.debug("Codec for type \"" + type.getQName().getLocalName() + "\" is not implemented yet.");
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);
64 @SuppressWarnings("unchecked")
66 public Object serialize(Object input) {
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);
73 TypeDefinitionAwareCodec<Object,? extends TypeDefinition<?>> typeAwarecodec = TypeDefinitionAwareCodec.from(type);
74 if (typeAwarecodec != null) {
75 return typeAwarecodec.serialize(input);
77 logger.debug("Codec for type \"" + type.getQName().getLocalName() + "\" is not implemented yet.");
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);
89 public static class IdentityrefCodecImpl implements IdentityrefCodec<IdentityValuesDTO> {
92 public IdentityValuesDTO serialize(QName data) {
93 return new IdentityValuesDTO(data.getNamespace().toString(), data.getLocalName(), data.getPrefix());
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);
104 return QName.create(validNamespace, null, valueWithNamespace.getValue());
109 public static class LeafrefCodecImpl implements LeafrefCodec<String> {
112 public String serialize(Object data) {
113 return String.valueOf(data);
117 public Object deserialize(String data) {