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;
11 import static com.google.common.base.Verify.verifyNotNull;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.mdsal.binding.generator.impl.reactor.CollisionDomain.Member;
16 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
17 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
18 import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition;
19 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
20 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
21 import org.opendaylight.mdsal.binding.model.util.BindingTypes;
22 import org.opendaylight.mdsal.binding.model.util.TypeComments;
23 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
32 * Generator corresponding to a {@code module} statement. These generators are roots for generating types for a
33 * particular {@link QNameModule} as mapped into the root package.
35 public final class ModuleGenerator extends AbstractCompositeGenerator<ModuleEffectiveStatement> {
36 private final @NonNull JavaTypeName yangModuleInfo;
37 private final @NonNull ClassPlacement placement;
40 * Note that for sake of simplicity of lookup and child mapping, this instance serves as the root for all child
41 * generators, but mapping to {@link CollisionDomain}s and their {@link Member}s is rather weird. This generator
42 * actually produces one of <em>secondary</em> members, more precisely the {@link BindingMapping#DATA_ROOT_SUFFIX}
43 * one. Counter-intuitively the other secondary members live as children of this generator. To support this we have
44 * also have this field, which is actually the <em>primary</em> derived from the module's name.
46 private final Member prefixMember;
48 ModuleGenerator(final ModuleEffectiveStatement statement) {
50 yangModuleInfo = JavaTypeName.create(javaPackage(), BindingMapping.MODULE_INFO_CLASS_NAME);
51 placement = computePlacement();
52 prefixMember = placement != ClassPlacement.NONE || haveSecondary()
53 ? domain().addPrefix(new ModuleNamingStrategy(statement.argument())) : null;
56 private @NonNull ClassPlacement computePlacement() {
57 return statement().findFirstEffectiveSubstatement(DataTreeEffectiveStatement.class).isPresent()
58 || statement().findFirstEffectiveSubstatement(ChoiceEffectiveStatement.class).isPresent()
59 ? ClassPlacement.TOP_LEVEL : ClassPlacement.NONE;
62 private boolean haveSecondary() {
63 for (Generator child : this) {
64 if (child instanceof AbstractImplicitGenerator) {
72 ModuleGenerator currentModule() {
77 void pushToInference(final SchemaInferenceStack dataTree) {
82 String createJavaPackage() {
83 return BindingMapping.getRootPackageName(statement().localQNameModule());
87 AbstractCompositeGenerator<?> getPackageParent() {
92 CollisionDomain parentDomain() {
97 ClassPlacement classPlacement() {
102 Member createMember(final CollisionDomain domain) {
103 return domain.addSecondary(prefixMember, BindingMapping.DATA_ROOT_SUFFIX);
107 GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
108 final GeneratedTypeBuilder builder = builderFactory.newGeneratedTypeBuilder(typeName());
109 builder.setModuleName(statement().argument().getLocalName());
110 builder.addImplementsType(BindingTypes.DATA_ROOT);
112 final int usesCount = addUsesInterfaces(builder, builderFactory);
113 // if we have more than 2 top level uses statements we need to define getImplementedInterface() on the top-level
116 narrowImplementedInterface(builder);
119 addGetterMethods(builder, builderFactory);
121 if (builderFactory instanceof TypeBuilderFactory.Codegen) {
122 final ModuleEffectiveStatement stmt = statement();
123 verify(stmt instanceof Module, "Unexpected module %s", stmt);
124 final Module module = (Module) stmt;
126 YangSourceDefinition.of(module).ifPresent(builder::setYangSourceDefinition);
127 TypeComments.description(module).ifPresent(builder::addComment);
128 module.getDescription().ifPresent(builder::setDescription);
129 module.getReference().ifPresent(builder::setReference);
132 return builder.build();
135 @NonNull Member getPrefixMember() {
136 return verifyNotNull(prefixMember);
139 void addQNameConstant(final GeneratedTypeBuilderBase<?> builder, final String localName) {
140 builder.addConstant(BindingTypes.QNAME, BindingMapping.QNAME_STATIC_FIELD_NAME,
141 Map.entry(yangModuleInfo, localName));