Expand StatementNamespace
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / ChoiceGenerator.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.ArrayList;
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;
26
27 /**
28  * Generator corresponding to a {@code choice} statement.
29  */
30 final class ChoiceGenerator extends CompositeSchemaTreeGenerator<ChoiceEffectiveStatement, ChoiceRuntimeType> {
31     static final class ChoiceBuilder extends CompositeRuntimeTypeBuilder<ChoiceEffectiveStatement, ChoiceRuntimeType> {
32         private final List<CaseRuntimeType> augmentedCases = new ArrayList<>();
33
34         ChoiceBuilder(final ChoiceEffectiveStatement statement) {
35             super(statement);
36         }
37
38         @Override
39         void processAugment(final AugmentResolver resolver, final AbstractAugmentGenerator augment) {
40             augment.fillRuntimeCasesIn(resolver, statement(), augmentedCases);
41         }
42
43         @Override
44         boolean isAugmentedChild(final QName qname) {
45             for (var augmented : augmentedCases) {
46                 if (qname.equals(augmented.statement().argument())) {
47                     return true;
48                 }
49             }
50             return false;
51         }
52
53         @Override
54         ChoiceRuntimeType build(final GeneratedType type, final ChoiceEffectiveStatement statement,
55                 final List<RuntimeType> children, final List<AugmentRuntimeType> augments) {
56             verify(augments.isEmpty(), "Unexpected augments %s", augments);
57             children.addAll(augmentedCases);
58             return new DefaultChoiceRuntimeType(type, statement, children);
59         }
60     }
61
62     ChoiceGenerator(final ChoiceEffectiveStatement statement, final AbstractCompositeGenerator<?, ?> parent) {
63         super(statement, parent);
64     }
65
66     @Override
67     StatementNamespace namespace() {
68         return StatementNamespace.CHOICE;
69     }
70
71     @Override
72     void pushToInference(final SchemaInferenceStack dataTree) {
73         // No-op
74     }
75
76     @Override
77     GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
78         final GeneratedTypeBuilder builder = builderFactory.newGeneratedTypeBuilder(typeName());
79         builder.addImplementsType(BindingTypes.choiceIn(Type.of(getParent().typeName())));
80
81         final ModuleGenerator module = currentModule();
82         module.addQNameConstant(builder, localName());
83
84         annotateDeprecatedIfNecessary(builder);
85         builderFactory.addCodegenInformation(module, statement(), builder);
86 //      newType.setSchemaPath(schemaNode.getPath());
87         builder.setModuleName(module.statement().argument().getLocalName());
88
89         return builder.build();
90     }
91
92     @Override
93     ChoiceBuilder createBuilder(final ChoiceEffectiveStatement statement) {
94         return new ChoiceBuilder(statement);
95     }
96 }