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 org.opendaylight.mdsal.binding.model.ri.BindingTypes.identifiable;
12 import java.util.List;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.mdsal.binding.generator.impl.rt.DefaultListRuntimeType;
15 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
16 import org.opendaylight.mdsal.binding.model.api.MethodSignature.ValueMechanics;
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.api.type.builder.MethodSignatureBuilder;
21 import org.opendaylight.mdsal.binding.model.ri.Types;
22 import org.opendaylight.mdsal.binding.runtime.api.AugmentRuntimeType;
23 import org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType;
24 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
25 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
26 import org.opendaylight.yangtools.yang.common.Ordering;
27 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
32 * Generator corresponding to a {@code list} statement.
34 final class ListGenerator extends CompositeSchemaTreeGenerator<ListEffectiveStatement, ListRuntimeType> {
35 private final @Nullable KeyGenerator keyGen;
37 ListGenerator(final ListEffectiveStatement statement, final AbstractCompositeGenerator<?, ?> parent) {
38 super(statement, parent);
39 keyGen = statement.findFirstEffectiveSubstatement(KeyEffectiveStatement.class)
40 .map(key -> new KeyGenerator(key, parent, this))
44 @Nullable KeyGenerator keyGenerator() {
49 void pushToInference(final SchemaInferenceStack dataTree) {
50 dataTree.enterDataTree(statement().getIdentifier());
54 GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
55 final GeneratedTypeBuilder builder = builderFactory.newGeneratedTypeBuilder(typeName());
56 addImplementsChildOf(builder);
57 addAugmentable(builder);
58 addUsesInterfaces(builder, builderFactory);
59 addConcreteInterfaceMethods(builder);
61 final ModuleGenerator module = currentModule();
62 module.addQNameConstant(builder, localName());
65 // Add yang.binding.Identifiable and its key() method
66 final GeneratedType keyType = keyGen.getGeneratedType(builderFactory);
67 builder.addImplementsType(identifiable(keyType));
68 builder.addMethod(BindingMapping.IDENTIFIABLE_KEY_NAME)
69 .setReturnType(keyType)
70 .addAnnotation(OVERRIDE_ANNOTATION);
73 addGetterMethods(builder, builderFactory);
75 annotateDeprecatedIfNecessary(builder);
76 builderFactory.addCodegenInformation(module, statement(), builder);
77 builder.setModuleName(module.statement().argument().getLocalName());
78 // builder.setSchemaPath(node.getPath());
80 return builder.build();
84 ListRuntimeType createRuntimeType(final GeneratedType type, final ListEffectiveStatement statement,
85 final List<RuntimeType> children, final List<AugmentRuntimeType> augments) {
86 return new DefaultListRuntimeType(type, statement, children, augments,
87 keyGen != null ? keyGen.runtimeType().orElseThrow() : null);
91 Type methodReturnType(final TypeBuilderFactory builderFactory) {
92 final Type generatedType = super.methodReturnType(builderFactory);
93 // We are wrapping the generated type in either a List or a Map based on presence of the key
94 if (keyGen != null && statement().ordering() == Ordering.SYSTEM) {
95 return Types.mapTypeFor(keyGen.getGeneratedType(builderFactory), generatedType);
98 return Types.listTypeFor(generatedType);
102 MethodSignatureBuilder constructGetter(final GeneratedTypeBuilderBase<?> builder, final Type returnType) {
103 final MethodSignatureBuilder ret = super.constructGetter(builder, returnType)
104 .setMechanics(ValueMechanics.NULLIFY_EMPTY);
106 final MethodSignatureBuilder nonnull = builder
107 .addMethod(BindingMapping.getNonnullMethodName(localName().getLocalName()))
108 .setReturnType(returnType)
110 annotateDeprecatedIfNecessary(nonnull);