Search in sources :

Example 1 with PathExpression

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();
}
Also used : TypedDataSchemaNode(org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode) LeafrefTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition) PathExpression(org.opendaylight.yangtools.yang.model.api.PathExpression) TypedDataSchemaNode(org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) CaseSchemaNode(org.opendaylight.yangtools.yang.model.api.CaseSchemaNode) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) ChoiceSchemaNode(org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode)

Example 2 with PathExpression

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());
}
Also used : TypedDataSchemaNode(org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode) LeafrefTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition) PathExpression(org.opendaylight.yangtools.yang.model.api.PathExpression) YangLocationPath(org.opendaylight.yangtools.yang.xpath.api.YangLocationPath) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) InstanceIdentifierTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition) NonNull(org.eclipse.jdt.annotation.NonNull) Objects.requireNonNull(java.util.Objects.requireNonNull)

Example 3 with PathExpression

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));
}
Also used : PathExpression(org.opendaylight.yangtools.yang.model.api.PathExpression) QName(org.opendaylight.yangtools.yang.common.QName) LocationPathSteps(org.opendaylight.yangtools.yang.model.api.PathExpression.LocationPathSteps) GroupingDefinition(org.opendaylight.yangtools.yang.model.api.GroupingDefinition) Test(org.junit.Test)

Example 4 with PathExpression

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());
}
Also used : Steps(org.opendaylight.yangtools.yang.model.api.PathExpression.Steps) DerefSteps(org.opendaylight.yangtools.yang.model.api.PathExpression.DerefSteps) LocationPathSteps(org.opendaylight.yangtools.yang.model.api.PathExpression.LocationPathSteps) PathExpression(org.opendaylight.yangtools.yang.model.api.PathExpression) DerefSteps(org.opendaylight.yangtools.yang.model.api.PathExpression.DerefSteps) Test(org.junit.Test)

Example 5 with PathExpression

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));
}
Also used : ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) PathExpression(org.opendaylight.yangtools.yang.model.api.PathExpression) QName(org.opendaylight.yangtools.yang.common.QName) LocationPathSteps(org.opendaylight.yangtools.yang.model.api.PathExpression.LocationPathSteps) Module(org.opendaylight.yangtools.yang.model.api.Module) Test(org.junit.Test)

Aggregations

PathExpression (org.opendaylight.yangtools.yang.model.api.PathExpression)9 Test (org.junit.Test)7 QName (org.opendaylight.yangtools.yang.common.QName)5 LeafrefTypeDefinition (org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition)5 LocationPathSteps (org.opendaylight.yangtools.yang.model.api.PathExpression.LocationPathSteps)3 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)2 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)2 Module (org.opendaylight.yangtools.yang.model.api.Module)2 TypedDataSchemaNode (org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode)2 Objects.requireNonNull (java.util.Objects.requireNonNull)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 QNameModule (org.opendaylight.yangtools.yang.common.QNameModule)1 CaseSchemaNode (org.opendaylight.yangtools.yang.model.api.CaseSchemaNode)1 ChoiceSchemaNode (org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode)1 ContainerSchemaNode (org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode)1 DataNodeContainer (org.opendaylight.yangtools.yang.model.api.DataNodeContainer)1 EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)1 GroupingDefinition (org.opendaylight.yangtools.yang.model.api.GroupingDefinition)1 DerefSteps (org.opendaylight.yangtools.yang.model.api.PathExpression.DerefSteps)1 Steps (org.opendaylight.yangtools.yang.model.api.PathExpression.Steps)1