BUG-4638: fix UnionTypeCodec
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / yangtools / binding / data / codec / impl / UnionTypeCodec.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.binding.data.codec.impl;
9
10 import com.google.common.collect.ImmutableSet;
11 import java.lang.reflect.Constructor;
12 import java.lang.reflect.InvocationTargetException;
13 import java.lang.reflect.Method;
14 import java.util.HashSet;
15 import java.util.Set;
16 import java.util.concurrent.Callable;
17 import org.opendaylight.yangtools.concepts.Codec;
18 import org.opendaylight.yangtools.yang.binding.BindingMapping;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
21
22 final class UnionTypeCodec extends ReflectionBasedCodec {
23
24     private final ImmutableSet<UnionValueOptionContext> typeCodecs;
25     private final Constructor<?> charConstructor;
26
27     private UnionTypeCodec(final Class<?> unionCls,final Set<UnionValueOptionContext> codecs) {
28         super(unionCls);
29         try {
30             charConstructor = unionCls.getConstructor(char[].class);
31             typeCodecs = ImmutableSet.copyOf(codecs);
32         } catch (NoSuchMethodException | SecurityException e) {
33            throw new IllegalStateException("Required constructor is not available.",e);
34         }
35     }
36
37     static Callable<UnionTypeCodec> loader(final Class<?> unionCls, final UnionTypeDefinition unionType) {
38         return new Callable<UnionTypeCodec>() {
39             @Override
40             public UnionTypeCodec call() throws NoSuchMethodException, SecurityException {
41                 Set<UnionValueOptionContext> values = new HashSet<>();
42                 for(TypeDefinition<?> subtype : unionType.getTypes()) {
43                     String methodName = "get" + BindingMapping.getClassName(subtype.getQName());
44                     Method valueGetter = unionCls.getMethod(methodName);
45                     Class<?> valueType = valueGetter.getReturnType();
46                     Codec<Object, Object> valueCodec = UnionTypeCodec.getCodecForType(valueType, subtype);
47                     values.add(new UnionValueOptionContext(valueType,valueGetter, valueCodec));
48                 }
49                 return new UnionTypeCodec(unionCls, values);
50             }
51         };
52     }
53
54     private static Codec<Object, Object> getCodecForType(final Class<?> valueType, final TypeDefinition<?> subtype) {
55         if (subtype.getBaseType() instanceof UnionTypeDefinition) {
56             try {
57                 return UnionTypeCodec.loader(valueType, (UnionTypeDefinition) subtype.getBaseType()).call();
58             } catch (final Exception e) {
59                 throw new IllegalStateException("Could not construct Union Type Codec");
60             }
61         } else {
62             return ValueTypeCodec.getCodecFor(valueType, subtype);
63         }
64     }
65
66     @Override
67     public Object deserialize(final Object input) {
68         try {
69             return charConstructor.newInstance((input.toString().toCharArray()));
70         } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
71             throw new IllegalStateException("Could not construct instance",e);
72         }
73     }
74
75     @Override
76     public Object serialize(final Object input) {
77         if (input != null) {
78             for (UnionValueOptionContext valCtx : typeCodecs) {
79                 Object domValue = valCtx.serialize(input);
80                 if (domValue != null) {
81                     return domValue;
82                 }
83             }
84         }
85         return null;
86     }
87
88 }