From f6e117a1e6a41447823258ac525ffcec4781ef0b Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Mon, 28 May 2018 19:51:15 +0200 Subject: [PATCH] Fix codegen of root choices 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 --- .../generator/impl/AbstractTypeGenerator.java | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/AbstractTypeGenerator.java b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/AbstractTypeGenerator.java index 9080f04656..d617d685dc 100644 --- a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/AbstractTypeGenerator.java +++ b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/AbstractTypeGenerator.java @@ -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 cases = choice.findCaseNodes(caseName); return cases.isEmpty() ? null : cases.get(0); -- 2.36.6