Fix inferred statements over undeclared statements 19/100119/3
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 14 Mar 2022 12:29:08 +0000 (13:29 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 14 Mar 2022 15:53:34 +0000 (16:53 +0100)
We must not assume we can access declared() statement. In order to do
that, we need to change how createInferredEffective() operates and push
it down to ReactorStmtCtx.

JIRA: YANGTOOLS-1407
Change-Id: I56823cb2ad497adcccaffa3e3386e45b0bb08089
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/AbstractResumedStatement.java
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/InferredStatementContext.java
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/ReactorStmtCtx.java
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/ReplicaStatementContext.java
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/UndeclaredStmtCtx.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1407Test.java [new file with mode: 0644]
parser/yang-parser-rfc7950/src/test/resources/bugs/YT1407/ietf-lisp-address-types.yang [new file with mode: 0644]
parser/yang-parser-rfc7950/src/test/resources/bugs/YT1407/odl-inet-binary-types.yang [new file with mode: 0644]
parser/yang-parser-rfc7950/src/test/resources/bugs/YT1407/odl-lisp-address-types.yang [new file with mode: 0644]
parser/yang-parser-rfc7950/src/test/resources/bugs/YT1407/odl-lisp-proto.yang [new file with mode: 0644]

index 17ce23e1509f065f2776773c397a013a062ff8a6..1358f284ffe477610b07b80791922d3aa02e7200 100644 (file)
@@ -106,27 +106,29 @@ abstract class AbstractResumedStatement<A, D extends DeclaredStatement<A>, E ext
 
     @Override
     final E createEffective(final StatementFactory<A, D, E> factory) {
-        return createEffective(factory, this);
+        return createEffective(factory, this, streamDeclared(), streamEffective());
     }
 
     // Creates EffectiveStatement through full materialization and assumes declared statement presence
     private @NonNull E createEffective(final StatementFactory<A, D, E> factory,
-            final StatementContextBase<A, D, E> ctx) {
+            final StatementContextBase<A, D, E> ctx, final Stream<? extends StmtContext<?, ?, ?>> declared,
+            final Stream<? extends StmtContext<?, ?, ?>> effective) {
         // Statement reference count infrastructure makes an assumption that effective statement is only built after
         // the declared statement is already done. Statements tracked by this class always have a declared view, and
         // we need to ensure that is built before we touch effective substatements.
         //
         // Once the effective substatement stream has been exhausted, reference counting will triggers a sweep, hence
         // the substatements may be gone by the time the factory attempts to acquire the declared statement.
-        declared();
+        ctx.declared();
 
-        return factory.createEffective(ctx, ctx.streamDeclared(), ctx.streamEffective());
+        return factory.createEffective(ctx, declared, effective);
     }
 
     @Override
     final E createInferredEffective(final StatementFactory<A, D, E> factory,
-            final InferredStatementContext<A, D, E> ctx) {
-        return createEffective(factory, ctx);
+            final InferredStatementContext<A, D, E> ctx, final Stream<? extends StmtContext<?, ?, ?>> declared,
+            final Stream<? extends StmtContext<?, ?, ?>> effective) {
+        return createEffective(factory, ctx, declared, effective);
     }
 
     /**
index b1693ae1d592d3090b0a45bcca01f4420f24506b..9349e3ff6ca2b5c6f31d462e85b96cc19c810075 100644 (file)
@@ -87,7 +87,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
 
     private final @NonNull StatementContextBase<A, D, E> prototype;
     private final @NonNull StatementContextBase<?, ?, ?> parent;
-    private final @NonNull StmtContext<A, D, E> originalCtx;
+    private final @NonNull ReactorStmtCtx<A, D, E> originalCtx;
     private final QNameModule targetModule;
     private final A argument;
 
@@ -122,7 +122,10 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         this.argument = targetModule == null ? prototype.argument()
                 : prototype.definition().adaptArgumentValue(prototype, targetModule);
         this.targetModule = targetModule;
-        this.originalCtx = prototype.getOriginalCtx().orElse(prototype);
+
+        final var origCtx = prototype.getOriginalCtx().orElse(prototype);
+        verify(origCtx instanceof ReactorStmtCtx, "Unexpected original %s", origCtx);
+        this.originalCtx = (ReactorStmtCtx<A, D, E>) origCtx;
 
         // Mark prototype as blocking statement cleanup
         prototype.incRef();
@@ -212,12 +215,18 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         // If we have not materialized we do not have a difference in effective substatements, hence we can forward
         // towards the source of the statement.
         accessSubstatements();
-        return substatements == null ? tryToReusePrototype(factory) : createInferredEffective(factory, this);
+        return substatements == null ? tryToReusePrototype(factory) : createInferredEffective(factory);
+    }
+
+    private @NonNull E createInferredEffective(final @NonNull StatementFactory<A, D, E> factory) {
+        return createInferredEffective(factory, this, streamDeclared(), streamEffective());
     }
 
     @Override
-    E createInferredEffective(final StatementFactory<A, D, E> factory, final InferredStatementContext<A, D, E> ctx) {
-        return prototype.createInferredEffective(factory, ctx);
+    E createInferredEffective(final StatementFactory<A, D, E> factory, final InferredStatementContext<A, D, E> ctx,
+            final Stream<? extends StmtContext<?, ?, ?>> declared,
+            final Stream<? extends StmtContext<?, ?, ?>> effective) {
+        return originalCtx.createInferredEffective(factory, ctx, declared, effective);
     }
 
     private @NonNull E tryToReusePrototype(final @NonNull StatementFactory<A, D, E> factory) {
@@ -279,7 +288,8 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         prototype.decRef();
 
         // Values are the effective copies, hence this efficiently deals with recursion.
-        return internAlongCopyAxis(factory, factory.createEffective(this, declared.stream(), effective.stream()));
+        return internAlongCopyAxis(factory,
+            originalCtx.createInferredEffective(factory, this, declared.stream(), effective.stream()));
     }
 
     private @NonNull E tryToReuseSubstatements(final @NonNull StatementFactory<A, D, E> factory,
@@ -293,7 +303,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
 
         // Fall back to full instantiation, which populates our substatements. Then check if we should be reusing
         // the substatement list, as this operation turned out to not affect them.
-        final E effective = createInferredEffective(factory, this);
+        final E effective = createInferredEffective(factory);
         // Since we have forced instantiation to deal with this case, we also need to reset the 'modified' flag
         setUnmodified();
 
index e0507835b9e28a0f3003f17d59b26e24bbf6df9d..73aef53d66b7e140caba5424019ecadf95927450 100644 (file)
@@ -17,6 +17,7 @@ import java.util.Collection;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
+import java.util.stream.Stream;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.common.Empty;
@@ -42,6 +43,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.ExecutionOrder;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StatementFactory;
 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.StmtContextUtils;
@@ -396,6 +398,20 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
 
     abstract @NonNull E createEffective();
 
+    /**
+     * Routing of the request to build an effective statement from {@link InferredStatementContext} towards the original
+     * definition site. This is needed to pick the correct instantiation method: for declared statements we will
+     * eventually land in {@link AbstractResumedStatement}, for underclared statements that will be
+     * {@link UndeclaredStmtCtx}.
+     *
+     * @param factory Statement factory
+     * @param ctx Inferred statement context, i.e. where the effective statement is instantiated
+     * @return Built effective stateue
+     */
+    abstract @NonNull E createInferredEffective(@NonNull StatementFactory<A, D, E> factory,
+        @NonNull InferredStatementContext<A, D, E> ctx, Stream<? extends StmtContext<?, ?, ?>> declared,
+        Stream<? extends StmtContext<?, ?, ?>> effective);
+
     /**
      * Attach an effective copy of this statement. This essentially acts as a map, where we make a few assumptions:
      * <ul>
index 58434926184c1d0a83ca160e61df216378c3f7b7..16bab426be499d7a043ed85b9a8d89f427ba2b5e 100644 (file)
@@ -12,6 +12,7 @@ import static java.util.Objects.requireNonNull;
 import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
+import java.util.stream.Stream;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
@@ -21,6 +22,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StatementFactory;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
@@ -49,6 +51,13 @@ final class ReplicaStatementContext<A, D extends DeclaredStatement<A>, E extends
         return source.buildEffective();
     }
 
+    @Override
+    E createInferredEffective(final StatementFactory<A, D, E> factory, final InferredStatementContext<A, D, E> ctx,
+            final Stream<? extends StmtContext<?, ?, ?>> declared,
+            final Stream<? extends StmtContext<?, ?, ?>> effective) {
+        return source.createInferredEffective(factory, ctx, declared, effective);
+    }
+
     @Override
     ReactorStmtCtx<A, D, E> unmodifiedEffectiveSource() {
         return source.unmodifiedEffectiveSource();
index 438c6e6365d87ad367c189b72323bcb47ccdfbf8..eb072d9a319ee5e91798de5aedc4117263287b80 100644 (file)
@@ -445,19 +445,6 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
 
     abstract @NonNull E createEffective(@NonNull StatementFactory<A, D, E> factory);
 
-    /**
-     * Routing of the request to build an effective statement from {@link InferredStatementContext} towards the original
-     * definition site. This is needed to pick the correct instantiation method: for declared statements we will
-     * eventually land in {@link AbstractResumedStatement}, for underclared statements that will be
-     * {@link UndeclaredStmtCtx}.
-     *
-     * @param factory Statement factory
-     * @param ctx Inferred statement context, i.e. where the effective statement is instantiated
-     * @return Built effective stateue
-     */
-    abstract @NonNull E createInferredEffective(@NonNull StatementFactory<A, D, E> factory,
-        @NonNull InferredStatementContext<A, D, E> ctx);
-
     /**
      * Return a stream of declared statements which can be built into an {@link EffectiveStatement}, as per
      * {@link StmtContext#buildEffective()} contract.
index 75b3aefb2de51c024289f17735b150a5530344e9..beef8d92fb27740baa099a686c3d35e44ec9b435 100644 (file)
@@ -123,8 +123,12 @@ final class UndeclaredStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
     }
 
     @Override
-    E createInferredEffective(final StatementFactory<A, D, E> factory, final InferredStatementContext<A, D, E> ctx) {
-        return createEffective(factory, new ForwardingUndeclaredCurrent<>(ctx), ctx.streamEffective());
+    E createInferredEffective(final StatementFactory<A, D, E> factory, final InferredStatementContext<A, D, E> ctx,
+            final Stream<? extends StmtContext<?, ?, ?>> declared,
+            final Stream<? extends StmtContext<?, ?, ?>> effective) {
+        final long declaredCount = declared.count();
+        verify(declaredCount == 0, "Unexpected non-empty declared statements in %s", ctx);
+        return createEffective(factory, new ForwardingUndeclaredCurrent<>(ctx), effective);
     }
 
     /*
diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1407Test.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1407Test.java
new file mode 100644 (file)
index 0000000..2fa809b
--- /dev/null
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2022 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 org.junit.Test;
+
+public class YT1407Test extends AbstractYangTest {
+    @Test
+    public void testUsedUndeclaredCase() {
+        assertEffectiveModelDir("/bugs/YT1407");
+    }
+}
diff --git a/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1407/ietf-lisp-address-types.yang b/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1407/ietf-lisp-address-types.yang
new file mode 100644 (file)
index 0000000..1827e95
--- /dev/null
@@ -0,0 +1,14 @@
+module ietf-lisp-address-types {
+  namespace "urn:ietf:params:xml:ns:yang:ietf-lisp-address-types";
+  prefix laddr;
+
+  grouping lisp-address {
+    choice address {
+      container null-address {
+        leaf address {
+          type empty;
+        }
+      }
+    }
+  }
+}
diff --git a/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1407/odl-inet-binary-types.yang b/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1407/odl-inet-binary-types.yang
new file mode 100644 (file)
index 0000000..ee7ae5f
--- /dev/null
@@ -0,0 +1,10 @@
+module odl-inet-binary-types {
+    namespace "urn:opendaylight:lfm:inet-binary-types";
+    prefix "inet-binary";
+
+    typedef ipv4-address-binary {
+        type binary {
+            length "4";
+        }
+    }
+}
diff --git a/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1407/odl-lisp-address-types.yang b/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1407/odl-lisp-address-types.yang
new file mode 100644 (file)
index 0000000..b4372eb
--- /dev/null
@@ -0,0 +1,19 @@
+module odl-lisp-address-types {
+    namespace "urn:opendaylight:lfm:lisp-binary-address-types";
+    prefix "lisp-binary";
+
+    import ietf-lisp-address-types { prefix laddr; }
+    import odl-inet-binary-types { prefix bin; }
+
+    grouping augmented-lisp-address {
+        uses laddr:lisp-address {
+            augment "address" {
+                case ipv4-binary {
+                    leaf ipv4-binary {
+                        type bin:ipv4-address-binary;
+                    }
+                }
+            }
+        }
+    }
+}
diff --git a/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1407/odl-lisp-proto.yang b/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1407/odl-lisp-proto.yang
new file mode 100644 (file)
index 0000000..fa20e9d
--- /dev/null
@@ -0,0 +1,22 @@
+module odl-lisp-proto {
+    namespace "urn:opendaylight:lfm:lisp-proto";
+    prefix "lisp-proto";
+
+    import odl-lisp-address-types { prefix odl-lisp-address; }
+
+    grouping eid-container {
+        container eid {
+            uses odl-lisp-address:augmented-lisp-address;
+        }
+    }
+
+    grouping eid-list {
+        list eid-item {
+            key "eid-item-id";
+            leaf eid-item-id {
+                type string;
+            }
+            uses eid-container;
+        }
+    }
+}