2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
10 import static com.google.common.base.Verify.verify;
11 import static com.google.common.base.Verify.verifyNotNull;
13 import com.google.common.collect.ImmutableSet;
14 import java.lang.reflect.Method;
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.concurrent.Callable;
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;
27 final class UnionTypeCodec extends ReflectionBasedCodec {
28 private final ImmutableSet<UnionValueOptionContext> typeCodecs;
30 private UnionTypeCodec(final Class<?> unionCls,final List<UnionValueOptionContext> codecs) {
32 typeCodecs = ImmutableSet.copyOf(codecs);
35 static Callable<UnionTypeCodec> loader(final Class<?> unionCls, final UnionTypeDefinition unionType,
36 final BindingCodecContext codecContext) {
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);
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 = BindingMapping.GETTER_PREFIX + BindingMapping.toFirstUpper(it.next());
48 final Method valueGetter = unionCls.getMethod(getterName);
49 final Class<?> valueType = valueGetter.getReturnType();
50 final IllegalArgumentCodec<Object, Object> codec = codecContext.getCodec(valueType, subtype);
52 values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, codec));
55 return new UnionTypeCodec(unionCls, values);
59 private static List<String> extractUnionProperties(final Type type) {
60 verify(type instanceof GeneratedTransferObject, "Unexpected runtime type %s", type);
62 GeneratedTransferObject gto = (GeneratedTransferObject) type;
64 if (gto instanceof RuntimeGeneratedUnion) {
65 return ((RuntimeGeneratedUnion) gto).typePropertyNames();
67 gto = verifyNotNull(gto.getSuperType(), "Cannot find union type information for %s", type);
72 public Object deserialize(final Object input) {
73 for (final UnionValueOptionContext member : typeCodecs) {
74 final Object ret = member.deserializeUnion(input);
80 throw new IllegalArgumentException(String.format("Failed to construct instance of %s for input %s",
81 getTypeClass(), input));
85 public Object serialize(final Object input) {
87 for (final UnionValueOptionContext valCtx : typeCodecs) {
88 final Object domValue = valCtx.serialize(input);
89 if (domValue != null) {