Binding generator v2 - uses statement - uses inner type #1 94/61594/1
authorJie Han <han.jie@zte.com.cn>
Tue, 18 Jul 2017 08:58:03 +0000 (16:58 +0800)
committerJie Han <han.jie@zte.com.cn>
Mon, 14 Aug 2017 08:11:55 +0000 (16:11 +0800)
- for current implementation of yangtools does not copy "types" from groupings,
but the original definition of a type is reused, so we should find inner type
added by uses by original node.
  this patch should be merged with:
  - https://git.opendaylight.org/gerrit/60529
  - https://git.opendaylight.org/gerrit/60582

- add test yangs

Change-Id: Ia82bc7b298c91bbc3ef34cfb26fea75eed5ff2b2
Signed-off-by: Jie Han <han.jie@zte.com.cn>
(cherry picked from commit 12ff5f7e52eb28e04c9749964329ca7a07649ed0)

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/yang/types/BaseYangTypes.java
binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/yang/types/TypeProviderImpl.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/uses-statement/test-uses-leaf-innertype-base.yang [new file with mode: 0644]
binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-uses-leaf-innertype.yang [new file with mode: 0644]
binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/util/Types.java

index 2b518db80b53fd28294d0c5c7f06f248521dbd28..a261112ffe2fe1bf731f7572121cd2b65a215ef1 100644 (file)
@@ -856,7 +856,18 @@ final class GenHelperUtil {
         Type returnType = null;
 
         final TypeDefinition<?> typeDef = leaf.getType();
-        if (isInnerType(leaf, typeDef)) {
+
+        if (leaf.isAddedByUses()) {
+            Preconditions.checkState(leaf instanceof DerivableSchemaNode);
+            LeafSchemaNode originalLeaf = (LeafSchemaNode)((DerivableSchemaNode) leaf).getOriginal().orNull();
+            Preconditions.checkNotNull(originalLeaf);
+            if (isInnerType(originalLeaf, typeDef)) {
+                returnType = genCtx.get(findParentModule(schemaContext, originalLeaf)).getInnerType(typeDef.getPath());
+            } else {
+                final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
+                returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions, genCtx.get(module));
+            }
+        } else if (isInnerType(leaf, typeDef)) {
             if (typeDef instanceof EnumTypeDefinition) {
                 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, genCtx.get(module));
                 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
@@ -893,6 +904,7 @@ final class GenHelperUtil {
             returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions, genCtx.get(module));
         }
 
+
         if (returnType == null) {
             return null;
         }
index f471a32d063bc366e31a9a9bb3144c4eeea8aba8..2866b182a91850bb49a8b36c0b0e4abc8c081c57 100644 (file)
@@ -173,27 +173,27 @@ public final class BaseYangTypes {
                 case "binary":
                     return restrictions == null ? Types.BYTE_ARRAY : Types.primitiveType("byte[]", restrictions);
                 case "decimal64":
-                    return Types.typeForClass(BigDecimal.class, restrictions);
+                    return Types.typeForClass(BigDecimal.class, restrictions, context);
                 case "enumeration":
-                    return Types.typeForClass(Enum.class, restrictions);
+                    return Types.typeForClass(Enum.class, restrictions, context);
                 case "int8":
-                    return Types.typeForClass(Byte.class, restrictions);
+                    return Types.typeForClass(Byte.class, restrictions, context);
                 case "int16":
-                    return Types.typeForClass(Short.class, restrictions);
+                    return Types.typeForClass(Short.class, restrictions, context);
                 case "int32":
-                    return Types.typeForClass(Integer.class, restrictions);
+                    return Types.typeForClass(Integer.class, restrictions, context);
                 case "int64":
-                    return Types.typeForClass(Long.class, restrictions);
+                    return Types.typeForClass(Long.class, restrictions, context);
                 case "string":
-                    return Types.typeForClass(String.class, restrictions);
+                    return Types.typeForClass(String.class, restrictions, context);
                 case "uint8":
-                    return Types.typeForClass(Short.class, restrictions);
+                    return Types.typeForClass(Short.class, restrictions, context);
                 case "uint16":
-                    return Types.typeForClass(Integer.class, restrictions);
+                    return Types.typeForClass(Integer.class, restrictions, context);
                 case "uint32":
-                    return Types.typeForClass(Long.class, restrictions);
+                    return Types.typeForClass(Long.class, restrictions, context);
                 case "uint64":
-                    return Types.typeForClass(BigInteger.class, restrictions);
+                    return Types.typeForClass(BigInteger.class, restrictions, context);
                 case "union" :
                     return UNION_TYPE;
                 default:
index 68b36174a48267a60fb60e6c9c54f6146ed98f13..88ff434cd50cba53051685bf2dfd22049a3622d1 100644 (file)
@@ -576,7 +576,7 @@ public final class TypeProviderImpl implements TypeProvider {
                 }
                 if (returnType == null) {
                     returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(
-                            baseTypeDef, typeDefinition, r, null);
+                            baseTypeDef, typeDefinition, r, context);
                 }
             }
         }
