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 com.google.common.collect.ImmutableList;
13 import java.util.List;
14 import org.opendaylight.mdsal.binding.generator.impl.rt.DefaultChoiceRuntimeType;
15 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
16 import org.opendaylight.mdsal.binding.model.api.Type;
17 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
18 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
19 import org.opendaylight.mdsal.binding.runtime.api.AugmentRuntimeType;
20 import org.opendaylight.mdsal.binding.runtime.api.CaseRuntimeType;
21 import org.opendaylight.mdsal.binding.runtime.api.ChoiceRuntimeType;
22 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
28 * Generator corresponding to a {@code choice} statement.
30 final class ChoiceGenerator extends CompositeSchemaTreeGenerator<ChoiceEffectiveStatement, ChoiceRuntimeType> {
31 static final class ChoiceBuilder extends CompositeRuntimeTypeBuilder<ChoiceEffectiveStatement, ChoiceRuntimeType> {
32 private ImmutableList<CaseRuntimeType> augmentedCases;
34 ChoiceBuilder(final ChoiceEffectiveStatement statement) {
39 CompositeRuntimeTypeBuilder<ChoiceEffectiveStatement, ChoiceRuntimeType> fillTypes(
40 final ChildLookup lookup,
41 final AbstractCompositeGenerator<ChoiceEffectiveStatement, ChoiceRuntimeType> generator) {
42 fillAugmentedCases(lookup, generator.augments());
43 return super.fillTypes(lookup, generator);
47 boolean isAugmentedChild(final ChildLookup lookup, final QName qname) {
48 for (var augmented : augmentedCases) {
49 if (qname.equals(augmented.statement().argument())) {
53 return super.isAugmentedChild(lookup, qname);
57 ChoiceRuntimeType build(final GeneratedType type, final ChoiceEffectiveStatement statement,
58 final List<RuntimeType> children, final List<AugmentRuntimeType> augments) {
59 verify(augments.isEmpty(), "Unexpected augments %s", augments);
60 children.addAll(augmentedCases);
61 return new DefaultChoiceRuntimeType(type, statement, children);
64 private void fillAugmentedCases(final ChildLookup lookup, final List<AbstractAugmentGenerator> augments) {
65 final var builder = ImmutableList.<CaseRuntimeType>builder();
66 for (var augment : augments) {
67 builder.addAll(augment.augmentedCasesIn(lookup, statement()));
69 augmentedCases = builder.build();
73 ChoiceGenerator(final ChoiceEffectiveStatement statement, final AbstractCompositeGenerator<?, ?> parent) {
74 super(statement, parent);
78 void pushToInference(final SchemaInferenceStack dataTree) {
83 GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
84 final GeneratedTypeBuilder builder = builderFactory.newGeneratedTypeBuilder(typeName());
85 builder.addImplementsType(BindingTypes.choiceIn(Type.of(getParent().typeName())));
87 final ModuleGenerator module = currentModule();
88 module.addQNameConstant(builder, localName());
90 annotateDeprecatedIfNecessary(builder);
91 builderFactory.addCodegenInformation(module, statement(), builder);
92 // newType.setSchemaPath(schemaNode.getPath());
93 builder.setModuleName(module.statement().argument().getLocalName());
95 return builder.build();
99 ChoiceBuilder createBuilder(final ChoiceEffectiveStatement statement) {
100 return new ChoiceBuilder(statement);