Search in sources :

Example 6 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project mdsal by opendaylight.

the class AugmentedTypeTest method augmentedAbstractTopologyTest.

@Test
public void augmentedAbstractTopologyTest() {
    final EffectiveModelContext context = YangParserTestUtils.parseYangResources(AugmentedTypeTest.class, "/augment-test-models/abstract-topology@2013-02-08.yang", "/augment-test-models/augment-abstract-topology@2013-05-03.yang", "/augment-test-models/augment-network-link-attributes@2013-05-03.yang", "/augment-test-models/augment-topology-tunnels@2013-05-03.yang", "/augment-test-models/ietf-interfaces@2012-11-15.yang");
    assertNotNull("Schema Context is null", context);
    final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(context);
    assertEquals(31, genTypes.size());
    GeneratedTransferObject gtInterfaceKey = null;
    GeneratedType gtInterface = null;
    GeneratedType gtTunnel = null;
    GeneratedTransferObject gtTunnelKey = null;
    GeneratedType gtNetworkLink2 = null;
    for (final GeneratedType type : genTypes) {
        if (!type.getPackageName().contains("augment._abstract.topology")) {
            continue;
        }
        if (type.getName().equals("InterfaceKey")) {
            gtInterfaceKey = (GeneratedTransferObject) type;
        } else if (type.getName().equals("Interface")) {
            gtInterface = type;
        } else if (type.getName().equals("Tunnel")) {
            gtTunnel = type;
        } else if (type.getName().equals("TunnelKey")) {
            gtTunnelKey = (GeneratedTransferObject) type;
        } else if (type.getName().equals("NetworkLink2")) {
            gtNetworkLink2 = type;
        }
    }
    // 'Interface
    assertNotNull("gtInterface is null", gtInterface);
    final List<MethodSignature> gtInterfaceMethods = gtInterface.getMethodDefinitions();
    assertNotNull("gtInterfaceMethods is null", gtInterfaceMethods);
    MethodSignature getIfcKeyMethod = null;
    for (final MethodSignature method : gtInterfaceMethods) {
        if (BindingMapping.IDENTIFIABLE_KEY_NAME.equals(method.getName())) {
            getIfcKeyMethod = method;
            break;
        }
    }
    assertNotNull("getIfcKeyMethod is null", getIfcKeyMethod);
    assertThat(getIfcKeyMethod.getReturnType(), instanceOf(GeneratedTransferObject.class));
    assertEquals(JavaTypeName.create("org.opendaylight.yang.gen.v1.urn.model.augment._abstract.topology.rev130503.topology.interfaces", "InterfaceKey"), getIfcKeyMethod.getReturnType().getIdentifier());
    MethodSignature getHigherLayerIfMethod = null;
    for (final MethodSignature method : gtInterfaceMethods) {
        if (method.getName().equals("getHigherLayerIf")) {
            getHigherLayerIfMethod = method;
            break;
        }
    }
    assertNotNull("getHigherLayerIf method is null", getHigherLayerIfMethod);
    assertEquals(Types.setTypeFor(Types.STRING), getHigherLayerIfMethod.getReturnType());
    // 'InterfaceKey'
    assertNotNull("InterfaceKey is null", gtInterfaceKey);
    final List<GeneratedProperty> properties = gtInterfaceKey.getProperties();
    assertNotNull("properties is null", properties);
    GeneratedProperty gtInterfaceId = null;
    for (final GeneratedProperty property : properties) {
        if (property.getName().equals("interfaceId")) {
            gtInterfaceId = property;
            break;
        }
    }
    assertNotNull("interfaceId is null", gtInterfaceId);
    assertEquals(Types.STRING, gtInterfaceId.getReturnType());
    // 'Tunnel'
    assertNotNull("Tunnel is null", gtTunnel);
    final List<MethodSignature> tunnelMethods = gtTunnel.getMethodDefinitions();
    assertNotNull("Tunnel methods are null", tunnelMethods);
    MethodSignature getTunnelKeyMethod = null;
    for (MethodSignature method : tunnelMethods) {
        if (BindingMapping.IDENTIFIABLE_KEY_NAME.equals(method.getName())) {
            getTunnelKeyMethod = method;
            break;
        }
    }
    assertNotNull("getKey method of Tunnel is null", getTunnelKeyMethod);
    var retType = getTunnelKeyMethod.getReturnType();
    assertThat(retType, instanceOf(GeneratedTransferObject.class));
    assertEquals(JavaTypeName.create("org.opendaylight.yang.gen.v1.urn.model.augment._abstract.topology.rev130503" + ".topology.network.links.network.link.tunnels", "TunnelKey"), retType.getIdentifier());
    // 'TunnelKey'
    assertNotNull("TunnelKey is null", gtTunnelKey);
    final List<GeneratedProperty> tunnelKeyProperties = gtTunnelKey.getProperties();
    assertNotNull("TunnelKey properties are null", tunnelKeyProperties);
    GeneratedProperty gtTunnelId = null;
    for (final GeneratedProperty property : tunnelKeyProperties) {
        if (property.getName().equals("tunnelId")) {
            gtTunnelId = property;
        }
    }
    assertNotNull("tunnelId is null", gtTunnelId);
    assertEquals(Types.typeForClass(Integer.class), gtTunnelId.getReturnType());
    // 'NetworkLink2'
    assertNotNull("NetworkLink2 is null", gtNetworkLink2);
    final List<MethodSignature> networkLink2Methods = gtNetworkLink2.getMethodDefinitions();
    assertNotNull("NetworkLink2 methods are null", networkLink2Methods);
    MethodSignature getIfcMethod = null;
    for (MethodSignature method : networkLink2Methods) {
        if (method.getName().equals("getInterface")) {
            getIfcMethod = method;
            break;
        }
    }
    assertNotNull("getInterface method is null", getIfcMethod);
    assertEquals(Types.STRING, getIfcMethod.getReturnType());
}
Also used : MethodSignature(org.opendaylight.mdsal.binding.model.api.MethodSignature) GeneratedType(org.opendaylight.mdsal.binding.model.api.GeneratedType) GeneratedTransferObject(org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject) GeneratedProperty(org.opendaylight.mdsal.binding.model.api.GeneratedProperty) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Test(org.junit.Test)

