73c48cf47fb771ddb13c0ae5903bcddd43e7e741
[yangtools.git] /
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.yangtools.yang.parser.spi.meta;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Strings;
13 import com.google.common.base.VerifyException;
14 import com.google.common.collect.ImmutableList;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Optional;
18 import java.util.Set;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.common.Revision;
23 import org.opendaylight.yangtools.yang.common.YangVersion;
24 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
25 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
27 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureExpr;
28 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.LeafStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.MinElementsStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.PresenceEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.UnknownStatement;
38 import org.opendaylight.yangtools.yang.parser.spi.ParserNamespaces;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
43 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
44
45 public final class StmtContextUtils {
46     private StmtContextUtils() {
47         // Hidden on purpose
48     }
49
50     @SuppressWarnings("unchecked")
51     public static <A, D extends DeclaredStatement<A>> A firstAttributeOf(
52             final Iterable<? extends StmtContext<?, ?, ?>> contexts, final Class<D> declaredType) {
53         for (var ctx : contexts) {
54             if (ctx.producesDeclared(declaredType)) {
55                 return (A) ctx.argument();
56             }
57         }
58         return null;
59     }
60
61     @SuppressWarnings("unchecked")
62     public static <A, D extends DeclaredStatement<A>> A firstAttributeOf(final StmtContext<?, ?, ?> ctx,
63             final Class<D> declaredType) {
64         return ctx.producesDeclared(declaredType) ? (A) ctx.argument() : null;
65     }
66
67     public static <A, D extends DeclaredStatement<A>> A firstSubstatementAttributeOf(
68             final StmtContext<?, ?, ?> ctx, final Class<D> declaredType) {
69         return firstAttributeOf(ctx.allSubstatements(), declaredType);
70     }
71
72     @SuppressWarnings("unchecked")
73     public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstDeclaredSubstatement(
74             final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
75         for (var subStmtContext : stmtContext.declaredSubstatements()) {
76             if (subStmtContext.producesDeclared(declaredType)) {
77                 return (StmtContext<A, ?, ?>) subStmtContext;
78             }
79         }
80         return null;
81     }
82
83     @SafeVarargs
84     public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
85             int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
86         if (startIndex >= types.length) {
87             return null;
88         }
89
90         for (var subStmtContext : stmtContext.declaredSubstatements()) {
91             if (subStmtContext.producesDeclared(types[startIndex])) {
92                 return startIndex + 1 == types.length ? subStmtContext : findFirstDeclaredSubstatement(subStmtContext,
93                         ++startIndex, types);
94             }
95         }
96         return null;
97     }
98
99     @SuppressWarnings("unchecked")
100     public static <A, D extends DeclaredStatement<A>> Collection<StmtContext<A, D, ?>> findAllDeclaredSubstatements(
101             final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
102         final var listBuilder = ImmutableList.<StmtContext<A, D, ?>>builder();
103         for (var subStmtContext : stmtContext.declaredSubstatements()) {
104             if (subStmtContext.producesDeclared(declaredType)) {
105                 listBuilder.add((StmtContext<A, D, ?>) subStmtContext);
106             }
107         }
108         return listBuilder.build();
109     }
110
111     @SuppressWarnings("unchecked")
112     public static <A, D extends DeclaredStatement<A>> Collection<StmtContext<A, D, ?>> findAllEffectiveSubstatements(
113             final StmtContext<?, ?, ?> stmtContext, final Class<D> type) {
114         final var listBuilder = ImmutableList.<StmtContext<A, D, ?>>builder();
115         for (var subStmtContext : stmtContext.effectiveSubstatements()) {
116             if (subStmtContext.producesDeclared(type)) {
117                 listBuilder.add((StmtContext<A, D, ?>) subStmtContext);
118             }
119         }
120         return listBuilder.build();
121     }
122
123     public static <A, D extends DeclaredStatement<A>> Collection<StmtContext<A, D, ?>> findAllSubstatements(
124             final StmtContext<?, ?, ?> stmtContext, final Class<D> type) {
125         return ImmutableList.<StmtContext<A, D, ?>>builder()
126             .addAll(findAllDeclaredSubstatements(stmtContext, type))
127             .addAll(findAllEffectiveSubstatements(stmtContext, type))
128             .build();
129     }
130
131     @SuppressWarnings("unchecked")
132     public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstEffectiveSubstatement(
133             final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
134         for (var subStmtContext : stmtContext.effectiveSubstatements()) {
135             if (subStmtContext.producesDeclared(declaredType)) {
136                 return (StmtContext<A, ?, ?>) subStmtContext;
137             }
138         }
139         return null;
140     }
141
142     public static <D extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
143             final StmtContext<?, ?, ?> stmtContext, final Class<? super D> declaredType, int sublevel) {
144         for (var subStmtContext : stmtContext.declaredSubstatements()) {
145             if (sublevel == 1 && subStmtContext.producesDeclared(declaredType)) {
146                 return subStmtContext;
147             }
148             if (sublevel > 1) {
149                 final var result = findFirstDeclaredSubstatementOnSublevel(subStmtContext, declaredType, --sublevel);
150                 if (result != null) {
151                     return result;
152                 }
153             }
154         }
155
156         return null;
157     }
158
159     public static <D extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
160             final StmtContext<?, ?, ?> stmtContext, final Class<? super D> declaredType) {
161         for (final var subStmtContext : stmtContext.declaredSubstatements()) {
162             if (subStmtContext.producesDeclared(declaredType)) {
163                 return subStmtContext;
164             }
165
166             final var result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
167             if (result != null) {
168                 return result;
169             }
170         }
171
172         return null;
173     }
174
175     // FIXME: 8.0.0: This method goes back as far as YANGTOOLS-365, when we were build EffectiveStatements for
176     //               unsupported YANG extensions. We are not doing that anymore, do we still need this method? Also, it
177     //               is only used in augment support to disable mechanics on unknown nodes.
178     //
179     //               It would seem we can move this method to AbstractAugmentStatementSupport at the very least, but
180     //               also: augments are defined to operate on schema tree nodes, hence even if we have an
181     //               UnknownStatement, but its EffectiveStatement projection supports SchemaTreeAwareEffectiveStatement
182     //               we should operate normally -- the StatementSupport exposing such semantics is responsible for
183     //               arranging the backend details.
184     public static boolean isInExtensionBody(final StmtContext<?, ?, ?> stmtCtx) {
185         var current = stmtCtx;
186
187         while (true) {
188             final var parent = current.coerceParentContext();
189             if (parent.getParentContext() == null) {
190                 return false;
191             }
192             if (isUnknownStatement(parent)) {
193                 return true;
194             }
195             current = parent;
196         }
197     }
198
199     /**
200      * Returns true if supplied statement context represents unknown statement,
201      * otherwise returns false.
202      *
203      * @param stmtCtx
204      *            statement context to be checked
205      * @return true if supplied statement context represents unknown statement,
206      *         otherwise false
207      * @throws NullPointerException
208      *             if supplied statement context is null
209      */
210     public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
211         return UnknownStatement.class.isAssignableFrom(stmtCtx.publicDefinition().getDeclaredRepresentationClass());
212     }
213
214     public static boolean checkFeatureSupport(final StmtContext<?, ?, ?> stmtContext,
215             final Set<QName> supportedFeatures) {
216         boolean isSupported = false;
217         boolean containsIfFeature = false;
218         for (var stmt : stmtContext.declaredSubstatements()) {
219             if (YangStmtMapping.IF_FEATURE.equals(stmt.publicDefinition())) {
220                 containsIfFeature = true;
221                 final var argument = (IfFeatureExpr) stmt.getArgument();
222                 if (argument.test(supportedFeatures)) {
223                     isSupported = true;
224                 } else {
225                     isSupported = false;
226                     break;
227                 }
228             }
229         }
230
231         return !containsIfFeature || isSupported;
232     }
233
234     /**
235      * Checks whether statement context is a presence container or not.
236      *
237      * @param stmtCtx
238      *            statement context
239      * @return true if it is a presence container
240      */
241     public static boolean isPresenceContainer(final StmtContext<?, ?, ?> stmtCtx) {
242         return stmtCtx.publicDefinition() == YangStmtMapping.CONTAINER && containsPresenceSubStmt(stmtCtx);
243     }
244
245     /**
246      * Checks whether statement context is a non-presence container or not.
247      *
248      * @param stmtCtx
249      *            statement context
250      * @return true if it is a non-presence container
251      */
252     public static boolean isNonPresenceContainer(final StmtContext<?, ?, ?> stmtCtx) {
253         return stmtCtx.publicDefinition() == YangStmtMapping.CONTAINER && !containsPresenceSubStmt(stmtCtx);
254     }
255
256     private static boolean containsPresenceSubStmt(final StmtContext<?, ?, ?> stmtCtx) {
257         return stmtCtx.hasSubstatement(PresenceEffectiveStatement.class);
258     }
259
260     /**
261      * Checks whether statement context is a mandatory leaf, choice, anyxml,
262      * list or leaf-list according to RFC6020 or not.
263      *
264      * @param stmtCtx
265      *            statement context
266      * @return true if it is a mandatory leaf, choice, anyxml, list or leaf-list
267      *         according to RFC6020.
268      */
269     public static boolean isMandatoryNode(final StmtContext<?, ?, ?> stmtCtx) {
270         if (stmtCtx.publicDefinition() instanceof YangStmtMapping mapping) {
271             return switch (mapping) {
272                 case LEAF, CHOICE, ANYXML -> Boolean.TRUE.equals(
273                     firstSubstatementAttributeOf(stmtCtx, MandatoryStatement.class));
274                 case LIST, LEAF_LIST -> {
275                     final Integer minElements = firstSubstatementAttributeOf(stmtCtx, MinElementsStatement.class);
276                     yield minElements != null && minElements > 0;
277                 }
278                 default -> false;
279             };
280         }
281         return false;
282     }
283
284     /**
285      * Checks whether a statement context is a statement of supplied statement
286      * definition and whether it is not mandatory leaf, choice, anyxml, list or
287      * leaf-list according to RFC6020.
288      *
289      * @param stmtCtx
290      *            statement context
291      * @param stmtDef
292      *            statement definition
293      * @return true if supplied statement context is a statement of supplied
294      *         statement definition and if it is not mandatory leaf, choice,
295      *         anyxml, list or leaf-list according to RFC6020
296      */
297     public static boolean isNotMandatoryNodeOfType(final StmtContext<?, ?, ?> stmtCtx,
298             final StatementDefinition stmtDef) {
299         return stmtCtx.publicDefinition().equals(stmtDef) && !isMandatoryNode(stmtCtx);
300     }
301
302     /**
303      * Checks whether at least one ancestor of a StatementContext matches one from a collection of statement
304      * definitions.
305      *
306      * @param stmt Statement context to be checked
307      * @param ancestorTypes collection of statement definitions
308      * @return true if at least one ancestor of a StatementContext matches one
309      *         from collection of statement definitions, otherwise false.
310      */
311     public static boolean hasAncestorOfType(final StmtContext<?, ?, ?> stmt,
312             final Collection<StatementDefinition> ancestorTypes) {
313         requireNonNull(ancestorTypes);
314         var current = stmt.getParentContext();
315         while (current != null) {
316             if (ancestorTypes.contains(current.publicDefinition())) {
317                 return true;
318             }
319             current = current.getParentContext();
320         }
321         return false;
322     }
323
324     /**
325      * Check whether all of StmtContext's {@code list} ancestors have a {@code key}.
326      *
327      * @param stmt EffectiveStmtCtx to be checked
328      * @param name Human-friendly statement name
329      * @throws SourceException if there is any keyless list ancestor
330      */
331     public static void validateNoKeylessListAncestorOf(final Mutable<?, ?, ?> stmt, final String name) {
332         requireNonNull(stmt);
333
334         // We do not expect this to by typically populated
335         final var incomplete = new ArrayList<Mutable<?, ?, ?>>(0);
336
337         var current = stmt.coerceParentContext();
338         var parent = current.getParentContext();
339         while (parent != null) {
340             if (YangStmtMapping.LIST == current.publicDefinition()
341                     && !current.hasSubstatement(KeyEffectiveStatement.class)) {
342                 if (ModelProcessingPhase.FULL_DECLARATION.isCompletedBy(current.getCompletedPhase())) {
343                     throw new SourceException(stmt, "%s %s is defined within a list that has no key statement", name,
344                         stmt.argument());
345                 }
346
347                 // Ancestor has not completed full declaration yet missing 'key' statement may materialize later
348                 incomplete.add(current);
349             }
350
351             current = parent;
352             parent = current.getParentContext();
353         }
354
355         // Deal with whatever incomplete ancestors we encountered
356         for (var ancestor : incomplete) {
357             // This check must complete during the ancestor's FULL_DECLARATION phase, i.e. the ancestor must not reach
358             // EFFECTIVE_MODEL until it is done.
359             final var action = ancestor.newInferenceAction(ModelProcessingPhase.FULL_DECLARATION);
360             action.apply(new InferenceAction() {
361                 @Override
362                 public void apply(final InferenceContext ctx) {
363                     if (!ancestor.hasSubstatement(KeyEffectiveStatement.class)) {
364                         throw new SourceException(stmt, "%s %s is defined within a list that has no key statement",
365                             name, stmt.argument());
366                     }
367                 }
368
369                 @Override
370                 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
371                     throw new VerifyException("Should never happen");
372                 }
373             });
374         }
375     }
376
377     /**
378      * Checks whether the parent of StmtContext is of specified type.
379      *
380      * @param ctx StmtContext to be checked
381      * @param parentType type of parent to check
382      * @return true if the parent of StmtContext is of specified type, otherwise false
383      */
384     public static boolean hasParentOfType(final StmtContext<?, ?, ?> ctx, final StatementDefinition parentType) {
385         requireNonNull(parentType);
386         final var parentContext = ctx.getParentContext();
387         return parentContext != null && parentType.equals(parentContext.publicDefinition());
388     }
389
390     /**
391      * Validates the specified statement context with regards to if-feature and when statement on list keys.
392      * The context can either be a leaf which is defined directly in the substatements of a keyed list or a uses
393      * statement defined in a keyed list (a uses statement may add leaves into the list).
394      *
395      * <p>
396      * If one of the list keys contains an if-feature or a when statement in YANG 1.1 model, an exception is thrown.
397      *
398      * @param ctx statement context to be validated
399      */
400     public static void validateIfFeatureAndWhenOnListKeys(final StmtContext<?, ?, ?> ctx) {
401         if (!isRelevantForIfFeatureAndWhenOnListKeysCheck(ctx)) {
402             return;
403         }
404
405         final var listStmtCtx = ctx.coerceParentContext();
406         final var keyStmtCtx = findFirstDeclaredSubstatement(listStmtCtx, KeyStatement.class);
407
408         if (YangStmtMapping.LEAF.equals(ctx.publicDefinition())) {
409             if (isListKey(ctx, keyStmtCtx)) {
410                 disallowIfFeatureAndWhenOnListKeys(ctx);
411             }
412         } else if (YangStmtMapping.USES.equals(ctx.publicDefinition())) {
413             findAllEffectiveSubstatements(listStmtCtx, LeafStatement.class).forEach(leafStmtCtx -> {
414                 if (isListKey(leafStmtCtx, keyStmtCtx)) {
415                     disallowIfFeatureAndWhenOnListKeys(leafStmtCtx);
416                 }
417             });
418         }
419     }
420
421     private static boolean isRelevantForIfFeatureAndWhenOnListKeysCheck(final StmtContext<?, ?, ?> ctx) {
422         return YangVersion.VERSION_1_1.equals(ctx.yangVersion()) && hasParentOfType(ctx, YangStmtMapping.LIST)
423                 && findFirstDeclaredSubstatement(ctx.coerceParentContext(), KeyStatement.class) != null;
424     }
425
426     private static boolean isListKey(final StmtContext<?, ?, ?> leafStmtCtx,
427             final StmtContext<Set<QName>, ?, ?> keyStmtCtx) {
428         return keyStmtCtx.getArgument().contains(leafStmtCtx.argument());
429     }
430
431     private static void disallowIfFeatureAndWhenOnListKeys(final StmtContext<?, ?, ?> leafStmtCtx) {
432         leafStmtCtx.allSubstatements().forEach(leafSubstmtCtx -> {
433             final var statementDef = leafSubstmtCtx.publicDefinition();
434             SourceException.throwIf(YangStmtMapping.IF_FEATURE.equals(statementDef)
435                     || YangStmtMapping.WHEN.equals(statementDef), leafStmtCtx,
436                     "%s statement is not allowed in %s leaf statement which is specified as a list key.",
437                     statementDef.getStatementName(), leafStmtCtx.argument());
438         });
439     }
440
441     public static @NonNull QName qnameFromArgument(StmtContext<?, ?, ?> ctx, final String value) {
442         if (Strings.isNullOrEmpty(value)) {
443             return ctx.publicDefinition().getStatementName();
444         }
445
446         QNameModule qnameModule;
447         String localName;
448         final var namesParts = value.split(":");
449         switch (namesParts.length) {
450             case 1:
451                 localName = namesParts[0];
452                 qnameModule = getModuleQName(ctx);
453                 break;
454             default:
455                 final var prefix = namesParts[0];
456                 localName = namesParts[1];
457                 qnameModule = getModuleQNameByPrefix(ctx, prefix);
458                 // in case of unknown statement argument, we're not going to parse it
459                 if (qnameModule == null && isUnknownStatement(ctx)) {
460                     localName = value;
461                     qnameModule = getModuleQName(ctx);
462                 }
463                 if (qnameModule == null && ctx.history().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) {
464                     ctx = ctx.getOriginalCtx().orElse(null);
465                     qnameModule = getModuleQNameByPrefix(ctx, prefix);
466                 }
467         }
468
469         return internedQName(ctx, InferenceException.throwIfNull(qnameModule, ctx,
470             "Cannot resolve QNameModule for '%s'", value), localName);
471     }
472
473     /**
474      * Parse a YANG identifier string in context of a statement.
475      *
476      * @param ctx Statement context
477      * @param str String to be parsed
478      * @return An interned QName
479      * @throws NullPointerException if any of the arguments are null
480      * @throws SourceException if the string is not a valid YANG identifier
481      */
482     public static @NonNull QName parseIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
483         SourceException.throwIf(str.isEmpty(), ctx, "Identifier may not be an empty string");
484         return internedQName(ctx, str);
485     }
486
487     public static @NonNull QName parseNodeIdentifier(final StmtContext<?, ?, ?> ctx, final String prefix,
488             final String localName) {
489         return internedQName(ctx,
490             InferenceException.throwIfNull(getModuleQNameByPrefix(ctx, prefix), ctx,
491                 "Cannot resolve QNameModule for '%s'", prefix),
492             localName);
493     }
494
495     /**
496      * Parse a YANG node identifier string in context of a statement.
497      *
498      * @param ctx Statement context
499      * @param str String to be parsed
500      * @return An interned QName
501      * @throws NullPointerException if any of the arguments are null
502      * @throws SourceException if the string is not a valid YANG node identifier
503      */
504     public static @NonNull QName parseNodeIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
505         SourceException.throwIf(str.isEmpty(), ctx, "Node identifier may not be an empty string");
506
507         final int colon = str.indexOf(':');
508         if (colon == -1) {
509             return internedQName(ctx, str);
510         }
511
512         final var prefix = str.substring(0, colon);
513         SourceException.throwIf(prefix.isEmpty(), ctx, "String '%s' has an empty prefix", str);
514         final var localName = str.substring(colon + 1);
515         SourceException.throwIf(localName.isEmpty(), ctx, "String '%s' has an empty identifier", str);
516
517         return parseNodeIdentifier(ctx, prefix, localName);
518     }
519
520     private static @NonNull QName internedQName(final StmtContext<?, ?, ?> ctx, final String localName) {
521         return internedQName(ctx, getModuleQName(ctx), localName);
522     }
523
524     private static @NonNull QName internedQName(final CommonStmtCtx ctx, final QNameModule module,
525             final String localName) {
526         final QName template;
527         try {
528             template = QName.create(module, localName);
529         } catch (IllegalArgumentException e) {
530             throw new SourceException(ctx, e, "Invalid identifier '%s'", localName);
531         }
532         return template.intern();
533     }
534
535     public static @NonNull QNameModule getModuleQName(final @NonNull StmtContext<?, ?, ?> ctx) {
536         return getModuleQName(ctx.getRoot());
537     }
538
539     public static @NonNull QNameModule getModuleQName(final @NonNull RootStmtContext<?, ?, ?> ctx) {
540         if (ctx.producesDeclared(ModuleStatement.class)) {
541             return lookupModuleQName(ctx, ctx);
542         } else if (ctx.producesDeclared(SubmoduleStatement.class)) {
543             final var belongsTo = ctx.namespace(ParserNamespaces.BELONGSTO_PREFIX_TO_MODULECTX);
544             if (belongsTo == null || belongsTo.isEmpty()) {
545                 throw new IllegalArgumentException(ctx + " does not have belongs-to linkage resolved");
546             }
547             return lookupModuleQName(ctx, belongsTo.values().iterator().next());
548         } else {
549             throw new IllegalArgumentException("Unsupported root " + ctx);
550         }
551     }
552
553     private static @NonNull QNameModule lookupModuleQName(final NamespaceStmtCtx storage,
554             final StmtContext<?, ?, ?> module) {
555         final var ret = storage.namespaceItem(ParserNamespaces.MODULECTX_TO_QNAME, module);
556         if (ret == null) {
557             throw new IllegalArgumentException("Failed to look up QNameModule for " + module + " in " + storage);
558         }
559         return ret;
560     }
561
562     public static QNameModule getModuleQNameByPrefix(final StmtContext<?, ?, ?> ctx, final String prefix) {
563         final var root = ctx.getRoot();
564         final var importedModule = root.namespaceItem(ParserNamespaces.IMPORT_PREFIX_TO_MODULECTX, prefix);
565         final var qnameModule = ctx.namespaceItem(ParserNamespaces.MODULECTX_TO_QNAME, importedModule);
566         if (qnameModule != null) {
567             return qnameModule;
568         }
569
570         if (root.producesDeclared(SubmoduleStatement.class)) {
571             return ctx.namespaceItem(ParserNamespaces.MODULE_NAME_TO_QNAME,
572                 root.namespaceItem(ParserNamespaces.BELONGSTO_PREFIX_TO_MODULE_NAME, prefix));
573         }
574
575         return null;
576     }
577
578     public static Optional<Revision> getLatestRevision(final Iterable<? extends StmtContext<?, ?, ?>> subStmts) {
579         Revision revision = null;
580         for (var subStmt : subStmts) {
581             if (subStmt.producesDeclared(RevisionStatement.class)) {
582                 if (revision == null && subStmt.argument() != null) {
583                     revision = (Revision) subStmt.argument();
584                 } else {
585                     final var subArg = (Revision) subStmt.argument();
586                     if (subArg != null && subArg.compareTo(revision) > 0) {
587                         revision = subArg;
588                     }
589                 }
590             }
591         }
592         return Optional.ofNullable(revision);
593     }
594 }