*/
package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.meta;
+import static com.google.common.base.Verify.verifyNotNull;
+
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
-import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.DeviationEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.DeviationStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
+import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatementDecorators;
import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
+import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleCtx;
import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
public final class DeviationStatementSupport
public void onFullDefinitionDeclared(final Mutable<Absolute, DeviationStatement, DeviationEffectiveStatement> ctx) {
super.onFullDefinitionDeclared(ctx);
- final QNameModule currentModule = ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot());
- final QNameModule targetModule = Iterables.getLast(ctx.getArgument().getNodeIdentifiers()).getModule();
+ StmtContext<?, ?, ?> root = ctx.getRoot();
+ if (root.producesDeclared(SubmoduleStatement.class)) {
+ // root is submodule, we need to find the module we belong to. We can rely on there being exactly one
+ // belongs-to statement, enforced SubmoduleStatementSupport's validator.
+ root = Iterables.getOnlyElement(
+ root.getAllFromNamespace(BelongsToPrefixToModuleCtx.class).values());
+ }
+ final var currentModule = verifyNotNull(ctx.getFromNamespace(ModuleCtxToModuleQName.class, root),
+ "Failed to find QName for %s", root);
+ final var targetModule = Iterables.getLast(ctx.getArgument().getNodeIdentifiers()).getModule();
if (currentModule.equals(targetModule)) {
throw new InferenceException(ctx,
"Deviation must not target the same module as the one it is defined in: %s", currentModule);
--- /dev/null
+/*
+ * Copyright (c) 2022 PANTHEON.tech s.r.o. 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.stmt;
+
+import static org.hamcrest.CoreMatchers.startsWith;
+
+import org.junit.Test;
+
+public class YT1448Test extends AbstractYangTest {
+
+ @Test
+ public void deviationFromSubmodule() {
+ assertEffectiveModelDir("/bugs/YT1448/valid");
+ }
+
+ @Test
+ public void deviationFromSubmoduleTargetedOwnModule() {
+ assertInferenceExceptionDir("/bugs/YT1448/invalid",
+ startsWith("Deviation must not target the same module as the one it is defined in"));
+ }
+}