Search in sources :

Example 6 with ContainerLike

use of org.opendaylight.yangtools.yang.model.api.ContainerLike in project netconf by opendaylight.

the class URIParametersParsing method prepareDomRpcNode.

private NormalizedNodeContext prepareDomRpcNode(final String datastore, final String scope) {
    final EffectiveModelContext schema = this.controllerContext.getGlobalSchema();
    final Module rpcSalRemoteModule = schema.findModule("sal-remote", Revision.of("2014-01-14")).get();
    final QName rpcQName = QName.create(rpcSalRemoteModule.getQNameModule(), "create-data-change-event-subscription");
    final RpcDefinition rpcDef = Mockito.mock(RpcDefinition.class);
    ContainerLike rpcInputSchemaNode = null;
    for (final RpcDefinition rpc : rpcSalRemoteModule.getRpcs()) {
        if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
            rpcInputSchemaNode = rpc.getInput();
            break;
        }
    }
    assertNotNull("RPC ContainerSchemaNode was not found!", rpcInputSchemaNode);
    final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> container = SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);
    final QName pathQName = QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "2014-01-14", "path");
    final DataSchemaNode pathSchemaNode = rpcInputSchemaNode.getDataChildByName(pathQName);
    assertTrue(pathSchemaNode instanceof LeafSchemaNode);
    final LeafNode<Object> pathNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) pathSchemaNode).withValue(YangInstanceIdentifier.builder().node(QName.create("urn:opendaylight:inventory", "2013-08-19", "nodes")).build()).build();
    container.withChild(pathNode);
    final AugmentationSchemaNode augmentationSchema = requireNonNull(rpcInputSchemaNode.getAvailableAugmentations().iterator().next());
    final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> augmentationBuilder = SchemaAwareBuilders.augmentationBuilder(augmentationSchema);
    final QName dataStoreQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "datastore");
    final DataSchemaNode dsSchemaNode = augmentationSchema.getDataChildByName(dataStoreQName);
    assertTrue(dsSchemaNode instanceof LeafSchemaNode);
    final LeafNode<Object> dsNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) dsSchemaNode).withValue(datastore).build();
    augmentationBuilder.withChild(dsNode);
    final QName scopeQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "scope");
    final DataSchemaNode scopeSchemaNode = augmentationSchema.getDataChildByName(scopeQName);
    assertTrue(scopeSchemaNode instanceof LeafSchemaNode);
    final LeafNode<Object> scopeNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) scopeSchemaNode).withValue(scope).build();
    augmentationBuilder.withChild(scopeNode);
    final QName outputQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "notification-output-type");
    final DataSchemaNode outputSchemaNode = augmentationSchema.getDataChildByName(outputQName);
    assertTrue(outputSchemaNode instanceof LeafSchemaNode);
    final LeafNode<Object> outputNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) outputSchemaNode).withValue("XML").build();
    augmentationBuilder.withChild(outputNode);
    container.withChild(augmentationBuilder.build());
    when(rpcDef.getInput()).thenReturn((InputSchemaNode) rpcInputSchemaNode);
    when(rpcDef.getPath()).thenReturn(SchemaPath.create(true, rpcQName));
    when(rpcDef.getQName()).thenReturn(rpcQName);
    return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcDef, null, schema), container.build());
}
Also used : ContainerLike(org.opendaylight.yangtools.yang.model.api.ContainerLike) AugmentationSchemaNode(org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode) AugmentationIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) NormalizedNodeContext(org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) Module(org.opendaylight.yangtools.yang.model.api.Module) AugmentationNode(org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 7 with ContainerLike

use of org.opendaylight.yangtools.yang.model.api.ContainerLike in project netconf by opendaylight.

the class InvokeRpcMethodTest method testInvokeRpcWithNoPayloadWithOutput_Success.

@Test
public void testInvokeRpcWithNoPayloadWithOutput_Success() {
    final SchemaContext schema = controllerContext.getGlobalSchema();
    final Module rpcModule = schema.findModules("toaster").iterator().next();
    assertNotNull(rpcModule);
    final QName rpcQName = QName.create(rpcModule.getQNameModule(), "testOutput");
    final QName rpcOutputQName = QName.create(rpcModule.getQNameModule(), "output");
    RpcDefinition rpcDef = null;
    ContainerLike rpcOutputSchemaNode = null;
    for (final RpcDefinition rpc : rpcModule.getRpcs()) {
        if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
            rpcOutputSchemaNode = rpc.getOutput();
            rpcDef = rpc;
            break;
        }
    }
    assertNotNull(rpcDef);
    assertNotNull(rpcOutputSchemaNode);
    final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> containerBuilder = SchemaAwareBuilders.containerBuilder(rpcOutputSchemaNode);
    final DataSchemaNode leafSchema = rpcOutputSchemaNode.getDataChildByName(QName.create(rpcModule.getQNameModule(), "textOut"));
    assertTrue(leafSchema instanceof LeafSchemaNode);
    final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) leafSchema);
    leafBuilder.withValue("brm");
    containerBuilder.withChild(leafBuilder.build());
    final ContainerNode container = containerBuilder.build();
    final DOMRpcResult result = new DefaultDOMRpcResult(container);
    doReturn(immediateFluentFuture(result)).when(brokerFacade).invokeRpc(eq(rpcDef.getQName()), any());
    final NormalizedNodeContext output = this.restconfImpl.invokeRpc("toaster:testOutput", null, uriInfo);
    assertNotNull(output);
    assertNotNull(output.getData());
    assertSame(container, output.getData());
    assertNotNull(output.getInstanceIdentifierContext());
    assertNotNull(output.getInstanceIdentifierContext().getSchemaContext());
}
Also used : ContainerLike(org.opendaylight.yangtools.yang.model.api.ContainerLike) DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) NormalizedNodeContext(org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) LeafNode(org.opendaylight.yangtools.yang.data.api.schema.LeafNode) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) Module(org.opendaylight.yangtools.yang.model.api.Module) Test(org.junit.Test)

