Split out BaseYangTypesProvider
[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 com.google.common.collect.ImmutableSet;
11 import java.lang.reflect.Method;
12 import java.util.LinkedHashSet;
13 import java.util.Set;
14 import java.util.concurrent.Callable;
15 import org.opendaylight.mdsal.binding.generator.util.BaseYangTypesProvider;
16 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
17 import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.PathExpression;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
27
28 final class UnionTypeCodec extends ReflectionBasedCodec {
29     private final ImmutableSet<UnionValueOptionContext> typeCodecs;
30
31     private UnionTypeCodec(final Class<?> unionCls,final Set<UnionValueOptionContext> codecs) {
32         super(unionCls);
33         typeCodecs = ImmutableSet.copyOf(codecs);
34     }
35
36     static Callable<UnionTypeCodec> loader(final Class<?> unionCls, final UnionTypeDefinition unionType,
37                                            final BindingCodecContext bindingCodecContext) {
38         return () -> {
39             final Set<UnionValueOptionContext> values = new LinkedHashSet<>();
40             for (final TypeDefinition<?> subtype : unionType.getTypes()) {
41                 if (subtype instanceof LeafrefTypeDefinition) {
42                     addLeafrefValueCodec(unionCls, unionType, bindingCodecContext, values, subtype);
43                 } else {
44                     final Method valueGetter = unionCls.getMethod(BindingMapping.GETTER_PREFIX
45                         + BindingMapping.getClassName(subtype.getQName()));
46                     final Class<?> valueType = valueGetter.getReturnType();
47                     final IllegalArgumentCodec<Object, Object> valueCodec =
48                             bindingCodecContext.getCodec(valueType, subtype);
49
50                     values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, valueCodec));
51                 }
52             }
53
54             return new UnionTypeCodec(unionCls, values);
55         };
56     }
57
58     /**
59      * Prepare codec for type from leaf's return type of leafref.
60      *
61      * @param unionCls
62      *            - union class
63      * @param unionType
64      *            - union type
65      * @param bindingCodecContext
66      *            - binding codec context
67      * @param values
68      *            - union values
69      * @param subtype
70      *            - subtype of union
71      * @throws NoSuchMethodException when the getter method is not found
72      */
73     private static void addLeafrefValueCodec(final Class<?> unionCls, final UnionTypeDefinition unionType,
74             final BindingCodecContext bindingCodecContext, final Set<UnionValueOptionContext> values,
75             final TypeDefinition<?> subtype) throws NoSuchMethodException {
76         final SchemaContext schemaContext = bindingCodecContext.getRuntimeContext().getSchemaContext();
77         final Module module = schemaContext.findModule(subtype.getQName().getModule()).get();
78         final PathExpression xpath = ((LeafrefTypeDefinition) subtype).getPathStatement();
79         // find schema node in schema context by xpath of leafref
80         final SchemaNode dataNode;
81         if (xpath.isAbsolute()) {
82             dataNode = SchemaContextUtil.findDataSchemaNode(schemaContext, module, xpath);
83         } else {
84             dataNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, module, unionType, xpath);
85         }
86         final String className = BindingMapping.getClassName(unionCls.getSimpleName());
87         final LeafSchemaNode typeNode = (LeafSchemaNode) dataNode;
88
89         // prepare name of type form return type of referenced leaf
90         final String typeName = BindingMapping.getClassName(BaseYangTypesProvider.INSTANCE
91                 .javaTypeForSchemaDefinitionType(typeNode.getType(), typeNode).getName());
92
93         // get method via reflection from generated code according to
94         // get_TypeName_Value method
95         final Method valueGetterParent = unionCls.getMethod(new StringBuilder().append(BindingMapping.GETTER_PREFIX)
96             .append(typeName).append(className).append("Value").toString());
97         final Class<?> returnType = valueGetterParent.getReturnType();
98
99         // prepare codec of union subtype according to return type of referenced
100         // leaf
101         final IllegalArgumentCodec<Object, Object> valueCodec = bindingCodecContext.getCodec(returnType, subtype);
102         values.add(new UnionValueOptionContext(unionCls, returnType, valueGetterParent, valueCodec));
103     }
104
105     @Override
106     public Object deserialize(final Object input) {
107         for (final UnionValueOptionContext member : typeCodecs) {
108             final Object ret = member.deserializeUnion(input);
109             if (ret != null) {
110                 return ret;
111             }
112         }
113
114         throw new IllegalArgumentException(String.format("Failed to construct instance of %s for input %s",
115             getTypeClass(), input));
116     }
117
118     @Override
119     public Object serialize(final Object input) {
120         if (input != null) {
121             for (final UnionValueOptionContext valCtx : typeCodecs) {
122                 final Object domValue = valCtx.serialize(input);
123                 if (domValue != null) {
124                     return domValue;
125                 }
126             }
127         }
128         return null;
129     }
130 }