Search in sources :

Example 1 with ContainerLike

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

the class InvokeRpcMethodTest method testInvokeRpcMethodWithInput.

@Test
@Ignore
public void testInvokeRpcMethodWithInput() {
    final DOMRpcResult expResult = mock(DOMRpcResult.class);
    final QName path = QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)make-toast");
    final Module rpcModule = schemaContext.findModules("toaster").iterator().next();
    assertNotNull(rpcModule);
    final QName rpcQName = QName.create(rpcModule.getQNameModule(), "make-toast");
    RpcDefinition rpcDef = null;
    ContainerLike rpcInputSchemaNode = null;
    for (final RpcDefinition rpc : rpcModule.getRpcs()) {
        if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
            rpcInputSchemaNode = rpc.getInput();
            rpcDef = rpc;
            break;
        }
    }
    assertNotNull(rpcDef);
    assertNotNull(rpcInputSchemaNode);
    final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> containerBuilder = SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);
    final NormalizedNodeContext payload = new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcInputSchemaNode, null, schemaContext), containerBuilder.build());
    doReturn(immediateFluentFuture(expResult)).when(brokerFacade).invokeRpc(eq(path), any(NormalizedNode.class));
    final NormalizedNodeContext output = this.restconfImpl.invokeRpc("toaster:make-toast", payload, uriInfo);
    assertNotNull(output);
    assertEquals(null, output.getData());
// additional validation in the fact that the restconfImpl does not
// throw an exception.
}
Also used : ContainerLike(org.opendaylight.yangtools.yang.model.api.ContainerLike) DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) QName(org.opendaylight.yangtools.yang.common.QName) 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) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) NormalizedNodeContext(org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with ContainerLike

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

the class XmlNormalizedNodeBodyReader method parse.

private NormalizedNodeContext parse(final InstanceIdentifierContext<?> pathContext, final Document doc) throws XMLStreamException, IOException, ParserConfigurationException, SAXException, URISyntaxException {
    final SchemaNode schemaNodeContext = pathContext.getSchemaNode();
    DataSchemaNode schemaNode;
    boolean isRpc = false;
    if (schemaNodeContext instanceof RpcDefinition) {
        schemaNode = ((RpcDefinition) schemaNodeContext).getInput();
        isRpc = true;
    } else if (schemaNodeContext instanceof DataSchemaNode) {
        schemaNode = (DataSchemaNode) schemaNodeContext;
    } else {
        throw new IllegalStateException("Unknown SchemaNode");
    }
    final String docRootElm = doc.getDocumentElement().getLocalName();
    final String docRootNamespace = doc.getDocumentElement().getNamespaceURI();
    final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
    if (isPost() && !isRpc) {
        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 (!isRpc) {
        final QName scQName = schemaNode.getQName();
        Preconditions.checkState(docRootElm.equals(scQName.getLocalName()) && docRootNamespace.equals(scQName.getNamespace().toString()), String.format("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.ofSchemaPath(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());
    return new NormalizedNodeContext(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) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) 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) 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 3 with ContainerLike

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

the class CreateStreamUtilTest method prepareDomPayload.

private NormalizedNodePayload prepareDomPayload(final String rpcName, final Function<RpcDefinition, ContainerLike> rpcToContainer, final String toasterValue, final String inputOutputName) {
    final EffectiveModelContext schema = refSchemaCtx;
    final Module rpcModule = schema.findModules("sal-remote").iterator().next();
    final QName rpcQName = QName.create(rpcModule.getQNameModule(), rpcName);
    ContainerLike rpcInputSchemaNode = null;
    for (final RpcDefinition rpc : rpcModule.getRpcs()) {
        if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
            rpcInputSchemaNode = rpcToContainer.apply(rpc);
            break;
        }
    }
    assertNotNull(rpcInputSchemaNode);
    final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> container = SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);
    final QName lfQName = QName.create(rpcModule.getQNameModule(), inputOutputName);
    final DataSchemaNode lfSchemaNode = rpcInputSchemaNode.findDataChildByName(lfQName).orElseThrow();
    assertThat(lfSchemaNode, instanceOf(LeafSchemaNode.class));
    final Object o;
    if ("toaster".equals(toasterValue)) {
        final QName rpcQname = QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", toasterValue);
        o = YangInstanceIdentifier.builder().node(rpcQname).build();
    } else {
        o = toasterValue;
    }
    final LeafNode<Object> lfNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) lfSchemaNode).withValue(o).build();
    container.withChild(lfNode);
    return NormalizedNodePayload.of(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) 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 4 with ContainerLike

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

the class NormalizedNodeStreamWriterStack method startContainerNode.

public SchemaNode startContainerNode(final NodeIdentifier name) {
    LOG.debug("Enter container {}", name);
    final SchemaNode schema;
    if (schemaStack.isEmpty() && root instanceof NotificationDefinition) {
        // Special case for stacks initialized at notification. We pretend the first container is contained within
        // itself.
        // FIXME: 8.0.0: factor this special case out to something more reasonable, like being initialized at the
        // Notification's parent and knowing to enterSchemaTree() instead of enterDataTree().
        schema = (NotificationDefinition) root;
    } else {
        schema = enterDataTree(name);
        checkArgument(schema instanceof ContainerLike, "Node %s is not a container", schema);
    }
    schemaStack.push(schema);
    return schema;
}
Also used : ContainerLike(org.opendaylight.yangtools.yang.model.api.ContainerLike) AnydataSchemaNode(org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode) AugmentationSchemaNode(org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) CaseSchemaNode(org.opendaylight.yangtools.yang.model.api.CaseSchemaNode) AnyxmlSchemaNode(org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) 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) NotificationDefinition(org.opendaylight.yangtools.yang.model.api.NotificationDefinition)