Example 8 with ContainerLike

use of org.opendaylight.yangtools.yang.model.api.ContainerLike in project netconf by opendaylight.

the class InvokeRpcMethodTest method prepareDomPayload.

private NormalizedNodeContext prepareDomPayload() {
    final EffectiveModelContext schema = controllerContext.getGlobalSchema();
    final Module rpcModule = schema.findModules("invoke-rpc-module").iterator().next();
    assertNotNull(rpcModule);
    final QName rpcQName = QName.create(rpcModule.getQNameModule(), "rpc-test");
    ContainerLike rpcInputSchemaNode = null;
    for (final RpcDefinition rpc : rpcModule.getRpcs()) {
        if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
            rpcInputSchemaNode = rpc.getInput();
            break;
        }
    }
    assertNotNull(rpcInputSchemaNode);
    final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> container = SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);
    final QName contQName = QName.create(rpcModule.getQNameModule(), "cont");
    final DataSchemaNode contSchemaNode = rpcInputSchemaNode.getDataChildByName(contQName);
    assertTrue(contSchemaNode instanceof ContainerSchemaNode);
    final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> contNode = SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) contSchemaNode);
    final QName lfQName = QName.create(rpcModule.getQNameModule(), "lf");
    final DataSchemaNode lfSchemaNode = ((ContainerSchemaNode) contSchemaNode).getDataChildByName(lfQName);
    assertTrue(lfSchemaNode instanceof LeafSchemaNode);
    final LeafNode<Object> lfNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) lfSchemaNode).withValue("any value").build();
    contNode.withChild(lfNode);
    container.withChild(contNode.build());
    return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcInputSchemaNode, null, schema), container.build());
}
Also used : ContainerLike(org.opendaylight.yangtools.yang.model.api.ContainerLike) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) NormalizedNodeContext(org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) Module(org.opendaylight.yangtools.yang.model.api.Module) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 9 with ContainerLike

