Search in sources :

Example 6 with Submodule

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

the class EffectiveModulesAndSubmodulesTest method modulesAndSubmodulesSimpleReferencesTest.

@Test
public void modulesAndSubmodulesSimpleReferencesTest() throws ReactorException {
    final SchemaContext result = RFC7950Reactors.defaultReactor().newBuild().addSources(ROOT_MODULE, IMPORTED_MODULE, SUBMODULE_1, SUBMODULE_2, SUBMODULE_TO_SUBMODULE_1).buildEffective();
    assertNotNull(result);
    final Collection<? extends Module> modules = result.getModules();
    assertNotNull(modules);
    assertEquals(2, modules.size());
    Module root = null;
    Module imported = null;
    for (final Module module : modules) {
        switch(module.getName()) {
            case "root-module":
                root = module;
                break;
            case "imported-module":
                imported = module;
                break;
            default:
        }
        printReferences(module, false, "");
        printChilds(module.getChildNodes(), "      ");
    }
    assertNotNull(root);
    assertNotNull(imported);
    final Collection<? extends DataSchemaNode> rootChildNodes = root.getChildNodes();
    final Collection<? extends DataSchemaNode> importedChildNodes = imported.getChildNodes();
    assertNotNull(rootChildNodes);
    assertNotNull(importedChildNodes);
    assertEquals(3, rootChildNodes.size());
    assertEquals(1, importedChildNodes.size());
    final Collection<? extends Submodule> rootSubmodules = root.getSubmodules();
    final Collection<? extends Submodule> importedSubmodules = imported.getSubmodules();
    assertNotNull(rootSubmodules);
    assertNotNull(importedSubmodules);
    assertEquals(2, rootSubmodules.size());
    assertEquals(0, importedSubmodules.size());
    Submodule sub1 = null;
    Submodule sub2 = null;
    for (final Submodule rootSubmodule : rootSubmodules) {
        switch(rootSubmodule.getName()) {
            case "submodule-1":
                sub1 = rootSubmodule;
                break;
            case "submodule-2":
                sub2 = rootSubmodule;
                break;
            default:
        }
    }
    assertNotNull(sub1);
    assertNotNull(sub2);
    assertEquals(ROOT, sub1.getQNameModule());
    assertEquals(ROOT, sub2.getQNameModule());
    final Collection<? extends DataSchemaNode> sub1ChildNodes = sub1.getChildNodes();
    final Collection<? extends DataSchemaNode> sub2ChildNodes = sub2.getChildNodes();
    assertNotNull(sub1ChildNodes);
    assertNotNull(sub2ChildNodes);
    assertEquals(1, sub1ChildNodes.size());
    assertEquals(1, sub2ChildNodes.size());
    final Collection<? extends Submodule> sub1Submodules = sub1.getSubmodules();
    final Collection<? extends Submodule> sub2Submodules = sub2.getSubmodules();
    assertNotNull(sub1Submodules);
    assertNotNull(sub2Submodules);
    assertEquals(1, sub1Submodules.size());
    assertEquals(0, sub2Submodules.size());
    Submodule sub1Submodule = null;
    for (final Submodule submodule : sub1Submodules) {
        switch(submodule.getName()) {
            case "submodule-to-submodule-1":
                sub1Submodule = submodule;
                break;
            default:
        }
    }
    assertNotNull(sub1Submodule);
    assertEquals(ROOT, sub1Submodule.getQNameModule());
    final Collection<? extends DataSchemaNode> sub1SubmoduleChildNodes = sub1Submodule.getChildNodes();
    assertNotNull(sub1SubmoduleChildNodes);
    assertEquals(1, sub1SubmoduleChildNodes.size());
    final Collection<? extends Submodule> sub1SubmoduleSubmodules = sub1Submodule.getSubmodules();
    assertNotNull(sub1SubmoduleSubmodules);
    assertEquals(0, sub1SubmoduleSubmodules.size());
    findModulesSubTest(result, root, imported);
    getDataChildByNameSubTest(result, root);
}
Also used : SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) Submodule(org.opendaylight.yangtools.yang.model.api.Submodule) Module(org.opendaylight.yangtools.yang.model.api.Module) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule) Test(org.junit.Test)

Example 7 with Submodule

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

the class DeclaredStatementsTest method testDeclaredModuleAndSubmodule.

