Rework AugmentRuntimeType and Choice/Case linkage
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / AbstractCompositeGenerator.java
index 810d2d876d493a3e36a6187c4e3f5da57e8249f0..769830433f7ff6ebd94c6e29b5ef573818c9cdbc 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.mdsal.binding.generator.impl.reactor;
 
 import static com.google.common.base.Verify.verify;
+import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
 import java.util.ArrayList;
@@ -19,8 +20,11 @@ import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.binding.model.api.Enumeration;
 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
+import org.opendaylight.mdsal.binding.model.api.Type;
 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
+import org.opendaylight.mdsal.binding.runtime.api.CompositeRuntimeType;
+import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AddedByUsesAware;
 import org.opendaylight.yangtools.yang.model.api.CopyableNode;
@@ -108,19 +112,20 @@ import org.slf4j.LoggerFactory;
  * with linking original instances in the tree iteration order. The part dealing with augment attachment lives mostly
  * in {@link AugmentRequirement}.
  */
-abstract class AbstractCompositeGenerator<T extends EffectiveStatement<?, ?>> extends AbstractExplicitGenerator<T> {
+public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?, ?>, R extends CompositeRuntimeType>
+        extends AbstractExplicitGenerator<S, R> {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractCompositeGenerator.class);
 
     // FIXME: we want to allocate this lazily to lower memory footprint
     private final @NonNull CollisionDomain domain = new CollisionDomain(this);
-    private final List<Generator> children;
+    private final @NonNull List<Generator> childGenerators;
 
     /**
      * List of {@code augment} statements targeting this generator. This list is maintained only for the primary
      * incarnation. This list is an evolving entity until after we have finished linkage of original statements. It is
      * expected to be stable at the start of {@code step 2} in {@link GeneratorReactor#execute(TypeBuilderFactory)}.
      */
-    private List<AbstractAugmentGenerator> augments = List.of();
+    private @NonNull List<AbstractAugmentGenerator> augments = List.of();
 
     /**
      * List of {@code grouping} statements this statement references. This field is set once by
@@ -133,44 +138,78 @@ abstract class AbstractCompositeGenerator<T extends EffectiveStatement<?, ?>> ex
      * have some children which have not completed linking. Once we have completed linking of all children, including
      * {@link #unlinkedChildren}, this will be set to {@code null}.
      */
-    private List<AbstractCompositeGenerator<?>> unlinkedComposites = List.of();
+    private List<AbstractCompositeGenerator<?, ?>> unlinkedComposites = List.of();
     /**
      * List of children which have not had their original linked. This list starts of as null. When we first attempt
      * linkage, it becomes non-null.
      */
     private List<Generator> unlinkedChildren;
 
-    AbstractCompositeGenerator(final T statement) {
+    AbstractCompositeGenerator(final S statement) {
         super(statement);
-        children = createChildren(statement);
+        childGenerators = createChildren(statement);
     }
 
-    AbstractCompositeGenerator(final T statement, final AbstractCompositeGenerator<?> parent) {
+    AbstractCompositeGenerator(final S statement, final AbstractCompositeGenerator<?, ?> parent) {
         super(statement, parent);
-        children = createChildren(statement);
+        childGenerators = createChildren(statement);
     }
 
     @Override
     public final Iterator<Generator> iterator() {
-        return children.iterator();
+        return childGenerators.iterator();
+    }
+
+    @Override
+    final GeneratedType runtimeJavaType() {
+        return generatedType().orElse(null);
+    }
+
+    final @NonNull List<AbstractAugmentGenerator> augments() {
+        return augments;
+    }
+
+    final @NonNull List<GroupingGenerator> groupings() {
+        return verifyNotNull(groupings, "Groupings not initialized in %s", this);
+    }
+
+    @Override
+    final R createExternalRuntimeType(final Type type) {
+        verify(type instanceof GeneratedType, "Unexpected type %s", type);
+
+        // FIXME: we should be able to use internal cache IFF when all augments end up being local to our statement
+        final var statement = statement();
+        return createBuilder(statement)
+            .fillTypes(ChildLookup.of(statement), this)
+            .build((GeneratedType) type);
+    }
+
+    abstract @NonNull CompositeRuntimeTypeBuilder<S, R> createBuilder(S statement);
+
+    @Override
+    final R createInternalRuntimeType(final ChildLookup lookup, final S statement, final Type type) {
+        verify(type instanceof GeneratedType, "Unexpected type %s", type);
+        return createBuilder(statement)
+            .fillTypes(lookup.inStatement(statement), this)
+            .build((GeneratedType) type);
     }
 
     @Override
     final boolean isEmpty() {
-        return children.isEmpty();
+        return childGenerators.isEmpty();
     }
 
-    final @Nullable AbstractExplicitGenerator<?> findGenerator(final List<EffectiveStatement<?, ?>> stmtPath) {
+    final @Nullable AbstractExplicitGenerator<?, ?> findGenerator(final List<EffectiveStatement<?, ?>> stmtPath) {
         return findGenerator(MatchStrategy.identity(), stmtPath, 0);
     }
 
-    final @Nullable AbstractExplicitGenerator<?> findGenerator(final MatchStrategy childStrategy,
+    final @Nullable AbstractExplicitGenerator<?, ?> findGenerator(final MatchStrategy childStrategy,
             // TODO: Wouldn't this method be nicer with Deque<EffectiveStatement<?, ?>> ?
             final List<EffectiveStatement<?, ?>> stmtPath, final int offset) {
         final EffectiveStatement<?, ?> stmt = stmtPath.get(offset);
 
         // Try direct children first, which is simple
-        AbstractExplicitGenerator<?> ret = childStrategy.findGenerator(stmt, children);
+        AbstractExplicitGenerator<?, ?> ret = childStrategy.findGenerator(stmt, childGenerators);
         if (ret != null) {
             final int next = offset + 1;
             if (stmtPath.size() == next) {
@@ -179,7 +218,7 @@ abstract class AbstractCompositeGenerator<T extends EffectiveStatement<?, ?>> ex
             }
             if (ret instanceof AbstractCompositeGenerator) {
                 // We know how to descend down
-                return ((AbstractCompositeGenerator<?>) ret).findGenerator(childStrategy, stmtPath, next);
+                return ((AbstractCompositeGenerator<?, ?>) ret).findGenerator(childStrategy, stmtPath, next);
             }
             // Yeah, don't know how to continue here
             return null;
@@ -242,12 +281,12 @@ abstract class AbstractCompositeGenerator<T extends EffectiveStatement<?, ?>> ex
     }
 
     final void startUsesAugmentLinkage(final List<AugmentRequirement> requirements) {
-        for (Generator child : children) {
+        for (Generator child : childGenerators) {
             if (child instanceof UsesAugmentGenerator) {
                 requirements.add(((UsesAugmentGenerator) child).startLinkage());
             }
             if (child instanceof AbstractCompositeGenerator) {
-                ((AbstractCompositeGenerator<?>) child).startUsesAugmentLinkage(requirements);
+                ((AbstractCompositeGenerator<?, ?>) child).startUsesAugmentLinkage(requirements);
             }
         }
     }
@@ -272,9 +311,9 @@ abstract class AbstractCompositeGenerator<T extends EffectiveStatement<?, ?>> ex
         }
 
         if (unlinkedChildren == null) {
-            unlinkedChildren = children.stream()
+            unlinkedChildren = childGenerators.stream()
                 .filter(AbstractExplicitGenerator.class::isInstance)
-                .map(child -> (AbstractExplicitGenerator<?>) child)
+                .map(child -> (AbstractExplicitGenerator<?, ?>) child)
                 .collect(Collectors.toList());
         }
 
@@ -285,7 +324,7 @@ abstract class AbstractCompositeGenerator<T extends EffectiveStatement<?, ?>> ex
             while (it.hasNext()) {
                 final var child = it.next();
                 if (child instanceof AbstractExplicitGenerator) {
-                    if (((AbstractExplicitGenerator<?>) child).linkOriginalGenerator()) {
+                    if (((AbstractExplicitGenerator<?, ?>) child).linkOriginalGenerator()) {
                         progress = LinkageProgress.SOME;
                         it.remove();
 
@@ -294,7 +333,7 @@ abstract class AbstractCompositeGenerator<T extends EffectiveStatement<?, ?>> ex
                             if (unlinkedComposites.isEmpty()) {
                                 unlinkedComposites = new ArrayList<>();
                             }
-                            unlinkedComposites.add((AbstractCompositeGenerator<?>) child);
+                            unlinkedComposites.add((AbstractCompositeGenerator<?, ?>) child);
                         }
                     }
                 }
@@ -329,20 +368,21 @@ abstract class AbstractCompositeGenerator<T extends EffectiveStatement<?, ?>> ex
     }
 
     @Override
-    final AbstractCompositeGenerator<?> getOriginal() {
-        return (AbstractCompositeGenerator<?>) super.getOriginal();
+    final AbstractCompositeGenerator<S, R> getOriginal() {
+        return (AbstractCompositeGenerator<S, R>) super.getOriginal();
     }
 
     @Override
-    final AbstractCompositeGenerator<?> tryOriginal() {
-        return (AbstractCompositeGenerator<?>) super.tryOriginal();
+    final AbstractCompositeGenerator<S, R> tryOriginal() {
+        return (AbstractCompositeGenerator<S, R>) super.tryOriginal();
     }
 
-    final @Nullable OriginalLink originalChild(final QName childQName) {
+    final <X extends EffectiveStatement<?, ?>, Y extends RuntimeType> @Nullable OriginalLink<X, Y> originalChild(
+            final QName childQName) {
         // First try groupings/augments ...
         var found = findInferredGenerator(childQName);
         if (found != null) {
-            return OriginalLink.partial(found);
+            return (OriginalLink<X, Y>) OriginalLink.partial(found);
         }
 
         // ... no luck, we really need to start looking at our origin
@@ -351,7 +391,7 @@ abstract class AbstractCompositeGenerator<T extends EffectiveStatement<?, ?>> ex
             final QName prevQName = childQName.bindTo(prev.getQName().getModule());
             found = prev.findSchemaTreeGenerator(prevQName);
             if (found != null) {
-                return  found.originalLink();
+                return (OriginalLink<X, Y>) found.originalLink();
             }
         }
 
@@ -359,8 +399,8 @@ abstract class AbstractCompositeGenerator<T extends EffectiveStatement<?, ?>> ex
     }
 
     @Override
-    final AbstractExplicitGenerator<?> findSchemaTreeGenerator(final QName qname) {
-        final AbstractExplicitGenerator<?> found = super.findSchemaTreeGenerator(qname);
+    final AbstractExplicitGenerator<?, ?> findSchemaTreeGenerator(final QName qname) {
+        final AbstractExplicitGenerator<?, ?> found = super.findSchemaTreeGenerator(qname);
         return found != null ? found : findInferredGenerator(qname);
     }
 
@@ -384,7 +424,7 @@ abstract class AbstractCompositeGenerator<T extends EffectiveStatement<?, ?>> ex
         return null;
     }
 
-    private @Nullable AbstractExplicitGenerator<?> findInferredGenerator(final QName qname) {
+    private @Nullable AbstractExplicitGenerator<?, ?> findInferredGenerator(final QName qname) {
         // First search our local groupings ...
         for (var grouping : groupings) {
             final var gen = grouping.findSchemaTreeGenerator(qname.bindTo(grouping.statement().argument().getModule()));
@@ -425,7 +465,7 @@ abstract class AbstractCompositeGenerator<T extends EffectiveStatement<?, ?>> ex
         for (Generator child : this) {
             // Only process explicit generators here
             if (child instanceof AbstractExplicitGenerator) {
-                ((AbstractExplicitGenerator<?>) child).addAsGetterMethod(builder, builderFactory);
+                ((AbstractExplicitGenerator<?, ?>) child).addAsGetterMethod(builder, builderFactory);
             }
 
             final GeneratedType enclosedType = child.enclosedType(builderFactory);
@@ -439,22 +479,22 @@ abstract class AbstractCompositeGenerator<T extends EffectiveStatement<?, ?>> ex
         }
     }
 
-    private List<Generator> createChildren(final EffectiveStatement<?, ?> statement) {
-        final List<Generator> tmp = new ArrayList<>();
-        final List<AbstractAugmentGenerator> tmpAug = new ArrayList<>();
+    private @NonNull List<Generator> createChildren(final EffectiveStatement<?, ?> statement) {
+        final var tmp = new ArrayList<Generator>();
+        final var tmpAug = new ArrayList<AbstractAugmentGenerator>();
 
-        for (EffectiveStatement<?, ?> stmt : statement.effectiveSubstatements()) {
+        for (var stmt : statement.effectiveSubstatements()) {
             if (stmt instanceof ActionEffectiveStatement) {
                 if (!isAugmenting(stmt)) {
                     tmp.add(new ActionGenerator((ActionEffectiveStatement) stmt, this));
                 }
             } else if (stmt instanceof AnydataEffectiveStatement) {
                 if (!isAugmenting(stmt)) {
-                    tmp.add(new OpaqueObjectGenerator<>((AnydataEffectiveStatement) stmt, this));
+                    tmp.add(new OpaqueObjectGenerator.Anydata((AnydataEffectiveStatement) stmt, this));
                 }
             } else if (stmt instanceof AnyxmlEffectiveStatement) {
                 if (!isAugmenting(stmt)) {
-                    tmp.add(new OpaqueObjectGenerator<>((AnyxmlEffectiveStatement) stmt, this));
+                    tmp.add(new OpaqueObjectGenerator.Anyxml((AnyxmlEffectiveStatement) stmt, this));
                 }
             } else if (stmt instanceof CaseEffectiveStatement) {
                 tmp.add(new CaseGenerator((CaseEffectiveStatement) stmt, this));
@@ -472,9 +512,10 @@ abstract class AbstractCompositeGenerator<T extends EffectiveStatement<?, ?>> ex
             } else if (stmt instanceof IdentityEffectiveStatement) {
                 tmp.add(new IdentityGenerator((IdentityEffectiveStatement) stmt, this));
             } else if (stmt instanceof InputEffectiveStatement) {
+                final var cast = (InputEffectiveStatement) stmt;
                 // FIXME: do not generate legacy RPC layout
-                tmp.add(this instanceof RpcGenerator ? new RpcContainerGenerator((InputEffectiveStatement) stmt, this)
-                    : new OperationContainerGenerator((InputEffectiveStatement) stmt, this));
+                tmp.add(this instanceof RpcGenerator ? new RpcInputGenerator(cast, this)
+                    : new InputGenerator(cast, this));
             } else if (stmt instanceof LeafEffectiveStatement) {
                 if (!isAugmenting(stmt)) {
                     tmp.add(new LeafGenerator((LeafEffectiveStatement) stmt, this));
@@ -498,9 +539,10 @@ abstract class AbstractCompositeGenerator<T extends EffectiveStatement<?, ?>> ex
                     tmp.add(new NotificationGenerator((NotificationEffectiveStatement) stmt, this));
                 }
             } else if (stmt instanceof OutputEffectiveStatement) {
+                final var cast = (OutputEffectiveStatement) stmt;
                 // FIXME: do not generate legacy RPC layout
-                tmp.add(this instanceof RpcGenerator ? new RpcContainerGenerator((OutputEffectiveStatement) stmt, this)
-                    : new OperationContainerGenerator((OutputEffectiveStatement) stmt, this));
+                tmp.add(this instanceof RpcGenerator ? new RpcOutputGenerator(cast, this)
+                    : new OutputGenerator(cast, this));
             } else if (stmt instanceof RpcEffectiveStatement) {
                 tmp.add(new RpcGenerator((RpcEffectiveStatement) stmt, this));
             } else if (stmt instanceof TypedefEffectiveStatement) {