Fixed RESTConf support for identity-ref build-in datatype
[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.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;
12
13 public class RestCodec {
14     
15     @SuppressWarnings("rawtypes")
16     public static final Codec IDENTITYREF_DEFAULT_CODEC = new IdentityrefCodecImpl();
17     
18     private RestCodec() {
19     }
20     
21     public static final Codec<Object, Object> from(TypeDefinition<?> typeDefinition) {
22         return new ObjectCodec(typeDefinition);
23     }
24     
25     public static final class ObjectCodec implements Codec<Object, Object> {
26
27         private TypeDefinition<?> type;
28         
29         private ObjectCodec(TypeDefinition<?> typeDefinition) {
30             type = typeDefinition;
31         }
32         
33         @SuppressWarnings("unchecked")
34         @Override
35         public Object deserialize(Object input) {
36             if (type instanceof IdentityrefTypeDefinition) {
37                 return IDENTITYREF_DEFAULT_CODEC.deserialize(input);
38             } else {
39                 return TypeDefinitionAwareCodec.from(type).deserialize(String.valueOf(input));
40             }
41         }
42
43         @SuppressWarnings("unchecked")
44         @Override
45         public Object serialize(Object input) {
46             if (type instanceof IdentityrefTypeDefinition) {
47                 return IDENTITYREF_DEFAULT_CODEC.serialize(input);
48             } else {
49                 return TypeDefinitionAwareCodec.from(type).serialize(input);
50             }
51         }
52         
53     }
54     
55     public static class IdentityrefCodecImpl implements IdentityrefCodec<IdentityValuesDTO> {
56
57         @Override
58         public IdentityValuesDTO serialize(QName data) {
59             return new IdentityValuesDTO(data.getNamespace().toString(), data.getLocalName(), data.getPrefix());
60         }
61
62         @Override
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);
69             }
70             return QName.create(validNamespace, null, valueWithNamespace.getValue());
71         }
72
73     }
74     
75 }