Search in sources :

Example 11 with StatementDefinition

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

the class YinStatementStreamSource method processElement.

private static boolean processElement(final int childId, final Element element, final StatementWriter writer, final QNameToStatementDefinition stmtDef) {
    final Optional<? extends ResumedStatement> optResumed = writer.resumeStatement(childId);
    final StatementSourceReference ref;
    final QName argName;
    final boolean allAttrs;
    final boolean allElements;
    if (optResumed.isPresent()) {
        final ResumedStatement resumed = optResumed.get();
        if (resumed.isFullyDefined()) {
            return true;
        }
        final StatementDefinition def = resumed.getDefinition();
        ref = resumed.getSourceReference();
        final Optional<ArgumentDefinition> optArgDef = def.getArgumentDefinition();
        if (optArgDef.isPresent()) {
            final ArgumentDefinition argDef = optArgDef.get();
            argName = argDef.getArgumentName();
            allAttrs = argDef.isYinElement();
            allElements = !allAttrs;
        } else {
            argName = null;
            allAttrs = false;
            allElements = false;
        }
    } else {
        ref = extractRef(element);
        final StatementDefinition def = getValidDefinition(element, writer, stmtDef, ref);
        if (def == null) {
            LOG.debug("Skipping element {}", element);
            return false;
        }
        final String argValue;
        final Optional<ArgumentDefinition> optArgDef = def.getArgumentDefinition();
        if (optArgDef.isPresent()) {
            final ArgumentDefinition argDef = optArgDef.get();
            argName = argDef.getArgumentName();
            allAttrs = argDef.isYinElement();
            allElements = !allAttrs;
            argValue = getArgValue(element, argName, allAttrs);
            SourceException.throwIfNull(argValue, ref, "Statement {} is missing mandatory argument %s", def.getStatementName(), argName);
        } else {
            argName = null;
            argValue = null;
            allAttrs = false;
            allElements = false;
        }
        writer.startStatement(childId, def.getStatementName(), argValue, ref);
    }
    // Child counter
    int childCounter = 0;
    boolean fullyDefined = true;
    // First process any statements defined as attributes. We need to skip argument, if present
    final NamedNodeMap attributes = element.getAttributes();
    if (attributes != null) {
        for (int i = 0, len = attributes.getLength(); i < len; ++i) {
            final Attr attr = (Attr) attributes.item(i);
            if ((allAttrs || !isArgument(argName, attr)) && !processAttribute(childCounter++, attr, writer, stmtDef, ref)) {
                fullyDefined = false;
            }
        }
    }
    // Now process child elements, if present
    final NodeList children = element.getChildNodes();
    for (int i = 0, len = children.getLength(); i < len; ++i) {
        final Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE && (allElements || !isArgument(argName, child)) && !processElement(childCounter++, (Element) child, writer, stmtDef)) {
            fullyDefined = false;
        }
    }
    writer.storeStatement(childCounter, fullyDefined);
    writer.endStatement(ref);
    return fullyDefined;
}
Also used : StatementSourceReference(org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference) NamedNodeMap(org.w3c.dom.NamedNodeMap) QName(org.opendaylight.yangtools.yang.common.QName) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArgumentDefinition(org.opendaylight.yangtools.yang.model.api.meta.ArgumentDefinition) Attr(org.w3c.dom.Attr) 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)

Example 12 with StatementDefinition

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

the class AbstractDeviateStatementSupport method deleteStatement.

