Rework AugmentRuntimeType and Choice/Case linkage
[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     void pushToInference(final SchemaInferenceStack dataTree) {
33         // No-op
34     }
35
36     @Override
37     GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
38
39         // We also are implementing target choice's type. This is tricky, as we need to cover two distinct cases:
40         // - being a child of a choice (i.e. normal definition)
41         // - being a child of an augment (i.e. augmented into a choice)
42         final AbstractCompositeGenerator<?, ?> parent = getParent();
43         final ChoiceGenerator choice;
44         if (parent instanceof AbstractAugmentGenerator) {
45             final AbstractCompositeGenerator<?, ?> target = ((AbstractAugmentGenerator) parent).targetGenerator();
46             verify(target instanceof ChoiceGenerator, "Unexpected parent augment %s target %s", parent, target);
47             choice = (ChoiceGenerator) target;
48         } else {
49             verify(parent instanceof ChoiceGenerator, "Unexpected parent %s", parent);
50             choice = (ChoiceGenerator) parent;
51         }
52
53         // Most generators have a parent->child dependency due to parent methods' return types and therefore children
54         // must not request parent's type. That is not true for choice->case relationship and hence we do not need to
55         // go through DefaultType here
56         final GeneratedTypeBuilder builder = builderFactory.newGeneratedTypeBuilder(typeName());
57         // Note: this needs to be the first type we mention as we are relying on that fact for global runtime type
58         //       choice/case indexing.
59         builder.addImplementsType(choice.getGeneratedType(builderFactory));
60
61         builder.addImplementsType(BindingTypes.DATA_OBJECT);
62         addAugmentable(builder);
63         addUsesInterfaces(builder, builderFactory);
64         addConcreteInterfaceMethods(builder);
65
66         final ModuleGenerator module = currentModule();
67         module.addQNameConstant(builder, localName());
68
69         addGetterMethods(builder, builderFactory);
70
71         annotateDeprecatedIfNecessary(builder);
72         builderFactory.addCodegenInformation(module, statement(), builder);
73         builder.setModuleName(module.statement().argument().getLocalName());
74
75         return builder.build();
76     }
77
78     @Override
79     CompositeRuntimeTypeBuilder<CaseEffectiveStatement, CaseRuntimeType> createBuilder(
80             final CaseEffectiveStatement statement) {
81         return new CompositeRuntimeTypeBuilder<>(statement) {
82             @Override
83             CaseRuntimeType build(final GeneratedType generatedType, final CaseEffectiveStatement statement,
84                     final List<RuntimeType> childTypes, final List<AugmentRuntimeType> augmentTypes) {
85                 return new DefaultCaseRuntimeType(generatedType, statement, childTypes, augmentTypes);
86             }
87         };
88     }
89 }