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.collect.ImmutableList;
15 import java.util.Comparator;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.function.Function;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.mdsal.binding.generator.impl.reactor.CollisionDomain.Member;
21 import org.opendaylight.mdsal.binding.generator.impl.rt.DefaultAugmentRuntimeType;
22 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
23 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
24 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
25 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
26 import org.opendaylight.mdsal.binding.runtime.api.AugmentRuntimeType;
27 import org.opendaylight.mdsal.binding.runtime.api.CaseRuntimeType;
28 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
29 import org.opendaylight.yangtools.odlext.model.api.AugmentIdentifierEffectiveStatement;
30 import org.opendaylight.yangtools.yang.common.AbstractQName;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement.SchemaTreeNamespace;
37 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
38 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
41 * A generator corresponding to a {@code augment} statement. This class is further specialized for the two distinct uses
44 abstract class AbstractAugmentGenerator
45 extends AbstractCompositeGenerator<AugmentEffectiveStatement, AugmentRuntimeType> {
47 * Comparator comparing target path length. This is useful for quickly determining order the order in which two
48 * (or more) {@link AbstractAugmentGenerator}s need to be evaluated. This is necessary when augments are layered on
67 * Evaluating these in the order of increasing argument component count solves this without having to perform a full
71 * Another problem we are solving here is augmentation target stability, as the declared order in YANG text may
72 * change, which does not really change the semantics. If we only relied on length of argument, such a move would
73 * result in changing the results of {@link #createMember(CollisionDomain)} and make upgrades rather unpredictable.
74 * We solve this by using {@link QName#compareTo(QName)} to determine order.
76 static final Comparator<? super AbstractAugmentGenerator> COMPARATOR = (o1, o2) -> {
77 final Iterator<QName> thisIt = o1.statement().argument().getNodeIdentifiers().iterator();
78 final Iterator<QName> otherIt = o2.statement().argument().getNodeIdentifiers().iterator();
80 while (thisIt.hasNext()) {
81 if (!otherIt.hasNext()) {
85 final int comp = thisIt.next().compareTo(otherIt.next());
91 return otherIt.hasNext() ? -1 : 0;
94 private AbstractCompositeGenerator<?, ?> targetGen;
96 AbstractAugmentGenerator(final AugmentEffectiveStatement statement, final AbstractCompositeGenerator<?, ?> parent) {
97 super(statement, parent);
101 final void pushToInference(final SchemaInferenceStack dataTree) {
102 dataTree.enterSchemaTree(statement().argument());
106 final AbstractQName localName() {
107 throw new UnsupportedOperationException();
111 ClassPlacement classPlacement() {
112 // if the target is a choice we are NOT creating an explicit augmentation, but we still need a phantom to
113 // reserve the appropriate package name
114 final AbstractCompositeGenerator<?, ?> target = targetGenerator();
115 return target instanceof ChoiceGenerator ? ClassPlacement.PHANTOM : super.classPlacement();
119 final Member createMember(final CollisionDomain domain) {
120 final AbstractQName explicitIdentifier = statement()
121 .findFirstEffectiveSubstatementArgument(AugmentIdentifierEffectiveStatement.class).orElse(null);
122 if (explicitIdentifier != null) {
123 return domain.addPrimary(this, new CamelCaseNamingStrategy(StatementNamespace.DEFAULT, explicitIdentifier));
126 final Member target = targetGenerator().getMember();
128 for (Generator gen : getParent()) {
132 if (gen instanceof AbstractAugmentGenerator
133 && target.equalRoot(((AbstractAugmentGenerator) gen).targetGenerator().getMember())) {
138 return domain.addSecondary(this, target, String.valueOf(offset), statement().argument());
142 final GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
143 final GeneratedTypeBuilder builder = builderFactory.newGeneratedTypeBuilder(typeName());
145 builder.addImplementsType(BindingTypes.augmentation(targetGenerator().getGeneratedType(builderFactory)));
146 addUsesInterfaces(builder, builderFactory);
147 addConcreteInterfaceMethods(builder);
149 addGetterMethods(builder, builderFactory);
150 annotateDeprecatedIfNecessary(builder);
152 return builder.build();
155 boolean matchesInstantiated(final AugmentEffectiveStatement statement) {
156 return statement().equals(statement);
159 final void fillRuntimeCasesIn(final AugmentResolver resolver, final ChoiceEffectiveStatement stmt,
160 final List<CaseRuntimeType> toList) {
161 toList.addAll(createBuilder(effectiveIn(stmt)).populate(resolver, this).getCaseChilden());
164 final @NonNull AugmentRuntimeType runtimeTypeIn(final AugmentResolver resolver,
165 final EffectiveStatement<?, ?> stmt) {
166 verify(stmt instanceof SchemaTreeAwareEffectiveStatement, "Unexpected target statement %s", stmt);
167 return verifyNotNull(createInternalRuntimeType(resolver,
168 effectiveIn((SchemaTreeAwareEffectiveStatement<?, ?>) stmt)));
171 abstract @NonNull TargetAugmentEffectiveStatement effectiveIn(SchemaTreeAwareEffectiveStatement<?, ?> target);
173 final @NonNull TargetAugmentEffectiveStatement effectiveIn(final SchemaTreeAwareEffectiveStatement<?, ?> target,
174 final Function<QName, QName> transform) {
175 final var augment = statement();
176 final var stmts = augment.effectiveSubstatements();
177 final var builder = ImmutableList.<EffectiveStatement<?, ?>>builderWithExpectedSize(stmts.size());
178 for (var child : stmts) {
179 if (child instanceof SchemaTreeEffectiveStatement) {
180 final var qname = ((SchemaTreeEffectiveStatement<?>) child).getIdentifier();
181 // Note: a match in target may be missing -- for example if it was 'deviate unsupported'
182 target.get(SchemaTreeNamespace.class, transform.apply(qname)).ifPresent(builder::add);
187 return new TargetAugmentEffectiveStatement(augment, target, builder.build());
191 final void addAsGetterMethod(final GeneratedTypeBuilderBase<?> builder, final TypeBuilderFactory builderFactory) {
192 // Augments are never added as getters, as they are handled via Augmentable mechanics
196 CompositeRuntimeTypeBuilder<AugmentEffectiveStatement, AugmentRuntimeType> createBuilder(
197 final AugmentEffectiveStatement statement) {
198 return new CompositeRuntimeTypeBuilder<>(statement) {
200 AugmentRuntimeType build(final GeneratedType type, final AugmentEffectiveStatement statement,
201 final List<RuntimeType> children, final List<AugmentRuntimeType> augments) {
202 // 'augment' cannot be targeted by augment
203 verify(augments.isEmpty(), "Unexpected augments %s", augments);
204 return new DefaultAugmentRuntimeType(type, statement, children);
209 final void setTargetGenerator(final AbstractCompositeGenerator<?, ?> targetGenerator) {
210 verify(targetGen == null, "Attempted to relink %s, already have target %s", this, targetGen);
211 targetGen = requireNonNull(targetGenerator);
214 final @NonNull AbstractCompositeGenerator<?, ?> targetGenerator() {
215 return verifyNotNull(targetGen, "No target for %s", this);