Mark DataContainerCodecPrototype.isChoice() for removal
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / DataObjectCodecContext.java
index aa1339a633b6f5c8b82e7442e75025d4aa61a754..fd1c2eea8bd9ee66a94922fbd777e740d8f4c5d6 100644 (file)
@@ -27,17 +27,16 @@ import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.binding.dom.codec.api.IncorrectNestingException;
-import org.opendaylight.mdsal.binding.model.api.DefaultType;
 import org.opendaylight.mdsal.binding.model.api.Type;
 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
 import org.opendaylight.yangtools.yang.binding.Augmentable;
 import org.opendaylight.yangtools.yang.binding.Augmentation;
+import org.opendaylight.yangtools.yang.binding.DataContainer;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item;
 import org.opendaylight.yangtools.yang.binding.OpaqueObject;
-import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
@@ -49,9 +48,7 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
-import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -101,7 +98,8 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Dat
         final Class<D> bindingClass = getBindingClass();
 
         final ImmutableMap<Method, ValueNodeCodecContext> tmpLeaves = factory().getLeafNodes(bindingClass, getSchema());
-        final Map<Class<?>, Method> clsToMethod = BindingReflections.getChildrenClassToMethod(bindingClass);
+        final Map<Class<? extends DataContainer>, Method> clsToMethod =
+            BindingReflections.getChildrenClassToMethod(bindingClass);
 
         final Map<YangInstanceIdentifier.PathArgument, NodeContextSupplier> byYangBuilder = new HashMap<>();
         final Map<Class<?>, DataContainerCodecPrototype<?>> byStreamClassBuilder = new HashMap<>();
@@ -110,28 +108,31 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Dat
         // Adds leaves to mapping
         final Builder<String, ValueNodeCodecContext> leafChildBuilder =
                 ImmutableMap.builderWithExpectedSize(tmpLeaves.size());
