Java Code Examples for org.opendaylight.yangtools.yang.model.api.LeafSchemaNode#getType()

The following examples show how to use org.opendaylight.yangtools.yang.model.api.LeafSchemaNode#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypesResolutionTest.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testBitsType1() {
    Module tested = TestUtils.findModule(context, "custom-types-test").get();
    LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
            QName.create(tested.getQNameModule(), "mybits"));
    BitsTypeDefinition leafType = (BitsTypeDefinition) leaf.getType();
    Iterator<? extends Bit> bits = leafType.getBits().iterator();

    Bit bit1 = bits.next();
    assertEquals("disable-nagle", bit1.getName());
    assertEquals(Uint32.ZERO, bit1.getPosition());

    Bit bit2 = bits.next();
    assertEquals("auto-sense-speed", bit2.getName());
    assertEquals(Uint32.ONE, bit2.getPosition());

    Bit bit3 = bits.next();
    assertEquals("only-10-Mb", bit3.getName());
    assertEquals(Uint32.TWO, bit3.getPosition());

    assertFalse(bits.hasNext());
}
 
Example 2
Source File: YangParserTest.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testTypedefDecimal1() {
    final LeafSchemaNode testleaf = (LeafSchemaNode) foo.getDataChildByName(QName.create(foo.getQNameModule(),
        "decimal-leaf"));

    assertTrue(testleaf.getType() instanceof DecimalTypeDefinition);
    final DecimalTypeDefinition type = (DecimalTypeDefinition) testleaf.getType();
    assertEquals(QName.create(BAR, "my-decimal-type"), type.getQName());
    assertEquals(Optional.empty(), type.getUnits());
    assertEquals(Optional.empty(), type.getDefaultValue());
    assertEquals(6, type.getFractionDigits());
    assertEquals(1, type.getRangeConstraint().get().getAllowedRanges().asRanges().size());

    final DecimalTypeDefinition typeBase = type.getBaseType();
    assertEquals(QName.create(BAR, "decimal64"), typeBase.getQName());
    assertEquals(Optional.empty(), typeBase.getUnits());
    assertEquals(Optional.empty(), typeBase.getDefaultValue());
    assertEquals(6, typeBase.getFractionDigits());
    assertEquals(1, typeBase.getRangeConstraint().get().getAllowedRanges().asRanges().size());

    assertNull(typeBase.getBaseType());
}
 
Example 3
Source File: Bug2872Test.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    final SchemaContext schema = StmtTestUtils.parseYangSources("/bugs/bug2872");
    assertNotNull(schema);

    final QNameModule bug2872module = QNameModule.create(URI.create("bug2872"), Revision.of("2016-06-08"));
    final QName foo = QName.create(bug2872module, "bar");

    final DataSchemaNode dataSchemaNode = schema.getDataChildByName(foo);
    assertTrue(dataSchemaNode instanceof LeafSchemaNode);
    final LeafSchemaNode myLeaf = (LeafSchemaNode) dataSchemaNode;

    final TypeDefinition<?> type = myLeaf.getType();
    assertTrue(type instanceof EnumTypeDefinition);
    final EnumTypeDefinition myEnum = (EnumTypeDefinition) type;

    final List<EnumTypeDefinition.EnumPair> values = myEnum.getValues();
    assertEquals(2, values.size());

    final List<String> valueNames = new ArrayList<>();
    for (EnumTypeDefinition.EnumPair pair : values) {
        valueNames.add(pair.getName());
    }
    assertTrue(valueNames.contains("value-one"));
    assertTrue(valueNames.contains("value-two"));
}
 
