Binding generator v2 - Identity fix 77/60177/3
authorJie Han <han.jie@zte.com.cn>
Fri, 30 Jun 2017 15:40:03 +0000 (23:40 +0800)
committerMartin Ciglan <martin.ciglan@pantheon.tech>
Mon, 17 Jul 2017 08:08:42 +0000 (08:08 +0000)
Change-Id: Iad246a2dfc355832ff5869a5b0bbd7b330140d00
Signed-off-by: Jie Han <han.jie@zte.com.cn>
(cherry picked from commit b9712d4e34708ae0c5c6779bd3218557f593007d)

binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/GenHelperUtil.java
binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/ModuleToGenType.java
binding2/mdsal-binding2-generator-impl/src/test/java/org/opendaylight/mdsal/binding/javav2/generator/impl/BindingGeneratorImplTest.java
binding2/mdsal-binding2-generator-impl/src/test/resources/identity/identity3.yang [new file with mode: 0644]
binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-identity-base.yang [new file with mode: 0644]
binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-identity.yang [new file with mode: 0644]

index 451e389e3cfc19705c0764656aaf1c4a32f86a4f..bea588774f8eae3402049fce3d5952767fae5ef8 100644 (file)
@@ -201,6 +201,16 @@ final class GenHelperUtil {
         return null;
      }
 
+    static GeneratedTOBuilder findIdentityByQname(final QName qname, final Map<Module, ModuleContext> genCtx) {
+        for (final ModuleContext ctx : genCtx.values()) {
+            final GeneratedTOBuilder result = ctx.getIdentities().get(qname);
+            if (result != null) {
+                return result;
+            }
+        }
+        return null;
+    }
+
     /**
      * Adds the methods to <code>typeBuilder</code> which represent subnodes of
      * node for which <code>typeBuilder</code> was created.
@@ -1336,60 +1346,55 @@ final class GenHelperUtil {
      */
     static Map<Module, ModuleContext> identityToGenType(final Module module, final String basePackageName,
             final IdentitySchemaNode identity, final SchemaContext schemaContext, Map<Module, ModuleContext> genCtx,
-            boolean verboseClassComments, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
-            final TypeProvider typeProvider, Map<QName, GeneratedTOBuilderImpl> generatedIdentities) {
+            boolean verboseClassComments) {
 
-        //check first if identity has been resolved as base identity of some other one
-        GeneratedTOBuilderImpl newType = generatedIdentities.get(identity.getQName());
+        resolveIdentitySchemaNode(basePackageName, schemaContext, identity, module, verboseClassComments, genCtx);
+        return genCtx;
+    }
+
+    private static GeneratedTOBuilder resolveIdentitySchemaNode(final String basePackageName, final SchemaContext schemaContext,
+            final IdentitySchemaNode identity, final Module module, final boolean verboseClassComments,
+            final Map<Module, ModuleContext> genCtx) {
+        Preconditions.checkNotNull(identity,"Identity can not be null!");
 
+        //check first if identity has been resolved as base identity of some other one
+        GeneratedTOBuilder newType = findIdentityByQname(identity.getQName(), genCtx);
         if (newType == null) {
+            final Module parentModule = SchemaContextUtil.findParentModule(schemaContext, identity);
+            Preconditions.checkState(module.equals(parentModule),
+                    "If the type is null ,it must be in the same module, otherwise it must has been"
+                            + "resolved by an imported module.");
+
             final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, identity.getPath(),
                     BindingNamespaceType.Identity);
             newType = new GeneratedTOBuilderImpl(packageName, identity.getQName().getLocalName(), true, false);
-        }
 
