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