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");
}
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));
}
}
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;
}
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;
}
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;
}
Aggregations