Search in sources :

Example 6 with StatementDefinition

use of org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition in project yangtools by opendaylight.

the class MountPointStatementSupport method onStatementAdded.

@Override
public void onStatementAdded(final Mutable<QName, MountPointStatement, MountPointEffectiveStatement> stmt) {
    final StatementDefinition parentDef = stmt.coerceParentContext().publicDefinition();
    SourceException.throwIf(YangStmtMapping.CONTAINER != parentDef && YangStmtMapping.LIST != parentDef, stmt, "Mount points may only be defined at either a container or a list");
}
Also used : StatementDefinition(org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition)

Example 7 with StatementDefinition

use of org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition in project yangtools by opendaylight.

the class SubstatementValidator method validate.

/**
 * Validate substatements inside a context.
 *
 * @param ctx Context to inspect
 * @throws InvalidSubstatementException when there is a disallowed statement present.
 * @throws MissingSubstatementException when a mandatory statement is missing.
 */
public void validate(final StmtContext<?, ?, ?> ctx) {
    final Map<StatementDefinition, Counter> stmtCounts = new HashMap<>();
    for (StmtContext<?, ?, ?> stmtCtx : ctx.allSubstatements()) {
        stmtCounts.computeIfAbsent(stmtCtx.publicDefinition(), key -> new Counter()).increment();
    }
    // Mark all mandatory statements as not present. We are using a Map instead of a Set, as it provides us with
    // explicit value in case of failure (which is not important) and a more efficient instantiation performance
    // (which is important).
    final Map<StatementDefinition, Cardinality> missingMandatory = new HashMap<>(mandatoryStatements);
    // Iterate over all statements
    for (Entry<StatementDefinition, Counter> entry : stmtCounts.entrySet()) {
        final StatementDefinition key = entry.getKey();
        final Cardinality cardinality = cardinalityMap.get(key);
        final int value = entry.getValue().getValue();
        if (cardinality == null) {
            if (ctx.getFromNamespace(ExtensionNamespace.class, key.getStatementName()) == null) {
                final StmtContext<?, ?, ?> root = ctx.getRoot();
                throw new InvalidSubstatementException(ctx, "%s is not valid for %s. Error in module %s (%s)", key, currentStatement, root.rawArgument(), ctx.getFromNamespace(ModuleCtxToModuleQName.class, root));
            }
            continue;
        }
        if (cardinality.getMin() > 0) {
            if (cardinality.getMin() > value) {
                final StmtContext<?, ?, ?> root = ctx.getRoot();
                throw new InvalidSubstatementException(ctx, "Minimal count of %s for %s is %s, detected %s. Error in module %s (%s)", key, currentStatement, cardinality.getMin(), value, root.rawArgument(), ctx.getFromNamespace(ModuleCtxToModuleQName.class, root));
            }
            // Encountered a mandatory statement, hence we are not missing it
            missingMandatory.remove(key);
        }
        if (cardinality.getMax() < value) {
            final StmtContext<?, ?, ?> root = ctx.getRoot();
            throw new InvalidSubstatementException(ctx, "Maximal count of %s for %s is %s, detected %s. Error in module %s (%s)", key, currentStatement, cardinality.getMax(), value, root.rawArgument(), ctx.getFromNamespace(ModuleCtxToModuleQName.class, root));
        }
    }
    // Check if there are any mandatory statements we have missed
    if (!missingMandatory.isEmpty()) {
        final Entry<StatementDefinition, Cardinality> e = missingMandatory.entrySet().iterator().next();
        final StmtContext<?, ?, ?> root = ctx.getRoot();
        throw new MissingSubstatementException(ctx, "%s is missing %s. Minimal count is %s. Error in module %s (%s)", currentStatement, e.getKey(), e.getValue().getMin(), root.rawArgument(), ctx.getFromNamespace(ModuleCtxToModuleQName.class, root));
    }
}
Also used : Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableMap(com.google.common.collect.ImmutableMap) ExtensionNamespace(org.opendaylight.yangtools.yang.parser.spi.ExtensionNamespace) Map(java.util.Map) StatementDefinition(org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition) Entry(java.util.Map.Entry) HashMap(java.util.HashMap) ModuleCtxToModuleQName(org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName) Maps(com.google.common.collect.Maps) HashMap(java.util.HashMap) StatementDefinition(org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition) ExtensionNamespace(org.opendaylight.yangtools.yang.parser.spi.ExtensionNamespace) ModuleCtxToModuleQName(org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName)

