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