938412c1d5e9ff787d1560d36d71d6bf08b26676
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / AbstractCompositeGenerator.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.generator.impl.reactor;
9
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;
13
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;
24 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
25 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
26 import org.opendaylight.mdsal.binding.runtime.api.CompositeRuntimeType;
27 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
28 import org.opendaylight.yangtools.rfc8040.model.api.YangDataEffectiveStatement;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.model.api.AddedByUsesAware;
31 import org.opendaylight.yangtools.yang.model.api.CopyableNode;
32 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.AnydataEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.AnyxmlEffectiveStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
38 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
39 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
40 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureEffectiveStatement;
41 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement;
42 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
43 import org.opendaylight.yangtools.yang.model.api.stmt.InputEffectiveStatement;
44 import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement;
45 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListEffectiveStatement;
46 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
47 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
48 import org.opendaylight.yangtools.yang.model.api.stmt.OutputEffectiveStatement;
49 import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement;
50 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
51 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
52 import org.opendaylight.yangtools.yang.model.api.stmt.UsesEffectiveStatement;
53 import org.opendaylight.yangtools.yang.model.ri.type.TypeBuilder;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 /**
58  * A composite generator. Composite generators may contain additional children, which end up being mapped into
59  * the naming hierarchy 'under' the composite generator. To support this use case, each composite has a Java package
60  * name assigned.
61  *
62  * <p>
63  * State tracking for resolution of children to their original declaration, i.e. back along the 'uses' and 'augment'
64  * axis. This is quite convoluted because we are traversing the generator tree recursively in the iteration order of
65  * children, but actual dependencies may require resolution in a different order, for example in the case of:
66  * <pre>
67  *   container foo {
68  *     uses bar {             // A
69  *       augment bar {        // B
70  *         container xyzzy;   // C
71  *       }
72  *     }
73  *
74  *     grouping bar {
75  *       container bar {      // D
76  *         uses baz;          // E
77  *       }
78  *     }
79  *
80  *     grouping baz {
81  *       leaf baz {           // F
82  *         type string;
83  *       }
84  *     }
85  *   }
86  *
87  *   augment /foo/bar/xyzzy { // G
88  *     leaf xyzzy {           // H
89  *       type string;
90  *     }
91  *   }
92  * </pre>
93  *
94  * <p>
95  * In this case we have three manifestations of 'leaf baz' -- marked A, E and F in the child iteration order. In order
96  * to perform a resolution, we first have to determine that F is the original definition, then establish that E is using
97  * the definition made by F and finally establish that A is using the definition made by F.
98  *
99  * <p>
100  * Dealing with augmentations is harder still, because we need to attach them to the original definition, hence for the
101  * /foo/bar container at A, we need to understand that its original definition is at D and we need to attach the augment
102  * at B to D. Futhermore we also need to establish that the augmentation at G attaches to container defined in C, so
103  * that the 'leaf xyzzy' existing as /foo/bar/xyzzy/xyzzy under C has its original definition at H.
104  *
105  * <p>
106  * Finally realize that the augment at G can actually exist in a different module and is shown in this example only
107  * the simplified form. That also means we could encounter G well before 'container foo' as well as we can have multiple
108  * such augments sprinkled across multiple modules having the same dependency rules as between C and G -- but they still
109  * have to form a directed acyclic graph and we partially deal with those complexities by having modules sorted by their
110  * dependencies.
111  *
112  * <p>
113  * For further details see {@link #linkOriginalGenerator()} and {@link #linkOriginalGeneratorRecursive()}, which deal
114  * with linking original instances in the tree iteration order. The part dealing with augment attachment lives mostly
115  * in {@link AugmentRequirement}.
116  */
117 public abstract class AbstractCompositeGenerator<S extends EffectiveStatement<?, ?>, R extends CompositeRuntimeType>
118         extends AbstractExplicitGenerator<S, R> {
119     private static final Logger LOG = LoggerFactory.getLogger(AbstractCompositeGenerator.class);
120
121     // FIXME: we want to allocate this lazily to lower memory footprint
122     private final @NonNull CollisionDomain domain = new CollisionDomain(this);
123     private final @NonNull List<Generator> childGenerators;
124
125     /**
126      * List of {@code augment} statements targeting this generator. This list is maintained only for the primary
127      * incarnation. This list is an evolving entity until after we have finished linkage of original statements. It is
128      * expected to be stable at the start of {@code step 2} in {@link GeneratorReactor#execute(TypeBuilderFactory)}.
129      */
130     private @NonNull List<AbstractAugmentGenerator> augments = List.of();
131
132     /**
133      * List of {@code grouping} statements this statement references. This field is set once by
134      * {@link #linkUsesDependencies(GeneratorContext)}.
135      */
136     private List<GroupingGenerator> groupings;
137
138     /**
139      * List of composite children which have not been recursively processed. This may become a mutable list when we
140      * have some children which have not completed linking. Once we have completed linking of all children, including
141      * {@link #unlinkedChildren}, this will be set to {@code null}.
142      */
143     private List<AbstractCompositeGenerator<?, ?>> unlinkedComposites = List.of();
144     /**
145      * List of children which have not had their original linked. This list starts of as null. When we first attempt
146      * linkage, it becomes non-null.
147      */
148     private List<Generator> unlinkedChildren;
149
150     AbstractCompositeGenerator(final S statement) {
151         super(statement);
152         childGenerators = createChildren(statement);
153     }
154
155     AbstractCompositeGenerator(final S statement, final AbstractCompositeGenerator<?, ?> parent) {
156         super(statement, parent);
157         childGenerators = createChildren(statement);
158     }
159
160     @Override
161     public final Iterator<Generator> iterator() {
162         return childGenerators.iterator();
163     }
164
165     final @NonNull List<AbstractAugmentGenerator> augments() {
166         return augments;
167     }
168
169     final @NonNull List<GroupingGenerator> groupings() {
170         return verifyNotNull(groupings, "Groupings not initialized in %s", this);
171     }
172
173     @Override
174     final R createExternalRuntimeType(final Type type) {
175         verify(type instanceof GeneratedType, "Unexpected type %s", type);
176         return createBuilder(statement()).populate(new AugmentResolver(), this).build((GeneratedType) type);
177     }
178
179     abstract @NonNull CompositeRuntimeTypeBuilder<S, R> createBuilder(S statement);
180
181     @Override
182     final R createInternalRuntimeType(final AugmentResolver resolver, final S statement, final Type type) {
183         verify(type instanceof GeneratedType, "Unexpected type %s", type);
184         return createBuilder(statement).populate(resolver, this).build((GeneratedType) type);
185     }
186
187     @Override
188     final boolean isEmpty() {
189         return childGenerators.isEmpty();
190     }
191
192     final @Nullable AbstractExplicitGenerator<?, ?> findGenerator(final List<EffectiveStatement<?, ?>> stmtPath) {
193         return findGenerator(MatchStrategy.identity(), stmtPath, 0);
194     }
195
196     final @Nullable AbstractExplicitGenerator<?, ?> findGenerator(final MatchStrategy childStrategy,
197             // TODO: Wouldn't this method be nicer with Deque<EffectiveStatement<?, ?>> ?
198             final List<EffectiveStatement<?, ?>> stmtPath, final int offset) {
199         final var stmt = stmtPath.get(offset);
200
201         // Try direct children first, which is simple
202         var ret = childStrategy.findGenerator(stmt, childGenerators);
203         if (ret != null) {
204             final int next = offset + 1;
205             if (stmtPath.size() == next) {
206                 // Final step, return child
207                 return ret;
208             }
209             if (ret instanceof AbstractCompositeGenerator<?, ?> composite) {
210                 // We know how to descend down
211                 return composite.findGenerator(childStrategy, stmtPath, next);
212             }
213             // Yeah, don't know how to continue here
214             return null;
215         }
216
217         // At this point we are about to fork for augments or groupings. In either case only schema tree statements can
218         // be found this way. The fun part is that if we find a match and need to continue, we will use the same
219         // strategy for children as well. We now know that this (and subsequent) statements need to have a QName
220         // argument.
221         if (stmt instanceof SchemaTreeEffectiveStatement) {
222             // grouping -> uses instantiation changes the namespace to the local namespace of the uses site. We are
223             // going the opposite direction, hence we are changing namespace from local to the grouping's namespace.
224             for (GroupingGenerator gen : groupings) {
225                 final MatchStrategy strat = MatchStrategy.grouping(gen);
226                 ret = gen.findGenerator(strat, stmtPath, offset);
227                 if (ret != null) {
228                     return ret;
229                 }
230             }
231
232             // All augments are dead simple: they need to match on argument (which we expect to be a QName)
233             final MatchStrategy strat = MatchStrategy.augment();
234             for (AbstractAugmentGenerator gen : augments) {
235                 ret = gen.findGenerator(strat, stmtPath, offset);
236                 if (ret != null) {
237                     return ret;
238                 }
239             }
240         }
241         return null;
242     }
243
244     final @NonNull CollisionDomain domain() {
245         return domain;
246     }
247
248     final void linkUsesDependencies(final GeneratorContext context) {
249         // We are establishing two linkages here:
250         // - we are resolving 'uses' statements to their corresponding 'grouping' definitions
251         // - we propagate those groupings as anchors to any augment statements, which takes out some amount of guesswork
252         //   from augment+uses resolution case, as groupings know about their immediate augments as soon as uses linkage
253         //   is resolved
254         final var tmp = new ArrayList<GroupingGenerator>();
255         for (var stmt : statement().effectiveSubstatements()) {
256             if (stmt instanceof UsesEffectiveStatement uses) {
257                 final var grouping = context.resolveTreeScoped(GroupingGenerator.class, uses.argument());
258                 tmp.add(grouping);
259
260                 // Trigger resolution of uses/augment statements. This looks like guesswork, but there may be multiple
261                 // 'augment' statements in a 'uses' statement and keeping a ListMultimap here seems wasteful.
262                 for (Generator gen : this) {
263                     if (gen instanceof UsesAugmentGenerator usesGen) {
264                         usesGen.resolveGrouping(uses, grouping);
265                     }
266                 }
267             }
268         }
269         groupings = List.copyOf(tmp);
270     }
271
272     final void startUsesAugmentLinkage(final List<AugmentRequirement> requirements) {
273         for (var child : childGenerators) {
274             if (child instanceof UsesAugmentGenerator uses) {
275                 requirements.add(uses.startLinkage());
276             }
277             if (child instanceof AbstractCompositeGenerator<?, ?> composite) {
278                 composite.startUsesAugmentLinkage(requirements);
279             }
280         }
281     }
282
283     final void addAugment(final AbstractAugmentGenerator augment) {
284         if (augments.isEmpty()) {
285             augments = new ArrayList<>(2);
286         }
287         augments.add(requireNonNull(augment));
288     }
289
290     /**
291      * Attempt to link the generator corresponding to the original definition for this generator's statements as well as
292      * to all child generators.
293      *
294      * @return Progress indication
295      */
296     final @NonNull LinkageProgress linkOriginalGeneratorRecursive() {
297         if (unlinkedComposites == null) {
298             // We have unset this list (see below), and there is nothing left to do
299             return LinkageProgress.DONE;
300         }
301
302         if (unlinkedChildren == null) {
303             unlinkedChildren = childGenerators.stream()
304                 .filter(AbstractExplicitGenerator.class::isInstance)
305                 .map(child -> (AbstractExplicitGenerator<?, ?>) child)
306                 .collect(Collectors.toList());
307         }
308
309         var progress = LinkageProgress.NONE;
310         if (!unlinkedChildren.isEmpty()) {
311             // Attempt to make progress on child linkage
312             final var it = unlinkedChildren.iterator();
313             while (it.hasNext()) {
314                 if (it.next() instanceof AbstractExplicitGenerator<?, ?> explicit && explicit.linkOriginalGenerator()) {
315                     progress = LinkageProgress.SOME;
316                     it.remove();
317
318                     // If this is a composite generator we need to process is further
319                     if (explicit instanceof AbstractCompositeGenerator<?, ?> composite) {
320                         if (unlinkedComposites.isEmpty()) {
321                             unlinkedComposites = new ArrayList<>();
322                         }
323                         unlinkedComposites.add(composite);
324                     }
325                 }
326             }
327
328             if (unlinkedChildren.isEmpty()) {
329                 // Nothing left to do, make sure any previously-allocated list can be scavenged
330                 unlinkedChildren = List.of();
331             }
332         }
333
334         // Process children of any composite children we have.
335         final var it = unlinkedComposites.iterator();
336         while (it.hasNext()) {
337             final var tmp = it.next().linkOriginalGeneratorRecursive();
338             if (tmp != LinkageProgress.NONE) {
339                 progress = LinkageProgress.SOME;
340             }
341             if (tmp == LinkageProgress.DONE) {
342                 it.remove();
343             }
344         }
345
346         if (unlinkedChildren.isEmpty() && unlinkedComposites.isEmpty()) {
347             // All done, set the list to null to indicate there is nothing left to do in this generator or any of our
348             // children.
349             unlinkedComposites = null;
350             return LinkageProgress.DONE;
351         }
352
353         return progress;
354     }
355
356     @Override
357     final AbstractCompositeGenerator<S, R> getOriginal() {
358         return (AbstractCompositeGenerator<S, R>) super.getOriginal();
359     }
360
361     @Override
362     final AbstractCompositeGenerator<S, R> tryOriginal() {
363         return (AbstractCompositeGenerator<S, R>) super.tryOriginal();
364     }
365
366     final <X extends EffectiveStatement<?, ?>, Y extends RuntimeType> @Nullable OriginalLink<X, Y> originalChild(
367             final QName childQName) {
368         // First try groupings/augments ...
369         var found = findInferredGenerator(childQName);
370         if (found != null) {
371             return (OriginalLink<X, Y>) OriginalLink.partial(found);
372         }
373
374         // ... no luck, we really need to start looking at our origin
375         final var prev = previous();
376         if (prev != null) {
377             final QName prevQName = childQName.bindTo(prev.getQName().getModule());
378             found = prev.findSchemaTreeGenerator(prevQName);
379             if (found != null) {
380                 return (OriginalLink<X, Y>) found.originalLink();
381             }
382         }
383
384         return null;
385     }
386
387     @Override
388     final AbstractExplicitGenerator<?, ?> findSchemaTreeGenerator(final QName qname) {
389         final var found = super.findSchemaTreeGenerator(qname);
390         return found != null ? found : findInferredGenerator(qname);
391     }
392
393     final @Nullable AbstractAugmentGenerator findAugmentForGenerator(final QName qname) {
394         for (var augment : augments) {
395             final var gen = augment.findSchemaTreeGenerator(qname);
396             if (gen != null) {
397                 return augment;
398             }
399         }
400         return null;
401     }
402
403     final @Nullable GroupingGenerator findGroupingForGenerator(final QName qname) {
404         for (var grouping : groupings) {
405             final var gen = grouping.findSchemaTreeGenerator(qname.bindTo(grouping.statement().argument().getModule()));
406             if (gen != null) {
407                 return grouping;
408             }
409         }
410         return null;
411     }
412
413     private @Nullable AbstractExplicitGenerator<?, ?> findInferredGenerator(final QName qname) {
414         // First search our local groupings ...
415         for (var grouping : groupings) {
416             final var gen = grouping.findSchemaTreeGenerator(qname.bindTo(grouping.statement().argument().getModule()));
417             if (gen != null) {
418                 return gen;
419             }
420         }
421         // ... next try local augments, which may have groupings themselves
422         for (var augment : augments) {
423             final var gen = augment.findSchemaTreeGenerator(qname);
424             if (gen != null) {
425                 return gen;
426             }
427         }
428         return null;
429     }
430
431     /**
432      * Update the specified builder to implement interfaces generated for the {@code grouping} statements this generator
433      * is using.
434      *
435      * @param builder Target builder
436      * @param builderFactory factory for creating {@link TypeBuilder}s
437      * @return The number of groupings this type uses.
438      */
439     final int addUsesInterfaces(final GeneratedTypeBuilder builder, final TypeBuilderFactory builderFactory) {
440         for (var grp : groupings) {
441             builder.addImplementsType(grp.getGeneratedType(builderFactory));
442         }
443         return groupings.size();
444     }
445
446     static final void addAugmentable(final GeneratedTypeBuilder builder) {
447         builder.addImplementsType(BindingTypes.augmentable(builder));
448     }
449
450     final void addGetterMethods(final GeneratedTypeBuilder builder, final TypeBuilderFactory builderFactory) {
451         for (var child : this) {
452             // Only process explicit generators here
453             if (child instanceof AbstractExplicitGenerator<?, ?> explicit) {
454                 explicit.addAsGetterMethod(builder, builderFactory);
455             }
456
457             final var enclosedType = child.enclosedType(builderFactory);
458             if (enclosedType instanceof GeneratedTransferObject gto) {
459                 builder.addEnclosingTransferObject(gto);
460             } else if (enclosedType instanceof Enumeration enumeration) {
461                 builder.addEnumeration(enumeration);
462             } else {
463                 verify(enclosedType == null, "Unhandled enclosed type %s in %s", enclosedType, child);
464             }
465         }
466     }
467
468     private @NonNull List<Generator> createChildren(final EffectiveStatement<?, ?> statement) {
469         final var tmp = new ArrayList<Generator>();
470         final var tmpAug = new ArrayList<AbstractAugmentGenerator>();
471
472         for (var stmt : statement.effectiveSubstatements()) {
473             if (stmt instanceof ActionEffectiveStatement action) {
474                 if (!isAugmenting(action)) {
475                     tmp.add(new ActionGenerator(action, this));
476                 }
477             } else if (stmt instanceof AnydataEffectiveStatement anydata) {
478                 if (!isAugmenting(anydata)) {
479                     tmp.add(new OpaqueObjectGenerator.Anydata(anydata, this));
480                 }
481             } else if (stmt instanceof AnyxmlEffectiveStatement anyxml) {
482                 if (!isAugmenting(anyxml)) {
483                     tmp.add(new OpaqueObjectGenerator.Anyxml(anyxml, this));
484                 }
485             } else if (stmt instanceof CaseEffectiveStatement cast) {
486                 tmp.add(new CaseGenerator(cast, this));
487             } else if (stmt instanceof ChoiceEffectiveStatement choice) {
488                 // FIXME: use isOriginalDeclaration() ?
489                 if (!isAddedByUses(choice)) {
490                     tmp.add(new ChoiceGenerator(choice, this));
491                 }
492             } else if (stmt instanceof ContainerEffectiveStatement container) {
493                 if (isOriginalDeclaration(container)) {
494                     tmp.add(new ContainerGenerator(container, this));
495                 }
496             } else if (stmt instanceof FeatureEffectiveStatement feature && this instanceof ModuleGenerator parent) {
497                 tmp.add(new FeatureGenerator(feature, parent));
498             } else if (stmt instanceof GroupingEffectiveStatement grouping) {
499                 tmp.add(new GroupingGenerator(grouping, this));
500             } else if (stmt instanceof IdentityEffectiveStatement identity) {
501                 tmp.add(new IdentityGenerator(identity, this));
502             } else if (stmt instanceof InputEffectiveStatement input) {
503                 tmp.add(new InputGenerator(input, this));
504             } else if (stmt instanceof LeafEffectiveStatement leaf) {
505                 if (!isAugmenting(leaf)) {
506                     tmp.add(new LeafGenerator(leaf, this));
507                 }
508             } else if (stmt instanceof LeafListEffectiveStatement leafList) {
509                 if (!isAugmenting(leafList)) {
510                     tmp.add(new LeafListGenerator(leafList, this));
511                 }
512             } else if (stmt instanceof ListEffectiveStatement list) {
513                 if (isOriginalDeclaration(list)) {
514                     final var listGen = new ListGenerator(list, this);
515                     tmp.add(listGen);
516
517                     final var keyGen = listGen.keyGenerator();
518                     if (keyGen != null) {
519                         tmp.add(keyGen);
520                     }
521                 }
522             } else if (stmt instanceof NotificationEffectiveStatement notification) {
523                 if (!isAugmenting(notification)) {
524                     tmp.add(new NotificationGenerator(notification, this));
525                 }
526             } else if (stmt instanceof OutputEffectiveStatement output) {
527                 tmp.add(new OutputGenerator(output, this));
528             } else if (stmt instanceof RpcEffectiveStatement rpc) {
529                 if (this instanceof ModuleGenerator module) {
530                     tmp.add(new RpcGenerator(rpc, module));
531                 }
532             } else if (stmt instanceof TypedefEffectiveStatement typedef) {
533                 tmp.add(new TypedefGenerator(typedef, this));
534             } else if (stmt instanceof AugmentEffectiveStatement augment) {
535                 // FIXME: MDSAL-695: So here we are ignoring any augment which is not in a module, while the 'uses'
536                 //                   processing takes care of the rest. There are two problems here:
537                 //
538                 //                   1) this could be an augment introduced through uses -- in this case we are picking
539                 //                      confusing it with this being its declaration site, we should probably be
540                 //                      ignoring it, but then
541                 //
542                 //                   2) we are losing track of AugmentEffectiveStatement for which we do not generate
543                 //                      interfaces -- and recover it at runtime through explicit walk along the
544                 //                      corresponding AugmentationSchemaNode.getOriginalDefinition() pointer
545                 //
546                 //                   So here is where we should decide how to handle this augment, and make sure we
547                 //                   retain information about this being an alias. That will serve as the base for keys
548                 //                   in the augment -> original map we provide to BindingRuntimeTypes.
549                 if (this instanceof ModuleGenerator module) {
550                     tmpAug.add(new ModuleAugmentGenerator(augment, module));
551                 }
552             } else if (stmt instanceof UsesEffectiveStatement uses) {
553                 for (var usesSub : uses.effectiveSubstatements()) {
554                     if (usesSub instanceof AugmentEffectiveStatement usesAug) {
555                         tmpAug.add(new UsesAugmentGenerator(usesAug, uses, this));
556                     }
557                 }
558             } else if (stmt instanceof YangDataEffectiveStatement yangData) {
559                 if (this instanceof ModuleGenerator moduleGen) {
560                     tmp.add(YangDataGenerator.of(yangData, moduleGen));
561                 }
562             } else {
563                 LOG.trace("Ignoring statement {}", stmt);
564             }
565         }
566
567         // Sort augments and add them last. This ensures child iteration order always reflects potential
568         // interdependencies, hence we do not need to worry about them. This is extremely important, as there are a
569         // number of places where we would have to either move the logic to parent statement and explicitly filter/sort
570         // substatements to establish this order.
571         tmpAug.sort(AbstractAugmentGenerator.COMPARATOR);
572         tmp.addAll(tmpAug);
573         return List.copyOf(tmp);
574     }
575
576     // Utility equivalent of (!isAddedByUses(stmt) && !isAugmenting(stmt)). Takes advantage of relationship between
577     // CopyableNode and AddedByUsesAware
578     private static boolean isOriginalDeclaration(final EffectiveStatement<?, ?> stmt) {
579         if (stmt instanceof AddedByUsesAware aware
580             && (aware.isAddedByUses() || stmt instanceof CopyableNode copyable && copyable.isAugmenting())) {
581             return false;
582         }
583         return true;
584     }
585
586     private static boolean isAddedByUses(final EffectiveStatement<?, ?> stmt) {
587         return stmt instanceof AddedByUsesAware aware && aware.isAddedByUses();
588     }
589
590     private static boolean isAugmenting(final EffectiveStatement<?, ?> stmt) {
591         return stmt instanceof CopyableNode copyable && copyable.isAugmenting();
592     }
593 }