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