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;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.mdsal.binding.generator.impl.reactor.CollisionDomain.Member;
18 import org.opendaylight.mdsal.binding.model.api.MethodSignature.ValueMechanics;
19 import org.opendaylight.mdsal.binding.model.api.Type;
20 import org.opendaylight.mdsal.binding.model.api.TypeMemberComment;
21 import org.opendaylight.mdsal.binding.model.api.type.builder.AnnotableTypeBuilder;
22 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
23 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
24 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
25 import org.opendaylight.yangtools.yang.common.AbstractQName;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.model.api.AddedByUsesAware;
28 import org.opendaylight.yangtools.yang.model.api.CopyableNode;
29 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * An explicit {@link Generator}, associated with a particular {@link EffectiveStatement}.
38 public abstract class AbstractExplicitGenerator<T extends EffectiveStatement<?, ?>> extends Generator
39 implements CopyableNode {
40 private static final Logger LOG = LoggerFactory.getLogger(AbstractExplicitGenerator.class);
42 private final @NonNull T statement;
45 * Field tracking previous incarnation (along reverse of 'uses' and 'augment' axis) of this statement. This field
46 * can either be one of:
48 * <li>{@code null} when not resolved, i.e. access is not legal, or</li>
49 * <li>{@code this} object if this is the original definition, or</li>
50 * <li>a generator which is one step closer to the original definition</li>
53 private AbstractExplicitGenerator<?> prev;
55 * Field holding the original incarnation, i.e. the terminal node along {@link #prev} links.
57 private AbstractExplicitGenerator<?> orig;
59 AbstractExplicitGenerator(final T statement) {
60 this.statement = requireNonNull(statement);
63 AbstractExplicitGenerator(final T statement, final AbstractCompositeGenerator<?> parent) {
65 this.statement = requireNonNull(statement);
69 * Return the {@link EffectiveStatement} associated with this generator.
71 * @return An EffectiveStatement
73 public final @NonNull T statement() {
78 public final boolean isAddedByUses() {
79 return statement instanceof AddedByUsesAware && ((AddedByUsesAware) statement).isAddedByUses();
83 public final boolean isAugmenting() {
84 return statement instanceof CopyableNode && ((CopyableNode) statement).isAugmenting();
88 * Attempt to link the generator corresponding to the original definition for this generator's statements as well as
89 * to all child generators.
91 * @return Number of generators that remain unlinked.
93 long linkOriginalGenerator() {
95 // Original already linked
100 LOG.trace("Linking {}", this);
102 if (!isAddedByUses() && !isAugmenting()) {
104 LOG.trace("Linked {} to self", this);
108 final var link = getParent().getOriginalChild(getQName());
109 prev = link.previous();
110 orig = link.original();
112 LOG.trace("Linked {} to {} original {}", this, prev, orig);
116 LOG.trace("Linked {} to intermediate {}", this, prev);
120 orig = prev.originalLink().original();
122 LOG.trace("Linked {} to original {}", this, orig);
129 * Return the previous incarnation of this generator, or {@code null} if this is the original generator.
131 * @return Previous incarnation or {@code null}
133 final @Nullable AbstractExplicitGenerator<?> previous() {
134 final var local = verifyNotNull(prev, "Generator %s does not have linkage to previous instance resolved", this);
135 return local == this ? null : local;
139 * Return the original incarnation of this generator, or self if this is the original generator.
141 * @return Original incarnation of this generator
143 @NonNull AbstractExplicitGenerator<?> getOriginal() {
144 return verifyNotNull(orig, "Generator %s does not have linkage to original instance resolved", this);
148 * Return the link towards the original generator.
150 * @return Link towards the original generator.
152 final @NonNull OriginalLink originalLink() {
153 final var local = prev;
155 return OriginalLink.partial(this);
156 } else if (local == this) {
157 return OriginalLink.complete(this);
159 return OriginalLink.partial(local);
163 @Nullable AbstractExplicitGenerator<?> findSchemaTreeGenerator(final QName qname) {
164 for (Generator child : this) {
165 if (child instanceof AbstractExplicitGenerator) {
166 final AbstractExplicitGenerator<?> gen = (AbstractExplicitGenerator<?>) child;
167 final EffectiveStatement<?, ?> stmt = gen.statement();
168 if (stmt instanceof SchemaTreeEffectiveStatement && qname.equals(stmt.argument())) {
176 final @NonNull QName getQName() {
177 final Object arg = statement.argument();
178 verify(arg instanceof QName, "Unexpected argument %s", arg);
182 @NonNull AbstractQName localName() {
183 // FIXME: this should be done in a nicer way
184 final Object argument = statement.argument();
185 verify(argument instanceof AbstractQName, "Illegal argument %s", argument);
186 return (AbstractQName) argument;
190 ClassPlacement classPlacement() {
191 // We process nodes introduced through augment or uses separately
192 // FIXME: this is not quite right!
193 return isAddedByUses() || isAugmenting() ? ClassPlacement.NONE : ClassPlacement.TOP_LEVEL;
197 Member createMember(final CollisionDomain domain) {
198 return domain.addPrimary(this, new CamelCaseNamingStrategy(namespace(), localName()));
201 void addAsGetterMethod(final @NonNull GeneratedTypeBuilderBase<?> builder,
202 final @NonNull TypeBuilderFactory builderFactory) {
203 if (isAugmenting()) {
204 // Do not process augmented nodes: they will be taken care of in their home augmentation
207 if (isAddedByUses()) {
208 // If this generator has been added by a uses node, it is already taken care of by the corresponding
209 // grouping. There is one exception to this rule: 'type leafref' can use a relative path to point
210 // outside of its home grouping. In this case we need to examine the instantiation until we succeed in
211 // resolving the reference.
212 addAsGetterMethodOverride(builder, builderFactory);
216 final Type returnType = methodReturnType(builderFactory);
217 constructGetter(builder, returnType);
218 constructRequire(builder, returnType);
221 MethodSignatureBuilder constructGetter(final GeneratedTypeBuilderBase<?> builder, final Type returnType) {
222 return constructGetter(builder, returnType, BindingMapping.getGetterMethodName(localName().getLocalName()));
225 final MethodSignatureBuilder constructGetter(final GeneratedTypeBuilderBase<?> builder,
226 final Type returnType, final String methodName) {
227 final MethodSignatureBuilder getMethod = builder.addMethod(methodName).setReturnType(returnType);
229 annotateDeprecatedIfNecessary(getMethod);
231 statement.findFirstEffectiveSubstatementArgument(DescriptionEffectiveStatement.class)
232 .map(TypeMemberComment::referenceOf).ifPresent(getMethod::setComment);
237 void constructRequire(final GeneratedTypeBuilderBase<?> builder, final Type returnType) {
238 // No-op in most cases
241 final void constructRequireImpl(final GeneratedTypeBuilderBase<?> builder, final Type returnType) {
242 constructGetter(builder, returnType, BindingMapping.getRequireMethodName(localName().getLocalName()))
244 .setMechanics(ValueMechanics.NONNULL);
247 void addAsGetterMethodOverride(final @NonNull GeneratedTypeBuilderBase<?> builder,
248 final @NonNull TypeBuilderFactory builderFactory) {
249 // No-op for most cases
252 @NonNull Type methodReturnType(final @NonNull TypeBuilderFactory builderFactory) {
253 return getGeneratedType(builderFactory);
256 final void annotateDeprecatedIfNecessary(final AnnotableTypeBuilder builder) {
257 annotateDeprecatedIfNecessary(statement, builder);
261 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
262 helper.add("argument", statement.argument());
264 if (isAddedByUses()) {
265 helper.addValue("addedByUses");
267 if (isAugmenting()) {
268 helper.addValue("augmenting");