@Test
public void testDeclaredModuleAndSubmodule() throws ReactorException {
    final StatementStreamSource parentModule = sourceForResource("/declared-statements-test/parent-module-declared-test.yang");
    final StatementStreamSource childModule = sourceForResource("/declared-statements-test/child-module-declared-test.yang");
    final SchemaContext schemaContext = StmtTestUtils.parseYangSources(parentModule, childModule);
    assertNotNull(schemaContext);
    final Module testModule = schemaContext.findModules("parent-module-declared-test").iterator().next();
    assertNotNull(testModule);
    final ModuleStatement moduleStatement = ((ModuleEffectiveStatement) testModule).getDeclared();
    assertNotNull(moduleStatement.argument());
    final YangVersionStatement moduleStatementYangVersion = moduleStatement.getYangVersion();
    assertNotNull(moduleStatementYangVersion);
    assertNotNull(moduleStatementYangVersion.argument());
    final NamespaceStatement moduleStatementNamspace = moduleStatement.getNamespace();
    assertNotNull(moduleStatementNamspace);
    assertNotNull(moduleStatementNamspace.argument());
    final PrefixStatement moduleStatementPrefix = moduleStatement.getPrefix();
    assertNotNull(moduleStatementPrefix);
    assertNotNull(moduleStatementPrefix.argument());
    assertEquals(1, moduleStatement.getIncludes().size());
    final IncludeStatement includeStatement = moduleStatement.getIncludes().iterator().next();
    assertEquals("child-module-declared-test", includeStatement.argument());
    final Collection<? extends Submodule> submodules = testModule.getSubmodules();
    assertNotNull(submodules);
    assertEquals(1, submodules.size());
    final Submodule submodule = submodules.iterator().next();
    final SubmoduleStatement submoduleStatement = ((SubmoduleEffectiveStatement) submodule).getDeclared();
    assertNotNull(submoduleStatement.argument());
    final YangVersionStatement submoduleStatementYangVersion = submoduleStatement.getYangVersion();
    assertNotNull(submoduleStatementYangVersion);
    final BelongsToStatement belongsToStatement = submoduleStatement.getBelongsTo();
    assertNotNull(belongsToStatement);
    assertNotNull(belongsToStatement.argument());
    assertNotNull(belongsToStatement.getPrefix());
}
Also used : NamespaceStatement(org.opendaylight.yangtools.yang.model.api.stmt.NamespaceStatement) StatementStreamSource(org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource) ModuleStatement(org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement) Submodule(org.opendaylight.yangtools.yang.model.api.Submodule) BelongsToStatement(org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement) ModuleEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement) PrefixStatement(org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement) YangVersionStatement(org.opendaylight.yangtools.yang.model.api.stmt.YangVersionStatement) SubmoduleEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement) IncludeStatement(org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement) SubmoduleStatement(org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) Module(org.opendaylight.yangtools.yang.model.api.Module) Test(org.junit.Test)

Example 8 with Submodule

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

the class IncludedStmtsTest method submoduleNamespaceTest.

@Test
public void submoduleNamespaceTest() {
    final Module testModule = result.findModules("root-module").iterator().next();
    assertNotNull(testModule);
    final Submodule subModule = testModule.getSubmodules().iterator().next();
    assertEquals("urn:opendaylight.org/root-module", subModule.getNamespace().toString());
}
Also used : Submodule(org.opendaylight.yangtools.yang.model.api.Submodule) Module(org.opendaylight.yangtools.yang.model.api.Module) Test(org.junit.Test)

Example 9 with Submodule

use of org.opendaylight.yangtools.yang.model.api.Submodule in project netconf by opendaylight.

the class NetconfDeviceSimulator method parseSchemasToModuleCapabilities.

