BUG-4638: fix decimal restrictions 40/30040/8
authorRobert Varga <robert.varga@pantheon.sk>
Sat, 21 Nov 2015 17:43:12 +0000 (18:43 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 30 Nov 2015 16:13:16 +0000 (16:13 +0000)
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 <robert.varga@pantheon.sk>
binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java
binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java

index 296b70147f893863eba39bc805844667c2332a4c..e6b5cd640c4e9d76e1e8df431b20400cc03555cf 100644 (file)
@@ -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);
             }
index f80ba6a7b40957de8000d0853edb3d3c41d2767a..1ae1557e7402a72fbd07815e361be781c8955856 100644 (file)
@@ -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<RangeConstraint> getRangeConstraints() {
+                            return decimal.getRangeConstraints();
+                        }
+
+                        @Override
+                        public List<PatternConstraint> getPatternConstraints() {
+                            return ImmutableList.of();
+                        }
+
+                        @Override
+                        public List<LengthConstraint> getLengthConstraints() {
+                            return ImmutableList.of();
+                        }
+                    };
+                }
+            }
+
             return EMPTY_RESTRICTIONS;
         }