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 com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.Strings;
15 import com.google.common.collect.ImmutableList;
16 import java.util.Collection;
17 import java.util.Optional;
19 import java.util.function.Predicate;
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.BelongsToStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.LeafStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.MinElementsStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.PresenceStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
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.model.api.stmt.UnrecognizedStatement;
39 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
40 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
41 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleName;
42 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
43 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
44 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
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 (final StmtContext<?, ?, ?> ctx : contexts) {
56 if (producesDeclared(ctx, declaredType)) {
57 return (A) ctx.getStatementArgument();
63 @SuppressWarnings("unchecked")
64 public static <A, D extends DeclaredStatement<A>> A firstAttributeOf(final StmtContext<?, ?, ?> ctx,
65 final Class<D> declaredType) {
66 return producesDeclared(ctx, declaredType) ? (A) ctx.getStatementArgument() : 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 (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
78 if (producesDeclared(subStmtContext, 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 (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
93 if (producesDeclared(subStmtContext, 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 ImmutableList.Builder<StmtContext<A, D, ?>> listBuilder = ImmutableList.builder();
105 for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
106 if (producesDeclared(subStmtContext, 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 ImmutableList.Builder<StmtContext<A, D, ?>> listBuilder = ImmutableList.builder();
117 for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
118 if (producesDeclared(subStmtContext, 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 final ImmutableList.Builder<StmtContext<A, D, ?>> listBuilder = ImmutableList.builder();
128 listBuilder.addAll(findAllDeclaredSubstatements(stmtContext, type));
129 listBuilder.addAll(findAllEffectiveSubstatements(stmtContext, type));
130 return listBuilder.build();
133 @SuppressWarnings("unchecked")
134 public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstEffectiveSubstatement(
135 final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
136 for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
137 if (producesDeclared(subStmtContext, declaredType)) {
138 return (StmtContext<A, ?, ?>) subStmtContext;
145 * Searches for the first substatement of the specified type in the specified statement context.
146 * First, it tries to find the substatement in the effective substatements of the statement context.
147 * If it was not found, then it proceeds to search in the declared substatements. If it still was not found,
148 * the method returns null.
150 * @param stmtContext statement context to search in
151 * @param declaredType substatement type to search for
152 * @param <A> statement argument type
153 * @param <D> declared statement type
154 * @return statement context that was searched for or null if was not found
156 public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstSubstatement(
157 final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
158 final StmtContext<A, ?, ?> effectiveSubstatement = findFirstEffectiveSubstatement(stmtContext, declaredType);
159 return effectiveSubstatement != null ? effectiveSubstatement : findFirstDeclaredSubstatement(stmtContext,
163 public static <D extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
164 final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType, int sublevel) {
165 for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
166 if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) {
167 return subStmtContext;
170 final StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(subStmtContext,
171 declaredType, --sublevel);
172 if (result != null) {
181 public static <D extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
182 final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
183 for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
184 if (producesDeclared(subStmtContext, declaredType)) {
185 return subStmtContext;
188 final StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
189 if (result != null) {
197 public static boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
198 final Class<? extends DeclaredStatement<?>> type) {
199 return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
202 public static boolean isInExtensionBody(final StmtContext<?, ?, ?> stmtCtx) {
203 StmtContext<?, ?, ?> current = stmtCtx;
204 while (current.coerceParentContext().getParentContext() != null) {
205 current = current.getParentContext();
206 if (isUnknownStatement(current)) {
215 * Returns true if supplied statement context represents unknown statement,
216 * otherwise returns false.
219 * statement context to be checked
220 * @return true if supplied statement context represents unknown statement,
222 * @throws NullPointerException
223 * if supplied statement context is null
225 public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
226 return UnknownStatement.class
227 .isAssignableFrom(stmtCtx.getPublicDefinition().getDeclaredRepresentationClass());
231 * Returns true if supplied statement context represents unrecognized
232 * statement, otherwise returns false.
235 * statement context to be checked
236 * @return true if supplied statement context represents unrecognized
237 * statement, otherwise false
238 * @throws NullPointerException
239 * if supplied statement context is null
241 public static boolean isUnrecognizedStatement(final StmtContext<?, ?, ?> stmtCtx) {
242 return UnrecognizedStatement.class
243 .isAssignableFrom(stmtCtx.getPublicDefinition().getDeclaredRepresentationClass());
246 public static boolean checkFeatureSupport(final StmtContext<?, ?, ?> stmtContext,
247 final Set<QName> supportedFeatures) {
248 boolean isSupported = false;
249 boolean containsIfFeature = false;
250 for (final StmtContext<?, ?, ?> stmt : stmtContext.declaredSubstatements()) {
251 if (YangStmtMapping.IF_FEATURE.equals(stmt.getPublicDefinition())) {
252 containsIfFeature = true;
253 @SuppressWarnings("unchecked")
254 final Predicate<Set<QName>> argument = (Predicate<Set<QName>>) stmt.coerceStatementArgument();
255 if (argument.test(supportedFeatures)) {
264 return !containsIfFeature || isSupported;
268 * Checks whether statement context is a presence container or not.
272 * @return true if it is a presence container
274 public static boolean isPresenceContainer(final StmtContext<?, ?, ?> stmtCtx) {
275 return stmtCtx.getPublicDefinition() == YangStmtMapping.CONTAINER && containsPresenceSubStmt(stmtCtx);
279 * Checks whether statement context is a non-presence container or not.
283 * @return true if it is a non-presence container
285 public static boolean isNonPresenceContainer(final StmtContext<?, ?, ?> stmtCtx) {
286 return stmtCtx.getPublicDefinition() == YangStmtMapping.CONTAINER && !containsPresenceSubStmt(stmtCtx);
289 private static boolean containsPresenceSubStmt(final StmtContext<?, ?, ?> stmtCtx) {
290 return findFirstSubstatement(stmtCtx, PresenceStatement.class) != null;
294 * Checks whether statement context is a mandatory leaf, choice, anyxml,
295 * list or leaf-list according to RFC6020 or not.
299 * @return true if it is a mandatory leaf, choice, anyxml, list or leaf-list
300 * according to RFC6020.
302 public static boolean isMandatoryNode(final StmtContext<?, ?, ?> stmtCtx) {
303 if (!(stmtCtx.getPublicDefinition() instanceof YangStmtMapping)) {
306 switch ((YangStmtMapping) stmtCtx.getPublicDefinition()) {
310 return Boolean.TRUE.equals(firstSubstatementAttributeOf(stmtCtx, MandatoryStatement.class));
313 final Integer minElements = firstSubstatementAttributeOf(stmtCtx, MinElementsStatement.class);
314 return minElements != null && minElements > 0;
321 * Checks whether a statement context is a statement of supplied statement
322 * definition and whether it is not mandatory leaf, choice, anyxml, list or
323 * leaf-list according to RFC6020.
328 * statement definition
329 * @return true if supplied statement context is a statement of supplied
330 * statement definition and if it is not mandatory leaf, choice,
331 * anyxml, list or leaf-list according to RFC6020
333 public static boolean isNotMandatoryNodeOfType(final StmtContext<?, ?, ?> stmtCtx,
334 final StatementDefinition stmtDef) {
335 return stmtCtx.getPublicDefinition().equals(stmtDef) && !isMandatoryNode(stmtCtx);
339 * Checks whether at least one ancestor of a StatementContext matches one from a collection of statement
343 * StatementContext to be checked
344 * @param ancestorTypes
345 * collection of statement definitions
346 * @return true if at least one ancestor of a StatementContext matches one
347 * from collection of statement definitions, otherwise false.
349 public static boolean hasAncestorOfType(final StmtContext<?, ?, ?> ctx,
350 final Collection<StatementDefinition> ancestorTypes) {
351 requireNonNull(ancestorTypes);
352 StmtContext<?, ?, ?> current = ctx.getParentContext();
353 while (current != null) {
354 if (ancestorTypes.contains(current.getPublicDefinition())) {
357 current = current.getParentContext();
363 * Checks whether all of StmtContext's ancestors of specified type have a child of specified type.
365 * @param ctx StmtContext to be checked
366 * @param ancestorType type of ancestor to search for
367 * @param ancestorChildType type of child to search for in the specified ancestor type
368 * @return true if all of StmtContext's ancestors of specified type have a child of specified type, otherwise false
370 public static <A, D extends DeclaredStatement<A>> boolean hasAncestorOfTypeWithChildOfType(
371 final StmtContext<?, ?, ?> ctx, final StatementDefinition ancestorType,
372 final StatementDefinition ancestorChildType) {
374 requireNonNull(ancestorType);
375 requireNonNull(ancestorChildType);
377 StmtContext<?, ?, ?> current = ctx.coerceParentContext();
378 StmtContext<?, ?, ?> parent = current.getParentContext();
379 while (parent != null) {
380 if (ancestorType.equals(current.getPublicDefinition())) {
381 @SuppressWarnings("unchecked")
382 final Class<D> ancestorChildTypeClass = (Class<D>) ancestorChildType.getDeclaredRepresentationClass();
383 if (findFirstSubstatement(current, ancestorChildTypeClass) == null) {
389 parent = current.getParentContext();
396 * Checks whether the parent of StmtContext is of specified type.
399 * StmtContext to be checked
401 * type of parent to check
402 * @return true if the parent of StmtContext is of specified type, otherwise false
404 public static boolean hasParentOfType(final StmtContext<?, ?, ?> ctx, final StatementDefinition parentType) {
405 requireNonNull(parentType);
406 final StmtContext<?, ?, ?> parentContext = ctx.getParentContext();
407 return parentContext != null && parentType.equals(parentContext.getPublicDefinition());
411 * Validates the specified statement context with regards to if-feature and when statement on list keys.
412 * The context can either be a leaf which is defined directly in the substatements of a keyed list or a uses
413 * statement defined in a keyed list (a uses statement may add leaves into the list).
416 * If one of the list keys contains an if-feature or a when statement in YANG 1.1 model, an exception is thrown.
418 * @param ctx statement context to be validated
420 public static void validateIfFeatureAndWhenOnListKeys(final StmtContext<?, ?, ?> ctx) {
421 if (!isRelevantForIfFeatureAndWhenOnListKeysCheck(ctx)) {
425 final StmtContext<?, ?, ?> listStmtCtx = ctx.getParentContext();
426 final StmtContext<Collection<SchemaNodeIdentifier>, ?, ?> keyStmtCtx = findFirstDeclaredSubstatement(
427 listStmtCtx, KeyStatement.class);
429 if (YangStmtMapping.LEAF.equals(ctx.getPublicDefinition())) {
430 if (isListKey(ctx, keyStmtCtx)) {
431 disallowIfFeatureAndWhenOnListKeys(ctx);
433 } else if (YangStmtMapping.USES.equals(ctx.getPublicDefinition())) {
434 findAllEffectiveSubstatements(listStmtCtx, LeafStatement.class).forEach(leafStmtCtx -> {
435 if (isListKey(leafStmtCtx, keyStmtCtx)) {
436 disallowIfFeatureAndWhenOnListKeys(leafStmtCtx);
442 private static boolean isRelevantForIfFeatureAndWhenOnListKeysCheck(final StmtContext<?, ?, ?> ctx) {
443 return YangVersion.VERSION_1_1.equals(ctx.getRootVersion()) && hasParentOfType(ctx, YangStmtMapping.LIST)
444 && findFirstDeclaredSubstatement(ctx.coerceParentContext(), KeyStatement.class) != null;
447 private static boolean isListKey(final StmtContext<?, ?, ?> leafStmtCtx,
448 final StmtContext<Collection<SchemaNodeIdentifier>, ?, ?> keyStmtCtx) {
449 for (final SchemaNodeIdentifier keyIdentifier : keyStmtCtx.coerceStatementArgument()) {
450 if (leafStmtCtx.getStatementArgument().equals(keyIdentifier.getLastComponent())) {
458 private static void disallowIfFeatureAndWhenOnListKeys(final StmtContext<?, ?, ?> leafStmtCtx) {
459 leafStmtCtx.allSubstatements().forEach(leafSubstmtCtx -> {
460 final StatementDefinition statementDef = leafSubstmtCtx.getPublicDefinition();
461 SourceException.throwIf(YangStmtMapping.IF_FEATURE.equals(statementDef)
462 || YangStmtMapping.WHEN.equals(statementDef), leafStmtCtx.getStatementSourceReference(),
463 "%s statement is not allowed in %s leaf statement which is specified as a list key.",
464 statementDef.getStatementName(), leafStmtCtx.getStatementArgument());
468 public static QName qnameFromArgument(StmtContext<?, ?, ?> ctx, final String value) {
469 if (Strings.isNullOrEmpty(value)) {
470 return ctx.getPublicDefinition().getStatementName();
474 QNameModule qnameModule = null;
475 String localName = null;
477 final String[] namesParts = value.split(":");
478 switch (namesParts.length) {
480 localName = namesParts[0];
481 qnameModule = getRootModuleQName(ctx);
484 prefix = namesParts[0];
485 localName = namesParts[1];
486 qnameModule = getModuleQNameByPrefix(ctx, prefix);
487 // in case of unknown statement argument, we're not going to parse it
488 if (qnameModule == null && isUnknownStatement(ctx)) {
490 qnameModule = getRootModuleQName(ctx);
492 if (qnameModule == null && ctx.getCopyHistory().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) {
493 ctx = ctx.getOriginalCtx().orElse(null);
494 qnameModule = getModuleQNameByPrefix(ctx, prefix);
498 return internedQName(ctx,
499 InferenceException.throwIfNull(qnameModule, ctx.getStatementSourceReference(),
500 "Cannot resolve QNameModule for '%s'", value), localName);
504 * Parse a YANG identifier string in context of a statement.
506 * @param ctx Statement context
507 * @param str String to be parsed
508 * @return An interned QName
509 * @throws NullPointerException if any of the arguments are null
510 * @throws SourceException if the string is not a valid YANG identifier
512 public static QName parseIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
513 SourceException.throwIf(str.isEmpty(), ctx.getStatementSourceReference(),
514 "Identifier may not be an empty string");
515 return internedQName(ctx, str);
518 public static QName parseNodeIdentifier(StmtContext<?, ?, ?> ctx, final String prefix,
519 final String localName) {
520 final QNameModule module = getModuleQNameByPrefix(ctx, prefix);
521 if (module != null) {
522 return internedQName(ctx, module, localName);
525 if (ctx.getCopyHistory().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) {
526 final Optional<StmtContext<?, ?, ?>> optOrigCtx = ctx.getOriginalCtx();
527 if (optOrigCtx.isPresent()) {
528 ctx = optOrigCtx.get();
529 final QNameModule origModule = getModuleQNameByPrefix(ctx, prefix);
530 if (origModule != null) {
531 return internedQName(ctx, origModule, localName);
536 throw new InferenceException(ctx.getStatementSourceReference(), "Cannot resolve QNameModule for '%s'", prefix);
540 * Parse a YANG node identifier string in context of a statement.
542 * @param ctx Statement context
543 * @param str String to be parsed
544 * @return An interned QName
545 * @throws NullPointerException if any of the arguments are null
546 * @throws SourceException if the string is not a valid YANG node identifier
548 public static QName parseNodeIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
549 SourceException.throwIf(str.isEmpty(), ctx.getStatementSourceReference(),
550 "Node identifier may not be an empty string");
552 final int colon = str.indexOf(':');
554 return internedQName(ctx, str);
557 final String prefix = str.substring(0, colon);
558 SourceException.throwIf(prefix.isEmpty(), ctx.getStatementSourceReference(),
559 "String '%s' has an empty prefix", str);
560 final String localName = str.substring(colon + 1);
561 SourceException.throwIf(localName.isEmpty(), ctx.getStatementSourceReference(),
562 "String '%s' has an empty identifier", str);
564 return parseNodeIdentifier(ctx, prefix, localName);
567 private static QName internedQName(final StmtContext<?, ?, ?> ctx, final String localName) {
568 return internedQName(ctx, getRootModuleQName(ctx), localName);
571 private static QName internedQName(final StmtContext<?, ?, ?> ctx, final QNameModule module,
572 final String localName) {
573 final QName template;
575 template = QName.create(module, localName);
576 } catch (IllegalArgumentException e) {
577 throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid identifier '%s'", localName);
579 return template.intern();
582 public static QNameModule getRootModuleQName(final StmtContext<?, ?, ?> ctx) {
587 final StmtContext<?, ?, ?> rootCtx = ctx.getRoot();
588 final QNameModule qnameModule;
590 if (producesDeclared(rootCtx, ModuleStatement.class)) {
591 qnameModule = rootCtx.getFromNamespace(ModuleCtxToModuleQName.class, rootCtx);
592 } else if (producesDeclared(rootCtx, SubmoduleStatement.class)) {
593 final String belongsToModuleName = firstAttributeOf(rootCtx.declaredSubstatements(),
594 BelongsToStatement.class);
595 qnameModule = rootCtx.getFromNamespace(ModuleNameToModuleQName.class, belongsToModuleName);
600 checkArgument(qnameModule != null, "Failed to look up root QNameModule for %s", ctx);
604 public static QNameModule getModuleQNameByPrefix(final StmtContext<?, ?, ?> ctx, final String prefix) {
605 final StmtContext<?, ?, ?> importedModule = ctx.getRoot().getFromNamespace(ImportPrefixToModuleCtx.class,
607 final QNameModule qnameModule = ctx.getFromNamespace(ModuleCtxToModuleQName.class, importedModule);
608 if (qnameModule != null) {
612 if (producesDeclared(ctx.getRoot(), SubmoduleStatement.class)) {
613 final String moduleName = ctx.getRoot().getFromNamespace(BelongsToPrefixToModuleName.class, prefix);
614 return ctx.getFromNamespace(ModuleNameToModuleQName.class, moduleName);
620 public static SourceIdentifier createSourceIdentifier(final StmtContext<?, ?, ?> root) {
621 final QNameModule qNameModule = root.getFromNamespace(ModuleCtxToModuleQName.class, root);
622 if (qNameModule != null) {
623 // creates SourceIdentifier for a module
624 return RevisionSourceIdentifier.create((String) root.getStatementArgument(), qNameModule.getRevision());
627 // creates SourceIdentifier for a submodule
628 final Optional<Revision> revision = getLatestRevision(root.declaredSubstatements());
629 return RevisionSourceIdentifier.create((String) root.getStatementArgument(), revision);
632 public static Optional<Revision> getLatestRevision(final Iterable<? extends StmtContext<?, ?, ?>> subStmts) {
633 Revision revision = null;
634 for (final StmtContext<?, ?, ?> subStmt : subStmts) {
635 if (subStmt.getPublicDefinition().getDeclaredRepresentationClass().isAssignableFrom(
636 RevisionStatement.class)) {
637 if (revision == null && subStmt.getStatementArgument() != null) {
638 revision = (Revision) subStmt.getStatementArgument();
640 final Revision subArg = (Revision) subStmt.getStatementArgument();
641 if (subArg != null && subArg.compareTo(revision) > 0) {
647 return Optional.ofNullable(revision);
651 * Determine if a specific statetement context is a child of a statement context which produces a grouping.
653 * @param stmt Statement to examine
654 * @return True if parent context is that of a grouping.
657 public static boolean isChildOfGrouping(final StmtContext<?, ?, ?> stmt) {
658 final StmtContext<?, ?, ?> parent = stmt.getParentContext();
659 return parent != null && parent.getPublicDefinition() == YangStmtMapping.GROUPING;