private Set<Capability> parseSchemasToModuleCapabilities(final SharedSchemaRepository consumer) {
    final Set<SourceIdentifier> loadedSources = new HashSet<>();
    consumer.registerSchemaSourceListener(TextToIRTransformer.create(consumer, consumer));
    consumer.registerSchemaSourceListener(new SchemaSourceListener() {

        @Override
        public void schemaSourceEncountered(final SchemaSourceRepresentation schemaSourceRepresentation) {
        }

        @Override
        public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> potentialSchemaSources) {
            for (final PotentialSchemaSource<?> potentialSchemaSource : potentialSchemaSources) {
                loadedSources.add(potentialSchemaSource.getSourceIdentifier());
            }
        }

        @Override
        public void schemaSourceUnregistered(final PotentialSchemaSource<?> potentialSchemaSource) {
        }
    });
    if (configuration.getSchemasDir() != null) {
        LOG.info("Loading models from directory.");
        final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(consumer, YangTextSchemaSource.class, configuration.getSchemasDir());
        consumer.registerSchemaSourceListener(cache);
    } else if (configuration.getModels() != null) {
        LOG.info("Loading models from classpath.");
        final SchemaSourceCache<YangTextSchemaSource> cache = new SchemaSourceCache<>(consumer, YangTextSchemaSource.class, configuration.getModels());
        consumer.registerSchemaSourceListener(cache);
    } else {
        LOG.info("Custom module loading skipped.");
    }
    configuration.getDefaultYangResources().forEach(r -> {
        final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create(r.getModuleName(), Revision.ofNullable(r.getRevision()));
        registerSource(consumer, r.getResourcePath(), sourceIdentifier);
    });
    try {
        // necessary for creating mdsal data stores and operations
        this.schemaContext = consumer.createEffectiveModelContextFactory().createEffectiveModelContext(loadedSources).get();
    } catch (final InterruptedException | ExecutionException e) {
        throw new RuntimeException("Cannot parse schema context. " + "Please read stack trace and check YANG files in schema directory.", e);
    }
    final Set<Capability> capabilities = new HashSet<>();
    for (final Module module : schemaContext.getModules()) {
        for (final Submodule subModule : module.getSubmodules()) {
            addModuleCapability(consumer, capabilities, subModule);
        }
        addModuleCapability(consumer, capabilities, module);
    }
    return capabilities;
}
Also used : YangTextSchemaSource(org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource) Capability(org.opendaylight.netconf.api.capability.Capability) YangModuleCapability(org.opendaylight.netconf.api.capability.YangModuleCapability) BasicCapability(org.opendaylight.netconf.api.capability.BasicCapability) SchemaSourceCache(org.opendaylight.netconf.test.tool.schemacache.SchemaSourceCache) FilesystemSchemaSourceCache(org.opendaylight.yangtools.yang.model.repo.fs.FilesystemSchemaSourceCache) SourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier) RevisionSourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier) PotentialSchemaSource(org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource) Submodule(org.opendaylight.yangtools.yang.model.api.Submodule) SchemaSourceRepresentation(org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation) SchemaSourceListener(org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener) FilesystemSchemaSourceCache(org.opendaylight.yangtools.yang.model.repo.fs.FilesystemSchemaSourceCache) ExecutionException(java.util.concurrent.ExecutionException) Module(org.opendaylight.yangtools.yang.model.api.Module) HashSet(java.util.HashSet)

Example 10 with Submodule

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

the class YinFileIncludeStmtTest method testInclude.

@Test
public void testInclude() throws Exception {
    Collection<? extends Submodule> submodules = TestUtils.loadYinModules(getClass().getResource("/semantic-statement-parser/yin/include-belongs-to-test").toURI()).findModules("parent").iterator().next().getSubmodules();
    assertEquals(1, submodules.size());
    Submodule childModule = submodules.iterator().next();
    assertEquals("child", childModule.getName());
    assertEquals(XMLNamespace.of("urn:opendaylight/parent"), childModule.getNamespace());
}
Also used : Submodule(org.opendaylight.yangtools.yang.model.api.Submodule) Test(org.junit.Test)

Aggregations

Submodule (org.opendaylight.yangtools.yang.model.api.Submodule)14 Module (org.opendaylight.yangtools.yang.model.api.Module)9 Test (org.junit.Test)7 SchemaContext (org.opendaylight.yangtools.yang.model.api.SchemaContext)5 QNameModule (org.opendaylight.yangtools.yang.common.QNameModule)3 HashSet (java.util.HashSet)2 EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)2 ModuleImport (org.opendaylight.yangtools.yang.model.api.ModuleImport)2 SourceIdentifier (org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier)2 YangTextSchemaSource (org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource)2 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 ExecutionException (java.util.concurrent.ExecutionException)1 BasicCapability (org.opendaylight.netconf.api.capability.BasicCapability)1 Capability (org.opendaylight.netconf.api.capability.Capability)1 YangModuleCapability (org.opendaylight.netconf.api.capability.YangModuleCapability)1 SchemaSourceCache (org.opendaylight.netconf.test.tool.schemacache.SchemaSourceCache)1 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)1 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)1 NotificationDefinition (org.opendaylight.yangtools.yang.model.api.NotificationDefinition)1