f6dd71b5f5a9b0dc3b5d3be13f0df5e5e2a93e0b
[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 org.eclipse.jdt.annotation.NonNull;
11 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
12 import org.opendaylight.mdsal.binding.model.api.Type;
13 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
14 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
15 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
16 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.InputEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.OutputEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
20
21 /**
22  * Generator corresponding to a {@code action} statement.
23  */
24 final class ActionGenerator extends CompositeSchemaTreeGenerator<ActionEffectiveStatement, ActionGenerator> {
25     ActionGenerator(final ActionEffectiveStatement statement, final AbstractCompositeGenerator<?> parent) {
26         super(statement, parent);
27     }
28
29     @Override
30     void pushToInference(final SchemaInferenceStack dataTree) {
31         dataTree.enterSchemaTree(statement().getIdentifier());
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     GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
44         final GeneratedTypeBuilder builder = builderFactory.newGeneratedTypeBuilder(typeName());
45         builder.addImplementsType(implementedType(builderFactory));
46
47         final ModuleGenerator module = currentModule();
48         module.addQNameConstant(builder, statement().argument());
49
50 //        addGetterMethods(builder, builderFactory);
51
52         annotateDeprecatedIfNecessary(builder);
53         builderFactory.addCodegenInformation(module, statement(), builder);
54
55         return builder.build();
56     }
57
58     private @NonNull Type implementedType(final TypeBuilderFactory builderFactory) {
59         final GeneratedType input = getChild(this, InputEffectiveStatement.class).getOriginal()
60             .getGeneratedType(builderFactory);
61         final GeneratedType output = getChild(this, OutputEffectiveStatement.class).getOriginal()
62             .getGeneratedType(builderFactory);
63
64         final AbstractCompositeGenerator<?> parent = getParent();
65         if (parent instanceof ListGenerator) {
66             final KeyGenerator keyGen = ((ListGenerator) parent).keyGenerator();
67             if (keyGen != null) {
68                 return BindingTypes.keyedListAction(Type.of(parent.typeName()), keyGen.getGeneratedType(builderFactory),
69                     input, output);
70             }
71         }
72
73         return BindingTypes.action(Type.of(parent.typeName()), input, output);
74     }
75
76     @Override
77     void addAsGetterMethod(final GeneratedTypeBuilderBase<?> builder, final TypeBuilderFactory builderFactory) {
78         // actions are a separate concept
79     }
80 }