Unify GeneratedType verification
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / AbstractCompositeGenerator.java
index d63e6cf43893ad4fceed882ecaa5f580b5b54f5f..37918184d9472df5afb4cc504b2e9d8d13916e0a 100644 (file)
@@ -19,12 +19,12 @@ 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;
@@ -36,6 +36,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.FeatureEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.InputEffectiveStatement;
@@ -170,16 +171,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
@@ -204,9 +203,9 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
                 // Final step, return child
                 return ret;
             }
-            if (ret instanceof AbstractCompositeGenerator) {
+            if (ret instanceof AbstractCompositeGenerator<?, ?> composite) {
                 // We know how to descend down
-                return ((AbstractCompositeGenerator<?, ?>) ret).findGenerator(childStrategy, stmtPath, next);
+                return composite.findGenerator(childStrategy, stmtPath, next);
             }
             // Yeah, don't know how to continue here
             return null;
@@ -309,19 +308,16 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
             // Attempt to make progress on child linkage
             final var it = unlinkedChildren.iterator();
             while (it.hasNext()) {
-                final var child = it.next();
-                if (child instanceof AbstractExplicitGenerator) {
-                    if (((AbstractExplicitGenerator<?, ?>) child).linkOriginalGenerator()) {
-                        progress = LinkageProgress.SOME;
-                        it.remove();
-
-                        // If this is a composite generator we need to process is further
-                        if (child instanceof AbstractCompositeGenerator<?, ?> composite) {
-                            if (unlinkedComposites.isEmpty()) {
-                                unlinkedComposites = new ArrayList<>();
-                            }
-                            unlinkedComposites.add(composite);
+                if (it.next() instanceof AbstractExplicitGenerator<?, ?> explicit && explicit.linkOriginalGenerator()) {
+                    progress = LinkageProgress.SOME;
+                    it.remove();
+
+                    // If this is a composite generator we need to process is further
+                    if (explicit instanceof AbstractCompositeGenerator<?, ?> composite) {
+                        if (unlinkedComposites.isEmpty()) {
+                            unlinkedComposites = new ArrayList<>();
                         }
+                        unlinkedComposites.add(composite);
                     }
                 }
             }
@@ -494,13 +490,14 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
                 if (isOriginalDeclaration(container)) {
                     tmp.add(new ContainerGenerator(container, this));
                 }
+            } else if (stmt instanceof FeatureEffectiveStatement feature && this instanceof ModuleGenerator parent) {
+                tmp.add(new FeatureGenerator(feature, parent));
             } else if (stmt instanceof GroupingEffectiveStatement grouping) {
                 tmp.add(new GroupingGenerator(grouping, this));
             } else if (stmt instanceof IdentityEffectiveStatement identity) {
                 tmp.add(new IdentityGenerator(identity, this));
             } else if (stmt instanceof InputEffectiveStatement input) {
-                tmp.add(this instanceof RpcGenerator ? new RpcInputGenerator(input, this)
-                    : new InputGenerator(input, this));
+                tmp.add(new InputGenerator(input, this));
             } else if (stmt instanceof LeafEffectiveStatement leaf) {
                 if (!isAugmenting(leaf)) {
                     tmp.add(new LeafGenerator(leaf, this));
@@ -524,10 +521,11 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
                     tmp.add(new NotificationGenerator(notification, this));
                 }
             } else if (stmt instanceof OutputEffectiveStatement output) {
-                tmp.add(this instanceof RpcGenerator ? new RpcOutputGenerator(output, this)
-                    : new OutputGenerator(output, this));
+                tmp.add(new OutputGenerator(output, this));
             } else if (stmt instanceof RpcEffectiveStatement rpc) {
-                tmp.add(new RpcGenerator(rpc, this));
+                if (this instanceof ModuleGenerator module) {
+                    tmp.add(new RpcGenerator(rpc, module));
+                }
             } else if (stmt instanceof TypedefEffectiveStatement typedef) {
                 tmp.add(new TypedefGenerator(typedef, this));
             } else if (stmt instanceof AugmentEffectiveStatement augment) {
@@ -545,8 +543,8 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
                 //                   So here is where we should decide how to handle this augment, and make sure we
                 //                   retain information about this being an alias. That will serve as the base for keys
                 //                   in the augment -> original map we provide to BindingRuntimeTypes.
-                if (this instanceof ModuleGenerator) {
-                    tmpAug.add(new ModuleAugmentGenerator(augment, this));
+                if (this instanceof ModuleGenerator module) {
+                    tmpAug.add(new ModuleAugmentGenerator(augment, module));
                 }
             } else if (stmt instanceof UsesEffectiveStatement uses) {
                 for (var usesSub : uses.effectiveSubstatements()) {
@@ -554,6 +552,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,37 +567,15 @@ 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);
     }
 
     // Utility equivalent of (!isAddedByUses(stmt) && !isAugmenting(stmt)). Takes advantage of relationship between
     // CopyableNode and AddedByUsesAware
     private static boolean isOriginalDeclaration(final EffectiveStatement<?, ?> stmt) {
-        if (stmt instanceof AddedByUsesAware aware) {
-            if (aware.isAddedByUses()
-                || stmt instanceof CopyableNode copyable && copyable.isAugmenting()) {
-                return false;
-            }
+        if (stmt instanceof AddedByUsesAware aware
+            && (aware.isAddedByUses() || stmt instanceof CopyableNode copyable && copyable.isAugmenting())) {
+            return false;
         }
         return true;
     }