Remove ValueTypeCodec
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / UnionTypeCodec.java
index 05053cc86dc5a8a3d07f525a9240278440695e9f..fab4cc3fd6ba7f15cc1ebba5adad332da0b5b550 100644 (file)
@@ -7,99 +7,65 @@
  */
 package org.opendaylight.mdsal.binding.dom.codec.impl;
 
+import static com.google.common.base.Verify.verify;
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.collect.ImmutableSet;
 import java.lang.reflect.Method;
-import java.util.LinkedHashSet;
-import java.util.Set;
-import java.util.concurrent.Callable;
-import org.opendaylight.mdsal.binding.generator.util.BaseYangTypesProvider;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
+import org.opendaylight.mdsal.binding.model.api.Type;
+import org.opendaylight.mdsal.binding.runtime.api.RuntimeGeneratedUnion;
 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
 import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.PathExpression;
-import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
-import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
 
-final class UnionTypeCodec extends ReflectionBasedCodec {
+final class UnionTypeCodec implements IllegalArgumentCodec<Object, Object> {
     private final ImmutableSet<UnionValueOptionContext> typeCodecs;
+    private final Class<?> unionClass;
 
-    private UnionTypeCodec(final Class<?> unionCls,final Set<UnionValueOptionContext> codecs) {
-        super(unionCls);
-        typeCodecs = ImmutableSet.copyOf(codecs);
+    private UnionTypeCodec(final Class<?> unionClass, final List<UnionValueOptionContext> typeCodecs) {
+        this.unionClass = requireNonNull(unionClass);
+        // Squashes duplicates
+        this.typeCodecs = ImmutableSet.copyOf(typeCodecs);
     }
 
-    static Callable<UnionTypeCodec> loader(final Class<?> unionCls, final UnionTypeDefinition unionType,
-                                           final BindingCodecContext bindingCodecContext) {
-        return () -> {
-            final Set<UnionValueOptionContext> values = new LinkedHashSet<>();
-            for (final TypeDefinition<?> subtype : unionType.getTypes()) {
-                if (subtype instanceof LeafrefTypeDefinition) {
-                    addLeafrefValueCodec(unionCls, unionType, bindingCodecContext, values, subtype);
-                } else {
-                    final Method valueGetter = unionCls.getMethod(BindingMapping.GETTER_PREFIX
-                        + BindingMapping.getClassName(subtype.getQName()));
-                    final Class<?> valueType = valueGetter.getReturnType();
-                    final IllegalArgumentCodec<Object, Object> valueCodec =
-                            bindingCodecContext.getCodec(valueType, subtype);
-
-                    values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, valueCodec));
-                }
-            }
+    static UnionTypeCodec of(final Class<?> unionCls, final UnionTypeDefinition unionType,
+            final BindingCodecContext codecContext) throws Exception {
+        final List<String> unionProperties = extractUnionProperties(codecContext.getRuntimeContext()
+            .getTypeWithSchema(unionCls).javaType());
+        final List<TypeDefinition<?>> unionTypes = unionType.getTypes();
+        verify(unionTypes.size() == unionProperties.size(), "Mismatched union types %s and properties %s",
+            unionTypes, unionProperties);
 
-            return new UnionTypeCodec(unionCls, values);
-        };
-    }
+        final List<UnionValueOptionContext> values = new ArrayList<>(unionTypes.size());
+        final Iterator<String> it = unionProperties.iterator();
+        for (final TypeDefinition<?> subtype : unionTypes) {
+            final String getterName = BindingMapping.GETTER_PREFIX + BindingMapping.toFirstUpper(it.next());
+            final Method valueGetter = unionCls.getMethod(getterName);
+            final Class<?> valueType = valueGetter.getReturnType();
+            final IllegalArgumentCodec<Object, Object> codec = codecContext.getCodec(valueType, subtype);
 
-    /**
-     * Prepare codec for type from leaf's return type of leafref.
-     *
-     * @param unionCls
-     *            - union class
-     * @param unionType
-     *            - union type
-     * @param bindingCodecContext
-     *            - binding codec context
-     * @param values
-     *            - union values
-     * @param subtype
-     *            - subtype of union
-     * @throws NoSuchMethodException when the getter method is not found
-     */
-    private static void addLeafrefValueCodec(final Class<?> unionCls, final UnionTypeDefinition unionType,
-            final BindingCodecContext bindingCodecContext, final Set<UnionValueOptionContext> values,
-            final TypeDefinition<?> subtype) throws NoSuchMethodException {
-        final EffectiveModelContext schemaContext = bindingCodecContext.getRuntimeContext().getEffectiveModelContext();
-        final Module module = schemaContext.findModule(subtype.getQName().getModule()).get();
-        final PathExpression xpath = ((LeafrefTypeDefinition) subtype).getPathStatement();
-        // find schema node in schema context by xpath of leafref
-        final SchemaNode dataNode;
-        if (xpath.isAbsolute()) {
-            dataNode = SchemaContextUtil.findDataSchemaNode(schemaContext, module, xpath);
-        } else {
-            dataNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, module, unionType, xpath);
+            values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, codec));
         }
-        final String className = BindingMapping.getClassName(unionCls.getSimpleName());
-        final LeafSchemaNode typeNode = (LeafSchemaNode) dataNode;
 
-        // prepare name of type form return type of referenced leaf
-        final String typeName = BindingMapping.getClassName(BaseYangTypesProvider.INSTANCE
-                .javaTypeForSchemaDefinitionType(typeNode.getType(), typeNode).getName());
+        return new UnionTypeCodec(unionCls, values);
+    }
 
-        // get method via reflection from generated code according to
-        // get_TypeName_Value method
-        final Method valueGetterParent = unionCls.getMethod(
-            BindingMapping.GETTER_PREFIX + BindingMapping.getUnionLeafrefMemberName(className, typeName));
-        final Class<?> returnType = valueGetterParent.getReturnType();
+    private static List<String> extractUnionProperties(final Type type) {
+        verify(type instanceof GeneratedTransferObject, "Unexpected runtime type %s", type);
 
-        // prepare codec of union subtype according to return type of referenced
-        // leaf
-        final IllegalArgumentCodec<Object, Object> valueCodec = bindingCodecContext.getCodec(returnType, subtype);
-        values.add(new UnionValueOptionContext(unionCls, returnType, valueGetterParent, valueCodec));
+        GeneratedTransferObject gto = (GeneratedTransferObject) type;
+        while (true) {
+            if (gto instanceof RuntimeGeneratedUnion) {
+                return ((RuntimeGeneratedUnion) gto).typePropertyNames();
+            }
+            gto = verifyNotNull(gto.getSuperType(), "Cannot find union type information for %s", type);
+        }
     }
 
     @Override
@@ -112,19 +78,17 @@ final class UnionTypeCodec extends ReflectionBasedCodec {
         }
 
         throw new IllegalArgumentException(String.format("Failed to construct instance of %s for input %s",
-            getTypeClass(), input));
+            unionClass, input));
     }
 
     @Override
     public Object serialize(final Object input) {
-        if (input != null) {
-            for (final UnionValueOptionContext valCtx : typeCodecs) {
-                final Object domValue = valCtx.serialize(input);
-                if (domValue != null) {
-                    return domValue;
-                }
+        for (final UnionValueOptionContext valCtx : typeCodecs) {
+            final Object domValue = valCtx.serialize(input);
+            if (domValue != null) {
+                return domValue;
             }
         }
-        return null;
+        throw new IllegalStateException("No codec matched value " + input);
     }
 }