Search in sources :

Example 26 with ModuleEffectiveStatement

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

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

the class Bug5531Test method test.

@Test
public void test() throws Exception {
    EffectiveModelContext schema = YangParserTestUtils.parseYangResourceDirectory("/bugs/bug5531");
    assertNotNull(schema);
    assertNotNull(schema.getModules());
    assertEquals(1, schema.getModules().size());
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
    // write small module of size less than 8kB
    for (ModuleEffectiveStatement module : schema.getModuleStatements().values()) {
        YinExportUtils.writeModuleAsYinText(module, bufferedOutputStream);
    }
    String output = byteArrayOutputStream.toString();
    // if all changes were flushed then following conditions are satisfied
    assertNotEquals("Output should not be empty", 0, output.length());
    assertTrue("Output should contains start of the module", output.contains("<module"));
    assertTrue("Output should contains end of the module", output.contains("</module>"));
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) ModuleEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement) Test(org.junit.Test)

Example 28 with ModuleEffectiveStatement

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

the class YT1313Test method testSubmoduleImportPrefixes.

@Test
public void testSubmoduleImportPrefixes() {
    final ModuleEffectiveStatement bar = YangParserTestUtils.parseYangResourceDirectory("/bugs/yt1313").getModuleStatement(QNameModule.create(XMLNamespace.of("bar")));
    final StatementPrefixResolver resolver = StatementPrefixResolver.forModule(bar);
    assertNotNull(resolver);
}
Also used : ModuleEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement) Test(org.junit.Test)

Example 29 with ModuleEffectiveStatement

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

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

the class YT1195Test method testKeyStatementReuse.

@Test
public void testKeyStatementReuse() throws Exception {
    final ModuleEffectiveStatement module = StmtTestUtils.parseYangSource("/bugs/YT1195/key.yang").getModuleStatement(QNameModule.create(XMLNamespace.of("foo")));
    final ListEffectiveStatement grpFoo = module.findFirstEffectiveSubstatement(GroupingEffectiveStatement.class).orElseThrow().findFirstEffectiveSubstatement(ListEffectiveStatement.class).orElseThrow();
    final ListEffectiveStatement foo = module.findFirstEffectiveSubstatement(ListEffectiveStatement.class).orElseThrow();
    // The statements should not be the same due history being part of ListSchemaNode
    assertNotSame(foo, grpFoo);
    // The statements are instantiated in the same module, hence they should have the same argument
    assertSame(foo.argument(), grpFoo.argument());
    // The statements' key substatement should be reused
    assertSame(foo.findFirstEffectiveSubstatement(KeyEffectiveStatement.class).orElseThrow(), grpFoo.findFirstEffectiveSubstatement(KeyEffectiveStatement.class).orElseThrow());
}
Also used : ListEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement) GroupingEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement) ModuleEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement) Test(org.junit.Test)

Aggregations

ModuleEffectiveStatement (org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement)45 Test (org.junit.Test)28 GroupingEffectiveStatement (org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement)20 ContainerEffectiveStatement (org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement)12 NonNull (org.eclipse.jdt.annotation.NonNull)9 Objects.requireNonNull (java.util.Objects.requireNonNull)8 NotificationEffectiveStatement (org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement)8 QNameModule (org.opendaylight.yangtools.yang.common.QNameModule)5 QName (org.opendaylight.yangtools.yang.common.QName)4 LeafEffectiveStatement (org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement)4 LeafListEffectiveStatement (org.opendaylight.yangtools.yang.model.api.stmt.LeafListEffectiveStatement)4 SubmoduleEffectiveStatement (org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement)4 ModuleStatement (org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement)3 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Entry (java.util.Map.Entry)2 Revision (org.opendaylight.yangtools.yang.common.Revision)2 EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)2 Module (org.opendaylight.yangtools.yang.model.api.Module)2