Binding generator v2 - uses statement - support choice 52/60452/2
authorJie Han <han.jie@zte.com.cn>
Tue, 27 Jun 2017 08:14:54 +0000 (16:14 +0800)
committerMartin Ciglan <martin.ciglan@pantheon.tech>
Mon, 17 Jul 2017 08:09:20 +0000 (08:09 +0000)
- 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 <han.jie@zte.com.cn>
(cherry picked from commit d68c7fe825e06409bdd721dd15437ae72be097c5)

binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/AuxiliaryGenUtils.java
binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/GenHelperUtil.java
binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-uses-choice.yang [new file with mode: 0644]
binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/javav2/java/api/generator/renderers/BuilderRenderer.java

index 7f5c52c0624662bc0087915aea97604e466956b0..4432555e945577263895d51e69709f7a52aa8fd6 100644 (file)
@@ -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;
index 31f88bcb82b55cf97b0d87ea8f627f7888c76eaa..d354d2b253fd0bacb5b69c6a34bf94f3803c3048 100644 (file)
@@ -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<Module, ModuleContext> 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<Module, ModuleContext> processUsesImplements(final DataNodeContainer node, final Module module,
+    static Map<Module, ModuleContext> processUsesImplements(final SchemaNode node, final Module module,
             final SchemaContext schemaContext, Map<Module, ModuleContext> 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<Module, ModuleContext> 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 (file)
index 0000000..f09c0ca
--- /dev/null
@@ -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
index 75d2cc069de8bd77170a51dc69e287321e7ca2bd..9decef13106f62e22979bf98335a78bbfb19425c 100644 (file)
@@ -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())) {