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.Empty;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.common.Revision;
24 import org.opendaylight.yangtools.yang.common.YangVersion;
25 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
26 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
28 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
29 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureExpr;
30 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.LeafStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.MinElementsStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.PresenceEffectiveStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
38 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
39 import org.opendaylight.yangtools.yang.model.api.stmt.UnknownStatement;
40 import org.opendaylight.yangtools.yang.parser.spi.ParserNamespaces;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
45 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
47 public final class StmtContextUtils {
48 private StmtContextUtils() {
52 @SuppressWarnings("unchecked")
53 public static <A, D extends DeclaredStatement<A>> A firstAttributeOf(
54 final Iterable<? extends StmtContext<?, ?, ?>> contexts, final Class<D> declaredType) {
55 for (var ctx : contexts) {
56 if (ctx.producesDeclared(declaredType)) {
57 return (A) ctx.argument();
63 @SuppressWarnings("unchecked")
64 public static <A, D extends DeclaredStatement<A>> A firstAttributeOf(final StmtContext<?, ?, ?> ctx,
65 final Class<D> declaredType) {
66 return ctx.producesDeclared(declaredType) ? (A) ctx.argument() : null;
69 public static <A, D extends DeclaredStatement<A>> A firstSubstatementAttributeOf(
70 final StmtContext<?, ?, ?> ctx, final Class<D> declaredType) {
71 return firstAttributeOf(ctx.allSubstatements(), declaredType);
74 @SuppressWarnings("unchecked")
75 public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstDeclaredSubstatement(
76 final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
77 for (var subStmtContext : stmtContext.declaredSubstatements()) {
78 if (subStmtContext.producesDeclared(declaredType)) {
79 return (StmtContext<A, ?, ?>) subStmtContext;
86 public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
87 int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
88 if (startIndex >= types.length) {
92 for (var subStmtContext : stmtContext.declaredSubstatements()) {
93 if (subStmtContext.producesDeclared(types[startIndex])) {
94 return startIndex + 1 == types.length ? subStmtContext : findFirstDeclaredSubstatement(subStmtContext,
101 @SuppressWarnings("unchecked")
102 public static <A, D extends DeclaredStatement<A>> Collection<StmtContext<A, D, ?>> findAllDeclaredSubstatements(
103 final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
104 final var listBuilder = ImmutableList.<StmtContext<A, D, ?>>builder();
105 for (var subStmtContext : stmtContext.declaredSubstatements()) {
106 if (subStmtContext.producesDeclared(declaredType)) {
107 listBuilder.add((StmtContext<A, D, ?>) subStmtContext);
110 return listBuilder.build();
113 @SuppressWarnings("unchecked")
114 public static <A, D extends DeclaredStatement<A>> Collection<StmtContext<A, D, ?>> findAllEffectiveSubstatements(
115 final StmtContext<?, ?, ?> stmtContext, final Class<D> type) {
116 final var listBuilder = ImmutableList.<StmtContext<A, D, ?>>builder();
117 for (var subStmtContext : stmtContext.effectiveSubstatements()) {
118 if (subStmtContext.producesDeclared(type)) {
119 listBuilder.add((StmtContext<A, D, ?>) subStmtContext);
122 return listBuilder.build();
125 public static <A, D extends DeclaredStatement<A>> Collection<StmtContext<A, D, ?>> findAllSubstatements(
126 final StmtContext<?, ?, ?> stmtContext, final Class<D> type) {
127 return ImmutableList.<StmtContext<A, D, ?>>builder()
128 .addAll(findAllDeclaredSubstatements(stmtContext, type))
129 .addAll(findAllEffectiveSubstatements(stmtContext, type))
133 @SuppressWarnings("unchecked")
134 public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstEffectiveSubstatement(
135 final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
136 for (var subStmtContext : stmtContext.effectiveSubstatements()) {
137 if (subStmtContext.producesDeclared(declaredType)) {
138 return (StmtContext<A, ?, ?>) subStmtContext;
144 public static <D extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
145 final StmtContext<?, ?, ?> stmtContext, final Class<? super D> declaredType, int sublevel) {
146 for (var subStmtContext : stmtContext.declaredSubstatements()) {
147 if (sublevel == 1 && subStmtContext.producesDeclared(declaredType)) {
148 return subStmtContext;
151 final var result = findFirstDeclaredSubstatementOnSublevel(subStmtContext, declaredType, --sublevel);
152 if (result != null) {
161 public static <D extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
162 final StmtContext<?, ?, ?> stmtContext, final Class<? super D> declaredType) {
163 for (final var subStmtContext : stmtContext.declaredSubstatements()) {
164 if (subStmtContext.producesDeclared(declaredType)) {
165 return subStmtContext;
168 final var result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
169 if (result != null) {
177 // FIXME: 8.0.0: This method goes back as far as YANGTOOLS-365, when we were build EffectiveStatements for
178 // unsupported YANG extensions. We are not doing that anymore, do we still need this method? Also, it
179 // is only used in augment support to disable mechanics on unknown nodes.
181 // It would seem we can move this method to AbstractAugmentStatementSupport at the very least, but
182 // also: augments are defined to operate on schema tree nodes, hence even if we have an
183 // UnknownStatement, but its EffectiveStatement projection supports SchemaTreeAwareEffectiveStatement
184 // we should operate normally -- the StatementSupport exposing such semantics is responsible for
185 // arranging the backend details.
186 public static boolean isInExtensionBody(final StmtContext<?, ?, ?> stmtCtx) {
187 var current = stmtCtx;
190 final var parent = current.coerceParentContext();
191 if (parent.getParentContext() == null) {
194 if (isUnknownStatement(parent)) {
202 * Returns true if supplied statement context represents unknown statement,
203 * otherwise returns false.
206 * statement context to be checked
207 * @return true if supplied statement context represents unknown statement,
209 * @throws NullPointerException
210 * if supplied statement context is null
212 public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
213 return UnknownStatement.class.isAssignableFrom(stmtCtx.publicDefinition().getDeclaredRepresentationClass());
217 * Evaluate {@code if-feature} substatement of a statement and indicate whether they result in the statement being
220 * @param stmt Parent statement
221 * @return {@code true} if the statement is indicated to be supported under currently-supported features
223 public static boolean evaluateIfFeatures(final @NonNull StmtContext<?, ?, ?> stmt) {
224 final var supportedFeatures = stmt.namespaceItem(ParserNamespaces.SUPPORTED_FEATURES, Empty.value());
225 return supportedFeatures == null || checkFeatureSupport(stmt, supportedFeatures);
228 public static boolean checkFeatureSupport(final StmtContext<?, ?, ?> stmtContext,
229 final FeatureSet supportedFeatures) {
230 boolean isSupported = false;
231 boolean containsIfFeature = false;
232 for (var stmt : stmtContext.declaredSubstatements()) {
233 if (YangStmtMapping.IF_FEATURE.equals(stmt.publicDefinition())) {
234 containsIfFeature = true;
235 final var argument = (IfFeatureExpr) stmt.getArgument();
236 if (argument.test(supportedFeatures)) {
245 return !containsIfFeature || isSupported;
249 * Checks whether statement context is a presence container or not.
253 * @return true if it is a presence container
255 public static boolean isPresenceContainer(final StmtContext<?, ?, ?> stmtCtx) {
256 return stmtCtx.publicDefinition() == YangStmtMapping.CONTAINER && containsPresenceSubStmt(stmtCtx);
260 * Checks whether statement context is a non-presence container or not.
264 * @return true if it is a non-presence container
266 public static boolean isNonPresenceContainer(final StmtContext<?, ?, ?> stmtCtx) {
267 return stmtCtx.publicDefinition() == YangStmtMapping.CONTAINER && !containsPresenceSubStmt(stmtCtx);
270 private static boolean containsPresenceSubStmt(final StmtContext<?, ?, ?> stmtCtx) {
271 return stmtCtx.hasSubstatement(PresenceEffectiveStatement.class);
275 * Checks whether statement context is a mandatory leaf, choice, anyxml,
276 * list or leaf-list according to RFC6020 or not.
280 * @return true if it is a mandatory leaf, choice, anyxml, list or leaf-list
281 * according to RFC6020.
283 public static boolean isMandatoryNode(final StmtContext<?, ?, ?> stmtCtx) {
284 if (stmtCtx.publicDefinition() instanceof YangStmtMapping mapping) {
285 return switch (mapping) {
286 case LEAF, CHOICE, ANYXML -> Boolean.TRUE.equals(
287 firstSubstatementAttributeOf(stmtCtx, MandatoryStatement.class));
288 case LIST, LEAF_LIST -> {
289 final Integer minElements = firstSubstatementAttributeOf(stmtCtx, MinElementsStatement.class);
290 yield minElements != null && minElements > 0;
299 * Checks whether a statement context is a statement of supplied statement
300 * definition and whether it is not mandatory leaf, choice, anyxml, list or
301 * leaf-list according to RFC6020.
306 * statement definition
307 * @return true if supplied statement context is a statement of supplied
308 * statement definition and if it is not mandatory leaf, choice,
309 * anyxml, list or leaf-list according to RFC6020
311 public static boolean isNotMandatoryNodeOfType(final StmtContext<?, ?, ?> stmtCtx,
312 final StatementDefinition stmtDef) {
313 return stmtCtx.publicDefinition().equals(stmtDef) && !isMandatoryNode(stmtCtx);
317 * Checks whether at least one ancestor of a StatementContext matches one from a collection of statement
320 * @param stmt Statement context to be checked
321 * @param ancestorTypes collection of statement definitions
322 * @return true if at least one ancestor of a StatementContext matches one
323 * from collection of statement definitions, otherwise false.
325 public static boolean hasAncestorOfType(final StmtContext<?, ?, ?> stmt,
326 final Collection<StatementDefinition> ancestorTypes) {
327 requireNonNull(ancestorTypes);
328 var current = stmt.getParentContext();
329 while (current != null) {
330 if (ancestorTypes.contains(current.publicDefinition())) {
333 current = current.getParentContext();
339 * Check whether all of StmtContext's {@code list} ancestors have a {@code key}.
341 * @param stmt EffectiveStmtCtx to be checked
342 * @param name Human-friendly statement name
343 * @throws SourceException if there is any keyless list ancestor
345 public static void validateNoKeylessListAncestorOf(final Mutable<?, ?, ?> stmt, final String name) {
346 requireNonNull(stmt);
348 // We do not expect this to by typically populated
349 final var incomplete = new ArrayList<Mutable<?, ?, ?>>(0);
351 var current = stmt.coerceParentContext();
352 var parent = current.getParentContext();
353 while (parent != null) {
354 if (YangStmtMapping.LIST == current.publicDefinition()
355 && !current.hasSubstatement(KeyEffectiveStatement.class)) {
356 if (ModelProcessingPhase.FULL_DECLARATION.isCompletedBy(current.getCompletedPhase())) {
357 throw new SourceException(stmt, "%s %s is defined within a list that has no key statement", name,
361 // Ancestor has not completed full declaration yet missing 'key' statement may materialize later
362 incomplete.add(current);
366 parent = current.getParentContext();
369 // Deal with whatever incomplete ancestors we encountered
370 for (var ancestor : incomplete) {
371 // This check must complete during the ancestor's FULL_DECLARATION phase, i.e. the ancestor must not reach
372 // EFFECTIVE_MODEL until it is done.
373 final var action = ancestor.newInferenceAction(ModelProcessingPhase.FULL_DECLARATION);
374 action.apply(new InferenceAction() {
376 public void apply(final InferenceContext ctx) {
377 if (!ancestor.hasSubstatement(KeyEffectiveStatement.class)) {
378 throw new SourceException(stmt, "%s %s is defined within a list that has no key statement",
379 name, stmt.argument());
384 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
385 throw new VerifyException("Should never happen");
392 * Checks whether the parent of StmtContext is of specified type.
394 * @param ctx StmtContext to be checked
395 * @param parentType type of parent to check
396 * @return true if the parent of StmtContext is of specified type, otherwise false
398 public static boolean hasParentOfType(final StmtContext<?, ?, ?> ctx, final StatementDefinition parentType) {
399 requireNonNull(parentType);
400 final var parentContext = ctx.getParentContext();
401 return parentContext != null && parentType.equals(parentContext.publicDefinition());
405 * Validates the specified statement context with regards to if-feature and when statement on list keys.
406 * The context can either be a leaf which is defined directly in the substatements of a keyed list or a uses
407 * statement defined in a keyed list (a uses statement may add leaves into the list).
410 * If one of the list keys contains an if-feature or a when statement in YANG 1.1 model, an exception is thrown.
412 * @param ctx statement context to be validated
414 public static void validateIfFeatureAndWhenOnListKeys(final StmtContext<?, ?, ?> ctx) {
415 if (!isRelevantForIfFeatureAndWhenOnListKeysCheck(ctx)) {
419 final var listStmtCtx = ctx.coerceParentContext();
420 final var keyStmtCtx = findFirstDeclaredSubstatement(listStmtCtx, KeyStatement.class);
422 if (YangStmtMapping.LEAF.equals(ctx.publicDefinition())) {
423 if (isListKey(ctx, keyStmtCtx)) {
424 disallowIfFeatureAndWhenOnListKeys(ctx);
426 } else if (YangStmtMapping.USES.equals(ctx.publicDefinition())) {
427 findAllEffectiveSubstatements(listStmtCtx, LeafStatement.class).forEach(leafStmtCtx -> {
428 if (isListKey(leafStmtCtx, keyStmtCtx)) {
429 disallowIfFeatureAndWhenOnListKeys(leafStmtCtx);
435 private static boolean isRelevantForIfFeatureAndWhenOnListKeysCheck(final StmtContext<?, ?, ?> ctx) {
436 return YangVersion.VERSION_1_1.equals(ctx.yangVersion()) && hasParentOfType(ctx, YangStmtMapping.LIST)
437 && findFirstDeclaredSubstatement(ctx.coerceParentContext(), KeyStatement.class) != null;
440 private static boolean isListKey(final StmtContext<?, ?, ?> leafStmtCtx,
441 final StmtContext<Set<QName>, ?, ?> keyStmtCtx) {
442 return keyStmtCtx.getArgument().contains(leafStmtCtx.argument());
445 private static void disallowIfFeatureAndWhenOnListKeys(final StmtContext<?, ?, ?> leafStmtCtx) {
446 leafStmtCtx.allSubstatements().forEach(leafSubstmtCtx -> {
447 final var statementDef = leafSubstmtCtx.publicDefinition();
448 SourceException.throwIf(YangStmtMapping.IF_FEATURE.equals(statementDef)
449 || YangStmtMapping.WHEN.equals(statementDef), leafStmtCtx,
450 "%s statement is not allowed in %s leaf statement which is specified as a list key.",
451 statementDef.getStatementName(), leafStmtCtx.argument());
455 public static @NonNull QName qnameFromArgument(StmtContext<?, ?, ?> ctx, final String value) {
456 if (Strings.isNullOrEmpty(value)) {
457 return ctx.publicDefinition().getStatementName();
460 QNameModule qnameModule;
462 final var namesParts = value.split(":");
463 switch (namesParts.length) {
465 localName = namesParts[0];
466 qnameModule = getModuleQName(ctx);
469 final var prefix = namesParts[0];
470 localName = namesParts[1];
471 qnameModule = getModuleQNameByPrefix(ctx, prefix);
472 // in case of unknown statement argument, we're not going to parse it
473 if (qnameModule == null && isUnknownStatement(ctx)) {
475 qnameModule = getModuleQName(ctx);
477 if (qnameModule == null && ctx.history().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) {
478 ctx = ctx.getOriginalCtx().orElse(null);
479 qnameModule = getModuleQNameByPrefix(ctx, prefix);
483 return internedQName(ctx, InferenceException.throwIfNull(qnameModule, ctx,
484 "Cannot resolve QNameModule for '%s'", value), localName);
488 * Parse a YANG identifier string in context of a statement.
490 * @param ctx Statement context
491 * @param str String to be parsed
492 * @return An interned QName
493 * @throws NullPointerException if any of the arguments are null
494 * @throws SourceException if the string is not a valid YANG identifier
496 public static @NonNull QName parseIdentifier(final @NonNull StmtContext<?, ?, ?> ctx, final String str) {
497 SourceException.throwIf(str.isEmpty(), ctx, "Identifier may not be an empty string");
498 return internedQName(ctx, str);
501 public static @NonNull QName parseNodeIdentifier(final @NonNull StmtContext<?, ?, ?> ctx, final String prefix,
502 final String localName) {
503 return internedQName(ctx,
504 InferenceException.throwIfNull(getModuleQNameByPrefix(ctx, prefix), ctx,
505 "Cannot resolve QNameModule for '%s'", prefix),
510 * Parse a YANG node identifier string in context of a statement.
512 * @param ctx Statement context
513 * @param str String to be parsed
514 * @return An interned QName
515 * @throws NullPointerException if any of the arguments are null
516 * @throws SourceException if the string is not a valid YANG node identifier
518 public static @NonNull QName parseNodeIdentifier(final @NonNull StmtContext<?, ?, ?> ctx, final String str) {
519 SourceException.throwIf(str.isEmpty(), ctx, "Node identifier may not be an empty string");
521 final int colon = str.indexOf(':');
523 return internedQName(ctx, str);
526 final var prefix = str.substring(0, colon);
527 SourceException.throwIf(prefix.isEmpty(), ctx, "String '%s' has an empty prefix", str);
528 final var localName = str.substring(colon + 1);
529 SourceException.throwIf(localName.isEmpty(), ctx, "String '%s' has an empty identifier", str);
531 return parseNodeIdentifier(ctx, prefix, localName);
534 private static @NonNull QName internedQName(final @NonNull StmtContext<?, ?, ?> ctx, final String localName) {
535 return internedQName(ctx, getModuleQName(ctx), localName);
538 private static @NonNull QName internedQName(final @NonNull CommonStmtCtx ctx, final QNameModule module,
539 final String localName) {
540 final QName template;
542 template = QName.create(module, localName);
543 } catch (IllegalArgumentException e) {
544 throw new SourceException(ctx, e, "Invalid identifier '%s'", localName);
546 return template.intern();
549 public static @NonNull QNameModule getModuleQName(final @NonNull StmtContext<?, ?, ?> ctx) {
550 return getModuleQName(ctx.getRoot());
553 public static @NonNull QNameModule getModuleQName(final @NonNull RootStmtContext<?, ?, ?> ctx) {
554 if (ctx.producesDeclared(ModuleStatement.class)) {
555 return lookupModuleQName(ctx, ctx);
556 } else if (ctx.producesDeclared(SubmoduleStatement.class)) {
557 final var belongsTo = ctx.namespace(ParserNamespaces.BELONGSTO_PREFIX_TO_MODULECTX);
558 if (belongsTo == null || belongsTo.isEmpty()) {
559 throw new IllegalArgumentException(ctx + " does not have belongs-to linkage resolved");
561 return lookupModuleQName(ctx, belongsTo.values().iterator().next());
563 throw new IllegalArgumentException("Unsupported root " + ctx);
567 private static @NonNull QNameModule lookupModuleQName(final NamespaceStmtCtx storage,
568 final StmtContext<?, ?, ?> module) {
569 final var ret = storage.namespaceItem(ParserNamespaces.MODULECTX_TO_QNAME, module);
571 throw new IllegalArgumentException("Failed to look up QNameModule for " + module + " in " + storage);
576 public static QNameModule getModuleQNameByPrefix(final StmtContext<?, ?, ?> ctx, final String prefix) {
577 final var root = ctx.getRoot();
578 final var importedModule = root.namespaceItem(ParserNamespaces.IMPORT_PREFIX_TO_MODULECTX, prefix);
579 final var qnameModule = ctx.namespaceItem(ParserNamespaces.MODULECTX_TO_QNAME, importedModule);
580 if (qnameModule != null) {
584 if (root.producesDeclared(SubmoduleStatement.class)) {
585 return ctx.namespaceItem(ParserNamespaces.MODULE_NAME_TO_QNAME,
586 root.namespaceItem(ParserNamespaces.BELONGSTO_PREFIX_TO_MODULE_NAME, prefix));
592 public static Optional<Revision> getLatestRevision(final Iterable<? extends StmtContext<?, ?, ?>> subStmts) {
593 Revision revision = null;
594 for (var subStmt : subStmts) {
595 if (subStmt.producesDeclared(RevisionStatement.class)) {
596 if (revision == null && subStmt.argument() != null) {
597 revision = (Revision) subStmt.argument();
599 final var subArg = (Revision) subStmt.argument();
600 if (subArg != null && subArg.compareTo(revision) > 0) {
606 return Optional.ofNullable(revision);