Example 4
Source File: YangParserWithContextTest.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testTypeFromContext() throws ReactorException {
    final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
            .addSources(IETF)
            .addSource(sourceForResource("/types/[email protected]"))
            .addSource(sourceForResource("/context-test/test1.yang"))
            .buildEffective();

    final Module module = context.findModule("test1", Revision.of("2013-06-18")).get();
    final LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName(QName.create(module.getQNameModule(),
            "id"));

    assertTrue(leaf.getType() instanceof Uint16TypeDefinition);
    final Uint16TypeDefinition leafType = (Uint16TypeDefinition) leaf.getType();
    QName qname = leafType.getQName();
    assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
    assertEquals(Revision.ofNullable("2013-06-18"), qname.getRevision());
    assertEquals("port-number", qname.getLocalName());

    final Uint16TypeDefinition leafBaseType = leafType.getBaseType();
    qname = leafBaseType.getQName();
    assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"), qname.getNamespace());
    assertEquals(Revision.ofNullable("2010-09-24"), qname.getRevision());
    assertEquals("port-number", qname.getLocalName());

    final Uint8TypeDefinition dscpExt = (Uint8TypeDefinition) TestUtils.findTypedef(module.getTypeDefinitions(),
        "dscp-ext");
    final Set<? extends Range<?>> ranges = dscpExt.getRangeConstraint().get().getAllowedRanges().asRanges();
    assertEquals(1, ranges.size());
    final Range<?> range = ranges.iterator().next();
    assertEquals(Uint8.valueOf(0), range.lowerEndpoint());
    assertEquals(Uint8.valueOf(63), range.upperEndpoint());
}
 
Example 5
Source File: XmlStreamUtilsTest.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
private static LeafrefTypeDefinition findLeafrefType(final LeafSchemaNode schemaNode) {
    final TypeDefinition<?> type = schemaNode.getType();
    if (type instanceof LeafrefTypeDefinition) {
        return (LeafrefTypeDefinition) type;
    }
    return null;
}
 
Example 6
Source File: Bug6887Test.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testRestrictedEnumeration() throws Exception {
    final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6887/foo.yang");
    assertNotNull(schemaContext);

    final Module foo = schemaContext.findModule("foo", Revision.of("2017-01-26")).get();
    final LeafSchemaNode myEnumerationLeaf = (LeafSchemaNode) foo.getDataChildByName(
            QName.create(foo.getQNameModule(), "my-enumeration-leaf"));
    assertNotNull(myEnumerationLeaf);

    EnumTypeDefinition enumerationType = (EnumTypeDefinition) myEnumerationLeaf.getType();

    List<EnumPair> enums = enumerationType.getValues();
    assertEquals(2, enums.size());
    final EnumPair yellowEnum = createEnumPair("yellow", 2);
    final EnumPair redEnum = createEnumPair("red", 3);
    assertContainsEnums(enums, yellowEnum, redEnum);

    enumerationType = enumerationType.getBaseType();
    enums = enumerationType.getValues();
    assertEquals(3, enums.size());
    final EnumPair blackEnum = createEnumPair("black", 4);
    assertContainsEnums(enums, yellowEnum, redEnum, blackEnum);

    enumerationType = enumerationType.getBaseType();
    enums = enumerationType.getValues();
    assertEquals(4, enums.size());
    final EnumPair whiteEnum = createEnumPair("white", 1);
    assertContainsEnums(enums, whiteEnum, yellowEnum, redEnum, blackEnum);

    final LeafSchemaNode myEnumerationLeaf2 = (LeafSchemaNode) foo.getDataChildByName(
            QName.create(foo.getQNameModule(), "my-enumeration-leaf-2"));
    assertNotNull(myEnumerationLeaf2);

    enumerationType = (EnumTypeDefinition) myEnumerationLeaf2.getType();
    enums = enumerationType.getValues();
    assertEquals(3, enums.size());
    assertContainsEnums(enums, yellowEnum, redEnum, blackEnum);
}
 
Example 7
Source File: Bug6316Test.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
private static void verifyEnumTypedefinition(final SchemaContext context) {
    final DataSchemaNode dataChildByName = context.getDataChildByName(QName.create("foo", "enum-leaf"));
    assertTrue(dataChildByName instanceof LeafSchemaNode);
    final LeafSchemaNode enumLeaf = (LeafSchemaNode) dataChildByName;
    final TypeDefinition<? extends TypeDefinition<?>> type = enumLeaf.getType();
    assertTrue(type instanceof EnumTypeDefinition);
    final EnumTypeDefinition myEnumeration = (EnumTypeDefinition) type;
    final List<EnumPair> values = myEnumeration.getValues();
    for (final EnumPair enumPair : values) {
        final String name = enumPair.getName();
        switch (name) {
            case "zero":
                assertEquals(0, enumPair.getValue());
                break;
            case "twenty":
                assertEquals(20, enumPair.getValue());
                break;
            case "twenty-one":
                assertEquals(21, enumPair.getValue());
                break;
            case "two":
                assertEquals(2, enumPair.getValue());
                break;
            case "twenty-two":
                assertEquals(22, enumPair.getValue());
                break;
            default:
                fail("Unexpected enum name.");
        }
    }
}
 
