1 package org.opendaylight.controller.sal.restconf.impl;
5 import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO.IdentityValue;
6 import org.opendaylight.yangtools.concepts.Codec;
7 import org.opendaylight.yangtools.yang.common.QName;
8 import org.opendaylight.yangtools.yang.data.api.codec.IdentityrefCodec;
9 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
10 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
11 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
13 public class RestCodec {
15 @SuppressWarnings("rawtypes")
16 public static final Codec IDENTITYREF_DEFAULT_CODEC = new IdentityrefCodecImpl();
21 public static final Codec<Object, Object> from(TypeDefinition<?> typeDefinition) {
22 return new ObjectCodec(typeDefinition);
25 public static final class ObjectCodec implements Codec<Object, Object> {
27 private TypeDefinition<?> type;
29 private ObjectCodec(TypeDefinition<?> typeDefinition) {
30 type = typeDefinition;
33 @SuppressWarnings("unchecked")
35 public Object deserialize(Object input) {
36 if (type instanceof IdentityrefTypeDefinition) {
37 return IDENTITYREF_DEFAULT_CODEC.deserialize(input);
39 return TypeDefinitionAwareCodec.from(type).deserialize(String.valueOf(input));
43 @SuppressWarnings("unchecked")
45 public Object serialize(Object input) {
46 if (type instanceof IdentityrefTypeDefinition) {
47 return IDENTITYREF_DEFAULT_CODEC.serialize(input);
49 return TypeDefinitionAwareCodec.from(type).serialize(input);
55 public static class IdentityrefCodecImpl implements IdentityrefCodec<IdentityValuesDTO> {
58 public IdentityValuesDTO serialize(QName data) {
59 return new IdentityValuesDTO(data.getNamespace().toString(), data.getLocalName(), data.getPrefix());
63 public QName deserialize(IdentityValuesDTO data) {
64 IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
65 String namespace = valueWithNamespace.getNamespace();
66 URI validNamespace = ControllerContext.getInstance().findNamespaceByModule(namespace);
67 if (validNamespace == null) {
68 validNamespace = URI.create(namespace);
70 return QName.create(validNamespace, null, valueWithNamespace.getValue());