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());
}
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>"));
}
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);
}
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));
}
}
}
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());
}
Aggregations