Example 8
Source File: Bug5410Test.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
private static PatternConstraint getPatternConstraintOf(final SchemaContext context, final String leafName) {
    final DataSchemaNode dataChildByName = context.getDataChildByName(foo(leafName));
    assertTrue(dataChildByName instanceof LeafSchemaNode);
    final LeafSchemaNode leaf = (LeafSchemaNode) dataChildByName;
    final TypeDefinition<? extends TypeDefinition<?>> type = leaf.getType();
    assertTrue(type instanceof StringTypeDefinition);
    final StringTypeDefinition strType = (StringTypeDefinition) type;
    return strType.getPatternConstraints().iterator().next();
}
 
Example 9
Source File: TypesResolutionTest.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testInstanceIdentifier2() {
    Module tested = TestUtils.findModule(context, "custom-types-test").get();
    LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
            QName.create(tested.getQNameModule(), "inst-id-leaf2"));
    InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
    assertFalse(leafType.requireInstance());
}
 
Example 10
Source File: TypesResolutionTest.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testInstanceIdentifier1() {
    Module tested = TestUtils.findModule(context, "custom-types-test").get();
    LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
            QName.create(tested.getQNameModule(), "inst-id-leaf1"));
    InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
    assertFalse(leafType.requireInstance());
    assertEquals(1, leaf.getUnknownSchemaNodes().size());
}
 
Example 11
Source File: YangParserTest.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testTypedefUnion() {
    final LeafSchemaNode unionleaf = (LeafSchemaNode) foo.getDataChildByName(QName.create(foo.getQNameModule(),
        "union-leaf"));

    assertTrue(unionleaf.getType() instanceof UnionTypeDefinition);
    final UnionTypeDefinition type = (UnionTypeDefinition) unionleaf.getType();
    assertEquals(QName.create(BAR, "my-union-ext"), type.getQName());
    assertEquals(Optional.empty(), type.getUnits());
    assertEquals(Optional.empty(), type.getDefaultValue());

    final UnionTypeDefinition baseType = type.getBaseType();
    assertEquals(QName.create(BAR, "my-union"), baseType.getQName());
    assertEquals(Optional.empty(), baseType.getUnits());
    assertEquals(Optional.empty(), baseType.getDefaultValue());

    final UnionTypeDefinition unionType = baseType.getBaseType();
    final List<TypeDefinition<?>> unionTypes = unionType.getTypes();
    assertEquals(2, unionTypes.size());

    final Int16TypeDefinition unionType1 = (Int16TypeDefinition) unionTypes.get(0);
    assertEquals(QName.create(BAR, "my-union"), baseType.getQName());
    assertEquals(Optional.empty(), unionType1.getUnits());
    assertEquals(Optional.empty(), unionType1.getDefaultValue());

    final RangeConstraint<?> ranges = unionType1.getRangeConstraint().get();
    assertEquals(1, ranges.getAllowedRanges().asRanges().size());
    final Range<?> range = ranges.getAllowedRanges().span();
    assertEquals((short)1, range.lowerEndpoint());
    assertEquals((short)100, range.upperEndpoint());
    assertEquals(BaseTypes.int16Type(), unionType1.getBaseType());

    assertEquals(BaseTypes.int32Type(), unionTypes.get(1));
}
 
