Search in sources :

Example 1 with Restrictions

use of org.opendaylight.mdsal.binding.model.api.Restrictions in project mdsal by opendaylight.

the class GeneratedTOBuilderImplTest method testSetRestrictions.

@Test
public void testSetRestrictions() {
    final CodegenGeneratedTOBuilder genTOBuilder = new CodegenGeneratedTOBuilder(JavaTypeName.create("org.opendaylight.yangtools.test", "Test"));
    final Restrictions restrictions = new Restrictions() {

        @Override
        public boolean isEmpty() {
            return false;
        }

        @Override
        public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
            return Optional.empty();
        }

        @Override
        public List<PatternConstraint> getPatternConstraints() {
            return null;
        }

        @Override
        public Optional<LengthConstraint> getLengthConstraint() {
            return Optional.empty();
        }
    };
    genTOBuilder.setRestrictions(restrictions);
    final GeneratedTransferObject genTO = genTOBuilder.build();
    assertNotNull(genTO.getRestrictions());
}
Also used : LengthConstraint(org.opendaylight.yangtools.yang.model.api.type.LengthConstraint) GeneratedTransferObject(org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject) PatternConstraint(org.opendaylight.yangtools.yang.model.api.type.PatternConstraint) Restrictions(org.opendaylight.mdsal.binding.model.api.Restrictions) Test(org.junit.Test)

Example 2 with Restrictions

use of org.opendaylight.mdsal.binding.model.api.Restrictions in project mdsal by opendaylight.

the class BindingGeneratorUtilTest method getDefaultDecimalRestrictionsTest.

@Test
public void getDefaultDecimalRestrictionsTest() {
    final DecimalTypeDefinition base = BaseTypes.decimalTypeBuilder(ROOT).setFractionDigits(10).build();
    final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(base, ROOT).build();
    final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
    assertNotNull(restrictions);
    assertFalse(restrictions.isEmpty());
    assertEquals(base.getRangeConstraint(), restrictions.getRangeConstraint());
    assertEquals(Optional.empty(), restrictions.getLengthConstraint());
    assertEquals(List.of(), restrictions.getPatternConstraints());
}
Also used : DecimalTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition) Restrictions(org.opendaylight.mdsal.binding.model.api.Restrictions) Test(org.junit.Test)

Example 3 with Restrictions

use of org.opendaylight.mdsal.binding.model.api.Restrictions in project mdsal by opendaylight.

the class BindingGeneratorUtilTest method getDefaultIntegerRestrictionsTest.

@Test
public void getDefaultIntegerRestrictionsTest() {
    final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.int16Type(), ROOT).build();
    final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
    assertNotNull(restrictions);
    assertFalse(restrictions.isEmpty());
    assertEquals(((Int16TypeDefinition) type.getBaseType()).getRangeConstraint(), restrictions.getRangeConstraint());
    assertEquals(Optional.empty(), restrictions.getLengthConstraint());
    assertEquals(List.of(), restrictions.getPatternConstraints());
}
Also used : Restrictions(org.opendaylight.mdsal.binding.model.api.Restrictions) Test(org.junit.Test)

Example 4 with Restrictions

use of org.opendaylight.mdsal.binding.model.api.Restrictions in project mdsal by opendaylight.

the class BindingGeneratorUtilTest method getDefaultUnsignedIntegerRestrictionsTest.

@Test
public void getDefaultUnsignedIntegerRestrictionsTest() {
    final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.uint16Type(), ROOT).build();
    final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
    assertNotNull(restrictions);
    assertFalse(restrictions.isEmpty());
    assertEquals(((Uint16TypeDefinition) type.getBaseType()).getRangeConstraint(), restrictions.getRangeConstraint());
    assertEquals(Optional.empty(), restrictions.getLengthConstraint());
    assertEquals(List.of(), restrictions.getPatternConstraints());
}
Also used : Restrictions(org.opendaylight.mdsal.binding.model.api.Restrictions) Test(org.junit.Test)

Example 5 with Restrictions

use of org.opendaylight.mdsal.binding.model.api.Restrictions in project mdsal by opendaylight.

the class BindingGeneratorUtil method getRestrictions.

