Binding v2 - fix wrong imports from binding v1
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / impl / UnionTypeCodec.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.javav2.dom.codec.impl;
9
10 import static org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifierNormalizer.normalizeSpecificIdentifier;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableSet;
14 import java.lang.reflect.Method;
15 import java.util.LinkedHashSet;
16 import java.util.Set;
17 import java.util.concurrent.Callable;
18 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.UnionValueOptionContext;
19 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.BindingCodecContext;
20 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value.ReflectionBasedCodec;
21 import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifier;
22 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.BaseYangTypes;
23 import org.opendaylight.yangtools.concepts.Codec;
24 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
33
34 /**
35  * Codec for serialize/deserialize union type.
36  *
37  */
38 @Beta
39 public final class UnionTypeCodec extends ReflectionBasedCodec {
40
41     private final ImmutableSet<UnionValueOptionContext> typeCodecs;
42
43     private UnionTypeCodec(final Class<?> unionCls, final Set<UnionValueOptionContext> codecs) {
44         super(unionCls);
45         typeCodecs = ImmutableSet.copyOf(codecs);
46     }
47
48     /**
49      * Loading union type codec for all subtypes of union.
50      *
51      * @param unionCls
52      *            - binding class of union
53      * @param unionType
54      *            - type definition of union
55      * @param bindingCodecContext
56      *            - binding codec context
57      * @return union codec
58      */
59     public static Callable<UnionTypeCodec> loader(final Class<?> unionCls, final UnionTypeDefinition unionType,
60             final BindingCodecContext bindingCodecContext) {
61         return () -> {
62             final Set<UnionValueOptionContext> values = new LinkedHashSet<>();
63             for (final TypeDefinition<?> subtype : unionType.getTypes()) {
64                 if (subtype instanceof LeafrefTypeDefinition) {
65                     addLeafrefValueCodec(unionCls, unionType, bindingCodecContext, values, subtype);
66                 } else {
67                     final Method valueGetter = unionCls.getMethod("get"
68                         + normalizeSpecificIdentifier(subtype.getQName().getLocalName(), JavaIdentifier.CLASS));
69                     final Class<?> valueType = valueGetter.getReturnType();
70                     final Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(valueType, subtype);
71                     values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, valueCodec));
72                 }
73             }
74             return new UnionTypeCodec(unionCls, values);
75         };
76     }
77
78     /**
79      * Prepare codec for type from leaf's return type of leafref.
80      *
81      * @param unionCls
82      *            - union class
83      * @param unionType
84      *            - union type
85      * @param bindingCodecContext
86      *            - binding codec context
87      * @param values
88      *            - union values
89      * @param subtype
90      *            - subtype of union
91      * @throws NoSuchMethodException when the getter method is not found
92      */
93     private static void addLeafrefValueCodec(final Class<?> unionCls, final UnionTypeDefinition unionType,
94             final BindingCodecContext bindingCodecContext, final Set<UnionValueOptionContext> values,
95             final TypeDefinition<?> subtype) throws NoSuchMethodException {
96         final SchemaContext schemaContext = bindingCodecContext.getRuntimeContext().getSchemaContext();
97         final Module module = schemaContext.findModule(subtype.getQName().getModule()).get();
98         final RevisionAwareXPath xpath = ((LeafrefTypeDefinition) subtype).getPathStatement();
99         // find schema node in schema context by xpath of leafref
100         final SchemaNode dataNode;
101         if (xpath.isAbsolute()) {
102             dataNode = SchemaContextUtil.findDataSchemaNode(schemaContext, module, xpath);
103         } else {
104             dataNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, module, unionType, xpath);
105         }
106
107         final LeafSchemaNode typeNode = (LeafSchemaNode) dataNode;
108
109         // prepare name of type form return type of referenced leaf
110         final String typeName = BaseYangTypes.BASE_YANG_TYPES_PROVIDER
111             .javaTypeForSchemaDefinitionType(typeNode.getType(), typeNode, null).getName();
112
113         // get method via reflection from generated code according to
114         // get_TypeName_Value method
115         final String method = normalizeSpecificIdentifier(new StringBuilder("get").append("_")
116             .append(typeName).append(unionCls.getSimpleName()).append("Value").toString(),
117             JavaIdentifier.METHOD);
118         final Method valueGetterParent = unionCls.getMethod(method);
119         final Class<?> returnType = valueGetterParent.getReturnType();
120
121         // prepare codec of union subtype according to return type of referenced
122         // leaf
123         final Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(returnType, subtype);
124         values.add(new UnionValueOptionContext(unionCls, returnType, valueGetterParent, valueCodec));
125     }
126
127     @Override
128     public Object deserialize(final Object input) {
129         for (final UnionValueOptionContext member : typeCodecs) {
130             final Object ret = member.deserializeUnion(input);
131             if (ret != null) {
132                 return ret;
133             }
134         }
135
136         throw new IllegalArgumentException(
137                 String.format("Failed to construct instance of %s for input %s", getTypeClass(), input));
138     }
139
140     @Override
141     public Object serialize(final Object input) {
142         if (input != null) {
143             for (final UnionValueOptionContext valCtx : typeCodecs) {
144                 final Object domValue = valCtx.serialize(input);
145                 if (domValue != null) {
146                     return domValue;
147                 }
148             }
149         }
150         return null;
151     }
152 }