Example 12
Source File: YangParserTest.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testTypedefInvalidPatternsResolving() {
    final LeafSchemaNode invalidPatternStringLeaf = (LeafSchemaNode) foo
            .getDataChildByName(QName.create(foo.getQNameModule(), "invalid-pattern-string-leaf"));
    StringTypeDefinition type = (StringTypeDefinition) invalidPatternStringLeaf.getType();
    assertEquals(QName.create(BAR, "invalid-string-pattern"), type.getQName());
    assertEquals(Optional.empty(), type.getUnits());
    assertEquals(Optional.empty(), type.getDefaultValue());
    List<PatternConstraint> patterns = type.getPatternConstraints();
    assertTrue(patterns.isEmpty());

    final LeafSchemaNode invalidDirectStringPatternDefLeaf = (LeafSchemaNode) foo
            .getDataChildByName(QName.create(foo.getQNameModule(), "invalid-direct-string-pattern-def-leaf"));
    type = (StringTypeDefinition) invalidDirectStringPatternDefLeaf.getType();

    assertEquals(QName.create(YangConstants.RFC6020_YANG_MODULE, "string"), type.getQName());
    assertEquals(Optional.empty(), type.getUnits());
    assertEquals(Optional.empty(), type.getDefaultValue());
    patterns = type.getPatternConstraints();
    assertTrue(patterns.isEmpty());

    final LeafSchemaNode multiplePatternStringLeaf = (LeafSchemaNode) foo
            .getDataChildByName(QName.create(foo.getQNameModule(), "multiple-pattern-string-leaf"));
    type = (StringTypeDefinition) multiplePatternStringLeaf.getType();
    assertEquals(QName.create(BAR, "multiple-pattern-string"), type.getQName());
    assertEquals(Optional.empty(), type.getUnits());
    assertEquals(Optional.empty(), type.getDefaultValue());
    patterns = type.getPatternConstraints();
    assertTrue(!patterns.isEmpty());
    assertEquals(1, patterns.size());
    final PatternConstraint pattern = patterns.iterator().next();
    assertEquals("^(?:[e-z]*)$", pattern.getJavaPatternString());
    assertEquals(1, type.getLengthConstraint().get().getAllowedRanges().asRanges().size());

    final LeafSchemaNode multiplePatternDirectStringDefLeaf = (LeafSchemaNode) foo
            .getDataChildByName(QName.create(foo.getQNameModule(), "multiple-pattern-direct-string-def-leaf"));
    type = (StringTypeDefinition) multiplePatternDirectStringDefLeaf.getType();
    assertEquals(QName.create(FOO, "string"), type.getQName());
    assertEquals(Optional.empty(), type.getUnits());
    assertEquals(Optional.empty(), type.getDefaultValue());
    patterns = type.getPatternConstraints();
    assertTrue(!patterns.isEmpty());
    assertEquals(2, patterns.size());

    boolean isEZPattern = false;
    boolean isADPattern = false;
    for (final PatternConstraint patternConstraint : patterns) {
        if (patternConstraint.getJavaPatternString().equals("^(?:[e-z]*)$")) {
            isEZPattern = true;
        } else if (patternConstraint.getJavaPatternString().equals("^(?:[a-d]*)$")) {
            isADPattern = true;
        }
    }
    assertTrue(isEZPattern);
    assertTrue(isADPattern);
}
 
Example 13
Source File: YangParserTest.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testTypedefPatternsResolving() {
    final LeafSchemaNode stringleaf = (LeafSchemaNode) foo.getDataChildByName(QName.create(foo.getQNameModule(),
        "string-leaf"));

    assertTrue(stringleaf.getType() instanceof StringTypeDefinition);
    final StringTypeDefinition type = (StringTypeDefinition) stringleaf.getType();
    final QName typeQName = type.getQName();
    assertEquals("string-ext4", typeQName.getLocalName());
    assertEquals(BAR, typeQName.getModule());
    assertEquals(Optional.empty(), type.getUnits());
    assertEquals(Optional.empty(), type.getDefaultValue());
    List<PatternConstraint> patterns = type.getPatternConstraints();
    assertEquals(1, patterns.size());
    PatternConstraint pattern = patterns.iterator().next();
    assertEquals("^(?:[e-z]*)$", pattern.getJavaPatternString());
    assertEquals(1, type.getLengthConstraint().get().getAllowedRanges().asRanges().size());

    final StringTypeDefinition baseType1 = type.getBaseType();
    final QName baseType1QName = baseType1.getQName();
    assertEquals("string-ext3", baseType1QName.getLocalName());
    assertEquals(BAR, baseType1QName.getModule());
    assertEquals(Optional.empty(), baseType1.getUnits());
    assertEquals(Optional.empty(), baseType1.getDefaultValue());
    patterns = baseType1.getPatternConstraints();
    assertEquals(1, patterns.size());
    pattern = patterns.iterator().next();
    assertEquals("^(?:[b-u]*)$", pattern.getJavaPatternString());
    assertEquals(1, baseType1.getLengthConstraint().get().getAllowedRanges().asRanges().size());

    final StringTypeDefinition baseType2 = baseType1.getBaseType();
    final QName baseType2QName = baseType2.getQName();
    assertEquals("string-ext2", baseType2QName.getLocalName());
    assertEquals(BAR, baseType2QName.getModule());
    assertEquals(Optional.empty(), baseType2.getUnits());
    assertEquals(Optional.empty(), baseType2.getDefaultValue());
    assertTrue(baseType2.getPatternConstraints().isEmpty());
    final RangeSet<Integer> baseType2Lengths = baseType2.getLengthConstraint().get().getAllowedRanges();
    assertEquals(1, baseType2Lengths.asRanges().size());
    Range<Integer> length = baseType2Lengths.span();
    assertEquals(6, length.lowerEndpoint().intValue());
    assertEquals(10, length.upperEndpoint().intValue());

    final StringTypeDefinition baseType3 = baseType2.getBaseType();
    assertEquals(QName.create(BAR, "string-ext1"), baseType3.getQName());
    assertEquals(Optional.empty(), baseType3.getUnits());
    assertEquals(Optional.empty(), baseType3.getDefaultValue());
    patterns = baseType3.getPatternConstraints();
    assertEquals(1, patterns.size());
    pattern = patterns.iterator().next();
    assertEquals("^(?:[a-k]*)$", pattern.getJavaPatternString());
    final RangeSet<Integer> baseType3Lengths = baseType3.getLengthConstraint().get().getAllowedRanges();
    assertEquals(1, baseType3Lengths.asRanges().size());
    length = baseType3Lengths.span();
    assertEquals(5, length.lowerEndpoint().intValue());
    assertEquals(11, length.upperEndpoint().intValue());

    assertEquals(BaseTypes.stringType(), baseType3.getBaseType());
}
 
