From: Jie Han Date: Tue, 27 Jun 2017 08:14:54 +0000 (+0800) Subject: Binding generator v2 - uses statement - support choice X-Git-Tag: release/carbon-sr2~54 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;ds=sidebyside;h=398f49eb45b0ece88908031ea343da1f8e1ecc21;p=mdsal.git Binding generator v2 - uses statement - support choice - generate type from uses choice which extends reference choice type - add test yang Note: fix getter in case type in grouping by "Binding generator v2 - Clean up codes of constructGetter Change-Id: I1e72a6812ac5c32974b2db2c1b6c343ae0944bd8 Signed-off-by: Jie Han (cherry picked from commit d68c7fe825e06409bdd721dd15437ae72be097c5) --- diff --git a/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/AuxiliaryGenUtils.java b/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/AuxiliaryGenUtils.java index 7f5c52c062..4432555e94 100644 --- a/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/AuxiliaryGenUtils.java +++ b/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/AuxiliaryGenUtils.java @@ -23,7 +23,6 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; -import org.opendaylight.mdsal.binding.javav2.generator.api.BindingGenerator; import org.opendaylight.mdsal.binding.javav2.generator.impl.txt.yangTemplateForModule; import org.opendaylight.mdsal.binding.javav2.generator.impl.txt.yangTemplateForNode; import org.opendaylight.mdsal.binding.javav2.generator.impl.txt.yangTemplateForNodes; diff --git a/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/GenHelperUtil.java b/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/GenHelperUtil.java index 31f88bcb82..d354d2b253 100644 --- a/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/GenHelperUtil.java +++ b/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/GenHelperUtil.java @@ -284,52 +284,71 @@ final class GenHelperUtil { return genCtx; } - static void addUsesImplements(final DataNodeContainer superNode, final Module superModule, - final DataNodeContainer node, final Module module, + static void addUsesImplements(final SchemaNode superNode, final Module superModule, + final SchemaNode node, final Module module, final SchemaContext schemaContext, Map genCtx, final BindingNamespaceType namespaceType ) { - for (SchemaNode superChildNode : superNode.getChildNodes()) { - if (superChildNode instanceof DataNodeContainer) { - final QName childQName = QName.create(((SchemaNode)node).getQName(), superChildNode.getQName().getLocalName()); - DataSchemaNode childNode = node.getDataChildByName(childQName); - Preconditions.checkNotNull(childNode, ((SchemaNode) node).getPath() + "->" + childQName.toString()); - - final GeneratedTypeBuilder type = genCtx.get(module).getChildNode(childNode.getPath()); - final GeneratedTypeBuilder superType = genCtx.get(superModule).getChildNode(superChildNode.getPath()); - - //TODO:delete this after supporting uses augment - if (type == null || superType == null) { - return; - } - Preconditions.checkNotNull(type, module.toString() + "->" + childNode.getPath().toString()); - Preconditions.checkNotNull(superType, superModule.toString() + "->" + superChildNode.getPath().toString()); - type.addImplementsType(superType); - if (superChildNode instanceof ListSchemaNode - && !((ListSchemaNode)superChildNode).getKeyDefinition().isEmpty()) { - if (namespaceType.equals(BindingNamespaceType.Grouping)) { - genCtx.get(module).getKeyType(childNode.getPath()) - .addImplementsType(genCtx.get(superModule).getKeyType(superChildNode.getPath())); - } else if (namespaceType.equals(BindingNamespaceType.Data)){ - genCtx.get(module).getKeyGenTO(childNode.getPath()) - .addImplementsType(genCtx.get(superModule).getKeyType(superChildNode.getPath())); + + if (superNode instanceof DataNodeContainer) { + for (DataSchemaNode superChildNode : ((DataNodeContainer)superNode).getChildNodes()) { + if (superChildNode instanceof DataNodeContainer || superChildNode instanceof ChoiceSchemaNode) { + final QName childQName = QName.create(node.getQName(), superChildNode.getQName().getLocalName()); + DataSchemaNode childNode = ((DataNodeContainer)node).getDataChildByName(childQName); + Preconditions.checkNotNull(childNode, node.getPath() + "->" + childQName.toString()); + + final GeneratedTypeBuilder type = genCtx.get(module).getChildNode(childNode.getPath()); + final GeneratedTypeBuilder superType = genCtx.get(superModule).getChildNode(superChildNode.getPath()); + + //TODO:delete this after supporting uses augment + if (type == null || superType == null) { + return; } + Preconditions.checkNotNull(type, module.toString() + "->" + childNode.getPath().toString()); + Preconditions.checkNotNull(superType, superModule.toString() + "->" + superChildNode.getPath().toString()); + type.addImplementsType(superType); + if (superChildNode instanceof ListSchemaNode + && !((ListSchemaNode) superChildNode).getKeyDefinition().isEmpty()) { + if (namespaceType.equals(BindingNamespaceType.Grouping)) { + genCtx.get(module).getKeyType(childNode.getPath()) + .addImplementsType(genCtx.get(superModule).getKeyType(superChildNode.getPath())); + } else if (namespaceType.equals(BindingNamespaceType.Data)) { + genCtx.get(module).getKeyGenTO(childNode.getPath()) + .addImplementsType(genCtx.get(superModule).getKeyType(superChildNode.getPath())); + } + } + addUsesImplements(superChildNode, superModule, childNode, module, schemaContext, genCtx, namespaceType); } - addUsesImplements((DataNodeContainer)superChildNode, superModule, (DataNodeContainer)childNode, module, schemaContext, genCtx, namespaceType); } + } else if (superNode instanceof ChoiceSchemaNode) { + for (ChoiceCaseNode superCaseNode : ((ChoiceSchemaNode)superNode).getCases()) { + final QName childQName = QName.create(node.getQName(), superCaseNode.getQName().getLocalName()); + ChoiceCaseNode caseNode = ((ChoiceSchemaNode)node).getCaseNodeByName(childQName); + Preconditions.checkNotNull(caseNode, node.getPath() + "->" + childQName.toString()); + + final GeneratedTypeBuilder type = genCtx.get(module).getCase(caseNode.getPath()); + final GeneratedTypeBuilder superType = genCtx.get(superModule).getCase(superCaseNode.getPath()); + Preconditions.checkNotNull(type, module.toString() + "->" + caseNode.getPath().toString()); + Preconditions.checkNotNull(superType, superModule.toString() + "->" + superCaseNode.getPath().toString()); + type.addImplementsType(superType); + addUsesImplements(superCaseNode, superModule, caseNode, module, schemaContext, genCtx, namespaceType); + } + } else { + throw new IllegalArgumentException("Not support node :" + node.getPath().toString()); } } - static Map processUsesImplements(final DataNodeContainer node, final Module module, + static Map processUsesImplements(final SchemaNode node, final Module module, final SchemaContext schemaContext, Map genCtx, final BindingNamespaceType namespaceType) { - - for (final UsesNode usesNode : node.getUses()) { - final SchemaNode groupingNode = SchemaContextUtil.findDataSchemaNode(schemaContext, usesNode.getGroupingPath()); - Preconditions.checkNotNull(groupingNode, module.toString() + "->" - + usesNode.getGroupingPath().toString()); - Preconditions.checkState(groupingNode instanceof GroupingDefinition, - module.toString() + "->" + usesNode.getGroupingPath().toString()); - final Module superModule = SchemaContextUtil.findParentModule(schemaContext, groupingNode); - GroupingDefinition grouping = (GroupingDefinition)groupingNode; - addUsesImplements(grouping, superModule, node, module, schemaContext, genCtx, namespaceType); + if (node instanceof DataNodeContainer) { + for (final UsesNode usesNode : ((DataNodeContainer)node).getUses()) { + final SchemaNode groupingNode = SchemaContextUtil.findDataSchemaNode(schemaContext, usesNode.getGroupingPath()); + Preconditions.checkNotNull(groupingNode, module.toString() + "->" + + usesNode.getGroupingPath().toString()); + Preconditions.checkState(groupingNode instanceof GroupingDefinition, + module.toString() + "->" + usesNode.getGroupingPath().toString()); + final Module superModule = SchemaContextUtil.findParentModule(schemaContext, groupingNode); + GroupingDefinition grouping = (GroupingDefinition) groupingNode; + addUsesImplements(grouping, superModule, node, module, schemaContext, genCtx, namespaceType); + } } return genCtx; } @@ -684,12 +703,14 @@ final class GenHelperUtil { final Map genCtx, final TypeProvider typeProvider, final BindingNamespaceType namespaceType) { checkArgument(basePackageName != null, "Base Package Name cannot be NULL."); checkArgument(choiceNode != null, "Choice Schema Node cannot be NULL."); - + final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(basePackageName, choiceNode, schemaContext, "", "", verboseClasssComments, genTypeBuilders, namespaceType); constructGetter(parent, choiceNode.getQName().getLocalName(), choiceNode.getDescription(), choiceTypeBuilder, choiceNode.getStatus()); - choiceTypeBuilder.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, choiceTypeBuilder)); + if (namespaceType.equals(BindingNamespaceType.Data)) { + choiceTypeBuilder.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, choiceTypeBuilder)); + } annotateDeprecatedIfNecessary(choiceNode.getStatus(), choiceTypeBuilder); genCtx.get(module).addChildNodeType(choiceNode, choiceTypeBuilder); generateTypesFromChoiceCases(module, schemaContext, genCtx, basePackageName, choiceTypeBuilder.toInstance(), diff --git a/binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-uses-choice.yang b/binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-uses-choice.yang new file mode 100644 index 0000000000..f09c0cab01 --- /dev/null +++ b/binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-uses-choice.yang @@ -0,0 +1,22 @@ +module test-uses-choice { + yang-version 1.1; + + namespace "urn:test:uses:choice"; + prefix test-uses-choice; + organization "test.type.org"; + revision "2017-06-30"; + + grouping my-grouping { + choice my-choice { + case my-case { + leaf my-leaf { + type string; + } + } + } + } + + container my-cont { + uses my-grouping; + } +} \ No newline at end of file diff --git a/binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/javav2/java/api/generator/renderers/BuilderRenderer.java b/binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/javav2/java/api/generator/renderers/BuilderRenderer.java index 75d2cc069d..9decef1310 100644 --- a/binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/javav2/java/api/generator/renderers/BuilderRenderer.java +++ b/binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/javav2/java/api/generator/renderers/BuilderRenderer.java @@ -175,11 +175,10 @@ public class BuilderRenderer extends BaseRenderer { for (Type implementedIfc : implementedIfcs) { if ((implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))) { final GeneratedType ifc = (GeneratedType) implementedIfc; - //TODO:temporarily eliminated for through compiling, to be restored in "support choice" patch. - // if (implementedIfc instanceof GeneratedTypeForBuilder) { + if (implementedIfc instanceof GeneratedTypeForBuilder) { methods.addAll(ifc.getMethodDefinitions()); - collectImplementedMethods(methods, ifc.getImplements()); - // } + } + collectImplementedMethods(methods, ifc.getImplements()); } else if (Augmentable.class.getName().equals(implementedIfc.getFullyQualifiedName())) { for (Method method : Augmentable.class.getMethods()) { if ("getAugmentation".equals(method.getName())) {