use of org.opendaylight.yangtools.yang.model.api.ContainerLike in project netconf by opendaylight.

the class XmlNormalizedNodeBodyReader method parse.

private NormalizedNodePayload parse(final InstanceIdentifierContext<?> pathContext, final Document doc) throws XMLStreamException, IOException, ParserConfigurationException, SAXException, URISyntaxException {
    final SchemaNode schemaNodeContext = pathContext.getSchemaNode();
    DataSchemaNode schemaNode;
    final boolean isOperation;
    if (schemaNodeContext instanceof OperationDefinition) {
        schemaNode = ((OperationDefinition) schemaNodeContext).getInput();
        isOperation = true;
    } else if (schemaNodeContext instanceof DataSchemaNode) {
        schemaNode = (DataSchemaNode) schemaNodeContext;
        isOperation = false;
    } else {
        throw new IllegalStateException("Unknown SchemaNode " + schemaNodeContext);
    }
    final String docRootElm = doc.getDocumentElement().getLocalName();
    final String docRootNamespace = doc.getDocumentElement().getNamespaceURI();
    final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
    if (isPost() && !isOperation) {
        final Deque<Object> foundSchemaNodes = findPathToSchemaNodeByName(schemaNode, docRootElm, docRootNamespace);
        if (foundSchemaNodes.isEmpty()) {
            throw new IllegalStateException(String.format("Child \"%s\" was not found in parent schema node \"%s\"", docRootElm, schemaNode.getQName()));
        }
        while (!foundSchemaNodes.isEmpty()) {
            final Object child = foundSchemaNodes.pop();
            if (child instanceof AugmentationSchemaNode) {
                final AugmentationSchemaNode augmentSchemaNode = (AugmentationSchemaNode) child;
                iiToDataList.add(DataSchemaContextNode.augmentationIdentifierFrom(augmentSchemaNode));
            } else if (child instanceof DataSchemaNode) {
                schemaNode = (DataSchemaNode) child;
                iiToDataList.add(new YangInstanceIdentifier.NodeIdentifier(schemaNode.getQName()));
            }
        }
    // PUT
    } else if (!isOperation) {
        final QName scQName = schemaNode.getQName();
        checkState(docRootElm.equals(scQName.getLocalName()) && docRootNamespace.equals(scQName.getNamespace().toString()), "Not correct message root element \"%s\", should be \"%s\"", docRootElm, scQName);
    }
    NormalizedNode parsed;
    final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
    final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
    if (schemaNode instanceof ContainerLike || schemaNode instanceof ListSchemaNode || schemaNode instanceof LeafSchemaNode) {
        final XmlParserStream xmlParser = XmlParserStream.create(writer, SchemaInferenceStack.ofInstantiatedPath(pathContext.getSchemaContext(), schemaNode.getPath()).toInference());
        xmlParser.traverse(new DOMSource(doc.getDocumentElement()));
        parsed = resultHolder.getResult();
        // Therefore we now have to extract the MapEntryNode from the parsed MapNode.
        if (parsed instanceof MapNode) {
            final MapNode mapNode = (MapNode) parsed;
            // extracting the MapEntryNode
            parsed = mapNode.body().iterator().next();
        }
        if (schemaNode instanceof ListSchemaNode && isPost()) {
            iiToDataList.add(parsed.getIdentifier());
        }
    } else {
        LOG.warn("Unknown schema node extension {} was not parsed", schemaNode.getClass());
        parsed = null;
    }
    final YangInstanceIdentifier fullIIToData = YangInstanceIdentifier.create(Iterables.concat(pathContext.getInstanceIdentifier().getPathArguments(), iiToDataList));
    final InstanceIdentifierContext<? extends SchemaNode> outIIContext = new InstanceIdentifierContext<>(fullIIToData, pathContext.getSchemaNode(), pathContext.getMountPoint(), pathContext.getSchemaContext());
    // FIXME: can result really be null?
    return NormalizedNodePayload.ofNullable(outIIContext, parsed);
}
Also used : ContainerLike(org.opendaylight.yangtools.yang.model.api.ContainerLike) XmlParserStream(org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream) DOMSource(javax.xml.transform.dom.DOMSource) ArrayList(java.util.ArrayList) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) OperationDefinition(org.opendaylight.yangtools.yang.model.api.OperationDefinition) AugmentationSchemaNode(org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) AugmentationSchemaNode(org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) CaseSchemaNode(org.opendaylight.yangtools.yang.model.api.CaseSchemaNode) ChoiceSchemaNode(org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode)

