Search in sources :

Example 6 with TypedDataSchemaNode

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

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));
}
Also used : SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) TypedDataSchemaNode(org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) Int16TypeDefinition(org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition) LeafrefTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition) QName(org.opendaylight.yangtools.yang.common.QName) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) BinaryTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Test(org.junit.Test)

Example 8 with TypedDataSchemaNode

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);
}
Also used : TypedDataSchemaNode(org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode) DOMSource(javax.xml.transform.dom.DOMSource) Document(org.w3c.dom.Document) AnydataSchemaNode(org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode) AnyxmlSchemaNode(org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode)

Example 9 with TypedDataSchemaNode

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;
}
Also used : AugmentationSchemaNode(org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode) TypedDataSchemaNode(org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) TypedDataSchemaNode(org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) CaseSchemaNode(org.opendaylight.yangtools.yang.model.api.CaseSchemaNode) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) AnydataSchemaNode(org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode) ChoiceSchemaNode(org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode) AnyxmlSchemaNode(org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode)

Aggregations

TypedDataSchemaNode (org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode)9 LeafrefTypeDefinition (org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition)5 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)4 DataNodeContainer (org.opendaylight.yangtools.yang.model.api.DataNodeContainer)3 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)3 QName (org.opendaylight.yangtools.yang.common.QName)2 AnydataSchemaNode (org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode)2 AnyxmlSchemaNode (org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode)2 CaseSchemaNode (org.opendaylight.yangtools.yang.model.api.CaseSchemaNode)2 ChoiceSchemaNode (org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode)2 PathExpression (org.opendaylight.yangtools.yang.model.api.PathExpression)2 ImmutableList (com.google.common.collect.ImmutableList)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 DOMSource (javax.xml.transform.dom.DOMSource)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Test (org.junit.Test)1 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)1 NodeWithValue (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue)1 AugmentationSchemaNode (org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode)1