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<T> prev;
55 * Field holding the original incarnation, i.e. the terminal node along {@link #prev} links.
57 private AbstractExplicitGenerator<T> 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.
90 * @return {@code true} if this generator is linked
92 final boolean linkOriginalGenerator() {
94 // Original already linked
99 LOG.trace("Linking {}", this);
101 if (!isAddedByUses() && !isAugmenting()) {
103 LOG.trace("Linked {} to self", this);
107 final var link = getParent().<T>originalChild(getQName());
109 LOG.trace("Cannot link {} yet", this);
113 prev = link.previous();
114 orig = link.original();
116 LOG.trace("Linked {} to {} original {}", this, prev, orig);
120 LOG.trace("Linked {} to intermediate {}", this, prev);
124 orig = prev.originalLink().original();
126 LOG.trace("Linked {} to original {}", this, orig);
133 * Return the previous incarnation of this generator, or {@code null} if this is the original generator.
135 * @return Previous incarnation or {@code null}
137 final @Nullable AbstractExplicitGenerator<T> previous() {
138 final var local = verifyNotNull(prev, "Generator %s does not have linkage to previous instance resolved", this);
139 return local == this ? null : local;
143 * Return the original incarnation of this generator, or self if this is the original generator.
145 * @return Original incarnation of this generator
147 @NonNull AbstractExplicitGenerator<T> getOriginal() {
148 return verifyNotNull(orig, "Generator %s does not have linkage to original instance resolved", this);
151 @Nullable AbstractExplicitGenerator<T> tryOriginal() {
156 * Return the link towards the original generator.
158 * @return Link towards the original generator.
160 final @NonNull OriginalLink<T> originalLink() {
161 final var local = prev;
163 return OriginalLink.partial(this);
164 } else if (local == this) {
165 return OriginalLink.complete(this);
167 return OriginalLink.partial(local);
171 @Nullable AbstractExplicitGenerator<?> findSchemaTreeGenerator(final QName qname) {
172 return findLocalSchemaTreeGenerator(qname);
175 final @Nullable AbstractExplicitGenerator<?> findLocalSchemaTreeGenerator(final QName qname) {
176 for (Generator child : this) {
177 if (child instanceof AbstractExplicitGenerator) {
178 final AbstractExplicitGenerator<?> gen = (AbstractExplicitGenerator<?>) child;
179 final EffectiveStatement<?, ?> stmt = gen.statement();
180 if (stmt instanceof SchemaTreeEffectiveStatement && qname.equals(stmt.argument())) {
188 final @NonNull QName getQName() {
189 final Object arg = statement.argument();
190 verify(arg instanceof QName, "Unexpected argument %s", arg);
194 @NonNull AbstractQName localName() {
195 // FIXME: this should be done in a nicer way
196 final Object argument = statement.argument();
197 verify(argument instanceof AbstractQName, "Illegal argument %s", argument);
198 return (AbstractQName) argument;
202 ClassPlacement classPlacement() {
203 // We process nodes introduced through augment or uses separately
204 // FIXME: this is not quite right!
205 return isAddedByUses() || isAugmenting() ? ClassPlacement.NONE : ClassPlacement.TOP_LEVEL;
209 Member createMember(final CollisionDomain domain) {
210 return domain.addPrimary(this, new CamelCaseNamingStrategy(namespace(), localName()));
213 void addAsGetterMethod(final @NonNull GeneratedTypeBuilderBase<?> builder,
214 final @NonNull TypeBuilderFactory builderFactory) {
215 if (isAugmenting()) {
216 // Do not process augmented nodes: they will be taken care of in their home augmentation
219 if (isAddedByUses()) {
220 // If this generator has been added by a uses node, it is already taken care of by the corresponding
221 // grouping. There is one exception to this rule: 'type leafref' can use a relative path to point
222 // outside of its home grouping. In this case we need to examine the instantiation until we succeed in
223 // resolving the reference.
224 addAsGetterMethodOverride(builder, builderFactory);
228 final Type returnType = methodReturnType(builderFactory);
229 constructGetter(builder, returnType);
230 constructRequire(builder, returnType);
233 MethodSignatureBuilder constructGetter(final GeneratedTypeBuilderBase<?> builder, final Type returnType) {
234 return constructGetter(builder, returnType, BindingMapping.getGetterMethodName(localName().getLocalName()));
237 final MethodSignatureBuilder constructGetter(final GeneratedTypeBuilderBase<?> builder,
238 final Type returnType, final String methodName) {
239 final MethodSignatureBuilder getMethod = builder.addMethod(methodName).setReturnType(returnType);
241 annotateDeprecatedIfNecessary(getMethod);
243 statement.findFirstEffectiveSubstatementArgument(DescriptionEffectiveStatement.class)
244 .map(TypeMemberComment::referenceOf).ifPresent(getMethod::setComment);
249 void constructRequire(final GeneratedTypeBuilderBase<?> builder, final Type returnType) {
250 // No-op in most cases
253 final void constructRequireImpl(final GeneratedTypeBuilderBase<?> builder, final Type returnType) {
254 constructGetter(builder, returnType, BindingMapping.getRequireMethodName(localName().getLocalName()))
256 .setMechanics(ValueMechanics.NONNULL);
259 void addAsGetterMethodOverride(final @NonNull GeneratedTypeBuilderBase<?> builder,
260 final @NonNull TypeBuilderFactory builderFactory) {
261 // No-op for most cases
264 @NonNull Type methodReturnType(final @NonNull TypeBuilderFactory builderFactory) {
265 return getGeneratedType(builderFactory);
268 final void annotateDeprecatedIfNecessary(final AnnotableTypeBuilder builder) {
269 annotateDeprecatedIfNecessary(statement, builder);
273 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
274 helper.add("argument", statement.argument());
276 if (isAddedByUses()) {
277 helper.addValue("addedByUses");
279 if (isAugmenting()) {
280 helper.addValue("augmenting");