Example 14
Source File: YangParserTest.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testTypedefRangesResolving() throws ParseException {
    final LeafSchemaNode int32Leaf = (LeafSchemaNode) foo.getDataChildByName(QName.create(foo.getQNameModule(),
        "int32-leaf"));

    final Int32TypeDefinition leafType = (Int32TypeDefinition) int32Leaf.getType();
    assertEquals(QName.create(FOO, "int32-ext2"), leafType.getQName());
    assertEquals(Optional.of("mile"), leafType.getUnits());
    assertEquals(Optional.of("11"), leafType.getDefaultValue());

    final RangeSet<? extends Number> rangeset = leafType.getRangeConstraint().get().getAllowedRanges();
    final Set<? extends Range<? extends Number>> ranges = rangeset.asRanges();
    assertEquals(1, ranges.size());

    final Range<? extends Number> range = ranges.iterator().next();
    assertEquals(12, range.lowerEndpoint().intValue());
    assertEquals(20, range.upperEndpoint().intValue());

    final Int32TypeDefinition firstBaseType = leafType.getBaseType();
    assertEquals(QName.create(BAR, "int32-ext2"), firstBaseType.getQName());
    assertEquals(Optional.of("mile"), firstBaseType.getUnits());
    assertEquals(Optional.of("11"), firstBaseType.getDefaultValue());

    final RangeSet<? extends Number> firstRangeset = firstBaseType.getRangeConstraint().get().getAllowedRanges();
    final Set<? extends Range<? extends Number>> baseRanges = firstRangeset.asRanges();
    assertEquals(2, baseRanges.size());

    final Iterator<? extends Range<? extends Number>> it = baseRanges.iterator();
    final Range<? extends Number> baseTypeRange1 = it.next();
    assertEquals(3, baseTypeRange1.lowerEndpoint().intValue());
    assertEquals(9, baseTypeRange1.upperEndpoint().intValue());
    final Range<? extends Number> baseTypeRange2 = it.next();
    assertEquals(11, baseTypeRange2.lowerEndpoint().intValue());
    assertEquals(20, baseTypeRange2.upperEndpoint().intValue());

    final Int32TypeDefinition secondBaseType = firstBaseType.getBaseType();
    final QName baseQName = secondBaseType.getQName();
    assertEquals("int32-ext1", baseQName.getLocalName());
    assertEquals(BAR, baseQName.getModule());
    assertEquals(Optional.empty(), secondBaseType.getUnits());
    assertEquals(Optional.empty(), secondBaseType.getDefaultValue());

    final Set<? extends Range<? extends Number>> secondRanges = secondBaseType.getRangeConstraint().get()
            .getAllowedRanges().asRanges();
    assertEquals(1, secondRanges.size());
    final Range<? extends Number> secondRange = secondRanges.iterator().next();
    assertEquals(2, secondRange.lowerEndpoint().intValue());
    assertEquals(20, secondRange.upperEndpoint().intValue());

    assertEquals(BaseTypes.int32Type(), secondBaseType.getBaseType());
}
 
