Adjust to yangtools-2.0.0 changes
[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.yang.types.BaseYangTypes;
16 import org.opendaylight.yangtools.concepts.Codec;
17 import org.opendaylight.yangtools.yang.binding.BindingMapping;
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.RevisionAwareXPath;
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 =
45                             unionCls.getMethod("get" + BindingMapping.getClassName(subtype.getQName()));
46                     final Class<?> valueType = valueGetter.getReturnType();
47                     final Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(valueType, subtype);
48
49                     values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, valueCodec));
50                 }
51             }
52
53             return new UnionTypeCodec(unionCls, values);
54         };
55     }
56
57     /**
58      * Prepare codec for type from leaf's return type of leafref.
59      *
60      * @param unionCls
61      *            - union class
62      * @param unionType
63      *            - union type
64      * @param bindingCodecContext
65      *            - binding codec context
66      * @param values
67      *            - union values
68      * @param subtype
69      *            - subtype of union
70      * @throws NoSuchMethodException when the getter method is not found
71      */
72     private static void addLeafrefValueCodec(final Class<?> unionCls, final UnionTypeDefinition unionType,
73             final BindingCodecContext bindingCodecContext, final Set<UnionValueOptionContext> values,
74             final TypeDefinition<?> subtype) throws NoSuchMethodException {
75         final SchemaContext schemaContext = bindingCodecContext.getRuntimeContext().getSchemaContext();
76         final Module module = schemaContext.findModule(subtype.getQName().getModule()).get();
77         final RevisionAwareXPath xpath = ((LeafrefTypeDefinition) subtype).getPathStatement();
78         // find schema node in schema context by xpath of leafref
79         final SchemaNode dataNode;
80         if (xpath.isAbsolute()) {
81             dataNode = SchemaContextUtil.findDataSchemaNode(schemaContext, module, xpath);
82         } else {
83             dataNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, module, unionType, xpath);
84         }
85         final String className = BindingMapping.getClassName(unionCls.getSimpleName());
86         final LeafSchemaNode typeNode = (LeafSchemaNode) dataNode;
87
88         // prepare name of type form return type of referenced leaf
89         final String typeName = BindingMapping.getClassName(BaseYangTypes.BASE_YANG_TYPES_PROVIDER
90                 .javaTypeForSchemaDefinitionType(typeNode.getType(), typeNode).getName());
91
92         // get method via reflection from generated code according to
93         // get_TypeName_Value method
94         final Method valueGetterParent = unionCls
95                 .getMethod(new StringBuilder("get").append(typeName).append(className).append("Value").toString());
96         final Class<?> returnType = valueGetterParent.getReturnType();
97
98         // prepare codec of union subtype according to return type of referenced
99         // leaf
100         final Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(returnType, subtype);
101         values.add(new UnionValueOptionContext(unionCls, returnType, valueGetterParent, valueCodec));
102     }
103
104     @Override
105     public Object deserialize(final Object input) {
106         for (final UnionValueOptionContext member : typeCodecs) {
107             final Object ret = member.deserializeUnion(input);
108             if (ret != null) {
109                 return ret;
110             }
111         }
112
113         throw new IllegalArgumentException(String.format("Failed to construct instance of %s for input %s",
114             getTypeClass(), input));
115     }
116
117     @Override
118     public Object serialize(final Object input) {
119         if (input != null) {
120             for (final UnionValueOptionContext valCtx : typeCodecs) {
121                 final Object domValue = valCtx.serialize(input);
122                 if (domValue != null) {
123                     return domValue;
124                 }
125             }
126         }
127         return null;
128     }
129 }