Fix runtime type search
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / CaseGenerator.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.generator.impl.reactor;
9
10 import static com.google.common.base.Verify.verify;
11
12 import java.util.List;
13 import org.opendaylight.mdsal.binding.generator.impl.rt.DefaultCaseRuntimeType;
14 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
15 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
16 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
17 import org.opendaylight.mdsal.binding.runtime.api.AugmentRuntimeType;
18 import org.opendaylight.mdsal.binding.runtime.api.CaseRuntimeType;
19 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
20 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
22
23 /**
24  * Generator corresponding to a {@code case} statement.
25  */
26 final class CaseGenerator extends CompositeSchemaTreeGenerator<CaseEffectiveStatement, CaseRuntimeType> {
27     CaseGenerator(final CaseEffectiveStatement statement, final AbstractCompositeGenerator<?, ?> parent) {
28         super(statement, parent);
29     }
30
31     @Override
32     StatementNamespace namespace() {
33         return StatementNamespace.CASE;
34     }
35
36     @Override
37     void pushToInference(final SchemaInferenceStack dataTree) {
38         // No-op
39     }
40
41     @Override
42     GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
43
44         // We also are implementing target choice's type. This is tricky, as we need to cover two distinct cases:
45         // - being a child of a choice (i.e. normal definition)
46         // - being a child of an augment (i.e. augmented into a choice)
47         final AbstractCompositeGenerator<?, ?> parent = getParent();
48         final ChoiceGenerator choice;
49         if (parent instanceof AbstractAugmentGenerator augGen) {
50             final AbstractCompositeGenerator<?, ?> target = augGen.targetGenerator();
51             verify(target instanceof ChoiceGenerator, "Unexpected parent augment %s target %s", parent, target);
52             choice = (ChoiceGenerator) target;
53         } else {
54             verify(parent instanceof ChoiceGenerator, "Unexpected parent %s", parent);
55             choice = (ChoiceGenerator) parent;
56         }
57
58         // Most generators have a parent->child dependency due to parent methods' return types and therefore children
59         // must not request parent's type. That is not true for choice->case relationship and hence we do not need to
60         // go through DefaultType here
61         final GeneratedTypeBuilder builder = builderFactory.newGeneratedTypeBuilder(typeName());
62         // Note: this needs to be the first type we mention as we are relying on that fact for global runtime type
63         //       choice/case indexing.
64         builder.addImplementsType(choice.getGeneratedType(builderFactory));
65
66         builder.addImplementsType(BindingTypes.DATA_OBJECT);
67         addAugmentable(builder);
68         addUsesInterfaces(builder, builderFactory);
69         addConcreteInterfaceMethods(builder);
70
71         final ModuleGenerator module = currentModule();
72         module.addQNameConstant(builder, localName());
73
74         addGetterMethods(builder, builderFactory);
75
76         annotateDeprecatedIfNecessary(builder);
77         builderFactory.addCodegenInformation(module, statement(), builder);
78         builder.setModuleName(module.statement().argument().getLocalName());
79
80         return builder.build();
81     }
82
83     @Override
84     CompositeRuntimeTypeBuilder<CaseEffectiveStatement, CaseRuntimeType> createBuilder(
85             final CaseEffectiveStatement statement) {
86         return new CompositeRuntimeTypeBuilder<>(statement) {
87             @Override
88             CaseRuntimeType build(final GeneratedType generatedType, final CaseEffectiveStatement statement,
89                     final List<RuntimeType> childTypes, final List<AugmentRuntimeType> augmentTypes) {
90                 return new DefaultCaseRuntimeType(generatedType, statement, childTypes, augmentTypes);
91             }
92         };
93     }
94 }