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