Track schema tree generator 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 org.opendaylight.mdsal.binding.model.api.GeneratedType;
13 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
14 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
15 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
17
18 /**
19  * Generator corresponding to a {@code case} statement.
20  */
21 final class CaseGenerator extends CompositeSchemaTreeGenerator<CaseEffectiveStatement, CaseGenerator> {
22     CaseGenerator(final CaseEffectiveStatement statement, final AbstractCompositeGenerator<?> parent) {
23         super(statement, parent);
24     }
25
26     @Override
27     void pushToInference(final SchemaInferenceStack dataTree) {
28         // No-op
29     }
30
31     @Override
32     GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
33         final GeneratedTypeBuilder builder = builderFactory.newGeneratedTypeBuilder(typeName());
34         builder.addImplementsType(BindingTypes.DATA_OBJECT);
35
36         // We also are implementing target choice's type. This is tricky, as we need to cover two distinct cases:
37         // - being a child of a choice (i.e. normal definition)
38         // - being a child of an augment (i.e. augmented into a choice)
39         final AbstractCompositeGenerator<?> parent = getParent();
40         final ChoiceGenerator choice;
41         if (parent instanceof AbstractAugmentGenerator) {
42             final AbstractCompositeGenerator<?> target = ((AbstractAugmentGenerator) parent).targetGenerator();
43             verify(target instanceof ChoiceGenerator, "Unexpected parent augment %s target %s", parent, target);
44             choice = (ChoiceGenerator) target;
45         } else {
46             verify(parent instanceof ChoiceGenerator, "Unexpected parent %s", parent);
47             choice = (ChoiceGenerator) parent;
48         }
49
50         // Most generators have a parent->child dependency due to parent methods' return types and therefore children
51         // must not request parent's type. That is not true for choice->case relationship and hence we do not need to
52         // go through DefaultType here.
53         builder.addImplementsType(choice.getGeneratedType(builderFactory));
54         addAugmentable(builder);
55         addUsesInterfaces(builder, builderFactory);
56         addConcreteInterfaceMethods(builder);
57
58         final ModuleGenerator module = currentModule();
59         module.addQNameConstant(builder, localName());
60
61         addGetterMethods(builder, builderFactory);
62
63         annotateDeprecatedIfNecessary(builder);
64         builderFactory.addCodegenInformation(module, statement(), builder);
65         builder.setModuleName(module.statement().argument().getLocalName());
66
67         return builder.build();
68     }
69 }