use of org.opendaylight.yangtools.yang.model.api.PathExpression in project yangtools by opendaylight.
the class LeafRefContextTreeBuilder method buildLeafRefContextReferencingTree.
private LeafRefContext buildLeafRefContextReferencingTree(final DataSchemaNode node, final SchemaInferenceStack stack) {
final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(node.getQName(), stack.toSchemaPath(), schemaContext);
if (node instanceof DataNodeContainer) {
for (final DataSchemaNode childNode : ((DataNodeContainer) node).getChildNodes()) {
stack.enterSchemaTree(childNode.getQName());
final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(childNode, stack);
stack.exit();
if (childLeafRefContext.hasReferencingChild() || childLeafRefContext.isReferencing()) {
currentLeafRefContextBuilder.addReferencingChild(childLeafRefContext, childLeafRefContext.getNodeName());
}
}
} else if (node instanceof ChoiceSchemaNode) {
// :FIXME choice without case
for (final CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
stack.enterSchemaTree(caseNode.getQName());
final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(caseNode, stack);
stack.exit();
if (childLeafRefContext.hasReferencingChild() || childLeafRefContext.isReferencing()) {
currentLeafRefContextBuilder.addReferencingChild(childLeafRefContext, childLeafRefContext.getNodeName());
}
}
} else if (node instanceof TypedDataSchemaNode) {
final TypedDataSchemaNode typedNode = (TypedDataSchemaNode) node;
final TypeDefinition<?> type = typedNode.getType();
// FIXME: fix case when type is e.g. typedef -> typedef -> leafref
if (type instanceof LeafrefTypeDefinition) {
final LeafrefTypeDefinition leafrefType = (LeafrefTypeDefinition) type;
final PathExpression path = leafrefType.getPathStatement();
final LeafRefPathParserImpl leafRefPathParser = new LeafRefPathParserImpl(leafrefType, typedNode);
final LeafRefPath leafRefPath = leafRefPathParser.parseLeafRefPath(path);
currentLeafRefContextBuilder.setLeafRefTargetPathString(path.getOriginalString());
currentLeafRefContextBuilder.setReferencing(true);
currentLeafRefContextBuilder.setLeafRefTargetPath(leafRefPath);
final LeafRefContext currentLeafRefContext = currentLeafRefContextBuilder.build();
leafRefs.add(currentLeafRefContext);
return currentLeafRefContext;
}
}
return currentLeafRefContextBuilder.build();
}
use of org.opendaylight.yangtools.yang.model.api.PathExpression 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.PathExpression in project yangtools by opendaylight.
the class SchemaInferenceStackTest method findDataSchemaNodeTest2.
@Test
public void findDataSchemaNodeTest2() {
final QName myLeafInGrouping2 = QName.create(myModule.getQNameModule(), "my-leaf-in-gouping2");
final PathExpression expr = mock(PathExpression.class);
doReturn(true).when(expr).isAbsolute();
doReturn(new LocationPathSteps(YangLocationPath.relative(YangXPathAxis.CHILD.asStep(myLeafInGrouping2)))).when(expr).getSteps();
final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
final SchemaInferenceStack stack = SchemaInferenceStack.of(context);
assertSame(grouping, stack.enterGrouping(grouping.getQName()));
assertEquals(grouping.getDataChildByName(myLeafInGrouping2), stack.resolvePathExpression(expr));
}
use of org.opendaylight.yangtools.yang.model.api.PathExpression in project yangtools by opendaylight.
the class PathExpressionParserTest method testDerefPath.
@Test
public void testDerefPath() {
// deref() is not valid as per RFC7950, but we tolarate it.
final PathExpression deref = parser.parseExpression(ctx, "deref(../id)/../type");
final Steps steps = deref.getSteps();
assertThat(steps, isA(DerefSteps.class));
final DerefSteps derefSteps = (DerefSteps) steps;
assertEquals(YangLocationPath.relative(YangXPathAxis.PARENT.asStep(), YangXPathAxis.CHILD.asStep(UnresolvedQName.unqualified("type"))), derefSteps.getRelativePath());
assertEquals(YangLocationPath.relative(YangXPathAxis.PARENT.asStep(), YangXPathAxis.CHILD.asStep(UnresolvedQName.unqualified("id"))), derefSteps.getDerefArgument());
}
use of org.opendaylight.yangtools.yang.model.api.PathExpression in project yangtools by opendaylight.
the class SchemaInferenceStackTest method findDataSchemaNodeTest.
@Test
public void findDataSchemaNodeTest() {
final Module importedModule = context.findModule(XMLNamespace.of("uri:imported-module"), Revision.of("2014-10-07")).get();
final QName myImportedContainer = QName.create(importedModule.getQNameModule(), "my-imported-container");
final QName myImportedLeaf = QName.create(importedModule.getQNameModule(), "my-imported-leaf");
final SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName(myImportedContainer)).getDataChildByName(myImportedLeaf);
final PathExpression expr = mock(PathExpression.class);
doReturn(true).when(expr).isAbsolute();
doReturn(new LocationPathSteps(YangLocationPath.absolute(YangXPathAxis.CHILD.asStep(myImportedContainer), YangXPathAxis.CHILD.asStep(myImportedLeaf)))).when(expr).getSteps();
assertEquals(testNode, SchemaInferenceStack.of(context).resolvePathExpression(expr));
}
Aggregations