Retain grouping/uses instantiation vectors
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / AbstractCompositeGenerator.java
index 55352d60e8cf5461763d326bf6a0268ea1ba3c0b..5d74b61ad083ffde5e27683cd134c432daa13ada 100644 (file)
@@ -14,17 +14,18 @@ import static java.util.Objects.requireNonNull;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
 import java.util.stream.Collectors;
 import org.eclipse.jdt.annotation.NonNull;
 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.rfc8040.model.api.YangDataEffectiveStatement;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AddedByUsesAware;
 import org.opendaylight.yangtools.yang.model.api.CopyableNode;
@@ -171,16 +172,14 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
 
     @Override
     final R createExternalRuntimeType(final Type type) {
-        verify(type instanceof GeneratedType, "Unexpected type %s", type);
-        return createBuilder(statement()).populate(new AugmentResolver(), this).build((GeneratedType) type);
+        return createBuilder(statement()).populate(new AugmentResolver(), this).build(verifyGeneratedType(type));
     }
 
     abstract @NonNull CompositeRuntimeTypeBuilder<S, R> createBuilder(S statement);
 
     @Override
     final R createInternalRuntimeType(final AugmentResolver resolver, final S statement, final Type type) {
-        verify(type instanceof GeneratedType, "Unexpected type %s", type);
-        return createBuilder(statement).populate(resolver, this).build((GeneratedType) type);
+        return createBuilder(statement).populate(resolver, this).build(verifyGeneratedType(type));
     }
 
     @Override
@@ -268,6 +267,29 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
         groupings = List.copyOf(tmp);
     }
 
+    // Iterate over a some generators recursively, linking them to the GroupingGenerators they use. GroupingGenerators
+    // are skipped and added to unprocessedGroupings for later processing.
+    final void linkUsedGroupings(final Set<GroupingGenerator> skippedChildren) {
+        // Link to used groupings IFF we have a corresponding generated Java class
+        switch (classPlacement()) {
+            case NONE:
+            case PHANTOM:
+                break;
+            default:
+                for (var grouping : groupings()) {
+                    grouping.addUser(this);
+                }
+        }
+
+        for (var child : childGenerators) {
+            if (child instanceof GroupingGenerator grouping) {
+                skippedChildren.add(grouping);
+            } else if (child instanceof AbstractCompositeGenerator<?, ?> composite) {
+                composite.linkUsedGroupings(skippedChildren);
+            }
+        }
+    }
+
     final void startUsesAugmentLinkage(final List<AugmentRequirement> requirements) {
         for (var child : childGenerators) {
             if (child instanceof UsesAugmentGenerator uses) {
@@ -554,6 +576,10 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
                         tmpAug.add(new UsesAugmentGenerator(usesAug, uses, this));
                     }
                 }
+            } else if (stmt instanceof YangDataEffectiveStatement yangData) {
+                if (this instanceof ModuleGenerator moduleGen) {
+                    tmp.add(YangDataGenerator.of(yangData, moduleGen));
+                }
             } else {
                 LOG.trace("Ignoring statement {}", stmt);
             }
@@ -565,26 +591,6 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
         // substatements to establish this order.
         tmpAug.sort(AbstractAugmentGenerator.COMPARATOR);
         tmp.addAll(tmpAug);
-
-        // Compatibility FooService and FooListener interfaces, only generated for modules.
-        if (this instanceof ModuleGenerator moduleGen) {
-            final List<NotificationGenerator> notifs = tmp.stream()
-                .filter(NotificationGenerator.class::isInstance)
-                .map(NotificationGenerator.class::cast)
-                .collect(Collectors.toUnmodifiableList());
-            if (!notifs.isEmpty()) {
-                tmp.add(new NotificationServiceGenerator(moduleGen, notifs));
-            }
-
-            final List<RpcGenerator> rpcs = tmp.stream()
-                .filter(RpcGenerator.class::isInstance)
-                .map(RpcGenerator.class::cast)
-                .collect(Collectors.toUnmodifiableList());
-            if (!rpcs.isEmpty()) {
-                tmp.add(new RpcServiceGenerator(moduleGen, rpcs));
-            }
-        }
-
         return List.copyOf(tmp);
     }
 
@@ -592,7 +598,7 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
     // CopyableNode and AddedByUsesAware
     private static boolean isOriginalDeclaration(final EffectiveStatement<?, ?> stmt) {
         if (stmt instanceof AddedByUsesAware aware
-            && (aware.isAddedByUses() || stmt instanceof CopyableNode copyable && copyable.isAugmenting())) {
+            && (aware.isAddedByUses() || aware instanceof CopyableNode copyable && copyable.isAugmenting())) {
             return false;
         }
         return true;