-        final Set<IdentitySchemaNode> baseIdentities = identity.getBaseIdentities();
-        if (baseIdentities.size() == 0) {
-            //no base - abstract
-            final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(BaseIdentity.class.getPackage().getName(),
-                BaseIdentity.class.getSimpleName());
-            newType.setExtendsType(gto.toInstance());
-            generatedIdentities.put(identity.getQName(), newType);
-        } else {
-            //one base - inheritance
-            final IdentitySchemaNode baseIdentity = baseIdentities.iterator().next();
-            final Module baseIdentityParentModule = SchemaContextUtil.findParentModule(schemaContext, baseIdentity);
-            final String returnTypePkgName = new StringBuilder(BindingMapping.getRootPackageName
-                    (baseIdentityParentModule))
-                    .append('.')
-                    .append(BindingNamespaceType.Identity.getPackagePrefix())
-                    .toString();
-
-            final GeneratedTOBuilderImpl existingIdentityGto = generatedIdentities.get(baseIdentity.getQName());
-            if (existingIdentityGto != null) {
-                newType.setExtendsType(existingIdentityGto.toInstance());
-            } else {
-                final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(returnTypePkgName,
-                        baseIdentity.getQName().getLocalName());
+            final Set<IdentitySchemaNode> baseIdentities = identity.getBaseIdentities();
+            if (baseIdentities.size() == 0) {
+                //no base - abstract
+                final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(BaseIdentity.class.getPackage().getName(),
+                        BaseIdentity.class.getSimpleName());
                 newType.setExtendsType(gto.toInstance());
-                generatedIdentities.put(baseIdentity.getQName(), gto);
+            } else {
+                //one base - inheritance
+                final IdentitySchemaNode baseIdentity = baseIdentities.iterator().next();
+                GeneratedTOBuilder baseType = resolveIdentitySchemaNode(basePackageName, schemaContext,
+                    baseIdentity, module, verboseClassComments, genCtx);
+                newType.setExtendsType(baseType.toInstance());
             }
 
-            //FIXME: more bases - possible composition, multiple inheritance not possible
-        }
-        generatedIdentities.put(identity.getQName(), newType);
-
-        newType.setAbstract(true);
-        newType.addComment(identity.getDescription());
-        newType.setDescription(createDescription(identity, newType.getFullyQualifiedName(), schemaContext,
-                verboseClassComments, BindingNamespaceType.Identity));
-        newType.setReference(identity.getReference());
-        newType.setModuleName(module.getName());
-        newType.setSchemaPath((List) identity.getPath().getPathFromRoot());
+            newType.setAbstract(true);
+            newType.addComment(identity.getDescription());
+            newType.setDescription(createDescription(identity, newType.getFullyQualifiedName(), schemaContext,
+                    verboseClassComments, BindingNamespaceType.Identity));
+            newType.setReference(identity.getReference());
+            newType.setModuleName(module.getName());
+            newType.setSchemaPath((List) identity.getPath().getPathFromRoot());
 
-        qNameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, identity.getQName());
+            qNameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, identity.getQName());
 
-        genCtx.get(module).addIdentityType(identity.getQName(), newType);
-        return genCtx;
+            genCtx.get(module).addIdentityType(identity.getQName(), newType);
+        }
+        return newType;
     }
 }
index 57207a3d65628cc0c866a0cbf0cd288ed2b56afa..ab9567d2403ef981241a87644768abbe5c571b08 100644 (file)
@@ -143,10 +143,9 @@ final class ModuleToGenType {
         final String basePackageName = BindingMapping.getRootPackageName(module);
 
         if (schemaIdentities != null && !schemaIdentities.isEmpty()) {
-            Map<QName, GeneratedTOBuilderImpl> generatedIdentities = new HashMap<>();
             for (final IdentitySchemaNode identity : schemaIdentities) {
                 GenHelperUtil.identityToGenType(module, basePackageName, identity, schemaContext, genCtx,
-                    verboseClassComments, genTypeBuilders, typeProvider, generatedIdentities);
+                    verboseClassComments);
             }
         }
 
index 073c6d7c9def6e77568de09870af21e28b278c6a..78b1c14eec3c353230e31c93c22582623fc3c0e4 100644 (file)
@@ -21,6 +21,7 @@ import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedTransferObject;
 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedType;
 import org.opendaylight.mdsal.binding.javav2.model.api.MethodSignature;
 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
+import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTOBuilder;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
@@ -134,6 +135,40 @@ public class BindingGeneratorImplTest {
         }
     }
 