private static void deleteStatement(final StmtContext<?, ?, ?> stmtCtxToBeDeleted, final Mutable<?, ?, ?> targetCtx) {
    final StatementDefinition stmtToBeDeleted = stmtCtxToBeDeleted.publicDefinition();
    final String stmtArgument = stmtCtxToBeDeleted.rawArgument();
    for (Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableEffectiveSubstatements()) {
        if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.publicDefinition(), targetCtxSubstatement.rawArgument())) {
            targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeDeleted, stmtArgument);
            return;
        }
    }
    for (Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) {
        if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.publicDefinition(), targetCtxSubstatement.rawArgument())) {
            targetCtxSubstatement.setUnsupported();
            return;
        }
    }
    LOG.error("Deviation cannot delete substatement {} with argument '{}' in target node {} because it does " + "not exist in the target node. At line: {}", stmtToBeDeleted.getStatementName(), stmtArgument, targetCtx.argument(), stmtCtxToBeDeleted.sourceReference());
}
Also used : StatementDefinition(org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition)

Example 13 with StatementDefinition

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

the class StatementContextVisitor method getValidStatementDefinition.

private QName getValidStatementDefinition(final Qualified keyword, final StatementSourceReference ref) {
    if (prefixes == null) {
        // No prefixes to look up from
        return null;
    }
    final QNameModule qNameModule = prefixes.get(keyword.prefix());
    if (qNameModule == null) {
        // Failed to look the namespace
        return null;
    }
    final StatementDefinition foundStmtDef = resolveStatement(qNameModule, keyword.identifier());
    return foundStmtDef != null ? foundStmtDef.getStatementName() : null;
}
Also used : QNameToStatementDefinition(org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition) StatementDefinition(org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule)

Example 14 with StatementDefinition

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

the class YinXMLEventReader method addStatement.

private void addStatement(final DeclaredStatement<?> statement) {
    final StatementDefinition def = statement.statementDefinition();
    final QName name = def.getStatementName();
    final Optional<ArgumentDefinition> optArgDef = def.getArgumentDefinition();
    if (optArgDef.isPresent()) {
        final ArgumentDefinition argDef = optArgDef.get();
        final QName argName = argDef.getArgumentName();
        if (argDef.isYinElement()) {
            events.addAll(Arrays.asList(startElement(name), startElement(argName), eventFactory.createCharacters(statement.rawArgument()), endElement(argName)));
        } else {
            final Entry<String, String> ns = namespaceContext.prefixAndNamespaceFor(name.getModule());
            events.add(eventFactory.createStartElement(ns.getKey(), ns.getValue(), name.getLocalName(), singletonIterator(attribute(argName, statement.rawArgument())), emptyIterator(), namespaceContext));
        }
    } else {
        // No attributes: just emit a start
        events.add(startElement(name));
    }
    stack.push(new OpenElement(name, statement.declaredSubstatements().iterator()));
}
Also used : StatementDefinition(org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition) QName(org.opendaylight.yangtools.yang.common.QName) ArgumentDefinition(org.opendaylight.yangtools.yang.model.api.meta.ArgumentDefinition)

Example 15 with StatementDefinition

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

the class UsesStatementSupport method addOrReplaceNode.

private static void addOrReplaceNode(final StmtContext<?, ?, ?> refineSubstatementCtx, final Mutable<?, ?, ?> refineTargetNodeCtx) {
    final StatementDefinition refineSubstatementDef = refineSubstatementCtx.publicDefinition();
    // FIXME: this is quite costly, use an explicit block
    SourceException.throwIf(!isSupportedRefineTarget(refineSubstatementCtx, refineTargetNodeCtx), refineSubstatementCtx, "Error in module '%s' in the refine of uses '%s': can not perform refine of '%s' for the target '%s'.", refineSubstatementCtx.getRoot().rawArgument(), refineSubstatementCtx.coerceParentContext().argument(), refineSubstatementCtx.publicDefinition(), refineTargetNodeCtx.publicDefinition());
    if (!isAllowedToAddByRefine(refineSubstatementDef)) {
        refineTargetNodeCtx.removeStatementFromEffectiveSubstatements(refineSubstatementDef);
    }
    // FIXME: childCopyOf() should handle this through per-statement copy policy, right?
    refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx.replicaAsChildOf(refineTargetNodeCtx));
}
Also used : StatementDefinition(org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition)

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