Bug 4145: Issue augmenting choice inside a list
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / yangtools / sal / binding / generator / impl / BindingGeneratorImpl.java
index 3854be4c7c237f5c5f5364b10c0e32215f89fe43..8bb235a466cebb04ddd70a9c7fcd2b1d3261ced2 100644 (file)
@@ -25,6 +25,9 @@ import static org.opendaylight.yangtools.binding.generator.util.Types.typeForCla
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findNodeInSchemaContext;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
+
+import com.google.common.base.Optional;
+
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
@@ -917,21 +920,32 @@ public class BindingGeneratorImpl implements BindingGenerator {
             return null;
         }
 
-        boolean fromUses = ((DataSchemaNode) result).isAddedByUses();
-        final Iterator<UsesNode> groupingUses = grouping.getUses().iterator();
-        while (groupingUses.hasNext() && fromUses) {
-            result = findOriginalTargetFromGrouping(targetPath, groupingUses.next());
-            if (result != null) {
-                fromUses = ((DataSchemaNode) result).isAddedByUses();
+        if (result instanceof DerivableSchemaNode) {
+            DerivableSchemaNode castedResult = (DerivableSchemaNode) result;
+            Optional<? extends SchemaNode> originalNode = castedResult
+                    .getOriginal();
+            if (castedResult.isAddedByUses() && originalNode.isPresent()) {
+                result = originalNode.get();
             }
         }
-        if (fromUses) {
-            // this indicates invalid yang and thus possible bug in code because
-            // invalid yang should be already spotted by parser
-            throw new IllegalStateException("Failed to generate code for augment in " + parentUsesNode);
-        }
 
-        return (DataSchemaNode) result;
+        if (result instanceof DataSchemaNode) {
+            DataSchemaNode resultDataSchemaNode = (DataSchemaNode) result;
+            if (resultDataSchemaNode.isAddedByUses()) {
+                // The original node is required, but we have only the copy of
+                // the original node.
+                // Maybe this indicates a bug in Yang parser.
+                throw new IllegalStateException(
+                        "Failed to generate code for augment in "
+                                + parentUsesNode);
+            } else {
+                return resultDataSchemaNode;
+            }
+        } else {
+            throw new IllegalStateException(
+                    "Target node of uses-augment statement must be DataSchemaNode. Failed to generate code for augment in "
+                            + parentUsesNode);
+        }
     }
 
     /**