BUG-1555: skip choices which are not valid in a particular context
[mdsal.git] / code-generator / binding-generator-impl / src / main / java / org / opendaylight / yangtools / sal / binding / generator / util / BindingRuntimeContext.java
index da57c36fb4c27e97537dd91ff890b8a06b671547..1b40b8895c57d82eadd8fe32f843e5eab5a8bbda 100644 (file)
@@ -43,6 +43,8 @@ import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 /**
  *
  * Runtime Context for Java YANG Binding classes
@@ -61,7 +63,7 @@ import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
  *
  */
 public class BindingRuntimeContext implements Immutable {
-
+    private static final Logger LOG = LoggerFactory.getLogger(BindingRuntimeContext.class);
     private final ClassLoadingStrategy strategy;
     private final SchemaContext schemaContext;
 
@@ -97,7 +99,6 @@ public class BindingRuntimeContext implements Immutable {
      * @return Instance of BindingRuntimeContext for supplied schema context.
      */
     public static final BindingRuntimeContext create(final ClassLoadingStrategy strategy, final SchemaContext ctx) {
-
         return new BindingRuntimeContext(strategy, ctx);
     }
 
@@ -141,9 +142,9 @@ public class BindingRuntimeContext implements Immutable {
      * @throws IllegalArgumentException If supplied class is not an augmentation or current context does not contain schema for augmentation.
      */
     public AugmentationSchema getAugmentationDefinition(final Class<?> augClass) throws IllegalArgumentException {
-        Preconditions.checkArgument(Augmentation.class.isAssignableFrom(augClass), "Class {} does not represent augmentation", augClass);
+        Preconditions.checkArgument(Augmentation.class.isAssignableFrom(augClass), "Class %s does not represent augmentation", augClass);
         final AugmentationSchema ret = augmentationToSchema.get(referencedType(augClass));
-        Preconditions.checkArgument(ret != null, "Supplied augmentation {} is not valid in current context", augClass);
+        Preconditions.checkArgument(ret != null, "Supplied augmentation %s is not valid in current context", augClass);
         return ret;
     }
 
@@ -162,7 +163,7 @@ public class BindingRuntimeContext implements Immutable {
      * @return Schema node, from which class was generated.
      */
     public DataSchemaNode getSchemaDefinition(final Class<?> cls) {
-        Preconditions.checkArgument(!Augmentation.class.isAssignableFrom(cls),"Supplied class must not be augmentation");
+        Preconditions.checkArgument(!Augmentation.class.isAssignableFrom(cls),"Supplied class must not be augmentation (%s is)", cls);
         return (DataSchemaNode) typeToDefiningSchema.get(referencedType(cls));
     }
 
@@ -199,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 {} is not case.");
+        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 {} is not valid case in schema", schema);
-        return found.get();
+        return found;
     }
 
     private static Type referencedType(final Class<?> type) {
@@ -253,6 +254,20 @@ public class BindingRuntimeContext implements Immutable {
         return ImmutableMap.copyOf(childToCase);
     }
 
+    public Set<Class<?>> getCases(final Class<?> choice) {
+        Collection<Type> cazes = choiceToCases.get(referencedType(choice));
+        Set<Class<?>> ret = new HashSet<>(cazes.size());
+        for(Type caze : cazes) {
+            try {
+                final Class<?> c = strategy.loadClass(caze);
+                ret.add(c);
+            } catch (ClassNotFoundException e) {
+                LOG.warn("Failed to load class for case {}, ignoring it", caze, e);
+            }
+        }
+        return ret;
+    }
+
     public Class<?> getClassForSchema(final DataSchemaNode childSchema) {
         DataSchemaNode origSchema = getOriginalSchema(childSchema);
         Type clazzType = typeToDefiningSchema.inverse().get(origSchema);
@@ -264,21 +279,26 @@ public class BindingRuntimeContext implements Immutable {
     }
 
     public ImmutableMap<AugmentationIdentifier,Type> getAvailableAugmentationTypes(final DataNodeContainer container) {
-        Map<AugmentationIdentifier,Type> identifierToType = new HashMap<>();
+        final Map<AugmentationIdentifier,Type> identifierToType = new HashMap<>();
         if (container instanceof AugmentationTarget) {
             Set<AugmentationSchema> augments = ((AugmentationTarget) container).getAvailableAugmentations();
             for (AugmentationSchema augment : augments) {
                 // Augmentation must have child nodes if is to be used with Binding classes
+                AugmentationSchema augOrig = augment;
+                while (augOrig.getOriginalDefinition().isPresent()) {
+                    augOrig = augOrig.getOriginalDefinition().get();
+                }
+
                 if (!augment.getChildNodes().isEmpty()) {
-                    Type augType = typeToDefiningSchema.inverse().get(augment);
+                    Type augType = typeToDefiningSchema.inverse().get(augOrig);
                     if (augType != null) {
                         identifierToType.put(getAugmentationIdentifier(augment),augType);
                     }
                 }
             }
         }
-        return ImmutableMap.copyOf(identifierToType);
 
+        return ImmutableMap.copyOf(identifierToType);
     }
 
     private AugmentationIdentifier getAugmentationIdentifier(final AugmentationSchema augment) {
@@ -325,7 +345,7 @@ public class BindingRuntimeContext implements Immutable {
 
     public Class<?> getIdentityClass(final QName input) {
         Type identityType = identities.get(input);
-        Preconditions.checkArgument(identityType != null, "Supplied QName is not valid identity");
+        Preconditions.checkArgument(identityType != null, "Supplied QName %s is not a valid identity", input);
         try {
             return strategy.loadClass(identityType);
         } catch (ClassNotFoundException e) {