2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.generator.impl.reactor;
10 import static com.google.common.base.Verify.verify;
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;
30 * Generator corresponding to a {@code action} statement.
32 final class ActionGenerator extends CompositeSchemaTreeGenerator<ActionEffectiveStatement, ActionRuntimeType> {
33 private static final JavaTypeName FUNCTIONAL_INTERFACE_ANNOTATION = JavaTypeName.create(FunctionalInterface.class);
35 ActionGenerator(final ActionEffectiveStatement statement, final AbstractCompositeGenerator<?, ?> parent) {
36 super(statement, parent);
40 void pushToInference(final SchemaInferenceStack dataTree) {
41 dataTree.enterSchemaTree(statement().getIdentifier());
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;
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);
59 final ModuleGenerator module = currentModule();
60 module.addQNameConstant(builder, statement().argument());
62 // addGetterMethods(builder, builderFactory);
64 annotateDeprecatedIfNecessary(builder);
65 builderFactory.addCodegenInformation(module, statement(), builder);
67 return builder.build();
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);
76 final AbstractCompositeGenerator<?, ?> parent = getParent();
77 if (parent instanceof ListGenerator) {
78 final KeyGenerator keyGen = ((ListGenerator) parent).keyGenerator();
80 return BindingTypes.keyedListAction(Type.of(parent.typeName()), keyGen.getGeneratedType(builderFactory),
85 return BindingTypes.action(Type.of(parent.typeName()), input, output);
89 void addAsGetterMethod(final GeneratedTypeBuilderBase<?> builder, final TypeBuilderFactory builderFactory) {
90 // actions are a separate concept
94 CompositeRuntimeTypeBuilder<ActionEffectiveStatement, ActionRuntimeType> createBuilder(
95 final ActionEffectiveStatement statement) {
96 return new CompositeRuntimeTypeBuilder<>(statement) {
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);