BUG-1555: skip choices which are not valid in a particular context
authorRobert Varga <rovarga@cisco.com>
Mon, 18 Aug 2014 11:44:43 +0000 (13:44 +0200)
committerRobert Varga <rovarga@cisco.com>
Mon, 18 Aug 2014 11:44:43 +0000 (13:44 +0200)
We are enumerating the use of a case in all the possible contexts, which
may partially fail. Instead of failing completely, just skip the
offending case.

Change-Id: Icbf137266784e0f469a258b2bb312db4e01bd8f6
Signed-off-by: Robert Varga <rovarga@cisco.com>
code-generator/binding-data-codec/src/main/java/org/opendaylight/yangtools/binding/data/codec/impl/ChoiceNodeCodecContext.java
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/BindingRuntimeContext.java

index de785acef686231e21700a1a0607c9128c200aa3..dcaecd34c8b637c19f4f1a92dad692aa5eb46c8a 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.yangtools.binding.data.codec.impl;
 
+import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Iterables;
@@ -23,9 +24,11 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 final class ChoiceNodeCodecContext extends DataContainerCodecContext<ChoiceNode> {
-
+    private static final Logger LOG = LoggerFactory.getLogger(ChoiceNodeCodecContext.class);
     private final ImmutableMap<YangInstanceIdentifier.PathArgument, DataContainerCodecPrototype<?>> byYangCaseChild;
     private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byClass;
     private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byCaseChildClass;
@@ -38,12 +41,14 @@ final class ChoiceNodeCodecContext extends DataContainerCodecContext<ChoiceNode>
 
         for (Class<?> caze : factory().getRuntimeContext().getCases(bindingClass())) {
             DataContainerCodecPrototype<ChoiceCaseNode> cazeDef = loadCase(caze);
-            byClassBuilder.put(cazeDef.getBindingClass(), cazeDef);
-            for (Class<? extends DataObject> cazeChild : BindingReflections.getChildrenClasses((Class) caze)) {
-                byCaseChildClassBuilder.put(cazeChild, cazeDef);
-            }
-            for (DataSchemaNode cazeChild : cazeDef.getSchema().getChildNodes()) {
-                byYangCaseChildBuilder.put(new NodeIdentifier(cazeChild.getQName()), cazeDef);
+            if (cazeDef != null) {
+                byClassBuilder.put(cazeDef.getBindingClass(), cazeDef);
+                for (Class<? extends DataObject> cazeChild : BindingReflections.getChildrenClasses((Class) caze)) {
+                    byCaseChildClassBuilder.put(cazeChild, cazeDef);
+                }
+                for (DataSchemaNode cazeChild : cazeDef.getSchema().getChildNodes()) {
+                    byYangCaseChildBuilder.put(new NodeIdentifier(cazeChild.getQName()), cazeDef);
+                }
             }
         }
 
@@ -62,8 +67,13 @@ final class ChoiceNodeCodecContext extends DataContainerCodecContext<ChoiceNode>
     }
 
     protected DataContainerCodecPrototype<ChoiceCaseNode> loadCase(final Class<?> childClass) {
-        ChoiceCaseNode childSchema = factory().getRuntimeContext().getCaseSchemaDefinition(schema(), childClass);
-        return DataContainerCodecPrototype.from(childClass, childSchema, factory());
+        Optional<ChoiceCaseNode> childSchema = factory().getRuntimeContext().getCaseSchemaDefinition(schema(), childClass);
+        if (childSchema.isPresent()) {
+            return DataContainerCodecPrototype.from(childClass, childSchema.get(), factory());
+        }
+
+        LOG.debug("Supplied class %s is not valid case in schema %s", childClass, schema());
+        return null;
     }
 
     @Override
index 59073cdbf5dcc5a2e0511d5d853e39e15f6e1952..1b40b8895c57d82eadd8fe32f843e5eab5a8bbda 100644 (file)
@@ -200,23 +200,23 @@ public class BindingRuntimeContext implements Immutable {
      *
      * @param schema Resolved parent choice schema
      * @param childClass Class representing case.
-     * @return Resolved case schema.
-     * @throws IllegalArgumentException If supplied class does not represent case or supplied case class is not
-     * valid in the context of parent choice schema.
+     * @return Optionally a resolved case schema, absent if the choice is not legal in
+     *         the given context.
+     * @throws IllegalArgumentException If supplied class does not represent case.
      */
-    public ChoiceCaseNode getCaseSchemaDefinition(final ChoiceNode schema, final Class<?> childClass) throws IllegalArgumentException {
+    public Optional<ChoiceCaseNode> getCaseSchemaDefinition(final ChoiceNode schema, final Class<?> childClass) throws IllegalArgumentException {
         DataSchemaNode origSchema = getSchemaDefinition(childClass);
         Preconditions.checkArgument(origSchema instanceof ChoiceCaseNode, "Supplied schema %s is not case.", origSchema);
+
         /* FIXME: Make sure that if there are multiple augmentations of same
          * named case, with same structure we treat it as equals
          * this is due property of Binding specification and copy builders
          * that user may be unaware that he is using incorrect case
          * which was generated for choice inside grouping.
          */
-        Optional<ChoiceCaseNode> found = BindingSchemaContextUtils.findInstantiatedCase(schema,
+        final Optional<ChoiceCaseNode> found = BindingSchemaContextUtils.findInstantiatedCase(schema,
                 (ChoiceCaseNode) origSchema);
-        Preconditions.checkArgument(found.isPresent(), "Supplied class %s (schema %s) is not valid case in schema %s", childClass, origSchema, schema);
-        return found.get();
+        return found;
     }
 
     private static Type referencedType(final Class<?> type) {