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
52 if (typeAwarecodec != null) {
53 return typeAwarecodec.deserialize(String.valueOf(input));
55 logger.debug("Codec for type \"" + type.getQName().getLocalName()
56 + "\" is not implemented yet.");
60 } catch (ClassCastException e) { // TODO remove this catch when
61 // everyone use codecs
63 "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input),
69 @SuppressWarnings("unchecked")
71 public Object serialize(Object input) {
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);
78 TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec = TypeDefinitionAwareCodec
80 if (typeAwarecodec != null) {
81 return typeAwarecodec.serialize(input);
83 logger.debug("Codec for type \"" + type.getQName().getLocalName()
84 + "\" is not implemented yet.");
88 } catch (ClassCastException e) { // TODO remove this catch when
89 // everyone use codecs
91 "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input),
99 public static class IdentityrefCodecImpl implements IdentityrefCodec<IdentityValuesDTO> {
102 public IdentityValuesDTO serialize(QName data) {
103 return new IdentityValuesDTO(data.getNamespace().toString(), data.getLocalName(), data.getPrefix());
107 public QName deserialize(IdentityValuesDTO data) {
108 IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
109 String namespace = valueWithNamespace.getNamespace();
110 URI validNamespace = ControllerContext.getInstance().findNamespaceByModuleName(namespace);
111 if (validNamespace == null) {
112 validNamespace = URI.create(namespace);
114 return QName.create(validNamespace, null, valueWithNamespace.getValue());
119 public static class LeafrefCodecImpl implements LeafrefCodec<String> {
122 public String serialize(Object data) {
123 return String.valueOf(data);
127 public Object deserialize(String data) {