-        for (final Entry<Method, ValueNodeCodecContext> entry : tmpLeaves.entrySet()) {
-            final ValueNodeCodecContext leaf = entry.getValue();
+        for (final ValueNodeCodecContext leaf : tmpLeaves.values()) {
             leafChildBuilder.put(leaf.getSchema().getQName().getLocalName(), leaf);
             byYangBuilder.put(leaf.getDomPathArgument(), leaf);
         }
         this.leafChild = leafChildBuilder.build();
 
         final Map<Method, Class<?>> tmpDataObjects = new HashMap<>();
-        for (final Entry<Class<?>, Method> childDataObj : clsToMethod.entrySet()) {
+        for (final Entry<Class<? extends DataContainer>, Method> childDataObj : clsToMethod.entrySet()) {
             final Method method = childDataObj.getValue();
             verify(!method.isDefault(), "Unexpected default method %s in %s", method, bindingClass);
 
-            final Class<?> retClass = childDataObj.getKey();
+            final Class<? extends DataContainer> retClass = childDataObj.getKey();
             if (OpaqueObject.class.isAssignableFrom(retClass)) {
                 // Filter OpaqueObjects, they are not containers
                 continue;
             }
 
             final DataContainerCodecPrototype<?> childProto = loadChildPrototype(retClass);
-            tmpDataObjects.put(method, childProto.getBindingClass());
-            byStreamClassBuilder.put(childProto.getBindingClass(), childProto);
+            final Class<?> childClass = childProto.getBindingClass();
+            tmpDataObjects.put(method, childClass);
+            byStreamClassBuilder.put(childClass, childProto);
             byYangBuilder.put(childProto.getYangArg(), childProto);
+
+            // FIXME: It really feels like we should be specializing DataContainerCodecPrototype so as to ditch
+            //        createInstance() and then we could do an instanceof check instead.
             if (childProto.isChoice()) {
                 final ChoiceNodeCodecContext<?> choice = (ChoiceNodeCodecContext<?>) childProto.get();
                 for (final Class<?> cazeChild : choice.getCaseChildrenClasses()) {
@@ -272,63 +273,19 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Dat
         return value;
     }
 
-    private DataContainerCodecPrototype<?> loadChildPrototype(final Class<?> childClass) {
-        final DataSchemaNode origDef = factory().getRuntimeContext().getSchemaDefinition(childClass);
-        // Direct instantiation or use in same module in which grouping
-        // was defined.
-        DataSchemaNode sameName;
-        try {
-            sameName = getSchema().dataChildByName(origDef.getQName());
-        } catch (final IllegalArgumentException e) {
-            LOG.trace("Failed to find schema for {}", origDef, e);
-            sameName = null;
-        }
-        final DataSchemaNode childSchema;
-        if (sameName != null) {
-            // Check if it is:
-            // - exactly same schema node, or
-            // - instantiated node was added via uses statement and is instantiation of same grouping
-            if (origDef.equals(sameName) || origDef.equals(getRootOriginalIfPossible(sameName))) {
-                childSchema = sameName;
-            } else {
-                // Node has same name, but clearly is different
-                childSchema = null;
-            }
-        } else {
-            // We are looking for instantiation via uses in other module
-            final QName instantiedName = origDef.getQName().bindTo(namespace());
-            final DataSchemaNode potential = getSchema().dataChildByName(instantiedName);
-            // We check if it is really instantiated from same definition as class was derived
-            if (potential != null && origDef.equals(getRootOriginalIfPossible(potential))) {
-                childSchema = potential;
-            } else {
-                childSchema = null;
-            }
-        }
-        final DataSchemaNode nonNullChild =
-                childNonNull(childSchema, childClass, "Node %s does not have child named %s", getSchema(), childClass);
-        return DataContainerCodecPrototype.from(createBindingArg(childClass, nonNullChild), nonNullChild, factory());
-    }
-
-    private static SchemaNode getRootOriginalIfPossible(final SchemaNode data) {
-        Optional<SchemaNode> previous = Optional.empty();
-        Optional<SchemaNode> next = getOriginalIfPossible(data);
-        while (next.isPresent()) {
-            previous = next;
-            next = getOriginalIfPossible(next.get());
-        }
-        return previous.orElse(null);
-    }
-
-    private static Optional<SchemaNode> getOriginalIfPossible(final SchemaNode node) {
-        if (node instanceof DerivableSchemaNode) {
-            @SuppressWarnings("unchecked")
-            final Optional<SchemaNode> ret  = (Optional<SchemaNode>) ((DerivableSchemaNode) node).getOriginal();
-            return ret;
-        }
-        return Optional.empty();
+    private DataContainerCodecPrototype<?> loadChildPrototype(final Class<? extends DataContainer> childClass) {
+        final DataSchemaNode childSchema = childNonNull(
+            factory().getRuntimeContext().findChildSchemaDefinition(getSchema(), namespace(), childClass), childClass,
+            "Node %s does not have child named %s", getSchema(), childClass);
+        return DataContainerCodecPrototype.from(createBindingArg(childClass, childSchema), childSchema, factory());
     }
 
+    // FIXME: MDSAL-697: move this method into BindingRuntimeContext
+    //                   This method is only called from loadChildPrototype() and exists only to be overridden by
+    //                   CaseNodeCodecContext. Since we are providing childClass and our schema to BindingRuntimeContext
+    //                   and receiving childSchema from it via findChildSchemaDefinition, we should be able to receive
+    //                   the equivalent of Map.Entry<Item, DataSchemaNode>, along with the override we create here. One
+    //                   more input we may need to provide is our bindingClass().
     @SuppressWarnings("unchecked")
     Item<?> createBindingArg(final Class<?> childClass, final DataSchemaNode childSchema) {
         return Item.of((Class<? extends DataObject>) childClass);
@@ -402,7 +359,7 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Dat
         final BindingRuntimeContext ctx = factory().getRuntimeContext();
         final Class<?> loaded;
         try {
-            loaded = ctx.loadClass(DefaultType.of(cls));
+            loaded = ctx.loadClass(Type.of(cls));
         } catch (ClassNotFoundException e) {
             LOG.debug("Proposed {} cannot be loaded in {}", cls, ctx, e);
             return false;