Pick up byte-buddy from yangtools
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / ChoiceGenerator.java
index 06373ff9611d3c832bbc5bafd4d7e8687a154b54..cd8a08ffbfcc0bb66ac0921a761631297197d063 100644 (file)
@@ -7,21 +7,68 @@
  */
 package org.opendaylight.mdsal.binding.generator.impl.reactor;
 
+import static com.google.common.base.Verify.verify;
+
+import java.util.ArrayList;
+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.binding.contract.StatementNamespace;
+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 AbstractCompositeGenerator<ChoiceEffectiveStatement> {
-    ChoiceGenerator(final ChoiceEffectiveStatement statement, final AbstractCompositeGenerator<?> parent) {
+final class ChoiceGenerator extends CompositeSchemaTreeGenerator<ChoiceEffectiveStatement, ChoiceRuntimeType> {
+    static final class ChoiceBuilder extends CompositeRuntimeTypeBuilder<ChoiceEffectiveStatement, ChoiceRuntimeType> {
+        private final List<CaseRuntimeType> augmentedCases = new ArrayList<>();
+
+        ChoiceBuilder(final ChoiceEffectiveStatement statement) {
+            super(statement);
+        }
+
+        @Override
+        void processAugment(final AugmentResolver resolver, final AbstractAugmentGenerator augment) {
+            augment.fillRuntimeCasesIn(resolver, statement(), augmentedCases);
+        }
+
+        @Override
+        boolean isAugmentedChild(final QName qname) {
+            for (var augmented : augmentedCases) {
+                if (qname.equals(augmented.statement().argument())) {
+                    return true;
+                }
+            }
+            return false;
+        }
+
+        @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);
+        }
+    }
+
+    ChoiceGenerator(final ChoiceEffectiveStatement statement, final AbstractCompositeGenerator<?, ?> parent) {
         super(statement, parent);
     }
 
+    @Override
+    StatementNamespace namespace() {
+        return StatementNamespace.CHOICE;
+    }
+
     @Override
     void pushToInference(final SchemaInferenceStack dataTree) {
         // No-op
@@ -36,12 +83,15 @@ final class ChoiceGenerator extends AbstractCompositeGenerator<ChoiceEffectiveSt
         module.addQNameConstant(builder, localName());
 
         annotateDeprecatedIfNecessary(builder);
-        if (builderFactory instanceof TypeBuilderFactory.Codegen) {
-            addCodegenInformation(module, statement(), builder);
-        }
+        builderFactory.addCodegenInformation(module, statement(), builder);
 //      newType.setSchemaPath(schemaNode.getPath());
         builder.setModuleName(module.statement().argument().getLocalName());
 
         return builder.build();
     }
+
+    @Override
+    ChoiceBuilder createBuilder(final ChoiceEffectiveStatement statement) {
+        return new ChoiceBuilder(statement);
+    }
 }