Search in sources :

Example 1 with SubmoduleEffectiveStatement

use of org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement 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 2 with SubmoduleEffectiveStatement

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

the class StatementPrefixResolver method forModule.

static StatementPrefixResolver forModule(final ModuleEffectiveStatement module) {
    final Map<QNameModule, String> imports = module.getAll(QNameModuleToPrefixNamespace.class);
    final Collection<SubmoduleEffectiveStatement> submodules = module.getAll(NameToEffectiveSubmoduleNamespace.class).values();
    if (submodules.isEmpty()) {
        // Simple: it's just the module
        return new StatementPrefixResolver(imports);
    }
    // Stage one: check what everyone thinks about imports
    final Map<String, Multimap<QNameModule, EffectiveStatement<?, ?>>> prefixToNamespaces = new HashMap<>();
    indexPrefixes(prefixToNamespaces, imports, module);
    for (SubmoduleEffectiveStatement submodule : submodules) {
        indexPrefixes(prefixToNamespaces, submodule.getAll(QNameModuleToPrefixNamespace.class), submodule);
    }
    // Stage two: see what QNameModule -> prefix mappings there are. We will need to understand this in step three
    final Multimap<QNameModule, String> namespaceToPrefixes = HashMultimap.create();
    for (Entry<String, Multimap<QNameModule, EffectiveStatement<?, ?>>> entry : prefixToNamespaces.entrySet()) {
        for (QNameModule namespace : entry.getValue().keySet()) {
            namespaceToPrefixes.put(namespace, entry.getKey());
        }
    }
    // Stage three: resolve first order of conflicts, potentially completely resolving mappings...
    final Builder<QNameModule, Object> builder = ImmutableMap.builderWithExpectedSize(prefixToNamespaces.size());
    // ... first resolve unambiguous mappings ...
    final Iterator<Entry<String, Multimap<QNameModule, EffectiveStatement<?, ?>>>> it = prefixToNamespaces.entrySet().iterator();
    while (it.hasNext()) {
        final Entry<String, Multimap<QNameModule, EffectiveStatement<?, ?>>> entry = it.next();
        final Multimap<QNameModule, EffectiveStatement<?, ?>> modules = entry.getValue();
        if (modules.size() == 1) {
            // Careful now: the namespace needs to be unambiguous
            final QNameModule namespace = modules.keys().iterator().next();
            if (namespaceToPrefixes.get(namespace).size() == 1) {
                builder.put(namespace, entry.getKey());
                it.remove();
            }
        }
    }
    // .. check for any remaining conflicts ...
    if (!prefixToNamespaces.isEmpty()) {
        final Multimap<QNameModule, Entry<DeclaredStatement<?>, String>> conflicts = ArrayListMultimap.create();
        for (Entry<String, Multimap<QNameModule, EffectiveStatement<?, ?>>> entry : prefixToNamespaces.entrySet()) {
            for (Entry<QNameModule, EffectiveStatement<?, ?>> namespace : entry.getValue().entries()) {
                conflicts.put(namespace.getKey(), new SimpleImmutableEntry<>(namespace.getValue().getDeclared(), entry.getKey()));
            }
        }
        builder.putAll(Maps.transformValues(conflicts.asMap(), Conflict::new));
    }
    return new StatementPrefixResolver(builder.build());
}
Also used : QNameModuleToPrefixNamespace(org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement.QNameModuleToPrefixNamespace) HashMap(java.util.HashMap) NameToEffectiveSubmoduleNamespace(org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement.NameToEffectiveSubmoduleNamespace) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) SubmoduleEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) Entry(java.util.Map.Entry) ModuleEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement) EffectiveStatement(org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement) SubmoduleEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule)

Example 3 with SubmoduleEffectiveStatement

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

the class YangTextSnippetTest method assertFormat.

private static void assertFormat(final Collection<? extends Module> modules) {
    for (Module module : modules) {
        assertTrue(module instanceof ModuleEffectiveStatement);
        final ModuleEffectiveStatement stmt = (ModuleEffectiveStatement) module;
        assertNotNull(formatModule(stmt));
        for (SubmoduleEffectiveStatement substmt : stmt.getAll(NameToEffectiveSubmoduleNamespace.class).values()) {
            assertNotNull(formatSubmodule(substmt));
        }
    }
}
Also used : NameToEffectiveSubmoduleNamespace(org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement.NameToEffectiveSubmoduleNamespace) SubmoduleEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement) Module(org.opendaylight.yangtools.yang.model.api.Module) ModuleEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement)

Example 4 with SubmoduleEffectiveStatement

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

the class SubmoduleStatementSupport method onLinkageDeclared.

@Override
public void onLinkageDeclared(final Mutable<Unqualified, SubmoduleStatement, SubmoduleEffectiveStatement> stmt) {
    final SourceIdentifier submoduleIdentifier = RevisionSourceIdentifier.create(stmt.getRawArgument(), StmtContextUtils.getLatestRevision(stmt.declaredSubstatements()));
    final StmtContext<?, SubmoduleStatement, SubmoduleEffectiveStatement> possibleDuplicateSubmodule = stmt.getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
    if (possibleDuplicateSubmodule != null && possibleDuplicateSubmodule != stmt) {
        throw new SourceException(stmt, "Submodule name collision: %s. At %s", stmt.rawArgument(), possibleDuplicateSubmodule.sourceReference());
    }
    stmt.addContext(SubmoduleNamespace.class, submoduleIdentifier, stmt);
    final String belongsToModuleName = firstAttributeOf(stmt.declaredSubstatements(), BelongsToStatement.class);
    final StmtContext<?, ?, ?> prefixSubStmtCtx = SourceException.throwIfNull(findFirstDeclaredSubstatement(stmt, 0, BelongsToStatement.class, PrefixStatement.class), stmt, "Prefix of belongsTo statement is missing in submodule [%s]", stmt.rawArgument());
    final String prefix = prefixSubStmtCtx.rawArgument();
    stmt.addToNs(BelongsToPrefixToModuleName.class, prefix, belongsToModuleName);
}
Also used : SubmoduleEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement) SourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier) RevisionSourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier) SubmoduleStatement(org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement) BelongsToStatement(org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement) SourceException(org.opendaylight.yangtools.yang.parser.spi.source.SourceException) PrefixStatement(org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement)

Aggregations

SubmoduleEffectiveStatement (org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement)4 ModuleEffectiveStatement (org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement)3 Module (org.opendaylight.yangtools.yang.model.api.Module)2 BelongsToStatement (org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement)2 NameToEffectiveSubmoduleNamespace (org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement.NameToEffectiveSubmoduleNamespace)2 PrefixStatement (org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement)2 SubmoduleStatement (org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement)2 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 Test (org.junit.Test)1 QNameModule (org.opendaylight.yangtools.yang.common.QNameModule)1 SchemaContext (org.opendaylight.yangtools.yang.model.api.SchemaContext)1 Submodule (org.opendaylight.yangtools.yang.model.api.Submodule)1 EffectiveStatement (org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement)1 IncludeStatement (org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement)1 QNameModuleToPrefixNamespace (org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement.QNameModuleToPrefixNamespace)1