Clean up constant reference
[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 java.util.List;
11 import org.opendaylight.mdsal.binding.generator.impl.rt.DefaultActionRuntimeType;
12 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
13 import org.opendaylight.mdsal.binding.model.api.Type;
14 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
15 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
16 import org.opendaylight.mdsal.binding.model.ri.Types;
17 import org.opendaylight.mdsal.binding.runtime.api.ActionRuntimeType;
18 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
19 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
20 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
21
22 /**
23  * Generator corresponding to a {@code action} statement.
24  */
25 final class ActionGenerator extends AbstractInvokableGenerator<ActionEffectiveStatement, ActionRuntimeType> {
26     ActionGenerator(final ActionEffectiveStatement statement, final AbstractCompositeGenerator<?, ?> parent) {
27         super(statement, parent);
28     }
29
30     @Override
31     StatementNamespace namespace() {
32         return StatementNamespace.ACTION;
33     }
34
35     @Override
36     ClassPlacement classPlacement() {
37         // We do not generate Actions for groupings as they are inexact, and do not capture an actual instantiation --
38         // therefore they do not have an InstanceIdentifier. We still need to allocate a package name for the purposes
39         // of generating shared classes for input/output
40         return getParent() instanceof GroupingGenerator ? ClassPlacement.PHANTOM : ClassPlacement.TOP_LEVEL;
41     }
42
43     @Override
44     void addImplementedType(final TypeBuilderFactory builderFactory, final GeneratedTypeBuilder builder,
45             final GeneratedType input, final GeneratedType output) {
46         final var parent = getParent();
47         final var parentType = Type.of(parent.typeName());
48         if (parent instanceof ListGenerator list) {
49             final var keyGen = list.keyGenerator();
50             if (keyGen != null) {
51                 final var keyType = keyGen.getGeneratedType(builderFactory);
52                 builder.addImplementsType(BindingTypes.keyedListAction(parentType, keyType, input, output));
53                 builder.addMethod(BindingMapping.ACTION_INVOKE_NAME).setAbstract(true)
54                     .addParameter(BindingTypes.keyedInstanceIdentifier(parentType, keyType), "path")
55                     .addParameter(input, "input")
56                     .setReturnType(Types.listenableFutureTypeFor(BindingTypes.rpcResult(output)))
57                     .addAnnotation(OVERRIDE_ANNOTATION);
58                 return;
59             }
60         }
61         builder.addImplementsType(BindingTypes.action(parentType, input, output));
62         builder.addMethod(BindingMapping.ACTION_INVOKE_NAME).setAbstract(true)
63             .addParameter(BindingTypes.instanceIdentifier(parentType), "path")
64             .addParameter(input, "input")
65             .setReturnType(Types.listenableFutureTypeFor(BindingTypes.rpcResult(output)))
66             .addAnnotation(OVERRIDE_ANNOTATION);
67     }
68
69     @Override
70     CompositeRuntimeTypeBuilder<ActionEffectiveStatement, ActionRuntimeType> createBuilder(
71             final ActionEffectiveStatement statement) {
72         return new InvokableRuntimeTypeBuilder<>(statement) {
73             @Override
74             ActionRuntimeType build(final GeneratedType generatedType, final ActionEffectiveStatement statement,
75                     final List<RuntimeType> childTypes) {
76                 return new DefaultActionRuntimeType(generatedType, statement, childTypes);
77             }
78         };
79     }
80 }