Rework AugmentRuntimeType and Choice/Case linkage
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / ActionGenerator.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.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.mdsal.binding.generator.impl.rt.DefaultActionRuntimeType;
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.api.type.builder.GeneratedTypeBuilderBase;
19 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
20 import org.opendaylight.mdsal.binding.runtime.api.ActionRuntimeType;
21 import org.opendaylight.mdsal.binding.runtime.api.AugmentRuntimeType;
22 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.InputEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.OutputEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
27
28 /**
29  * Generator corresponding to a {@code action} statement.
30  */
31 final class ActionGenerator extends CompositeSchemaTreeGenerator<ActionEffectiveStatement, ActionRuntimeType> {
32     ActionGenerator(final ActionEffectiveStatement statement, final AbstractCompositeGenerator<?, ?> parent) {
33         super(statement, parent);
34     }
35
36     @Override
37     void pushToInference(final SchemaInferenceStack dataTree) {
38         dataTree.enterSchemaTree(statement().getIdentifier());
39     }
40
41     @Override
42     ClassPlacement classPlacement() {
43         // We do not generate Actions for groupings as they are inexact, and do not capture an actual instantiation --
44         // therefore they do not have an InstanceIdentifier. We still need to allocate a package name for the purposes
45         // of generating shared classes for input/output
46         return getParent() instanceof GroupingGenerator ? ClassPlacement.PHANTOM : ClassPlacement.TOP_LEVEL;
47     }
48
49     @Override
50     GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
51         final GeneratedTypeBuilder builder = builderFactory.newGeneratedTypeBuilder(typeName());
52         builder.addImplementsType(implementedType(builderFactory));
53
54         final ModuleGenerator module = currentModule();
55         module.addQNameConstant(builder, statement().argument());
56
57 //        addGetterMethods(builder, builderFactory);
58
59         annotateDeprecatedIfNecessary(builder);
60         builderFactory.addCodegenInformation(module, statement(), builder);
61
62         return builder.build();
63     }
64
65     private @NonNull Type implementedType(final TypeBuilderFactory builderFactory) {
66         final GeneratedType input = getChild(this, InputEffectiveStatement.class).getOriginal()
67             .getGeneratedType(builderFactory);
68         final GeneratedType output = getChild(this, OutputEffectiveStatement.class).getOriginal()
69             .getGeneratedType(builderFactory);
70
71         final AbstractCompositeGenerator<?, ?> parent = getParent();
72         if (parent instanceof ListGenerator) {
73             final KeyGenerator keyGen = ((ListGenerator) parent).keyGenerator();
74             if (keyGen != null) {
75                 return BindingTypes.keyedListAction(Type.of(parent.typeName()), keyGen.getGeneratedType(builderFactory),
76                     input, output);
77             }
78         }
79
80         return BindingTypes.action(Type.of(parent.typeName()), input, output);
81     }
82
83     @Override
84     void addAsGetterMethod(final GeneratedTypeBuilderBase<?> builder, final TypeBuilderFactory builderFactory) {
85         // actions are a separate concept
86     }
87
88     @Override
89     CompositeRuntimeTypeBuilder<ActionEffectiveStatement, ActionRuntimeType> createBuilder(
90             final ActionEffectiveStatement statement) {
91         return new CompositeRuntimeTypeBuilder<>(statement) {
92             @Override
93             ActionRuntimeType build(final GeneratedType generatedType, final ActionEffectiveStatement statement,
94                     final List<RuntimeType> childTypes, final List<AugmentRuntimeType> augmentTypes) {
95                 verify(augmentTypes.isEmpty(), "Unexpected augments %s", augmentTypes);
96                 return new DefaultActionRuntimeType(generatedType, statement, childTypes);
97             }
98         };
99     }
100 }