Example 15
Source File: Bug5396Test.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void test() throws Exception {
    SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5396");
    assertNotNull(context);

    QName root = QName.create("foo", "root");
    QName myLeaf2 = QName.create("foo", "my-leaf2");

    SchemaPath schemaPath = SchemaPath.create(true, root, myLeaf2);
    SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
    assertTrue(findDataSchemaNode instanceof LeafSchemaNode);

    LeafSchemaNode leaf2 = (LeafSchemaNode) findDataSchemaNode;
    TypeDefinition<?> type = leaf2.getType();
    assertTrue(type instanceof UnionTypeDefinition);

    UnionTypeDefinition union = (UnionTypeDefinition) type;
    List<TypeDefinition<?>> types = union.getTypes();

    assertEquals(4, types.size());

    TypeDefinition<?> type0 = types.get(0);
    TypeDefinition<?> type1 = types.get(1);
    TypeDefinition<?> type2 = types.get(2);
    TypeDefinition<?> type3 = types.get(3);

    assertFalse(type0.equals(type1));
    assertFalse(type0.equals(type2));
    assertFalse(type0.equals(type3));

    assertTrue(type0 instanceof StringTypeDefinition);
    assertTrue(type1 instanceof StringTypeDefinition);
    assertTrue(type2 instanceof StringTypeDefinition);
    assertTrue(type3 instanceof StringTypeDefinition);

    StringTypeDefinition stringType0 = (StringTypeDefinition) type0;
    StringTypeDefinition stringType1 = (StringTypeDefinition) type1;
    StringTypeDefinition stringType2 = (StringTypeDefinition) type2;
    StringTypeDefinition stringType3 = (StringTypeDefinition) type3;

    final List<PatternConstraint> patternConstraints0 = stringType0.getPatternConstraints();
    final List<PatternConstraint> patternConstraints1 = stringType1.getPatternConstraints();
    final List<PatternConstraint> patternConstraints2 = stringType2.getPatternConstraints();
    final List<PatternConstraint> patternConstraints3 = stringType3.getPatternConstraints();

    assertEquals(1, patternConstraints0.size());
    assertEquals(1, patternConstraints1.size());
    assertEquals(1, patternConstraints2.size());
    assertEquals(1, patternConstraints3.size());

    final PatternConstraint patternConstraint0 = patternConstraints0.get(0);
    final PatternConstraint patternConstraint1 = patternConstraints1.get(0);
    final PatternConstraint patternConstraint2 = patternConstraints2.get(0);
    final PatternConstraint patternConstraint3 = patternConstraints3.get(0);

    assertEquals("^(?:dp[0-9]+o[0-9]+(d[0-9]+)?)$", patternConstraint0.getJavaPatternString());
    assertEquals("^(?:dp[0-9]+s[0-9]+(f[0-9]+)?(d[0-9]+)?)$", patternConstraint1.getJavaPatternString());
    assertEquals("^(?:dp[0-9]+(P[0-9]+)?p[0-9]{1,3}s[0-9]{1,3}(f[0-9]+)?(d[0-9]+)?)$",
            patternConstraint2.getJavaPatternString());
    assertEquals("^(?:dp[0-9]+p[0-9]+p[0-9]+)$", patternConstraint3.getJavaPatternString());
}
 