public static Restrictions getRestrictions(final TypeDefinition<?> type) {
    // binding to core Java types. Hence we'll emit empty restrictions for base types.
    if (type == null || type.getBaseType() == null) {
        // comparison
        if (type instanceof DecimalTypeDefinition) {
            final DecimalTypeDefinition decimal = (DecimalTypeDefinition) type;
            final DecimalTypeBuilder tmpBuilder = BaseTypes.decimalTypeBuilder(decimal.getQName());
            tmpBuilder.setFractionDigits(decimal.getFractionDigits());
            final DecimalTypeDefinition tmp = tmpBuilder.build();
            if (!tmp.getRangeConstraint().equals(decimal.getRangeConstraint())) {
                return new Restrictions() {

                    @Override
                    public boolean isEmpty() {
                        return false;
                    }

                    @Override
                    public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
                        return decimal.getRangeConstraint();
                    }

                    @Override
                    public List<PatternConstraint> getPatternConstraints() {
                        return ImmutableList.of();
                    }

                    @Override
                    public Optional<LengthConstraint> getLengthConstraint() {
                        return Optional.empty();
                    }
                };
            }
        }
        return EMPTY_RESTRICTIONS;
    }
    final Optional<LengthConstraint> length;
    final List<PatternConstraint> pattern;
    final Optional<? extends RangeConstraint<?>> range;
    /*
         * Take care of extended types.
         *
         * Other types which support constraints are check afterwards. There is a slight twist with them, as returned
         * constraints are the effective view, e.g. they are inherited from base type. Since the constraint is already
         * enforced by the base type, we want to skip them and not perform duplicate checks.
         *
         * We end up emitting ConcreteType instances for YANG base types, which leads to their constraints not being
         * enforced (most notably decimal64). Therefore we need to make sure we do not strip the next-to-last
         * restrictions.
         *
         * FIXME: this probably not the best solution and needs further analysis.
         */
    if (type instanceof BinaryTypeDefinition) {
        final BinaryTypeDefinition binary = (BinaryTypeDefinition) type;
        final BinaryTypeDefinition base = binary.getBaseType();
        if (base != null && base.getBaseType() != null) {
            length = currentOrEmpty(binary.getLengthConstraint(), base.getLengthConstraint());
        } else {
            length = binary.getLengthConstraint();
        }
        pattern = ImmutableList.of();
        range = Optional.empty();
    } else if (type instanceof DecimalTypeDefinition) {
        length = Optional.empty();
        pattern = ImmutableList.of();
        final DecimalTypeDefinition decimal = (DecimalTypeDefinition) type;
        final DecimalTypeDefinition base = decimal.getBaseType();
        if (base != null && base.getBaseType() != null) {
            range = currentOrEmpty(decimal.getRangeConstraint(), base.getRangeConstraint());
        } else {
            range = decimal.getRangeConstraint();
        }
    } else if (type instanceof RangeRestrictedTypeDefinition) {
        // Integer-like types
        length = Optional.empty();
        pattern = ImmutableList.of();
        range = extractRangeConstraint((RangeRestrictedTypeDefinition<?, ?>) type);
    } else if (type instanceof StringTypeDefinition) {
        final StringTypeDefinition string = (StringTypeDefinition) type;
        final StringTypeDefinition base = string.getBaseType();
        if (base != null && base.getBaseType() != null) {
            length = currentOrEmpty(string.getLengthConstraint(), base.getLengthConstraint());
        } else {
            length = string.getLengthConstraint();
        }
        pattern = uniquePatterns(string);
        range = Optional.empty();
    } else {
        length = Optional.empty();
        pattern = ImmutableList.of();
        range = Optional.empty();
    }
    // Now, this may have ended up being empty, too...
    if (!length.isPresent() && pattern.isEmpty() && !range.isPresent()) {
        return EMPTY_RESTRICTIONS;
    }
    // Nope, not empty allocate a holder
    return new Restrictions() {

        @Override
        public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
            return range;
        }

        @Override
        public List<PatternConstraint> getPatternConstraints() {
            return pattern;
        }

        @Override
        public Optional<LengthConstraint> getLengthConstraint() {
            return length;
        }

        @Override
        public boolean isEmpty() {
            return false;
        }
    };
}
Also used : LengthConstraint(org.opendaylight.yangtools.yang.model.api.type.LengthConstraint) DecimalTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition) DecimalTypeBuilder(org.opendaylight.yangtools.yang.model.ri.type.DecimalTypeBuilder) RangeRestrictedTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.RangeRestrictedTypeDefinition) StringTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition) PatternConstraint(org.opendaylight.yangtools.yang.model.api.type.PatternConstraint) BinaryTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition) Restrictions(org.opendaylight.mdsal.binding.model.api.Restrictions)

Aggregations

Restrictions (org.opendaylight.mdsal.binding.model.api.Restrictions)7 Test (org.junit.Test)6 PatternConstraint (org.opendaylight.yangtools.yang.model.api.type.PatternConstraint)3 DecimalTypeDefinition (org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition)2 LengthConstraint (org.opendaylight.yangtools.yang.model.api.type.LengthConstraint)2 GeneratedTransferObject (org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject)1 ConstraintMetaDefinition (org.opendaylight.yangtools.yang.model.api.ConstraintMetaDefinition)1 BinaryTypeDefinition (org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition)1 RangeRestrictedTypeDefinition (org.opendaylight.yangtools.yang.model.api.type.RangeRestrictedTypeDefinition)1 StringTypeDefinition (org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition)1 DecimalTypeBuilder (org.opendaylight.yangtools.yang.model.ri.type.DecimalTypeBuilder)1 StringTypeBuilder (org.opendaylight.yangtools.yang.model.ri.type.StringTypeBuilder)1