Augments should get disabled if target is unsupported 49/98949/2
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 24 Nov 2021 10:43:31 +0000 (11:43 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 17 Dec 2021 07:53:18 +0000 (08:53 +0100)
AugmentInferenceAction should pay attention to unavailable target
nodes, so we disable the augmentation.

JIRA: YANGTOOLS-1370
Change-Id: Ib1f0bde83a90e4bb45a7c4fa1fc62af205f4ea6c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 43fb7cd8d8bd9330988c02566ef376d00eb7b461)

parser/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/augment/AugmentInferenceAction.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1370Test.java [new file with mode: 0644]
parser/yang-parser-rfc7950/src/test/resources/bugs/YT1370/foo.yang [new file with mode: 0644]

index 2f19c1a1cdafe0e34f32d866af6c361c88cad4b1..9c540052c86db8f33d2ead14e77bc7417e2c07c0 100644 (file)
@@ -14,6 +14,7 @@ import static java.util.Objects.requireNonNull;
 import com.google.common.collect.ImmutableSet;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
 import java.util.Objects;
 import java.util.Optional;
 import org.opendaylight.yangtools.yang.common.Empty;
@@ -62,6 +63,11 @@ final class AugmentInferenceAction implements InferenceAction {
 
     @Override
     public void apply(final InferenceContext ctx) {
+        if (!augmentNode.isSupportedToBuildEffective()) {
+            // We are not building effective model, hence we should not be performing any effects
+            return;
+        }
+
         final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.resolve(ctx);
         if (!isSupportedAugmentTarget(augmentTargetCtx)
                 || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
@@ -99,6 +105,15 @@ final class AugmentInferenceAction implements InferenceAction {
         throw new InferenceException(augmentNode, "Augment target '%s' not found", augmentNode.argument());
     }
 
+    @Override
+    public void prerequisiteUnavailable(final Prerequisite<?> unavail) {
+        if (target.equals(unavail)) {
+            augmentNode.setIsSupportedToBuildEffective(false);
+        } else {
+            prerequisiteFailed(List.of(unavail));
+        }
+    }
+
     private void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
             final StatementContextBase<?, ?, ?> targetCtx) {
         final CopyType typeOfCopy = sourceCtx.coerceParentContext().producesDeclared(UsesStatement.class)
diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1370Test.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1370Test.java
new file mode 100644 (file)
index 0000000..ab9e70c
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * 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.stmt;
+
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Set;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
+
+public class YT1370Test {
+    @Test
+    public void testAugmentUnsupportedByFeatures() throws Exception {
+        assertNotNull(StmtTestUtils.parseYangSources("/bugs/YT1370", Set.of(), YangParserConfiguration.DEFAULT));
+    }
+}
diff --git a/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1370/foo.yang b/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1370/foo.yang
new file mode 100644 (file)
index 0000000..20446ae
--- /dev/null
@@ -0,0 +1,14 @@
+module foo {
+  prefix foo;
+  namespace foo;
+
+  feature foo;
+
+  container foo {
+    if-feature foo;
+  }
+
+  augment /foo {
+    container bar;
+  }
+}