RFC8040 'rc:yang-data' support for mdsal binding generator
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / AbstractCompositeGenerator.java
index d63e6cf43893ad4fceed882ecaa5f580b5b54f5f..93f8dbde3c1ad4fe9fa36b6c33d567e85335f61c 100644 (file)
@@ -25,6 +25,7 @@ import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilde
 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 +37,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;
@@ -204,9 +206,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 +311,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 +493,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 +524,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 +546,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 +555,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);
             }
@@ -591,11 +596,9 @@ public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?,
     // 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;
     }