Fix codegen of root choices 77/72377/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 28 May 2018 17:51:15 +0000 (19:51 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 28 May 2018 19:28:00 +0000 (21:28 +0200)
If a choice which is a direct child of a module is targeted via
an augment, codegen fails.

The reason for this is that it attempts to use an empty SchemaPath
to find a DataSchemaNode: something that cannot be done. The problem
is the assumption that a Module is a SchemaNode -- which it is not.

To fix this, recognize when the parent path is empty and short-circuit
to the defining module if that is the case.

JIRA: MDSAL-345
Change-Id: I52679c822b220271d943bfbc421049d2dc394d70
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/AbstractTypeGenerator.java

index 9080f046569ad3676723b0f12970aa0003536b97..d617d685dcb6a7dc8e925f2a6a8740a9ac8c3a7d 100644 (file)
@@ -1169,25 +1169,7 @@ abstract class AbstractTypeGenerator {
                 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(context, caseNode);
                 caseTypeBuilder.addImplementsType(targetType);
 
-                SchemaNode parent;
-                final SchemaPath nodeSp = targetNode.getPath();
-                parent = findDataSchemaNode(schemaContext, nodeSp.getParent());
-
-                GeneratedTypeBuilder childOfType = null;
-                if (parent instanceof Module) {
-                    childOfType = moduleContext(((Module) parent).getQNameModule()).getModuleNode();
-                } else if (parent instanceof CaseSchemaNode) {
-                    childOfType = findCaseByPath(parent.getPath());
-                } else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) {
-                    childOfType = findChildNodeByPath(parent.getPath());
-                } else if (parent instanceof GroupingDefinition) {
-                    childOfType = findGroupingByPath(parent.getPath());
-                }
-
-                if (childOfType == null) {
-                    throw new IllegalArgumentException("Failed to find parent type of choice " + targetNode);
-                }
-
+                GeneratedTypeBuilder childOfType = findChildOfType(targetNode);
                 CaseSchemaNode node = null;
                 final String caseLocalName = caseNode.getQName().getLocalName();
                 if (caseNode instanceof CaseSchemaNode) {
@@ -1214,6 +1196,30 @@ abstract class AbstractTypeGenerator {
         }
     }
 
+    private GeneratedTypeBuilder findChildOfType(final ChoiceSchemaNode targetNode) {
+        final SchemaPath nodePath = targetNode.getPath();
+        final SchemaPath parentSp = nodePath.getParent();
+        if (parentSp.getParent() == null) {
+            return moduleContext(nodePath.getLastComponent().getModule()).getModuleNode();
+        }
+
+        final SchemaNode parent = findDataSchemaNode(schemaContext, parentSp);
+        GeneratedTypeBuilder childOfType = null;
+        if (parent instanceof CaseSchemaNode) {
+            childOfType = findCaseByPath(parent.getPath());
+        } else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) {
+            childOfType = findChildNodeByPath(parent.getPath());
+        } else if (parent instanceof GroupingDefinition) {
+            childOfType = findGroupingByPath(parent.getPath());
+        }
+
+        if (childOfType == null) {
+            throw new IllegalArgumentException("Failed to find parent type of choice " + targetNode);
+        }
+
+        return childOfType;
+    }
+
     private static CaseSchemaNode findNamedCase(final ChoiceSchemaNode choice, final String caseName) {
         final List<CaseSchemaNode> cases = choice.findCaseNodes(caseName);
         return cases.isEmpty() ? null : cases.get(0);