Example 16
Source File: Bug4623Test.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testStringTypeWithUnknownSchemaNodeOnTheStartOfTypeDefinition() throws Exception {
    // given
    final File extdef = new File(getClass().getResource("/bugs/bug4623/extension-def.yang").toURI());
    final File stringWithExt = new File(getClass().getResource("/bugs/bug4623/string-with-ext.yang").toURI());

    // when
    final SchemaContext schemaContext = TestUtils.parseYangSources(extdef, stringWithExt);

    final LeafSchemaNode leaf = (LeafSchemaNode) typesModule(schemaContext).getDataChildByName(
            QName.create(URI.create("urn:custom.types.demo"), "leaf-unknown-length-pattern"));

    // then
    Assert.assertNotNull(leaf);

    final TypeDefinition<?> type = leaf.getType();
    Assert.assertNotNull(type);
    final Collection<? extends UnknownSchemaNode> unknownSchemaNodes = type.getUnknownSchemaNodes();
    Assert.assertNotNull(unknownSchemaNodes);
    Assert.assertFalse(unknownSchemaNodes.size() == 0);

    final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
    Assert.assertEquals(unknownSchemaNode.getNodeParameter(), "unknown");
    Assert.assertEquals(unknownSchemaNode.getNodeType().getModule().getNamespace().toString(),
        "urn:simple.extension.typedefs");

    final LengthConstraint lengthConstraints =
            ((StringTypeDefinition) type).getLengthConstraint().get();
    final List<PatternConstraint> patternConstraints = ((StringTypeDefinition) type).getPatternConstraints();

    Assert.assertNotNull(lengthConstraints);
    Assert.assertNotNull(patternConstraints);
    Assert.assertEquals(1, lengthConstraints.getAllowedRanges().asRanges().size());
    Assert.assertFalse(patternConstraints.size() == 0);

    final Range<Integer> lengthConstraint = lengthConstraints.getAllowedRanges().span();
    Assert.assertEquals(Integer.valueOf(2), lengthConstraint.lowerEndpoint());
    Assert.assertEquals(Integer.valueOf(10), lengthConstraint.upperEndpoint());

    final PatternConstraint patternConstraint = patternConstraints.get(0);
    Assert.assertEquals(patternConstraint.getRegularExpressionString(), "[0-9a-fA-F]");
}
 
Example 17
Source File: Bug4623Test.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testStringTypeWithUnknownSchemaNodeAtTheEndOfTypeDefinition() throws Exception {
    // given
    final File extdef = new File(getClass().getResource("/bugs/bug4623/extension-def.yang").toURI());
    final File stringWithExt = new File(getClass().getResource("/bugs/bug4623/string-with-ext.yang").toURI());

    // when
    final SchemaContext schemaContext = TestUtils.parseYangSources(extdef, stringWithExt);

    final LeafSchemaNode leaf = (LeafSchemaNode) typesModule(schemaContext).getDataChildByName(
        QName.create(URI.create("urn:custom.types.demo"), "leaf-length-pattern-unknown"));

    // then
    Assert.assertNotNull(leaf);

    final TypeDefinition<?> type = leaf.getType();
    Assert.assertNotNull(type);
    final Collection<? extends UnknownSchemaNode> unknownSchemaNodes = type.getUnknownSchemaNodes();
    Assert.assertNotNull(unknownSchemaNodes);
    Assert.assertFalse(unknownSchemaNodes.size() == 0);

    final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.iterator().next();
    Assert.assertEquals(unknownSchemaNode.getNodeParameter(), "unknown");
    Assert.assertEquals(unknownSchemaNode.getNodeType().getModule().getNamespace().toString(),
        "urn:simple.extension.typedefs");

    final LengthConstraint lengthConstraint = ((StringTypeDefinition) type).getLengthConstraint().get();
    final List<PatternConstraint> patternConstraints = ((StringTypeDefinition) type).getPatternConstraints();

    Assert.assertNotNull(lengthConstraint);
    Assert.assertNotNull(patternConstraints);
    Assert.assertFalse(lengthConstraint.getAllowedRanges().isEmpty());
    Assert.assertFalse(patternConstraints.isEmpty());

    final Range<Integer> span = lengthConstraint.getAllowedRanges().span();
    Assert.assertEquals(Integer.valueOf(2), span.lowerEndpoint());
    Assert.assertEquals(Integer.valueOf(10), span.upperEndpoint());

    final PatternConstraint patternConstraint = patternConstraints.get(0);
    Assert.assertEquals(patternConstraint.getRegularExpressionString(), "[0-9a-fA-F]");
}
 