+    @Test
+    public void generateTypesIdentityTest() throws Exception {
+        final BindingGenerator bg = new BindingGeneratorImpl(true);
+        final SchemaContext context = YangParserTestUtils.parseYangSources("/identity/");
+        assertNotNull(context);
+
+        final List<Type> generateTypes = bg.generateTypes(context, context.getModules());
+        assertNotNull(generateTypes);
+        assertTrue(!generateTypes.isEmpty());
+        for (final Type type : generateTypes) {
+            if (type.getFullyQualifiedName()
+                    .equals("org.opendaylight.mdsal.gen.javav2.identity3.module.rev170708.ident.Iden1")) {
+                final GeneratedTransferObject genTO = (GeneratedTransferObject)type;
+                assertEquals("org.opendaylight.mdsal.gen.javav2.identity3.module.rev170708.ident.Iden2",
+                        genTO.getSuperType().getFullyQualifiedName());
+
+            }
+            if (type.getFullyQualifiedName()
+                    .equals("org.opendaylight.mdsal.gen.javav2.identity3.module.rev170708.ident.Iden2")) {
+                final GeneratedTransferObject genTO = (GeneratedTransferObject)type;
+                assertEquals("org.opendaylight.mdsal.gen.javav2.identity.import_.rev170602.ident.Iden1",
+                        genTO.getSuperType().getFullyQualifiedName());
+
+            }
+            if (type.getFullyQualifiedName()
+                    .equals("org.opendaylight.mdsal.gen.javav2.identity3.module.rev170708.ident.Iden3")) {
+                final GeneratedTransferObject genTO = (GeneratedTransferObject)type;
+                assertEquals("org.opendaylight.mdsal.gen.javav2.identity3.module.rev170708.ident.Iden1",
+                        genTO.getSuperType().getFullyQualifiedName());
+
+            }
+        }
+    }
+
     private void testActualType(final GeneratedType t, final int[] test_i) {
         MethodSignature methodSignature = null;
         switch (t.getName()) {
diff --git a/binding2/mdsal-binding2-generator-impl/src/test/resources/identity/identity3.yang b/binding2/mdsal-binding2-generator-impl/src/test/resources/identity/identity3.yang
new file mode 100644 (file)
index 0000000..0baf326
--- /dev/null
@@ -0,0 +1,25 @@
+module identity3-module {
+    yang-version 1.1;
+    namespace "identity3:module";
+
+    prefix "ide3mod";
+
+    revision 2017-07-08;
+
+    import identity-import {
+        prefix "ideimp";
+    }
+
+    identity iden1 {
+        base iden2;
+    }
+
+    identity iden2 {
+        base "ideimp:iden1";
+    }
+
+    identity iden3 {
+        base iden1;
+    }
+
+}
\ No newline at end of file
diff --git a/binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-identity-base.yang b/binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-identity-base.yang
new file mode 100644 (file)
index 0000000..1e7eb3e
--- /dev/null
@@ -0,0 +1,23 @@
+module test-identity-base {
+    yang-version 1.1;
+
+    namespace "urn:test:identity:base";
+    prefix test-identity-base;
+
+    organization "test.type.org";
+    revision "2017-06-30";
+
+    identity test-root;
+
+    identity test-one {
+        base test-root;
+    }
+
+    identity test-two {
+        base test-root;
+    }
+
+    container base-cont {
+
+    }
+}
\ No newline at end of file
diff --git a/binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-identity.yang b/binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-identity.yang
new file mode 100644 (file)
index 0000000..7f82bd6
--- /dev/null
@@ -0,0 +1,22 @@
+module test-identity {
+    yang-version 1.1;
+
+    namespace "urn:test:identity";
+    prefix test-identity;
+
+    import test-identity-base {
+        prefix base;
+        revision-date 2017-06-30;
+    }
+
+    organization "test.type.org";
+    revision "2017-06-30";
+
+    identity third-party {
+        base base:test-root;
+    }
+
+    container my-cont {
+
+    }
+}
\ No newline at end of file