From 1027266a165e7cbbe2020e4ab32578e51a0b3fdb Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 21 Nov 2015 18:43:12 +0100 Subject: [PATCH] BUG-4638: fix decimal restrictions We need to special-case the handling of decimal64, which the new parser emits as a base type even when the model specifies non-default restrictions. Change-Id: I6a905d53e488f7a0dc50188ab9c67c9d7367b4e5 Signed-off-by: Robert Varga --- .../binding/yang/types/TypeProviderImpl.java | 19 +++++++-- .../generator/util/BindingGeneratorUtil.java | 41 +++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java index 296b70147f..e6b5cd640c 100644 --- a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java +++ b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java @@ -78,6 +78,7 @@ import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.Decimal64; import org.opendaylight.yangtools.yang.model.util.ExtendedType; import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl; import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; @@ -200,10 +201,22 @@ public final class TypeProviderImpl implements TypeProvider { return returnType; } + // Now deal with base types if (typeDefinition.getBaseType() == null) { - // Now deal with base types - returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForYangType(typeDefinition.getQName() - .getLocalName()); + // We have to deal with differing handling of decimal64. The old parser used a fixed Decimal64 type + // and generated an enclosing ExtendedType to hold any range constraints. The new parser instantiates + // a base type which holds these constraints -- and the class is not a Decimal64. + if (typeDefinition instanceof DecimalTypeDefinition && !(typeDefinition instanceof Decimal64)) { + returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(typeDefinition, + parentNode, r); + } + + if (returnType == null) { + // FIXME: it looks as though we could be using the same codepath as above... + returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForYangType(typeDefinition.getQName() + .getLocalName()); + } + if (returnType == null) { LOG.debug("Failed to resolve Java type for {}", typeDefinition); } diff --git a/binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java b/binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java index f80ba6a7b4..1ae1557e74 100644 --- a/binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java +++ b/binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java @@ -42,7 +42,10 @@ import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.Decimal64; import org.opendaylight.yangtools.yang.model.util.ExtendedType; +import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; +import org.opendaylight.yangtools.yang.model.util.type.DecimalTypeBuilder; /** * Contains the methods for converting strings to valid JAVA language strings @@ -453,6 +456,44 @@ public final class BindingGeneratorUtil { // Old parser generated types which actually contained based restrictions, but our code deals with that when // binding to core Java types. Hence we'll emit empty restrictions for base types. if (type == null || type.getBaseType() == null) { + // Handling of decimal64 has changed in the new parser. It contains range restrictions applied to the type + // directly, without an extended type. We need to capture such constraints. In order to retain behavior we + // need to analyze the new semantics and see if the constraints have been overridden. To do that we + // instantiate a temporary unconstrained type and compare them. + // + // FIXME: looking at the generated code it looks as though we need to pass the restrictions without + // comparison and also even in the case of the old parser + if (type instanceof DecimalTypeDefinition && !(type instanceof Decimal64)) { + final DecimalTypeDefinition decimal = (DecimalTypeDefinition) type; + final DecimalTypeBuilder tmpBuilder = BaseTypes.decimalTypeBuilder(decimal.getPath()); + tmpBuilder.setFractionDigits(decimal.getFractionDigits()); + final DecimalTypeDefinition tmp = tmpBuilder.build(); + + if (!tmp.getRangeConstraints().equals(decimal.getRangeConstraints())) { + return new Restrictions() { + @Override + public boolean isEmpty() { + return false; + } + + @Override + public List getRangeConstraints() { + return decimal.getRangeConstraints(); + } + + @Override + public List getPatternConstraints() { + return ImmutableList.of(); + } + + @Override + public List getLengthConstraints() { + return ImmutableList.of(); + } + }; + } + } + return EMPTY_RESTRICTIONS; } -- 2.36.6