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.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;
24 * Generator corresponding to a {@code case} statement.
26 final class CaseGenerator extends CompositeSchemaTreeGenerator<CaseEffectiveStatement, CaseRuntimeType> {
27 CaseGenerator(final CaseEffectiveStatement statement, final AbstractCompositeGenerator<?, ?> parent) {
28 super(statement, parent);
32 void pushToInference(final SchemaInferenceStack dataTree) {
37 GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
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;
49 verify(parent instanceof ChoiceGenerator, "Unexpected parent %s", parent);
50 choice = (ChoiceGenerator) parent;
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));
61 builder.addImplementsType(BindingTypes.DATA_OBJECT);
62 addAugmentable(builder);
63 addUsesInterfaces(builder, builderFactory);
64 addConcreteInterfaceMethods(builder);
66 final ModuleGenerator module = currentModule();
67 module.addQNameConstant(builder, localName());
69 addGetterMethods(builder, builderFactory);
71 annotateDeprecatedIfNecessary(builder);
72 builderFactory.addCodegenInformation(module, statement(), builder);
73 builder.setModuleName(module.statement().argument().getLocalName());
75 return builder.build();
79 CompositeRuntimeTypeBuilder<CaseEffectiveStatement, CaseRuntimeType> createBuilder(
80 final CaseEffectiveStatement statement) {
81 return new CompositeRuntimeTypeBuilder<>(statement) {
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);