From 1a9c0114c393e12547459d564d1a92fe69178a07 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 20 Nov 2015 18:27:36 +0100 Subject: [PATCH] BUG-4638: fix resolveLeafSchemaNodeAsMethod() Deal with new parser's way of emitting types for leaf and not using ExtendedType. Backward compatibility with the old parser is maintained. For new parser, the input into code generation will change in case an the leaf defines a default value: the type will have a default value and its schema path will match the leaf. Code generation will need to deal with this and emit the same code snippets, effectively fudging the inner restricted type. That will be addressed in a follow-up patch. Change-Id: If66305d2716a138628ba84e651bb280d588cb773 Signed-off-by: Robert Varga --- .../generator/impl/BindingGeneratorImpl.java | 76 +++++++++++++------ 1 file changed, 51 insertions(+), 25 deletions(-) diff --git a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java index 83f5bb92b8..f86301c932 100644 --- a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java +++ b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java @@ -101,9 +101,11 @@ import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; import org.opendaylight.yangtools.yang.model.util.BaseTypes; import org.opendaylight.yangtools.yang.model.util.DataNodeIterator; +import org.opendaylight.yangtools.yang.model.util.ExtendedType; import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils; import org.opendaylight.yangtools.yang.model.util.UnionType; +import org.opendaylight.yangtools.yang.model.util.type.CompatUtils; import org.opendaylight.yangtools.yang.parser.builder.util.Comparators; import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort; import org.slf4j.Logger; @@ -1326,6 +1328,30 @@ public class BindingGeneratorImpl implements BindingGenerator { } } + private static boolean isInnerType(final LeafSchemaNode leaf, final TypeDefinition type) { + // Deal with old parser, clearing out references to typedefs + if (type instanceof ExtendedType) { + return false; + } + + // New parser with encapsulated type + if (leaf.getPath().equals(type.getPath())) { + return true; + } + + // Embedded type definition with new parser. Also takes care of the old parser with bits + if (leaf.getPath().equals(type.getPath().getParent())) { + return true; + } + + // Old parser uses broken Union type, which does not change its schema path + if (type instanceof UnionType) { + return true; + } + + return false; + } + /** * Converts leaf to the getter method which is added to * typeBuilder. @@ -1355,34 +1381,34 @@ public class BindingGeneratorImpl implements BindingGenerator { return null; } - Type returnType = null; final Module parentModule = findParentModule(schemaContext, leaf); + Type returnType = null; - // FIXME: cascade assumes that ExtendedType does not resolve into any of the TypeDefinitions and - // will pass on to the default case. That default case will be also taken for base types. - // There is another twist here, which is the fact that the new parser will wrap the type - // if we have redefined the default value -- which is not something that was done before. - - final TypeDefinition typeDef = leaf.getType(); - if (typeDef instanceof EnumTypeDefinition) { - returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf); - final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef; - final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(), - typeBuilder, module); + final TypeDefinition typeDef = CompatUtils.compatLeafType(leaf); + if (isInnerType(leaf, typeDef)) { + if (typeDef instanceof EnumTypeDefinition) { + returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf); + final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef; + final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(), + typeBuilder, module); - if (enumBuilder != null) { - returnType = enumBuilder.toInstance(typeBuilder); - } - ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType); - } else if (typeDef instanceof UnionTypeDefinition) { - GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule); - if (genTOBuilder != null) { - returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule); - } - } else if (typeDef instanceof BitsTypeDefinition) { - GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule); - if (genTOBuilder != null) { - returnType = genTOBuilder.toInstance(); + if (enumBuilder != null) { + returnType = enumBuilder.toInstance(typeBuilder); + } + ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType); + } else if (typeDef instanceof UnionTypeDefinition) { + GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule); + if (genTOBuilder != null) { + returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule); + } + } else if (typeDef instanceof BitsTypeDefinition) { + GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule); + if (genTOBuilder != null) { + returnType = genTOBuilder.toInstance(); + } + } else { + final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef); + returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions); } } else { final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef); -- 2.36.6