Adopt odlparent-10.0.0/yangtools-8.0.0-SNAPSHOT
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / UnionTypeCodec.java
index ff4873734f175b44370a84fb66c90c265c0d6b65..6e4bbdb85f22c16693197aae621abd7cc198c37f 100644 (file)
@@ -7,98 +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 com.google.common.collect.ImmutableSet;
 import java.lang.reflect.Method;
-import java.util.LinkedHashSet;
-import java.util.Set;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
 import java.util.concurrent.Callable;
-import org.opendaylight.mdsal.binding.yang.types.BaseYangTypes;
-import org.opendaylight.yangtools.concepts.Codec;
-import org.opendaylight.yangtools.yang.binding.BindingMapping;
-import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+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.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 {
     private final ImmutableSet<UnionValueOptionContext> typeCodecs;
 
-    private UnionTypeCodec(final Class<?> unionCls,final Set<UnionValueOptionContext> codecs) {
+    private UnionTypeCodec(final Class<?> unionCls,final List<UnionValueOptionContext> codecs) {
         super(unionCls);
         typeCodecs = ImmutableSet.copyOf(codecs);
     }
 
     static Callable<UnionTypeCodec> loader(final Class<?> unionCls, final UnionTypeDefinition unionType,
-                                           final BindingCodecContext bindingCodecContext) {
+            final BindingCodecContext codecContext) {
         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("get" + BindingMapping.getClassName(subtype.getQName()));
-                    final Class<?> valueType = valueGetter.getReturnType();
-                    final Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(valueType, subtype);
+            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);
+
+            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);
 
-                    values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, valueCodec));
-                }
+                values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, codec));
             }
 
             return new UnionTypeCodec(unionCls, values);
         };
     }
 
-    /**
-     * 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 SchemaContext schemaContext = bindingCodecContext.getRuntimeContext().getSchemaContext();
-        final Module module = schemaContext.findModule(subtype.getQName().getModule()).get();
-        final RevisionAwareXPath 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);
-        }
-        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(BaseYangTypes.BASE_YANG_TYPES_PROVIDER
-                .javaTypeForSchemaDefinitionType(typeNode.getType(), typeNode).getName());
+    private static List<String> extractUnionProperties(final Type type) {
+        verify(type instanceof GeneratedTransferObject, "Unexpected runtime type %s", type);
 
-        // get method via reflection from generated code according to
-        // get_TypeName_Value method
-        final Method valueGetterParent = unionCls
-                .getMethod(new StringBuilder("get").append(typeName).append(className).append("Value").toString());
-        final Class<?> returnType = valueGetterParent.getReturnType();
-
-        // prepare codec of union subtype according to return type of referenced
-        // leaf
-        final Codec<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
@@ -116,14 +83,12 @@ final class UnionTypeCodec extends ReflectionBasedCodec {
 
     @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);
     }
 }