7b94e9bb46cdbfb2d03cdd72de1217e70575366a
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / 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.mdsal.binding.dom.codec.impl;
9
10 import static com.google.common.base.Verify.verify;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
13
14 import com.google.common.collect.ImmutableSet;
15 import java.lang.reflect.Method;
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
20 import org.opendaylight.mdsal.binding.model.api.Type;
21 import org.opendaylight.mdsal.binding.runtime.api.RuntimeGeneratedUnion;
22 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
23 import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
26
27 final class UnionTypeCodec extends ValueTypeCodec {
28     private final ImmutableSet<UnionValueOptionContext> typeCodecs;
29     private final Class<?> unionClass;
30
31     private UnionTypeCodec(final Class<?> unionClass, final List<UnionValueOptionContext> typeCodecs) {
32         this.unionClass = requireNonNull(unionClass);
33         // Squashes duplicates
34         this.typeCodecs = ImmutableSet.copyOf(typeCodecs);
35     }
36
37     static UnionTypeCodec of(final Class<?> unionCls, final UnionTypeDefinition unionType,
38             final BindingCodecContext codecContext) throws Exception {
39         final List<String> unionProperties = extractUnionProperties(codecContext.getRuntimeContext()
40             .getTypeWithSchema(unionCls).javaType());
41         final List<TypeDefinition<?>> unionTypes = unionType.getTypes();
42         verify(unionTypes.size() == unionProperties.size(), "Mismatched union types %s and properties %s",
43             unionTypes, unionProperties);
44
45         final List<UnionValueOptionContext> values = new ArrayList<>(unionTypes.size());
46         final Iterator<String> it = unionProperties.iterator();
47         for (final TypeDefinition<?> subtype : unionTypes) {
48             final String getterName = BindingMapping.GETTER_PREFIX + BindingMapping.toFirstUpper(it.next());
49             final Method valueGetter = unionCls.getMethod(getterName);
50             final Class<?> valueType = valueGetter.getReturnType();
51             final IllegalArgumentCodec<Object, Object> codec = codecContext.getCodec(valueType, subtype);
52
53             values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, codec));
54         }
55
56         return new UnionTypeCodec(unionCls, values);
57     }
58
59     private static List<String> extractUnionProperties(final Type type) {
60         verify(type instanceof GeneratedTransferObject, "Unexpected runtime type %s", type);
61
62         GeneratedTransferObject gto = (GeneratedTransferObject) type;
63         while (true) {
64             if (gto instanceof RuntimeGeneratedUnion) {
65                 return ((RuntimeGeneratedUnion) gto).typePropertyNames();
66             }
67             gto = verifyNotNull(gto.getSuperType(), "Cannot find union type information for %s", type);
68         }
69     }
70
71     @Override
72     public Object deserialize(final Object input) {
73         for (final UnionValueOptionContext member : typeCodecs) {
74             final Object ret = member.deserializeUnion(input);
75             if (ret != null) {
76                 return ret;
77             }
78         }
79
80         throw new IllegalArgumentException(String.format("Failed to construct instance of %s for input %s",
81             unionClass, input));
82     }
83
84     @Override
85     public Object serialize(final Object input) {
86         for (final UnionValueOptionContext valCtx : typeCodecs) {
87             final Object domValue = valCtx.serialize(input);
88             if (domValue != null) {
89                 return domValue;
90             }
91         }
92         throw new IllegalStateException("No codec matched value " + input);
93     }
94 }