import static java.util.Objects.requireNonNull;
import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Maps;
private final Collection<Entry<DeclaredStatement<?>, String>> statements;
Conflict(final Collection<Entry<DeclaredStatement<?>, String>> entries) {
- this.statements = requireNonNull(entries);
+ statements = requireNonNull(entries);
}
@Nullable String findPrefix(final DeclaredStatement<?> stmt) {
private final Map<QNameModule, ?> lookup;
private StatementPrefixResolver(final Map<QNameModule, String> map) {
- this.lookup = ImmutableMap.copyOf(map);
+ lookup = ImmutableMap.copyOf(map);
}
private StatementPrefixResolver(final ImmutableMap<QNameModule, ?> map) {
- this.lookup = requireNonNull(map);
+ lookup = requireNonNull(map);
}
static StatementPrefixResolver forModule(final ModuleEffectiveStatement module) {
indexPrefixes(prefixToNamespaces, submodule.getAll(QNameModuleToPrefixNamespace.class), submodule);
}
- // Stage two: resolve first order of conflicts, potentially completely resolving mappings...
+ // 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 Entry<String, Multimap<QNameModule, EffectiveStatement<?, ?>>> entry = it.next();
final Multimap<QNameModule, EffectiveStatement<?, ?>> modules = entry.getValue();
if (modules.size() == 1) {
- builder.put(modules.keys().iterator().next(), entry.getKey());
- it.remove();
+ // 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();
+ }
}
}
--- /dev/null
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.model.export;
+
+import static org.junit.Assert.assertNotNull;
+
+import java.net.URI;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public class YT1313Test {
+ @Test
+ public void testSubmoduleImportPrefixes() {
+ final ModuleEffectiveStatement bar = YangParserTestUtils.parseYangResourceDirectory("/bugs/yt1313")
+ .getModuleStatements().get(QNameModule.create(URI.create("bar")));
+
+ final StatementPrefixResolver resolver = StatementPrefixResolver.forModule(bar);
+ assertNotNull(resolver);
+ }
+}