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