From 5e3f23bf8cc98145dce3aee6d2dcb94cff92698d Mon Sep 17 00:00:00 2001 From: Jie Han Date: Tue, 18 Jul 2017 16:58:03 +0800 Subject: [PATCH] Binding generator v2 - uses statement - uses inner type #1 - 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 (cherry picked from commit 12ff5f7e52eb28e04c9749964329ca7a07649ed0) --- .../javav2/generator/impl/GenHelperUtil.java | 14 ++++++++- .../generator/yang/types/BaseYangTypes.java | 22 ++++++------- .../yang/types/TypeProviderImpl.java | 2 +- .../impl/BindingGeneratorImplTest.java | 28 +++++++++++++++++ .../test-uses-leaf-innertype-base.yang | 31 +++++++++++++++++++ .../test-uses-leaf-innertype.yang | 12 +++++++ .../binding/javav2/generator/util/Types.java | 20 ++++++++++++ 7 files changed, 116 insertions(+), 13 deletions(-) create mode 100644 binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-uses-leaf-innertype-base.yang create mode 100644 binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-uses-leaf-innertype.yang 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 2b518db80b..a261112ffe 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 @@ -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; } diff --git a/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/yang/types/BaseYangTypes.java b/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/yang/types/BaseYangTypes.java index f471a32d06..2866b182a9 100644 --- a/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/yang/types/BaseYangTypes.java +++ b/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/yang/types/BaseYangTypes.java @@ -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: diff --git a/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/yang/types/TypeProviderImpl.java b/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/yang/types/TypeProviderImpl.java index 68b36174a4..88ff434cd5 100644 --- a/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/yang/types/TypeProviderImpl.java +++ b/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/yang/types/TypeProviderImpl.java @@ -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); } } } diff --git a/binding2/mdsal-binding2-generator-impl/src/test/java/org/opendaylight/mdsal/binding/javav2/generator/impl/BindingGeneratorImplTest.java b/binding2/mdsal-binding2-generator-impl/src/test/java/org/opendaylight/mdsal/binding/javav2/generator/impl/BindingGeneratorImplTest.java index 674632cc3b..201c3ec5a7 100644 --- a/binding2/mdsal-binding2-generator-impl/src/test/java/org/opendaylight/mdsal/binding/javav2/generator/impl/BindingGeneratorImplTest.java +++ b/binding2/mdsal-binding2-generator-impl/src/test/java/org/opendaylight/mdsal/binding/javav2/generator/impl/BindingGeneratorImplTest.java @@ -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 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 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 index 0000000000..d05879562e --- /dev/null +++ b/binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-uses-leaf-innertype-base.yang @@ -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 index 0000000000..4212f78cef --- /dev/null +++ b/binding2/mdsal-binding2-generator-impl/src/test/resources/uses-statement/test-uses-leaf-innertype.yang @@ -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 diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/util/Types.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/util/Types.java index 3d3a8f4bd0..a25bd4e120 100644 --- a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/util/Types.java +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/util/Types.java @@ -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}<K,V> @@ -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; -- 2.36.6