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.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.stream.Collectors;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.mdsal.binding.model.api.Enumeration;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
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.ri.BindingTypes;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.AddedByUsesAware;
27 import org.opendaylight.yangtools.yang.model.api.CopyableNode;
28 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.AnydataEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.AnyxmlEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
38 import org.opendaylight.yangtools.yang.model.api.stmt.InputEffectiveStatement;
39 import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement;
40 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListEffectiveStatement;
41 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
42 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
43 import org.opendaylight.yangtools.yang.model.api.stmt.OutputEffectiveStatement;
44 import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement;
45 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
46 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
47 import org.opendaylight.yangtools.yang.model.api.stmt.UsesEffectiveStatement;
48 import org.opendaylight.yangtools.yang.model.ri.type.TypeBuilder;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * A composite generator. Composite generators may contain additional children, which end up being mapped into
54 * the naming hierarchy 'under' the composite generator. To support this use case, each composite has a Java package
57 abstract class AbstractCompositeGenerator<T extends EffectiveStatement<?, ?>> extends AbstractExplicitGenerator<T> {
58 private static final Logger LOG = LoggerFactory.getLogger(AbstractCompositeGenerator.class);
60 private final @NonNull CollisionDomain domain = new CollisionDomain(this);
61 private final List<Generator> children;
63 private List<AbstractAugmentGenerator> augments = List.of();
64 private List<GroupingGenerator> groupings;
66 AbstractCompositeGenerator(final T statement) {
68 children = createChildren(statement);
71 AbstractCompositeGenerator(final T statement, final AbstractCompositeGenerator<?> parent) {
72 super(statement, parent);
73 children = createChildren(statement);
77 public final Iterator<Generator> iterator() {
78 return children.iterator();
82 final boolean isEmpty() {
83 return children.isEmpty();
86 final @Nullable AbstractExplicitGenerator<?> findGenerator(final List<EffectiveStatement<?, ?>> stmtPath) {
87 return findGenerator(MatchStrategy.identity(), stmtPath, 0);
90 final @Nullable AbstractExplicitGenerator<?> findGenerator(final MatchStrategy childStrategy,
91 // TODO: Wouldn't this method be nicer with Deque<EffectiveStatement<?, ?>> ?
92 final List<EffectiveStatement<?, ?>> stmtPath, final int offset) {
93 final EffectiveStatement<?, ?> stmt = stmtPath.get(offset);
95 // Try direct children first, which is simple
96 AbstractExplicitGenerator<?> ret = childStrategy.findGenerator(stmt, children);
98 final int next = offset + 1;
99 if (stmtPath.size() == next) {
100 // Final step, return child
103 if (ret instanceof AbstractCompositeGenerator) {
104 // We know how to descend down
105 return ((AbstractCompositeGenerator<?>) ret).findGenerator(childStrategy, stmtPath, next);
107 // Yeah, don't know how to continue here
111 // At this point we are about to fork for augments or groupings. In either case only schema tree statements can
112 // be found this way. The fun part is that if we find a match and need to continue, we will use the same
113 // strategy for children as well. We now know that this (and subsequent) statements need to have a QName
115 if (stmt instanceof SchemaTreeEffectiveStatement) {
116 // grouping -> uses instantiation changes the namespace to the local namespace of the uses site. We are
117 // going the opposite direction, hence we are changing namespace from local to the grouping's namespace.
118 for (GroupingGenerator gen : groupings) {
119 final MatchStrategy strat = MatchStrategy.grouping(gen);
120 ret = gen.findGenerator(strat, stmtPath, offset);
126 // All augments are dead simple: they need to match on argument (which we expect to be a QName)
127 final MatchStrategy strat = MatchStrategy.augment();
128 for (AbstractAugmentGenerator gen : augments) {
129 ret = gen.findGenerator(strat, stmtPath, offset);
138 final @NonNull CollisionDomain domain() {
142 final void linkUsesDependencies(final GeneratorContext context) {
143 // We are establishing two linkages here:
144 // - we are resolving 'uses' statements to their corresponding 'grouping' definitions
145 // - we propagate those groupings as anchors to any augment statements
146 final List<GroupingGenerator> tmp = new ArrayList<>();
147 for (EffectiveStatement<?, ?> stmt : statement().effectiveSubstatements()) {
148 if (stmt instanceof UsesEffectiveStatement) {
149 final UsesEffectiveStatement uses = (UsesEffectiveStatement) stmt;
150 final GroupingGenerator grouping = context.resolveTreeScoped(GroupingGenerator.class, uses.argument());
153 for (Generator gen : this) {
154 if (gen instanceof UsesAugmentGenerator) {
155 ((UsesAugmentGenerator) gen).linkGroupingDependency(uses, grouping);
160 groupings = List.copyOf(tmp);
163 final void addAugment(final AbstractAugmentGenerator augment) {
164 if (augments.isEmpty()) {
165 augments = new ArrayList<>(2);
167 augments.add(requireNonNull(augment));
171 final AbstractCompositeGenerator<?> getOriginal() {
172 return (AbstractCompositeGenerator<?>) super.getOriginal();
175 final @NonNull AbstractExplicitGenerator<?> getOriginalChild(final QName childQName) {
176 // First try groupings/augments ...
177 final AbstractExplicitGenerator<?> found = findInferredGenerator(childQName);
182 // ... no luck, we really need to start looking at our origin
183 final AbstractExplicitGenerator<?> prev = verifyNotNull(previous(),
184 "Failed to find %s in scope of %s", childQName, this);
186 final QName prevQName = childQName.bindTo(prev.getQName().getModule());
187 return verifyNotNull(prev.findSchemaTreeGenerator(prevQName),
188 "Failed to find child %s (proxy for %s) in %s", prevQName, childQName, prev).getOriginal();
192 final @Nullable AbstractExplicitGenerator<?> findSchemaTreeGenerator(final QName qname) {
193 final AbstractExplicitGenerator<?> found = super.findSchemaTreeGenerator(qname);
194 return found != null ? found : findInferredGenerator(qname);
197 private @Nullable AbstractExplicitGenerator<?> findInferredGenerator(final QName qname) {
198 // First search our local groupings ...
199 for (GroupingGenerator grouping : groupings) {
200 final AbstractExplicitGenerator<?> gen = grouping.findSchemaTreeGenerator(
201 qname.bindTo(grouping.statement().argument().getModule()));
206 // ... next try local augments, which may have groupings themselves
207 for (AbstractAugmentGenerator augment : augments) {
208 final AbstractExplicitGenerator<?> gen = augment.findSchemaTreeGenerator(qname);
217 * Update the specified builder to implement interfaces generated for the {@code grouping} statements this generator
220 * @param builder Target builder
221 * @param builderFactory factory for creating {@link TypeBuilder}s
222 * @return The number of groupings this type uses.
224 final int addUsesInterfaces(final GeneratedTypeBuilder builder, final TypeBuilderFactory builderFactory) {
225 for (GroupingGenerator grp : groupings) {
226 builder.addImplementsType(grp.getGeneratedType(builderFactory));
228 return groupings.size();
231 static final void addAugmentable(final GeneratedTypeBuilder builder) {
232 builder.addImplementsType(BindingTypes.augmentable(builder));
235 final void addGetterMethods(final GeneratedTypeBuilder builder, final TypeBuilderFactory builderFactory) {
236 for (Generator child : this) {
237 // Only process explicit generators here
238 if (child instanceof AbstractExplicitGenerator) {
239 ((AbstractExplicitGenerator<?>) child).addAsGetterMethod(builder, builderFactory);
242 final GeneratedType enclosedType = child.enclosedType(builderFactory);
243 if (enclosedType instanceof GeneratedTransferObject) {
244 builder.addEnclosingTransferObject((GeneratedTransferObject) enclosedType);
245 } else if (enclosedType instanceof Enumeration) {
246 builder.addEnumeration((Enumeration) enclosedType);
248 verify(enclosedType == null, "Unhandled enclosed type %s in %s", enclosedType, child);
253 private List<Generator> createChildren(final EffectiveStatement<?, ?> statement) {
254 final List<Generator> tmp = new ArrayList<>();
255 final List<AbstractAugmentGenerator> tmpAug = new ArrayList<>();
257 for (EffectiveStatement<?, ?> stmt : statement.effectiveSubstatements()) {
258 if (stmt instanceof ActionEffectiveStatement) {
259 if (!isAugmenting(stmt)) {
260 tmp.add(new ActionGenerator((ActionEffectiveStatement) stmt, this));
262 } else if (stmt instanceof AnydataEffectiveStatement) {
263 if (!isAugmenting(stmt)) {
264 tmp.add(new OpaqueObjectGenerator<>((AnydataEffectiveStatement) stmt, this));
266 } else if (stmt instanceof AnyxmlEffectiveStatement) {
267 if (!isAugmenting(stmt)) {
268 tmp.add(new OpaqueObjectGenerator<>((AnyxmlEffectiveStatement) stmt, this));
270 } else if (stmt instanceof CaseEffectiveStatement) {
271 tmp.add(new CaseGenerator((CaseEffectiveStatement) stmt, this));
272 } else if (stmt instanceof ChoiceEffectiveStatement) {
273 // FIXME: use isOriginalDeclaration() ?
274 if (!isAddedByUses(stmt)) {
275 tmp.add(new ChoiceGenerator((ChoiceEffectiveStatement) stmt, this));
277 } else if (stmt instanceof ContainerEffectiveStatement) {
278 if (isOriginalDeclaration(stmt)) {
279 tmp.add(new ContainerGenerator((ContainerEffectiveStatement) stmt, this));
281 } else if (stmt instanceof GroupingEffectiveStatement) {
282 tmp.add(new GroupingGenerator((GroupingEffectiveStatement) stmt, this));
283 } else if (stmt instanceof IdentityEffectiveStatement) {
284 tmp.add(new IdentityGenerator((IdentityEffectiveStatement) stmt, this));
285 } else if (stmt instanceof InputEffectiveStatement) {
286 // FIXME: do not generate legacy RPC layout
287 tmp.add(this instanceof RpcGenerator ? new RpcContainerGenerator((InputEffectiveStatement) stmt, this)
288 : new OperationContainerGenerator((InputEffectiveStatement) stmt, this));
289 } else if (stmt instanceof LeafEffectiveStatement) {
290 if (!isAugmenting(stmt)) {
291 tmp.add(new LeafGenerator((LeafEffectiveStatement) stmt, this));
293 } else if (stmt instanceof LeafListEffectiveStatement) {
294 if (!isAugmenting(stmt)) {
295 tmp.add(new LeafListGenerator((LeafListEffectiveStatement) stmt, this));
297 } else if (stmt instanceof ListEffectiveStatement) {
298 if (isOriginalDeclaration(stmt)) {
299 final ListGenerator listGen = new ListGenerator((ListEffectiveStatement) stmt, this);
302 final KeyGenerator keyGen = listGen.keyGenerator();
303 if (keyGen != null) {
307 } else if (stmt instanceof NotificationEffectiveStatement) {
308 if (!isAugmenting(stmt)) {
309 tmp.add(new NotificationGenerator((NotificationEffectiveStatement) stmt, this));
311 } else if (stmt instanceof OutputEffectiveStatement) {
312 // FIXME: do not generate legacy RPC layout
313 tmp.add(this instanceof RpcGenerator ? new RpcContainerGenerator((OutputEffectiveStatement) stmt, this)
314 : new OperationContainerGenerator((OutputEffectiveStatement) stmt, this));
315 } else if (stmt instanceof RpcEffectiveStatement) {
316 tmp.add(new RpcGenerator((RpcEffectiveStatement) stmt, this));
317 } else if (stmt instanceof TypedefEffectiveStatement) {
318 tmp.add(new TypedefGenerator((TypedefEffectiveStatement) stmt, this));
319 } else if (stmt instanceof AugmentEffectiveStatement) {
320 // FIXME: MDSAL-695: So here we are ignoring any augment which is not in a module, while the 'uses'
321 // processing takes care of the rest. There are two problems here:
323 // 1) this could be an augment introduced through uses -- in this case we are picking
324 // confusing it with this being its declaration site, we should probably be
325 // ignoring it, but then
327 // 2) we are losing track of AugmentEffectiveStatement for which we do not generate
328 // interfaces -- and recover it at runtime through explicit walk along the
329 // corresponding AugmentationSchemaNode.getOriginalDefinition() pointer
331 // So here is where we should decide how to handle this augment, and make sure we
332 // retain information about this being an alias. That will serve as the base for keys
333 // in the augment -> original map we provide to BindingRuntimeTypes.
334 if (this instanceof ModuleGenerator) {
335 tmpAug.add(new ModuleAugmentGenerator((AugmentEffectiveStatement) stmt, this));
337 } else if (stmt instanceof UsesEffectiveStatement) {
338 final UsesEffectiveStatement uses = (UsesEffectiveStatement) stmt;
339 for (EffectiveStatement<?, ?> usesSub : uses.effectiveSubstatements()) {
340 if (usesSub instanceof AugmentEffectiveStatement) {
341 tmpAug.add(new UsesAugmentGenerator((AugmentEffectiveStatement) usesSub, this, uses));
345 LOG.trace("Ignoring statement {}", stmt);
350 // Sort augments and add them last. This ensures child iteration order always reflects potential
351 // interdependencies, hence we do not need to worry about them.
352 tmpAug.sort(AbstractAugmentGenerator.COMPARATOR);
355 // Compatibility FooService and FooListener interfaces, only generated for modules.
356 if (this instanceof ModuleGenerator) {
357 final ModuleGenerator moduleGen = (ModuleGenerator) this;
359 final List<NotificationGenerator> notifs = tmp.stream()
360 .filter(NotificationGenerator.class::isInstance)
361 .map(NotificationGenerator.class::cast)
362 .collect(Collectors.toUnmodifiableList());
363 if (!notifs.isEmpty()) {
364 tmp.add(new NotificationServiceGenerator(moduleGen, notifs));
367 final List<RpcGenerator> rpcs = tmp.stream()
368 .filter(RpcGenerator.class::isInstance)
369 .map(RpcGenerator.class::cast)
370 .collect(Collectors.toUnmodifiableList());
371 if (!rpcs.isEmpty()) {
372 tmp.add(new RpcServiceGenerator(moduleGen, rpcs));
376 return List.copyOf(tmp);
379 // Utility equivalent of (!isAddedByUses(stmt) && !isAugmenting(stmt)). Takes advantage of relationship between
380 // CopyableNode and AddedByUsesAware
381 private static boolean isOriginalDeclaration(final EffectiveStatement<?, ?> stmt) {
382 if (stmt instanceof AddedByUsesAware) {
383 if (((AddedByUsesAware) stmt).isAddedByUses()
384 || stmt instanceof CopyableNode && ((CopyableNode) stmt).isAugmenting()) {
391 private static boolean isAddedByUses(final EffectiveStatement<?, ?> stmt) {
392 return stmt instanceof AddedByUsesAware && ((AddedByUsesAware) stmt).isAddedByUses();
395 private static boolean isAugmenting(final EffectiveStatement<?, ?> stmt) {
396 return stmt instanceof CopyableNode && ((CopyableNode) stmt).isAugmenting();