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 java.util.Comparator;
15 import java.util.Iterator;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.mdsal.binding.generator.impl.reactor.CollisionDomain.Member;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
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.ri.BindingTypes;
22 import org.opendaylight.yangtools.odlext.model.api.AugmentIdentifierEffectiveStatement;
23 import org.opendaylight.yangtools.yang.common.AbstractQName;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
29 * A generator corresponding to a {@code augment} statement. This class is further specialized for the two distinct uses
32 abstract class AbstractAugmentGenerator extends AbstractCompositeGenerator<AugmentEffectiveStatement> {
34 * Comparator comparing target path length. This is useful for quickly determining order the order in which two
35 * (or more) {@link AbstractAugmentGenerator}s need to be evaluated. This is necessary when augments are layered on
54 * Evaluating these in the order of increasing argument component count solves this without having to perform a full
58 * Another problem we are solving here is augmentation target stability, as the declared order in YANG text may
59 * change, which does not really change the semantics. If we only relied on length of argument, such a move would
60 * result in changing the results of {@link #createMember(CollisionDomain)} and make upgrades rather unpredictable.
61 * We solve this by using {@link QName#compareTo(QName)} to determine order.
63 static final Comparator<? super AbstractAugmentGenerator> COMPARATOR = (o1, o2) -> {
64 final Iterator<QName> thisIt = o1.statement().argument().getNodeIdentifiers().iterator();
65 final Iterator<QName> otherIt = o2.statement().argument().getNodeIdentifiers().iterator();
67 while (thisIt.hasNext()) {
68 if (!otherIt.hasNext()) {
72 final int comp = thisIt.next().compareTo(otherIt.next());
78 return otherIt.hasNext() ? -1 : 0;
81 private AbstractCompositeGenerator<?> targetGen;
83 AbstractAugmentGenerator(final AugmentEffectiveStatement statement, final AbstractCompositeGenerator<?> parent) {
84 super(statement, parent);
88 final void pushToInference(final SchemaInferenceStack dataTree) {
89 dataTree.enterSchemaTree(statement().argument());
93 final AbstractQName localName() {
94 throw new UnsupportedOperationException();
98 ClassPlacement classPlacement() {
99 // if the target is a choice we are NOT creating an explicit augmentation, but we still need a phantom to
100 // reserve the appropriate package name
101 final AbstractCompositeGenerator<?> target = targetGenerator();
102 return target instanceof ChoiceGenerator ? ClassPlacement.PHANTOM : super.classPlacement();
106 final Member createMember(final CollisionDomain domain) {
107 final AbstractQName explicitIdentifier = statement()
108 .findFirstEffectiveSubstatementArgument(AugmentIdentifierEffectiveStatement.class).orElse(null);
109 if (explicitIdentifier != null) {
110 return domain.addPrimary(this, new CamelCaseNamingStrategy(StatementNamespace.DEFAULT, explicitIdentifier));
113 final Member target = targetGenerator().getMember();
115 for (Generator gen : getParent()) {
119 if (gen instanceof AbstractAugmentGenerator
120 && target.equalRoot(((AbstractAugmentGenerator) gen).targetGenerator().getMember())) {
125 return domain.addSecondary(this, target, String.valueOf(offset), statement().argument());
129 final GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
130 final GeneratedTypeBuilder builder = builderFactory.newGeneratedTypeBuilder(typeName());
132 builder.addImplementsType(BindingTypes.augmentation(targetGenerator().getGeneratedType(builderFactory)));
133 addUsesInterfaces(builder, builderFactory);
134 addConcreteInterfaceMethods(builder);
136 addGetterMethods(builder, builderFactory);
137 annotateDeprecatedIfNecessary(builder);
139 return builder.build();
143 final void addAsGetterMethod(final GeneratedTypeBuilderBase<?> builder, final TypeBuilderFactory builderFactory) {
144 // Augments are never added as getters, as they are handled via Augmentable mechanics
147 final void setTargetGenerator(final AbstractCompositeGenerator<?> targetGenerator) {
148 verify(targetGen == null, "Attempted to relink %s, already have target %s", this, targetGen);
149 targetGen = requireNonNull(targetGenerator);
152 final @NonNull AbstractCompositeGenerator<?> targetGenerator() {
153 return verifyNotNull(targetGen, "No target for %s", this);