1 package org.opendaylight.controller.sal.restconf.impl;
5 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
6 import org.opendaylight.controller.sal.rest.impl.RestUtil;
7 import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO.IdentityValue;
8 import org.opendaylight.yangtools.concepts.Codec;
9 import org.opendaylight.yangtools.yang.common.QName;
10 import org.opendaylight.yangtools.yang.data.api.codec.IdentityrefCodec;
11 import org.opendaylight.yangtools.yang.data.api.codec.LeafrefCodec;
12 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
13 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
14 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
15 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 public class RestCodec {
24 public static final Codec<Object, Object> from(TypeDefinition<?> typeDefinition, MountInstance mountPoint) {
25 return new ObjectCodec(typeDefinition, mountPoint);
28 @SuppressWarnings("rawtypes")
29 public static final class ObjectCodec implements Codec<Object, Object> {
31 private final Logger logger = LoggerFactory.getLogger(RestCodec.class);
33 public static final Codec LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl();
34 private final Codec identityrefCodec;
36 private final TypeDefinition<?> type;
38 private ObjectCodec(TypeDefinition<?> typeDefinition, MountInstance mountPoint) {
39 type = RestUtil.resolveBaseTypeFrom(typeDefinition);
40 if (type instanceof IdentityrefTypeDefinition) {
41 identityrefCodec = new IdentityrefCodecImpl(mountPoint);
43 identityrefCodec = null;
47 @SuppressWarnings("unchecked")
49 public Object deserialize(Object input) {
51 if (type instanceof IdentityrefTypeDefinition) {
52 return identityrefCodec.deserialize(input);
53 } else if (type instanceof LeafrefTypeDefinition) {
54 return LEAFREF_DEFAULT_CODEC.deserialize(input);
56 TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec = TypeDefinitionAwareCodec
58 if (typeAwarecodec != null) {
59 return typeAwarecodec.deserialize(String.valueOf(input));
61 logger.debug("Codec for type \"" + type.getQName().getLocalName()
62 + "\" is not implemented yet.");
66 } catch (ClassCastException e) { // TODO remove this catch when
67 // everyone use codecs
69 "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input),
75 @SuppressWarnings("unchecked")
77 public Object serialize(Object input) {
79 if (type instanceof IdentityrefTypeDefinition) {
80 return identityrefCodec.serialize(input);
81 } else if (type instanceof LeafrefTypeDefinition) {
82 return LEAFREF_DEFAULT_CODEC.serialize(input);
84 TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec = TypeDefinitionAwareCodec
86 if (typeAwarecodec != null) {
87 return typeAwarecodec.serialize(input);
89 logger.debug("Codec for type \"" + type.getQName().getLocalName()
90 + "\" is not implemented yet.");
94 } catch (ClassCastException e) { // TODO remove this catch when
95 // everyone use codecs
97 "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input),
105 public static class IdentityrefCodecImpl implements IdentityrefCodec<IdentityValuesDTO> {
107 private final MountInstance mountPoint;
109 public IdentityrefCodecImpl(MountInstance mountPoint) {
110 this.mountPoint = mountPoint;
114 public IdentityValuesDTO serialize(QName data) {
115 return new IdentityValuesDTO(data.getNamespace().toString(), data.getLocalName(), data.getPrefix());
119 public QName deserialize(IdentityValuesDTO data) {
120 IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
121 String namespace = valueWithNamespace.getNamespace();
123 if (mountPoint != null) {
124 validNamespace = ControllerContext.getInstance().findNamespaceByModuleName(mountPoint, namespace);
126 validNamespace = ControllerContext.getInstance().findNamespaceByModuleName(namespace);
128 if (validNamespace == null) {
129 validNamespace = URI.create(namespace);
131 return QName.create(validNamespace, null, valueWithNamespace.getValue());
136 public static class LeafrefCodecImpl implements LeafrefCodec<String> {
139 public String serialize(Object data) {
140 return String.valueOf(data);
144 public Object deserialize(String data) {