Rework BindingCodecTreeNode
[yangtools.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / BindingCodecContext.java
index 4a5083f636f603557d47dc21b2385138936f50e3..ea35425667392a804097808ab721812a7be7d002 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.mdsal.binding.dom.codec.impl;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkState;
+import static com.google.common.base.Verify.verify;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.ImmutableMap;
@@ -22,11 +23,13 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 import java.util.concurrent.Callable;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
 import org.opendaylight.mdsal.binding.dom.codec.impl.NodeCodecContext.CodecContextFactory;
 import org.opendaylight.mdsal.binding.dom.codec.util.BindingSchemaMapping;
 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
@@ -56,7 +59,6 @@ import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
@@ -144,16 +146,16 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree
      *         binding representation (choice, case, leaf).
      *
      */
-    @Nullable NodeCodecContext<?> getCodecContextNode(final @NonNull YangInstanceIdentifier dom,
+    @Nullable BindingDataObjectCodecTreeNode<?> getCodecContextNode(final @NonNull YangInstanceIdentifier dom,
             final @Nullable Collection<InstanceIdentifier.PathArgument> bindingArguments) {
-        NodeCodecContext<?> currentNode = root;
+        NodeCodecContext currentNode = root;
         ListNodeCodecContext<?> currentList = null;
 
         for (final YangInstanceIdentifier.PathArgument domArg : dom.getPathArguments()) {
             checkArgument(currentNode instanceof DataContainerCodecContext,
                 "Unexpected child of non-container node %s", currentNode);
             final DataContainerCodecContext<?,?> previous = (DataContainerCodecContext<?, ?>) currentNode;
-            final NodeCodecContext<?> nextNode = previous.yangPathArgumentChild(domArg);
+            final NodeCodecContext nextNode = previous.yangPathArgumentChild(domArg);
 
             /*
              * List representation in YANG Instance Identifier consists of two
@@ -210,7 +212,12 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree
             }
             return currentList;
         }
-        return currentNode;
+        if (currentNode != null) {
+            verify(currentNode instanceof BindingDataObjectCodecTreeNode, "Illegal return node %s for identifier %s",
+                currentNode, dom);
+            return (BindingDataObjectCodecTreeNode<?>) currentNode;
+        }
+        return null;
     }
 
     NotificationCodecContext<?> getNotificationContext(final SchemaPath notification) {
@@ -226,7 +233,7 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree
     }
 
     @Override
-    public ImmutableMap<String, LeafNodeCodecContext<?>> getLeafNodes(final Class<?> parentClass,
+    public ImmutableMap<String, LeafNodeCodecContext> getLeafNodes(final Class<?> parentClass,
             final DataNodeContainer childSchema) {
         final Map<String, DataSchemaNode> getterToLeafSchema = new HashMap<>();
         for (final DataSchemaNode leaf : childSchema.getChildNodes()) {
@@ -237,18 +244,26 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree
         return getLeafNodesUsingReflection(parentClass, getterToLeafSchema);
     }
 
-    private ImmutableMap<String, LeafNodeCodecContext<?>> getLeafNodesUsingReflection(final Class<?> parentClass,
+    private ImmutableMap<String, LeafNodeCodecContext> getLeafNodesUsingReflection(final Class<?> parentClass,
             final Map<String, DataSchemaNode> getterToLeafSchema) {
-        final Map<String, LeafNodeCodecContext<?>> leaves = new HashMap<>();
+        final Map<String, LeafNodeCodecContext> leaves = new HashMap<>();
         for (final Method method : parentClass.getMethods()) {
             if (method.getParameterCount() == 0) {
                 final DataSchemaNode schema = getterToLeafSchema.get(method.getName());
+                if (!(schema instanceof TypedDataSchemaNode)) {
+                    // We do not have schema for leaf, so we will ignore it (e.g. getClass).
+                    continue;
+                }
+                final TypedDataSchemaNode typedSchema = (TypedDataSchemaNode) schema;
                 final Class<?> valueType;
                 if (schema instanceof LeafSchemaNode) {
                     valueType = method.getReturnType();
                 } else if (schema instanceof LeafListSchemaNode) {
-                    final Type genericType = ClassLoaderUtils.getFirstGenericParameter(method.getGenericReturnType());
+                    final Optional<Type> optType = ClassLoaderUtils.getFirstGenericParameter(
+                        method.getGenericReturnType());
+                    checkState(optType.isPresent(), "Failed to find return type for %s", method);
 
+                    final Type genericType = optType.get();
                     if (genericType instanceof Class<?>) {
                         valueType = (Class<?>) genericType;
                     } else if (genericType instanceof ParameterizedType) {
@@ -257,11 +272,11 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree
                         throw new IllegalStateException("Unexpected return type " + genericType);
                     }
                 } else {
-                    // We do not have schema for leaf, so we will ignore it (e.g. getClass).
-                    continue;
+                    throw new IllegalStateException("Unhandled typed schema " + typedSchema);
                 }
-                final Codec<Object, Object> codec = getCodec(valueType, schema);
-                final LeafNodeCodecContext<?> leafNode = new LeafNodeCodecContext<>(schema, codec, method,
+
+                final Codec<Object, Object> codec = getCodec(valueType, typedSchema.getType());
+                final LeafNodeCodecContext leafNode = new LeafNodeCodecContext(typedSchema, codec, method,
                         context.getSchemaContext());
                 leaves.put(schema.getQName().getLocalName(), leafNode);
             }
@@ -269,11 +284,6 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree
         return ImmutableMap.copyOf(leaves);
     }
 
-    private Codec<Object, Object> getCodec(final Class<?> valueType, final DataSchemaNode schema) {
-        checkArgument(schema instanceof TypedDataSchemaNode, "Unsupported leaf node type %s", schema);
-        return getCodec(valueType, ((TypedDataSchemaNode)schema).getType());
-    }
-
     Codec<Object, Object> getCodec(final Class<?> valueType, final TypeDefinition<?> instantiatedType) {
         if (Class.class.equals(valueType)) {
             @SuppressWarnings({ "unchecked", "rawtypes" })
@@ -283,10 +293,6 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree
             @SuppressWarnings({ "unchecked", "rawtypes" })
             final Codec<Object, Object> casted = (Codec) instanceIdentifierCodec;
             return casted;
-        } else if (Boolean.class.equals(valueType)) {
-            if (instantiatedType instanceof EmptyTypeDefinition) {
-                return ValueTypeCodec.EMPTY_CODEC;
-            }
         } else if (BindingReflections.isBindingClass(valueType)) {
             return getCodecForBindingClass(valueType, instantiatedType);
         }
@@ -318,10 +324,13 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree
 
     @Override
     public IdentifiableItemCodec getPathArgumentCodec(final Class<?> listClz, final ListSchemaNode schema) {
-        final Class<? extends Identifier<?>> identifier = ClassLoaderUtils.findFirstGenericArgument(listClz,
+        final Optional<Class<Identifier<?>>> optIdentifier = ClassLoaderUtils.findFirstGenericArgument(listClz,
                 Identifiable.class);
+        checkState(optIdentifier.isPresent(), "Failed to find identifier for %s", listClz);
+
+        final Class<Identifier<?>> identifier = optIdentifier.get();
         final Map<QName, ValueContext> valueCtx = new HashMap<>();
-        for (final LeafNodeCodecContext<?> leaf : getLeafNodes(identifier, schema).values()) {
+        for (final LeafNodeCodecContext leaf : getLeafNodes(identifier, schema).values()) {
             final QName name = leaf.getDomPathArgument().getNodeType();
             valueCtx.put(name, new ValueContext(identifier, leaf));
         }
@@ -330,18 +339,18 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree
 
     @SuppressWarnings("unchecked")
     @Override
-    public <T extends DataObject> BindingCodecTreeNode<T> getSubtreeCodec(final InstanceIdentifier<T> path) {
+    public <T extends DataObject> BindingDataObjectCodecTreeNode<T> getSubtreeCodec(final InstanceIdentifier<T> path) {
         // TODO Do we need defensive check here?
-        return (BindingCodecTreeNode<T>) getCodecContextNode(path, null);
+        return (BindingDataObjectCodecTreeNode<T>) getCodecContextNode(path, null);
     }
 
     @Override
-    public BindingCodecTreeNode<?> getSubtreeCodec(final YangInstanceIdentifier path) {
+    public BindingCodecTreeNode getSubtreeCodec(final YangInstanceIdentifier path) {
         return getCodecContextNode(path, null);
     }
 
     @Override
-    public BindingCodecTreeNode<?> getSubtreeCodec(final SchemaPath path) {
+    public BindingCodecTreeNode getSubtreeCodec(final SchemaPath path) {
         throw new UnsupportedOperationException("Not implemented yet.");
     }
 }