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