Fix inferred statements over undeclared statements
[yangtools.git] / parser / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / InferredStatementContext.java
index aff44fd619a8574db6c3f0f7b5696fa88fdee7d3..9349e3ff6ca2b5c6f31d462e85b96cc19c810075 100644 (file)
@@ -15,6 +15,7 @@ import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Streams;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -34,6 +35,7 @@ import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStatementState;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.OnDemandSchemaTreeStorageNode;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
@@ -85,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;
 
@@ -120,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();
@@ -137,13 +142,13 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
     }
 
     @Override
-    public Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
+    public Iterable<? extends @NonNull StmtContext<?, ?, ?>> allSubstatements() {
         // No need to concat with declared
         return effectiveSubstatements();
     }
 
     @Override
-    public Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
+    public Stream<? extends @NonNull StmtContext<?, ?, ?>> allSubstatementsStream() {
         // No need to concat with declared
         return effectiveSubstatements().stream();
     }
@@ -192,6 +197,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
     @Override
     public void addEffectiveSubstatement(final Mutable<?, ?, ?> substatement) {
         substatements = addEffectiveSubstatement(ensureEffectiveSubstatements(), substatement);
+        afterAddEffectiveSubstatement(substatement);
     }
 
     @Override
@@ -209,18 +215,28 @@ 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) : super.createEffective(factory);
+        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,
+            final Stream<? extends StmtContext<?, ?, ?>> declared,
+            final Stream<? extends StmtContext<?, ?, ?>> effective) {
+        return originalCtx.createInferredEffective(factory, ctx, declared, effective);
     }
 
-    private @NonNull E tryToReusePrototype(final StatementFactory<A, D, E> factory) {
+    private @NonNull E tryToReusePrototype(final @NonNull StatementFactory<A, D, E> factory) {
         final E origEffective = prototype.buildEffective();
         final Collection<? extends @NonNull EffectiveStatement<?, ?>> origSubstatements =
             origEffective.effectiveSubstatements();
 
         // First check if we can reuse the entire prototype
         if (!factory.canReuseCurrent(this, prototype, origSubstatements)) {
-            // FIXME: YANGTOOLS-1214: deduplicate this return
-            return tryToReuseSubstatements(factory, origEffective);
+            return internAlongCopyAxis(factory, tryToReuseSubstatements(factory, origEffective));
         }
 
         // We can reuse this statement let's see if all statements agree...
@@ -272,11 +288,12 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         prototype.decRef();
 
         // Values are the effective copies, hence this efficiently deals with recursion.
-        // FIXME: YANGTOOLS-1214: deduplicate this return
-        return factory.createEffective(this, declared.stream(), effective.stream());
+        return internAlongCopyAxis(factory,
+            originalCtx.createInferredEffective(factory, this, declared.stream(), effective.stream()));
     }
 
-    private @NonNull E tryToReuseSubstatements(final StatementFactory<A, D, E> factory, final @NonNull E original) {
+    private @NonNull E tryToReuseSubstatements(final @NonNull StatementFactory<A, D, E> factory,
+            final @NonNull E original) {
         if (allSubstatementsContextIndependent()) {
             LOG.debug("Reusing substatements of: {}", prototype);
             substatements = noRefs() ? REUSED_SUBSTATEMENTS : reusePrototypeReplicas();
@@ -286,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 = super.createEffective(factory);
+        final E effective = createInferredEffective(factory);
         // Since we have forced instantiation to deal with this case, we also need to reset the 'modified' flag
         setUnmodified();
 
@@ -297,6 +314,16 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         return effective;
     }
 
+    private @NonNull E internAlongCopyAxis(final StatementFactory<A, D, E> factory, final @NonNull E stmt) {
+        if (!isModified()) {
+            final EffectiveStatementState state = factory.extractEffectiveState(stmt);
+            if (state != null) {
+                return prototype.unmodifiedEffectiveSource().attachEffectiveCopy(state, stmt);
+            }
+        }
+        return stmt;
+    }
+
     private List<ReactorStmtCtx<?, ?, ?>> reusePrototypeReplicas() {
         return reusePrototypeReplicas(Streams.concat(prototype.streamDeclared(), prototype.streamEffective()));
     }
@@ -452,17 +479,17 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
     }
 
     @Override
-    Iterable<ReactorStmtCtx<?, ?, ?>> effectiveChildrenToComplete() {
+    Iterator<ReactorStmtCtx<?, ?, ?>> effectiveChildrenToComplete() {
         // When we have not initialized, there are no statements to catch up: we will catch up when we are copying
         // from prototype (which is already at ModelProcessingPhase.EFFECTIVE_MODEL).
         if (substatements == null) {
-            return ImmutableList.of();
+            return Collections.emptyIterator();
         }
         accessSubstatements();
         if (substatements instanceof HashMap) {
-            return castMaterialized(substatements).values();
+            return castMaterialized(substatements).values().iterator();
         } else {
-            return castEffective(substatements);
+            return castEffective(substatements).iterator();
         }
     }