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