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