index 674632cc3b726ff0da277043877eb4f3e50bb873..201c3ec5a79ef5eb42f93ce89c4b4e106c5a108c 100644 (file)
@@ -12,6 +12,7 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.util.ArrayList;
 import java.util.List;
 import org.junit.Test;
 import org.opendaylight.mdsal.binding.javav2.generator.api.BindingGenerator;
@@ -69,6 +70,33 @@ public class BindingGeneratorImplTest {
         }
     }
 
+    @Test
+    public void generatedTypesUsesEnumLeafTest() throws Exception {
+        final BindingGenerator bg = new BindingGeneratorImpl(false);
+        final List<String> sources = new ArrayList<>();
+        sources.add("/uses-statement/test-uses-leaf-innertype-base.yang");
+        sources.add("/uses-statement/test-uses-leaf-innertype.yang");
+        final SchemaContext context = YangParserTestUtils.parseYangSources(sources);
+        final List<Type> generateTypes = bg.generateTypes(context);
+        assertNotNull(generateTypes);
+        assertTrue(!generateTypes.isEmpty());
+        for (final Type type : generateTypes) {
+            if (type.getName().equals("MyCont") && type.getPackageName()
+                    .equals("org.opendaylight.mdsal.gen.javav2.urn.test.uses.leaf.innertype.base.rev170809.data")) {
+                final GeneratedType gt = (GeneratedType) type;
+                final MethodSignature methodSignature = gt.getMethodDefinitions().get(0);
+                assertEquals("ErrorType", methodSignature.getReturnType().getName());
+            }
+
+            if (type.getName().equals("MyCont") && type.getPackageName()
+                    .equals("org.opendaylight.mdsal.gen.javav2.urn.test.uses.leaf.innertype.rev170809.data")) {
+                final GeneratedType gt = (GeneratedType) type;
+                final MethodSignature methodSignature = gt.getMethodDefinitions().get(0);
+                assertEquals("ErrorType", methodSignature.getReturnType().getName());
+            }
+        }
+    }
+
     @Test
     public void generatedTypesTest() throws Exception {
         final BindingGenerator bg = new BindingGeneratorImpl(false);
diff --git a/binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-uses-leaf-innertype-base.yang b/binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-uses-leaf-innertype-base.yang
new file mode 100644 (file)
index 0000000..d058795
--- /dev/null
@@ -0,0 +1,31 @@
+module test-uses-leaf-innertype-base {
+    namespace "urn:test:uses:leaf:innertype:base";
+    prefix uses-leaf;
+    revision 2017-08-09;
+
+    grouping errors {
+      leaf error-type {
+        type enumeration {
+          enum transport {
+            description "The transport layer";
+          }
+          enum rpc {
+            description "The rpc or notification layer";
+          }
+          enum protocol {
+            description "The protocol operation layer";
+          }
+          enum application {
+            description "The server application layer";
+          }
+        }
+        mandatory true;
+        description
+          "The protocol layer where the error occurred.";
+      }
+    }
+
+    container my-cont {
+      uses errors;
+    }
+}
\ No newline at end of file
diff --git a/binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-uses-leaf-innertype.yang b/binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-uses-leaf-innertype.yang
new file mode 100644 (file)
index 0000000..4212f78
--- /dev/null
@@ -0,0 +1,12 @@
+module test-uses-leaf-innertype {
+    namespace "urn:test:uses:leaf:innertype";
+    prefix uses-leaf;
+    revision 2017-08-09;
+    import test-uses-leaf-innertype-base {
+      prefix "base";
+    }
+
+    container my-cont {
+      uses base:errors;
+    }
+}
\ No newline at end of file
index 3d3a8f4bd049ee8ed01cd4994437592748af54bb..a25bd4e12022f0e1d3a40231dd9000a4aad7867a 100644 (file)
@@ -123,6 +123,20 @@ public final class Types {
         }
     }
 
+    public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions,
+            final ModuleContext moduleContext) {
+        if (restrictions != null) {
+            if (restrictions instanceof DefaultRestrictions) {
+                return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
+            } else {
+                return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions,
+                        moduleContext);
+            }
+        } else {
+            return typeForClass(cls);
+        }
+    }
+
     /**
      * Returns an instance of {@link ParameterizedType} describing the typed
      * {@link Map}&lt;K,V&gt;
@@ -322,6 +336,12 @@ public final class Types {
             this.restrictions = Preconditions.checkNotNull(restrictions);
         }
 
+        private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions,
+                final ModuleContext moduleContext) {
+            super(pkName, name, moduleContext);
+            this.restrictions = Preconditions.checkNotNull(restrictions);
+        }
+
         @Override
         public Restrictions getRestrictions() {
             return this.restrictions;