2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.generator.impl.reactor;
10 import static com.google.common.base.Verify.verify;
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;
25 * Generator corresponding to a {@code case} statement.
27 final class CaseGenerator extends CompositeSchemaTreeGenerator<CaseEffectiveStatement, CaseRuntimeType> {
28 CaseGenerator(final CaseEffectiveStatement statement, final AbstractCompositeGenerator<?, ?> parent) {
29 super(statement, parent);
33 StatementNamespace namespace() {
34 return StatementNamespace.CASE;
38 void pushToInference(final SchemaInferenceStack dataTree) {
43 GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
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;
55 verify(parent instanceof ChoiceGenerator, "Unexpected parent %s", parent);
56 choice = (ChoiceGenerator) parent;
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));
67 builder.addImplementsType(BindingTypes.DATA_OBJECT);
68 addAugmentable(builder);
69 addUsesInterfaces(builder, builderFactory);
70 addConcreteInterfaceMethods(builder);
72 final ModuleGenerator module = currentModule();
73 module.addQNameConstant(builder, localName());
75 addGetterMethods(builder, builderFactory);
77 annotateDeprecatedIfNecessary(builder);
78 builderFactory.addCodegenInformation(module, statement(), builder);
79 builder.setModuleName(module.statement().argument().getLocalName());
81 return builder.build();
85 CompositeRuntimeTypeBuilder<CaseEffectiveStatement, CaseRuntimeType> createBuilder(
86 final CaseEffectiveStatement statement) {
87 return new CompositeRuntimeTypeBuilder<>(statement) {
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);