2 * Copyright (c) 2015 Cisco Systems, Inc. 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.yangtools.yang.parser.spi.meta;
10 import static java.util.Objects.requireNonNull;
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;
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;
45 public final class StmtContextUtils {
46 private StmtContextUtils() {
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();
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;
67 public static <A, D extends DeclaredStatement<A>> A firstSubstatementAttributeOf(
68 final StmtContext<?, ?, ?> ctx, final Class<D> declaredType) {
69 return firstAttributeOf(ctx.allSubstatements(), declaredType);
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;
84 public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
85 int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
86 if (startIndex >= types.length) {
90 for (var subStmtContext : stmtContext.declaredSubstatements()) {
91 if (subStmtContext.producesDeclared(types[startIndex])) {
92 return startIndex + 1 == types.length ? subStmtContext : findFirstDeclaredSubstatement(subStmtContext,
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);
108 return listBuilder.build();
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);
120 return listBuilder.build();
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))
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;
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;
149 final var result = findFirstDeclaredSubstatementOnSublevel(subStmtContext, declaredType, --sublevel);
150 if (result != null) {
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;
166 final var result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
167 if (result != null) {
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.
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;
188 final var parent = current.coerceParentContext();
189 if (parent.getParentContext() == null) {
192 if (isUnknownStatement(parent)) {
200 * Returns true if supplied statement context represents unknown statement,
201 * otherwise returns false.
204 * statement context to be checked
205 * @return true if supplied statement context represents unknown statement,
207 * @throws NullPointerException
208 * if supplied statement context is null
210 public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
211 return UnknownStatement.class.isAssignableFrom(stmtCtx.publicDefinition().getDeclaredRepresentationClass());
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)) {
231 return !containsIfFeature || isSupported;
235 * Checks whether statement context is a presence container or not.
239 * @return true if it is a presence container
241 public static boolean isPresenceContainer(final StmtContext<?, ?, ?> stmtCtx) {
242 return stmtCtx.publicDefinition() == YangStmtMapping.CONTAINER && containsPresenceSubStmt(stmtCtx);
246 * Checks whether statement context is a non-presence container or not.
250 * @return true if it is a non-presence container
252 public static boolean isNonPresenceContainer(final StmtContext<?, ?, ?> stmtCtx) {
253 return stmtCtx.publicDefinition() == YangStmtMapping.CONTAINER && !containsPresenceSubStmt(stmtCtx);
256 private static boolean containsPresenceSubStmt(final StmtContext<?, ?, ?> stmtCtx) {
257 return stmtCtx.hasSubstatement(PresenceEffectiveStatement.class);
261 * Checks whether statement context is a mandatory leaf, choice, anyxml,
262 * list or leaf-list according to RFC6020 or not.
266 * @return true if it is a mandatory leaf, choice, anyxml, list or leaf-list
267 * according to RFC6020.
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;
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.
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
297 public static boolean isNotMandatoryNodeOfType(final StmtContext<?, ?, ?> stmtCtx,
298 final StatementDefinition stmtDef) {
299 return stmtCtx.publicDefinition().equals(stmtDef) && !isMandatoryNode(stmtCtx);
303 * Checks whether at least one ancestor of a StatementContext matches one from a collection of statement
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.
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())) {
319 current = current.getParentContext();
325 * Check whether all of StmtContext's {@code list} ancestors have a {@code key}.
327 * @param stmt EffectiveStmtCtx to be checked
328 * @param name Human-friendly statement name
329 * @throws SourceException if there is any keyless list ancestor
331 public static void validateNoKeylessListAncestorOf(final Mutable<?, ?, ?> stmt, final String name) {
332 requireNonNull(stmt);
334 // We do not expect this to by typically populated
335 final var incomplete = new ArrayList<Mutable<?, ?, ?>>(0);
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,
347 // Ancestor has not completed full declaration yet missing 'key' statement may materialize later
348 incomplete.add(current);
352 parent = current.getParentContext();
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() {
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());
370 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
371 throw new VerifyException("Should never happen");
378 * Checks whether the parent of StmtContext is of specified type.
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
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());
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).
396 * If one of the list keys contains an if-feature or a when statement in YANG 1.1 model, an exception is thrown.
398 * @param ctx statement context to be validated
400 public static void validateIfFeatureAndWhenOnListKeys(final StmtContext<?, ?, ?> ctx) {
401 if (!isRelevantForIfFeatureAndWhenOnListKeysCheck(ctx)) {
405 final var listStmtCtx = ctx.coerceParentContext();
406 final var keyStmtCtx = findFirstDeclaredSubstatement(listStmtCtx, KeyStatement.class);
408 if (YangStmtMapping.LEAF.equals(ctx.publicDefinition())) {
409 if (isListKey(ctx, keyStmtCtx)) {
410 disallowIfFeatureAndWhenOnListKeys(ctx);
412 } else if (YangStmtMapping.USES.equals(ctx.publicDefinition())) {
413 findAllEffectiveSubstatements(listStmtCtx, LeafStatement.class).forEach(leafStmtCtx -> {
414 if (isListKey(leafStmtCtx, keyStmtCtx)) {
415 disallowIfFeatureAndWhenOnListKeys(leafStmtCtx);
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;
426 private static boolean isListKey(final StmtContext<?, ?, ?> leafStmtCtx,
427 final StmtContext<Set<QName>, ?, ?> keyStmtCtx) {
428 return keyStmtCtx.getArgument().contains(leafStmtCtx.argument());
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());
441 public static @NonNull QName qnameFromArgument(StmtContext<?, ?, ?> ctx, final String value) {
442 if (Strings.isNullOrEmpty(value)) {
443 return ctx.publicDefinition().getStatementName();
446 QNameModule qnameModule;
448 final var namesParts = value.split(":");
449 switch (namesParts.length) {
451 localName = namesParts[0];
452 qnameModule = getModuleQName(ctx);
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)) {
461 qnameModule = getModuleQName(ctx);
463 if (qnameModule == null && ctx.history().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) {
464 ctx = ctx.getOriginalCtx().orElse(null);
465 qnameModule = getModuleQNameByPrefix(ctx, prefix);
469 return internedQName(ctx, InferenceException.throwIfNull(qnameModule, ctx,
470 "Cannot resolve QNameModule for '%s'", value), localName);
474 * Parse a YANG identifier string in context of a statement.
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
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);
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),
496 * Parse a YANG node identifier string in context of a statement.
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
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");
507 final int colon = str.indexOf(':');
509 return internedQName(ctx, str);
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);
517 return parseNodeIdentifier(ctx, prefix, localName);
520 private static @NonNull QName internedQName(final StmtContext<?, ?, ?> ctx, final String localName) {
521 return internedQName(ctx, getModuleQName(ctx), localName);
524 private static @NonNull QName internedQName(final CommonStmtCtx ctx, final QNameModule module,
525 final String localName) {
526 final QName template;
528 template = QName.create(module, localName);
529 } catch (IllegalArgumentException e) {
530 throw new SourceException(ctx, e, "Invalid identifier '%s'", localName);
532 return template.intern();
535 public static @NonNull QNameModule getModuleQName(final @NonNull StmtContext<?, ?, ?> ctx) {
536 return getModuleQName(ctx.getRoot());
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");
547 return lookupModuleQName(ctx, belongsTo.values().iterator().next());
549 throw new IllegalArgumentException("Unsupported root " + ctx);
553 private static @NonNull QNameModule lookupModuleQName(final NamespaceStmtCtx storage,
554 final StmtContext<?, ?, ?> module) {
555 final var ret = storage.namespaceItem(ParserNamespaces.MODULECTX_TO_QNAME, module);
557 throw new IllegalArgumentException("Failed to look up QNameModule for " + module + " in " + storage);
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) {
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));
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();
585 final var subArg = (Revision) subStmt.argument();
586 if (subArg != null && subArg.compareTo(revision) > 0) {
592 return Optional.ofNullable(revision);