Example 18
Source File: TypedefConstraintsTest.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void decimalRangeConstraintsTest() throws Exception {
    final SchemaContext context = StmtTestUtils.parseYangSources("/stmt-test/constraints");

    assertNotNull(context);

    final Collection<? extends TypeDefinition<?>> typeDefinitions = context.getTypeDefinitions();
    assertNotNull(typeDefinitions);
    assertEquals(1, typeDefinitions.size());

    final TypeDefinition<?> myDecimal = typeDefinitions.iterator().next();

    assertNotNull(myDecimal);
    assertTrue(myDecimal instanceof DecimalTypeDefinition);

    final Set<? extends Range<?>> rangeConstraints = ((DecimalTypeDefinition) myDecimal).getRangeConstraint()
            .get().getAllowedRanges().asRanges();

    assertNotNull(rangeConstraints);
    assertEquals(1, rangeConstraints.size());

    final DataSchemaNode dataNode = context.getDataChildByName(QName.create("urn:opendaylight.foo", "2013-10-08",
        "id-decimal64"));
    assertNotNull(dataNode);
    assertTrue(dataNode instanceof LeafSchemaNode);

    final LeafSchemaNode leafDecimal = (LeafSchemaNode) dataNode;
    final TypeDefinition<?> type = leafDecimal.getType();

    assertTrue(type instanceof DecimalTypeDefinition);
    final DecimalTypeDefinition decType = (DecimalTypeDefinition) type;

    final Set<? extends Range<?>> decRangeConstraints = decType.getRangeConstraint().get().getAllowedRanges()
            .asRanges();

    assertEquals(1, decRangeConstraints.size());

    final Range<?> range = decRangeConstraints.iterator().next();
    assertEquals(new BigDecimal(1.5), range.lowerEndpoint());
    assertEquals(new BigDecimal(5.5), range.upperEndpoint());

    assertTrue(decType.getQName().getModule().equals(leafDecimal.getQName().getModule()));
    assertTrue(decType.getQName().getLocalName().equals(BaseTypes.DECIMAL64_QNAME.getLocalName()));
    assertNull(decType.getBaseType());
}
 
Example 19
Source File: Bug6887Test.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testRestrictedBits() throws Exception {
    final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6887/bar.yang");
    assertNotNull(schemaContext);

    final Module bar = schemaContext.findModule("bar", Revision.of("2017-02-02")).get();
    final LeafSchemaNode myBitsLeaf = (LeafSchemaNode) bar.getDataChildByName(
            QName.create(bar.getQNameModule(), "my-bits-leaf"));
    assertNotNull(myBitsLeaf);

    BitsTypeDefinition bitsType = (BitsTypeDefinition) myBitsLeaf.getType();

    Collection<? extends Bit> bits = bitsType.getBits();
    assertEquals(2, bits.size());
    Bit bitB = createBit("bit-b", 2);
    Bit bitC = createBit("bit-c", 3);
    assertContainsBits(bits, bitB, bitC);

    bitsType = bitsType.getBaseType();
    bits = bitsType.getBits();
    assertEquals(3, bits.size());
    bitB = createBit("bit-b", 2);
    bitC = createBit("bit-c", 3);
    Bit bitD = createBit("bit-d", 4);
    assertContainsBits(bits, bitB, bitC, bitD);

    bitsType = bitsType.getBaseType();
    bits = bitsType.getBits();
    assertEquals(4, bits.size());
    final Bit bitA = createBit("bit-a", 1);
    bitB = createBit("bit-b", 2);
    bitC = createBit("bit-c", 3);
    bitD = createBit("bit-d", 4);
    assertContainsBits(bits, bitA, bitB, bitC, bitD);

    final LeafSchemaNode myBitsLeaf2 = (LeafSchemaNode) bar.getDataChildByName(
            QName.create(bar.getQNameModule(), "my-bits-leaf-2"));
    assertNotNull(myBitsLeaf2);

    bitsType = (BitsTypeDefinition) myBitsLeaf2.getType();
    bits = bitsType.getBits();
    assertEquals(3, bits.size());
    bitB = createBit("bit-b", 2);
    bitC = createBit("bit-c", 3);
    bitD = createBit("bit-d", 4);
    assertContainsBits(bits, bitB, bitC, bitD);
}
 
Example 20
Source File: LeafrefStatementTest.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
private static void assertRequireInstanceInLeafref(final LeafSchemaNode leaf, final boolean requireInstance) {
    final LeafrefTypeDefinition leafrefTypeDefnition = (LeafrefTypeDefinition) leaf.getType();
    assertEquals(requireInstance, leafrefTypeDefnition.requireInstance());
}