Example 8 with StatementDefinition

use of org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition in project yangtools by opendaylight.

the class AbstractAugmentStatementSupport method parseArgumentValue.

@Override
public final SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
    SourceException.throwIf(PATH_REL_PATTERN1.matcher(value).matches() || PATH_REL_PATTERN2.matcher(value).matches(), ctx, "Augment argument \'%s\' is not valid, it can be only absolute path; or descendant if used in uses", value);
    // As per:
    // https://tools.ietf.org/html/rfc6020#section-7.15
    // https://tools.ietf.org/html/rfc7950#section-7.17
    // 
    // The argument is either Absolute or Descendant based on whether the statement is declared within a 'uses'
    // statement. The mechanics differs wildly between the two cases, so let's start by ensuring our argument
    // is in the correct domain.
    final SchemaNodeIdentifier result = ArgumentUtils.nodeIdentifierFromPath(ctx, value);
    final StatementDefinition parent = ctx.coerceParentContext().publicDefinition();
    if (parent == YangStmtMapping.USES) {
        SourceException.throwIf(result instanceof Absolute, ctx, "Absolute schema node identifier is not allowed when used within a uses statement");
    } else {
        SourceException.throwIf(result instanceof Descendant, ctx, "Descendant schema node identifier is not allowed when used outside of a uses statement");
    }
    return result;
}
Also used : StatementDefinition(org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition) Descendant(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant) SchemaNodeIdentifier(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier) Absolute(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute)

Example 9 with StatementDefinition

use of org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition in project yangtools by opendaylight.

the class YinStatementStreamSource method getValidDefinition.

private static StatementDefinition getValidDefinition(final Node node, final StatementWriter writer, final QNameToStatementDefinition stmtDef, final StatementSourceReference ref) {
    final XMLNamespace uri = NS_CACHE.getUnchecked(node.getNamespaceURI());
    final StatementDefinition def = stmtDef.getByNamespaceAndLocalName(uri, node.getLocalName());
    if (def == null) {
        SourceException.throwIf(writer.getPhase().equals(ModelProcessingPhase.FULL_DECLARATION), ref, "%s is not a YIN statement or use of extension.", node.getLocalName());
    }
    return def;
}
Also used : QNameToStatementDefinition(org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition) StatementDefinition(org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace)

Example 10 with StatementDefinition

use of org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition in project yangtools by opendaylight.

the class YinStatementStreamSource method processAttribute.

private static boolean processAttribute(final int childId, final Attr attr, final StatementWriter writer, final QNameToStatementDefinition stmtDef, final StatementSourceReference ref) {
    final Optional<? extends ResumedStatement> optResumed = writer.resumeStatement(childId);
    if (optResumed.isPresent()) {
        final ResumedStatement resumed = optResumed.get();
        checkState(resumed.isFullyDefined(), "Statement %s is not fully defined", resumed);
        return true;
    }
    final StatementDefinition def = getValidDefinition(attr, writer, stmtDef, ref);
    if (def == null) {
        return false;
    }
    final String value = attr.getValue();
    writer.startStatement(childId, def.getStatementName(), value.isEmpty() ? null : value, ref);
    writer.storeStatement(0, true);
    writer.endStatement(ref);
    return true;
}
Also used : QNameToStatementDefinition(org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition) StatementDefinition(org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition) ResumedStatement(org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter.ResumedStatement)

Aggregations

StatementDefinition (org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition)15 QNameToStatementDefinition (org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition)4 QName (org.opendaylight.yangtools.yang.common.QName)2 ArgumentDefinition (org.opendaylight.yangtools.yang.model.api.meta.ArgumentDefinition)2 ResumedStatement (org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter.ResumedStatement)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Maps (com.google.common.collect.Maps)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 QNameModule (org.opendaylight.yangtools.yang.common.QNameModule)1 XMLNamespace (org.opendaylight.yangtools.yang.common.XMLNamespace)1 SchemaNodeIdentifier (org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier)1 Absolute (org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute)1 Descendant (org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant)1 ExtensionNamespace (org.opendaylight.yangtools.yang.parser.spi.ExtensionNamespace)1 Parent (org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Parent)1 InferenceException (org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException)1 ModuleCtxToModuleQName (org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName)1