Example 7 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project mdsal by opendaylight.

the class Bug1862Test method restrictedTypedefTransformationTest.

@Test
public void restrictedTypedefTransformationTest() {
    final EffectiveModelContext context = YangParserTestUtils.parseYangResources(Bug1862Test.class, "/base-yang-types.yang", "/test-type-provider.yang");
    final List<GeneratedType> types = DefaultBindingGenerator.generateFor(context);
    assertEquals(42, types.size());
    final MethodSignature fooGetter = types.stream().filter(type -> type.getFullyQualifiedName().equals("org.opendaylight.yang.gen.v1.urn.opendaylight.org.test.type.provider.model.rev140912.Foo")).findFirst().orElseThrow().getMethodDefinitions().stream().filter(method -> method.getName().equals("getBug1862RestrictedTypedef")).findFirst().orElseThrow();
    final Type returnType = fooGetter.getReturnType();
    assertThat(returnType, instanceOf(GeneratedTransferObject.class));
    final RangeSet<?> range = ((GeneratedTransferObject) returnType).getRestrictions().getRangeConstraint().orElseThrow().getAllowedRanges();
    assertEquals(ImmutableRangeSet.of(Range.closed((byte) 1, (byte) 100)), range);
}
Also used : RangeSet(com.google.common.collect.RangeSet) Type(org.opendaylight.mdsal.binding.model.api.Type) MethodSignature(org.opendaylight.mdsal.binding.model.api.MethodSignature) Range(com.google.common.collect.Range) Test(org.junit.Test) YangParserTestUtils(org.opendaylight.yangtools.yang.test.util.YangParserTestUtils) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) List(java.util.List) GeneratedTransferObject(org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ImmutableRangeSet(com.google.common.collect.ImmutableRangeSet) Assert.assertEquals(org.junit.Assert.assertEquals) GeneratedType(org.opendaylight.mdsal.binding.model.api.GeneratedType) Type(org.opendaylight.mdsal.binding.model.api.Type) GeneratedType(org.opendaylight.mdsal.binding.model.api.GeneratedType) MethodSignature(org.opendaylight.mdsal.binding.model.api.MethodSignature) GeneratedType(org.opendaylight.mdsal.binding.model.api.GeneratedType) GeneratedTransferObject(org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Test(org.junit.Test)

Example 8 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project mdsal by opendaylight.

the class Bug4621Test method testMissingLeafrefTarget.

@Test
public void testMissingLeafrefTarget() {
    final EffectiveModelContext context = YangParserTestUtils.parseYangResource("/bug4621.yang");
    final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> DefaultBindingGenerator.generateFor(context));
    assertEquals("Failed to find leafref target /foo:neighbor/foo:mystring1", ex.getMessage());
}
Also used : EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Test(org.junit.Test)

Example 9 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project mdsal by opendaylight.

the class LeafrefResolutionTest method testLeafRefRelativeSelfReference.

@Test
public void testLeafRefRelativeSelfReference() {
    final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResource("/leafref-relative-invalid.yang");
    final IllegalArgumentException iae = assertThrows(IllegalArgumentException.class, () -> DefaultBindingGenerator.generateFor(schemaContext));
    assertEquals("Effective model contains self-referencing leaf (urn:xml:ns:yang:lrr?revision=2015-02-25)neighbor-id", iae.getMessage());
}
Also used : EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Test(org.junit.Test)

Example 10 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project mdsal by opendaylight.

the class BuilderGeneratorTest method builderTemplateGenerateToEqualsComparingOrderTest.

@Test
public void builderTemplateGenerateToEqualsComparingOrderTest() {
    final EffectiveModelContext context = YangParserTestUtils.parseYangResource("/test-types.yang");
    final List<GeneratedType> types = new DefaultBindingGenerator().generateTypes(context);
    assertEquals(29, types.size());
    final BuilderTemplate bt = BuilderGenerator.templateForType(types.stream().filter(t -> t.getName().equals("Nodes")).findFirst().orElseThrow());
    final List<String> sortedProperties = bt.properties.stream().sorted(ByTypeMemberComparator.getInstance()).map(BuilderGeneratedProperty::getName).collect(Collectors.toList());
    assertEquals(List.of(// numeric types (boolean, byte, short, int, long, biginteger, bigdecimal), identityrefs, empty
    "id16", "id16Def", "id32", "id32Def", "id64", "id64Def", "id8", "id8Def", "idBoolean", "idBooleanDef", "idDecimal64", "idDecimal64Def", "idEmpty", "idEmptyDef", "idIdentityref", "idIdentityrefDef", "idLeafref", "idLeafrefDef", "idU16", "idU16Def", "idU32", "idU32Def", "idU64", "idU64Def", "idU8", "idU8Def", // string, binary, bits
    "idBinary", "idBinaryDef", "idBits", "idBitsDef", "idGroupLeafString", "idLeafrefContainer1", "idLeafrefContainer1Def", "idString", "idStringDef", // instance identifier
    "idInstanceIdentifier", "idInstanceIdentifierDef", // other types
    "idContainer1", "idContainer2", "idEnumeration", "idEnumerationDef", "idGroupContainer", "idList", "idUnion", "idUnionDef"), sortedProperties);
}
Also used : Mockito.doCallRealMethod(org.mockito.Mockito.doCallRealMethod) Type(org.opendaylight.mdsal.binding.model.api.Type) MethodSignature(org.opendaylight.mdsal.binding.model.api.MethodSignature) DefaultBindingGenerator(org.opendaylight.mdsal.binding.generator.impl.DefaultBindingGenerator) Test(org.junit.Test) YangParserTestUtils(org.opendaylight.yangtools.yang.test.util.YangParserTestUtils) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Mockito.spy(org.mockito.Mockito.spy) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) JavaTypeName(org.opendaylight.mdsal.binding.model.api.JavaTypeName) ValueMechanics(org.opendaylight.mdsal.binding.model.api.MethodSignature.ValueMechanics) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) GeneratedType(org.opendaylight.mdsal.binding.model.api.GeneratedType) GeneratedType(org.opendaylight.mdsal.binding.model.api.GeneratedType) DefaultBindingGenerator(org.opendaylight.mdsal.binding.generator.impl.DefaultBindingGenerator) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Test(org.junit.Test)

Aggregations

EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)182 Test (org.junit.Test)99 Module (org.opendaylight.yangtools.yang.model.api.Module)37 QName (org.opendaylight.yangtools.yang.common.QName)29 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)28 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)26 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)24 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)24 ContainerSchemaNode (org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode)21 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)18 QNameModule (org.opendaylight.yangtools.yang.common.QNameModule)16 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)16 NormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter)16 IRSchemaSource (org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource)14 DOMMountPoint (org.opendaylight.mdsal.dom.api.DOMMountPoint)13 RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)13 RpcDefinition (org.opendaylight.yangtools.yang.model.api.RpcDefinition)13 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)11 IOException (java.io.IOException)9 Collection (java.util.Collection)9