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