use of org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode in project yangtools by opendaylight.
the class SchemaInferenceStack method resolveDeref.
@NonNull
private EffectiveStatement<?, ?> resolveDeref(final DerefSteps deref) {
final EffectiveStatement<?, ?> leafRefSchemaNode = currentStatement();
final YangLocationPath.Relative derefArg = deref.getDerefArgument();
final EffectiveStatement<?, ?> derefStmt = resolveLocationPath(derefArg);
checkArgument(derefStmt != null, "Cannot find deref(%s) target node %s in context of %s", derefArg, leafRefSchemaNode);
checkArgument(derefStmt instanceof TypedDataSchemaNode, "deref(%s) resolved to non-typed %s", derefArg, derefStmt);
// We have a deref() target, decide what to do about it
final TypeDefinition<?> targetType = ((TypedDataSchemaNode) derefStmt).getType();
if (targetType instanceof InstanceIdentifierTypeDefinition) {
// FIXME: dedicated exception, users can recover from it, derive from IAE
throw new UnsupportedOperationException("Cannot infer instance-identifier reference " + targetType);
}
// deref() is defined only for instance-identifier and leafref types, handle the latter
checkArgument(targetType instanceof LeafrefTypeDefinition, "Illegal target type %s", targetType);
final PathExpression dereferencedLeafRefPath = ((LeafrefTypeDefinition) targetType).getPathStatement();
EffectiveStatement<?, ?> derefNode = resolvePathExpression(dereferencedLeafRefPath);
checkArgument(derefStmt != null, "Can not find target node of dereferenced node %s", derefStmt);
checkArgument(derefNode instanceof LeafSchemaNode, "Unexpected %s reference in %s", deref, dereferencedLeafRefPath);
return resolveLocationPath(deref.getRelativePath());
}
use of org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode in project yangtools by opendaylight.
the class YT588Test method test.
@Test
public void test() {
EffectiveModelContext context = YangParserTestUtils.parseYangResource("/yt588.yang");
QName root = QName.create(NS, REV, "root");
QName leafRef2 = QName.create(NS, REV, "leaf-ref-2");
QName conGrp = QName.create(NS, REV, "con-grp");
QName leafRef = QName.create(NS, REV, "leaf-ref");
SchemaNode findDataSchemaNode = context.findDataTreeChild(root, conGrp, leafRef).get();
SchemaNode findDataSchemaNode2 = context.findDataTreeChild(root, leafRef2).get();
assertThat(findDataSchemaNode, isA(LeafSchemaNode.class));
assertThat(findDataSchemaNode2, isA(LeafSchemaNode.class));
LeafSchemaNode leafRefNode = (LeafSchemaNode) findDataSchemaNode;
LeafSchemaNode leafRefNode2 = (LeafSchemaNode) findDataSchemaNode2;
assertThat(leafRefNode.getType(), isA(LeafrefTypeDefinition.class));
assertThat(leafRefNode2.getType(), isA(LeafrefTypeDefinition.class));
EffectiveStatement<?, ?> found = SchemaInferenceStack.ofDataTreePath(context, root, conGrp, leafRef).resolvePathExpression(((LeafrefTypeDefinition) leafRefNode.getType()).getPathStatement());
assertThat(((TypedDataSchemaNode) found).getType(), isA(BinaryTypeDefinition.class));
found = SchemaInferenceStack.ofDataTreePath(context, root, leafRef2).resolvePathExpression(((LeafrefTypeDefinition) leafRefNode2.getType()).getPathStatement());
assertThat(((TypedDataSchemaNode) found).getType(), isA(Int16TypeDefinition.class));
}
use of org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode in project yangtools by opendaylight.
the class XmlParserStream method translateValueByType.
private Object translateValueByType(final Object value, final DataSchemaNode node, final NamespaceContext namespaceCtx) {
if (node instanceof AnyxmlSchemaNode) {
checkArgument(value instanceof Document);
/*
* FIXME: Figure out some YANG extension dispatch, which will reuse JSON parsing or XML parsing -
* anyxml is not well-defined in JSON.
*/
return new DOMSource(((Document) value).getDocumentElement());
}
if (node instanceof AnydataSchemaNode) {
checkArgument(value instanceof Document);
return new DOMSourceAnydata(new DOMSource(((Document) value).getDocumentElement()));
}
checkArgument(node instanceof TypedDataSchemaNode);
checkArgument(value instanceof String);
return codecs.codecFor((TypedDataSchemaNode) node, stack).parseValue(namespaceCtx, (String) value);
}
use of org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode in project mdsal by opendaylight.
the class DataContainerCodecPrototype method computeChildAddressabilitySummary.
private static ChildAddressabilitySummary computeChildAddressabilitySummary(final Object nodeSchema) {
// FIXME: rework this to work on EffectiveStatements
if (nodeSchema instanceof DataNodeContainer) {
boolean haveAddressable = false;
boolean haveUnaddressable = false;
for (DataSchemaNode child : ((DataNodeContainer) nodeSchema).getChildNodes()) {
if (child instanceof ContainerSchemaNode || child instanceof AugmentationSchemaNode) {
haveAddressable = true;
} else if (child instanceof ListSchemaNode) {
if (((ListSchemaNode) child).getKeyDefinition().isEmpty()) {
haveUnaddressable = true;
} else {
haveAddressable = true;
}
} else if (child instanceof AnydataSchemaNode || child instanceof AnyxmlSchemaNode || child instanceof TypedDataSchemaNode) {
haveUnaddressable = true;
} else if (child instanceof ChoiceSchemaNode) {
switch(computeChildAddressabilitySummary(child)) {
case ADDRESSABLE:
haveAddressable = true;
break;
case MIXED:
haveAddressable = true;
haveUnaddressable = true;
break;
case UNADDRESSABLE:
haveUnaddressable = true;
break;
default:
throw new IllegalStateException("Unhandled accessibility summary for " + child);
}
} else {
LOG.warn("Unhandled child node {}", child);
}
}
if (!haveAddressable) {
// Empty or all are unaddressable
return ChildAddressabilitySummary.UNADDRESSABLE;
}
return haveUnaddressable ? ChildAddressabilitySummary.MIXED : ChildAddressabilitySummary.ADDRESSABLE;
} else if (nodeSchema instanceof ChoiceSchemaNode) {
boolean haveAddressable = false;
boolean haveUnaddressable = false;
for (CaseSchemaNode child : ((ChoiceSchemaNode) nodeSchema).getCases()) {
switch(computeChildAddressabilitySummary(child)) {
case ADDRESSABLE:
haveAddressable = true;
break;
case UNADDRESSABLE:
haveUnaddressable = true;
break;
case MIXED:
// A child is mixed, which means we are mixed, too
return ChildAddressabilitySummary.MIXED;
default:
throw new IllegalStateException("Unhandled accessibility summary for " + child);
}
}
if (!haveAddressable) {
// Empty or all are unaddressable
return ChildAddressabilitySummary.UNADDRESSABLE;
}
return haveUnaddressable ? ChildAddressabilitySummary.MIXED : ChildAddressabilitySummary.ADDRESSABLE;
}
// No child nodes possible: return unaddressable
return ChildAddressabilitySummary.UNADDRESSABLE;
}
Aggregations