Generate Rpc services
[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.ParameterizedType;
14 import org.opendaylight.mdsal.binding.model.api.Type;
15 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
16 import org.opendaylight.mdsal.binding.runtime.api.ActionRuntimeType;
17 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
19
20 /**
21  * Generator corresponding to a {@code action} statement.
22  */
23 final class ActionGenerator extends AbstractInvokableGenerator<ActionEffectiveStatement, ActionRuntimeType> {
24     ActionGenerator(final ActionEffectiveStatement statement, final AbstractCompositeGenerator<?, ?> parent) {
25         super(statement, parent);
26     }
27
28     @Override
29     ClassPlacement classPlacement() {
30         // We do not generate Actions for groupings as they are inexact, and do not capture an actual instantiation --
31         // therefore they do not have an InstanceIdentifier. We still need to allocate a package name for the purposes
32         // of generating shared classes for input/output
33         return getParent() instanceof GroupingGenerator ? ClassPlacement.PHANTOM : ClassPlacement.TOP_LEVEL;
34     }
35
36     @Override
37     ParameterizedType implementedType(final TypeBuilderFactory builderFactory, final GeneratedType input,
38             final GeneratedType output) {
39         final var parent = getParent();
40         final var parentType = Type.of(parent.typeName());
41         if (parent instanceof ListGenerator list) {
42             final var keyGen = list.keyGenerator();
43             if (keyGen != null) {
44                 return BindingTypes.keyedListAction(parentType, keyGen.getGeneratedType(builderFactory), input, output);
45             }
46         }
47         return BindingTypes.action(parentType, input, output);
48     }
49
50     @Override
51     CompositeRuntimeTypeBuilder<ActionEffectiveStatement, ActionRuntimeType> createBuilder(
52             final ActionEffectiveStatement statement) {
53         return new InvokableRuntimeTypeBuilder<>(statement) {
54             @Override
55             ActionRuntimeType build(final GeneratedType generatedType, final ActionEffectiveStatement statement,
56                     final List<RuntimeType> childTypes) {
57                 return new DefaultActionRuntimeType(generatedType, statement, childTypes);
58             }
59         };
60     }
61 }