/** * Searches for the first substatement of the specified type in the specified statement context. * First, it tries to find the substatement in the effective substatements of the statement context. * If it was not found, then it proceeds to search in the declared substatements. If it still was not found, * the method returns null. * * @param stmtContext statement context to search in * @param declaredType substatement type to search for * @param <A> statement argument type * @param <D> declared statement type * @return statement context that was searched for or null if was not found */ public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstSubstatement( final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) { final StmtContext<A, ?, ?> effectiveSubstatement = findFirstEffectiveSubstatement(stmtContext, declaredType); return effectiveSubstatement != null ? effectiveSubstatement : findFirstDeclaredSubstatement(stmtContext, declaredType); }
@SafeVarargs public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext, int startIndex, final Class<? extends DeclaredStatement<?>>... types) { if (startIndex >= types.length) { return null; } for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) { if (producesDeclared(subStmtContext, types[startIndex])) { return startIndex + 1 == types.length ? subStmtContext : findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types); } } return null; }
@Override public final void onFullDefinitionDeclared( final Mutable<QName, RpcStatement, EffectiveStatement<QName, RpcStatement>> stmt) { super.onFullDefinitionDeclared(stmt); if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, InputStatement.class) == null) { ((StatementContextBase<?, ?, ?>) stmt).appendImplicitStatement(implictInput()); } if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, OutputStatement.class) == null) { ((StatementContextBase<?, ?, ?>) stmt).appendImplicitStatement(implictOutput()); } }
@Override public void onFullDefinitionDeclared(final StmtContext.Mutable<QName, ActionStatement, EffectiveStatement<QName, ActionStatement>> stmt) { super.onFullDefinitionDeclared(stmt); if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, InputStatement.class) == null) { ((StatementContextBase<?, ?, ?>) stmt).appendImplicitStatement(InputStatementRFC7950Support.getInstance()); } if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, OutputStatement.class) == null) { ((StatementContextBase<?, ?, ?>) stmt).appendImplicitStatement(OutputStatementRFC7950Support.getInstance()); } }
private static boolean isRelevantForIfFeatureAndWhenOnListKeysCheck(final StmtContext<?, ?, ?> ctx) { return YangVersion.VERSION_1_1.equals(ctx.getRootVersion()) && StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.LIST) && StmtContextUtils.findFirstDeclaredSubstatement(ctx.coerceParentContext(), KeyStatement.class) != null; }
@Override public void apply(final InferenceContext ctx) { StmtContext<?, ?, ?> belongsToModuleCtx = belongsToPrereq.resolve(ctx); belongsToCtx.addToNs(BelongsToModuleContext.class, belongsToSourceIdentifier, belongsToModuleCtx); belongsToCtx.addToNs(BelongsToPrefixToModuleCtx.class, findFirstDeclaredSubstatement(belongsToCtx, PrefixStatement.class).coerceStatementArgument(), belongsToModuleCtx); }
static SourceIdentifier getImportedSourceIdentifier(final StmtContext<String, ImportStatement, ?> stmt) { final StmtContext<Revision, ?, ?> revision = findFirstDeclaredSubstatement(stmt, RevisionDateStatement.class); return revision == null ? RevisionSourceIdentifier.create(stmt.getStatementArgument()) : RevisionSourceIdentifier.create(stmt.getStatementArgument(), revision.getStatementArgument()); } }
@Override public final void onPreLinkageDeclared( final Mutable<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> stmt) { final StmtContext<Revision, ?, ?> revision = findFirstDeclaredSubstatement(stmt, RevisionDateStatement.class); stmt.addRequiredSource(revision == null ? RevisionSourceIdentifier.create(stmt.getStatementArgument()) : RevisionSourceIdentifier.create(stmt.getStatementArgument(), revision.getStatementArgument())); }
@Override public void onStatementDefinitionDeclared( final Mutable<QName, ExtensionStatement, EffectiveStatement<QName, ExtensionStatement>> stmt) { super.onStatementDefinitionDeclared(stmt); QName stmtName = stmt.coerceStatementArgument(); if (OpenConfigStatements.OPENCONFIG_VERSION.getStatementName().isEqualWithoutRevision(stmtName)) { stmtName = stmtName.withoutRevision(); } stmt.addContext(ExtensionNamespace.class, stmtName, stmt); final StmtContext<QName, ?, ?> argument = StmtContextUtils.findFirstDeclaredSubstatement(stmt, ArgumentStatement.class); final StmtContext<Boolean, ?, ?> yinElement = StmtContextUtils.findFirstDeclaredSubstatement(stmt, YinElementStatement.class); stmt.addToNs(StatementDefinitionNamespace.class, stmt.getStatementArgument(), new ModelDefinedStatementSupport(new ModelDefinedStatementDefinition(stmt.getStatementArgument(), argument != null ? argument.getStatementArgument() : null, yinElement != null ? yinElement.getStatementArgument() : false))); }
final Mutable<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> stmt) { final String submoduleName = stmt.coerceStatementArgument(); final StmtContext<Revision, ?, ?> revision = findFirstDeclaredSubstatement(stmt, RevisionDateStatement.class);
/** * Validates the specified statement context with regards to if-feature and when statement on list keys. * The context can either be a leaf which is defined directly in the substatements of a keyed list or a uses * statement defined in a keyed list (a uses statement may add leaves into the list). * * <p> * If one of the list keys contains an if-feature or a when statement in YANG 1.1 model, an exception is thrown. * * @param ctx statement context to be validated */ public static void validateIfFeatureAndWhenOnListKeys(final StmtContext<?, ?, ?> ctx) { if (!isRelevantForIfFeatureAndWhenOnListKeysCheck(ctx)) { return; } final StmtContext<?, ?, ?> listStmtCtx = ctx.getParentContext(); final StmtContext<Collection<SchemaNodeIdentifier>, ?, ?> keyStmtCtx = StmtContextUtils.findFirstDeclaredSubstatement(listStmtCtx, KeyStatement.class); if (YangStmtMapping.LEAF.equals(ctx.getPublicDefinition())) { if (isListKey(ctx, keyStmtCtx)) { disallowIfFeatureAndWhenOnListKeys(ctx); } } else if (YangStmtMapping.USES.equals(ctx.getPublicDefinition())) { StmtContextUtils.findAllEffectiveSubstatements(listStmtCtx, LeafStatement.class).forEach(leafStmtCtx -> { if (isListKey(leafStmtCtx, keyStmtCtx)) { disallowIfFeatureAndWhenOnListKeys(leafStmtCtx); } }); } }
@Override public final void onLinkageDeclared( final Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> stmt) { final SourceIdentifier submoduleIdentifier = RevisionSourceIdentifier.create(stmt.coerceStatementArgument(), StmtContextUtils.getLatestRevision(stmt.declaredSubstatements())); final StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> possibleDuplicateSubmodule = stmt.getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier); if (possibleDuplicateSubmodule != null && possibleDuplicateSubmodule != stmt) { throw new SourceException(stmt.getStatementSourceReference(), "Submodule name collision: %s. At %s", stmt.getStatementArgument(), possibleDuplicateSubmodule.getStatementSourceReference()); } stmt.addContext(SubmoduleNamespace.class, submoduleIdentifier, stmt); final String belongsToModuleName = firstAttributeOf(stmt.declaredSubstatements(), BelongsToStatement.class); final StmtContext<?, ?, ?> prefixSubStmtCtx = findFirstDeclaredSubstatement(stmt, 0, BelongsToStatement.class, PrefixStatement.class); SourceException.throwIfNull(prefixSubStmtCtx, stmt.getStatementSourceReference(), "Prefix of belongsTo statement is missing in submodule [%s]", stmt.getStatementArgument()); final String prefix = (String) prefixSubStmtCtx.getStatementArgument(); stmt.addToNs(BelongsToPrefixToModuleName.class, prefix, belongsToModuleName); } }