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