Example 5 with ContainerLike

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

the class XmlParserStream method parse.

/**
 * This method parses the XML source and emits node events into a NormalizedNodeStreamWriter based on the
 * YANG-modeled data contained in the XML source.
 *
 * @param reader
 *              StAX reader which is to used to walk through the XML source
 * @return
 *              instance of XmlParserStream
 * @throws XMLStreamException
 *              if a well-formedness error or an unexpected processing condition occurs while parsing the XML
 * @throws URISyntaxException
 *              if the namespace URI of an XML element contains a syntax error
 * @throws IOException
 *              if an error occurs while parsing the value of an anyxml node
 * @throws SAXException
 *              if an error occurs while parsing the value of an anyxml node
 */
public XmlParserStream parse(final XMLStreamReader reader) throws XMLStreamException, URISyntaxException, IOException, SAXException {
    if (reader.hasNext()) {
        reader.nextTag();
        final AbstractNodeDataWithSchema<?> nodeDataWithSchema;
        if (parentNode instanceof ContainerLike) {
            nodeDataWithSchema = new ContainerNodeDataWithSchema((ContainerLike) parentNode);
        } else if (parentNode instanceof ListSchemaNode) {
            nodeDataWithSchema = new ListNodeDataWithSchema((ListSchemaNode) parentNode);
        } else if (parentNode instanceof AnyxmlSchemaNode) {
            nodeDataWithSchema = new AnyXmlNodeDataWithSchema((AnyxmlSchemaNode) parentNode);
        } else if (parentNode instanceof LeafSchemaNode) {
            nodeDataWithSchema = new LeafNodeDataWithSchema((LeafSchemaNode) parentNode);
        } else if (parentNode instanceof LeafListSchemaNode) {
            nodeDataWithSchema = new LeafListNodeDataWithSchema((LeafListSchemaNode) parentNode);
        } else if (parentNode instanceof AnydataSchemaNode) {
            nodeDataWithSchema = new AnydataNodeDataWithSchema((AnydataSchemaNode) parentNode);
        } else {
            throw new IllegalStateException("Unsupported schema node type " + parentNode.getClass() + ".");
        }
        read(reader, nodeDataWithSchema, reader.getLocalName());
        nodeDataWithSchema.write(writer);
    }
    return this;
}
Also used : ContainerLike(org.opendaylight.yangtools.yang.model.api.ContainerLike) LeafListNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.LeafListNodeDataWithSchema) ListNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.ListNodeDataWithSchema) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) AnydataNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.AnydataNodeDataWithSchema) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) LeafNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.LeafNodeDataWithSchema) ContainerNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.ContainerNodeDataWithSchema) AnyxmlSchemaNode(org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode) LeafListNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.LeafListNodeDataWithSchema) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) AnydataSchemaNode(org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode) AnyXmlNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.AnyXmlNodeDataWithSchema)

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