Rework AugmentRuntimeType and Choice/Case linkage
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / ChoiceGenerator.java
index c45a09985725ff61e8ca9da4f20ac7adc09e4e44..1b73a7d4d55be018dd55f98437c46d0770e0a535 100644 (file)
@@ -7,18 +7,70 @@
  */
 package org.opendaylight.mdsal.binding.generator.impl.reactor;
 
+import static com.google.common.base.Verify.verify;
+
+import com.google.common.collect.ImmutableList;
+import java.util.List;
+import org.opendaylight.mdsal.binding.generator.impl.rt.DefaultChoiceRuntimeType;
 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.AugmentRuntimeType;
+import org.opendaylight.mdsal.binding.runtime.api.CaseRuntimeType;
+import org.opendaylight.mdsal.binding.runtime.api.ChoiceRuntimeType;
+import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
 
 /**
  * Generator corresponding to a {@code choice} statement.
  */
-final class ChoiceGenerator extends CompositeSchemaTreeGenerator<ChoiceEffectiveStatement, ChoiceGenerator> {
-    ChoiceGenerator(final ChoiceEffectiveStatement statement, final AbstractCompositeGenerator<?> parent) {
+final class ChoiceGenerator extends CompositeSchemaTreeGenerator<ChoiceEffectiveStatement, ChoiceRuntimeType> {
+    static final class ChoiceBuilder extends CompositeRuntimeTypeBuilder<ChoiceEffectiveStatement, ChoiceRuntimeType> {
+        private ImmutableList<CaseRuntimeType> augmentedCases;
+
+        ChoiceBuilder(final ChoiceEffectiveStatement statement) {
+            super(statement);
+        }
+
+        @Override
+        CompositeRuntimeTypeBuilder<ChoiceEffectiveStatement, ChoiceRuntimeType> fillTypes(
+                final ChildLookup lookup,
+                final AbstractCompositeGenerator<ChoiceEffectiveStatement, ChoiceRuntimeType> generator) {
+            fillAugmentedCases(lookup, generator.augments());
+            return super.fillTypes(lookup, generator);
+        }
+
+        @Override
+        boolean isAugmentedChild(final ChildLookup lookup, final QName qname) {
+            for (var augmented : augmentedCases) {
+                if (qname.equals(augmented.statement().argument())) {
+                    return true;
+                }
+            }
+            return super.isAugmentedChild(lookup, qname);
+        }
+
+        @Override
+        ChoiceRuntimeType build(final GeneratedType type, final ChoiceEffectiveStatement statement,
+                final List<RuntimeType> children, final List<AugmentRuntimeType> augments) {
+            verify(augments.isEmpty(), "Unexpected augments %s", augments);
+            children.addAll(augmentedCases);
+            return new DefaultChoiceRuntimeType(type, statement, children);
+        }
+
+        private void fillAugmentedCases(final ChildLookup lookup, final List<AbstractAugmentGenerator> augments) {
+            final var builder = ImmutableList.<CaseRuntimeType>builder();
+            for (var augment : augments) {
+                builder.addAll(augment.augmentedCasesIn(lookup, statement()));
+            }
+            augmentedCases = builder.build();
+        }
+    }
+
+    ChoiceGenerator(final ChoiceEffectiveStatement statement, final AbstractCompositeGenerator<?, ?> parent) {
         super(statement, parent);
     }
 
@@ -42,4 +94,9 @@ final class ChoiceGenerator extends CompositeSchemaTreeGenerator<ChoiceEffective
 
         return builder.build();
     }
+
+    @Override
+    ChoiceBuilder createBuilder(final ChoiceEffectiveStatement statement) {
+        return new ChoiceBuilder(statement);
+    }
 }