Example 10 with ContainerLike

use of org.opendaylight.yangtools.yang.model.api.ContainerLike in project netconf by opendaylight.

the class TestRestconfUtils method parse.

private static NormalizedNode parse(final InstanceIdentifierContext<?> iiContext, final Document doc) throws XMLStreamException, IOException, ParserConfigurationException, SAXException, URISyntaxException {
    final SchemaNode schemaNodeContext = iiContext.getSchemaNode();
    DataSchemaNode schemaNode = null;
    if (schemaNodeContext instanceof RpcDefinition) {
        if ("input".equalsIgnoreCase(doc.getDocumentElement().getLocalName())) {
            schemaNode = ((RpcDefinition) schemaNodeContext).getInput();
        } else if ("output".equalsIgnoreCase(doc.getDocumentElement().getLocalName())) {
            schemaNode = ((RpcDefinition) schemaNodeContext).getOutput();
        } else {
            throw new IllegalStateException("Unknown Rpc input node");
        }
    } else if (schemaNodeContext instanceof DataSchemaNode) {
        schemaNode = (DataSchemaNode) schemaNodeContext;
    } else {
        throw new IllegalStateException("Unknow SchemaNode");
    }
    final String docRootElm = doc.getDocumentElement().getLocalName();
    final String schemaNodeName = iiContext.getSchemaNode().getQName().getLocalName();
    if (!schemaNodeName.equalsIgnoreCase(docRootElm)) {
        for (final DataSchemaNode child : ((DataNodeContainer) schemaNode).getChildNodes()) {
            if (child.getQName().getLocalName().equalsIgnoreCase(docRootElm)) {
                schemaNode = child;
                break;
            }
        }
    }
    final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
    final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
    final XmlParserStream xmlParser = XmlParserStream.create(writer, SchemaInferenceStack.ofInstantiatedPath(iiContext.getSchemaContext(), schemaNode.getPath()).toInference());
    if (schemaNode instanceof ContainerLike || schemaNode instanceof ListSchemaNode) {
        xmlParser.traverse(new DOMSource(doc.getDocumentElement()));
        return resultHolder.getResult();
    }
    // FIXME : add another DataSchemaNode extensions e.g. LeafSchemaNode
    return null;
}
Also used : ContainerLike(org.opendaylight.yangtools.yang.model.api.ContainerLike) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) XmlParserStream(org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream) DOMSource(javax.xml.transform.dom.DOMSource) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer)

Aggregations

ContainerLike (org.opendaylight.yangtools.yang.model.api.ContainerLike)11 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)8 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)8 QName (org.opendaylight.yangtools.yang.common.QName)7 RpcDefinition (org.opendaylight.yangtools.yang.model.api.RpcDefinition)7 ListSchemaNode (org.opendaylight.yangtools.yang.model.api.ListSchemaNode)6 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)5 AugmentationSchemaNode (org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode)5 Module (org.opendaylight.yangtools.yang.model.api.Module)5 NormalizedNodeContext (org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext)4 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)4 CaseSchemaNode (org.opendaylight.yangtools.yang.model.api.CaseSchemaNode)4 SchemaNode (org.opendaylight.yangtools.yang.model.api.SchemaNode)4 DOMSource (javax.xml.transform.dom.DOMSource)3 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)3 NormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter)3 XmlParserStream (org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream)3 ImmutableNormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter)3 NormalizedNodeResult (org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult)3 ChoiceSchemaNode (org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode)3