Search in sources :

Example 26 with DOMSourceAnyxmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.

the class SchemalessRpcStructureTransformerTest method testCreateEditConfigStructure.

@Test
public void testCreateEditConfigStructure() throws Exception {
    DOMSourceAnyxmlNode data = Builders.anyXmlBuilder().withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName())).withValue(source).build();
    if (expectedException != null) {
        assertThrows(expectedException, () -> adapter.createEditConfigStructure(Optional.of(data), path, Optional.of(ModifyAction.REPLACE)));
        return;
    }
    final DOMSourceAnyxmlNode anyXmlNode = adapter.createEditConfigStructure(Optional.of(data), path, Optional.of(ModifyAction.REPLACE));
    final String s = XmlUtil.toString((Element) anyXmlNode.body().getNode());
    Diff diff = new Diff(expectedConfig, s);
    assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
}
Also used : DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) Diff(org.custommonkey.xmlunit.Diff) Test(org.junit.Test)

Example 27 with DOMSourceAnyxmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.

the class SchemalessRpcStructureTransformerTest method testSelectFromDataStructure.

@Test
public void testSelectFromDataStructure() throws Exception {
    DOMSourceAnyxmlNode data = Builders.anyXmlBuilder().withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName())).withValue(new DOMSource(XmlUtil.readXmlToDocument(getConfigData).getDocumentElement())).build();
    final DOMSourceAnyxmlNode dataStructure = (DOMSourceAnyxmlNode) adapter.selectFromDataStructure(data, path).get();
    final XmlElement s = XmlElement.fromDomDocument((Document) dataStructure.body().getNode());
    final String dataFromReply = XmlUtil.toString(s.getOnlyChildElement().getDomElement());
    final String expectedData = XmlUtil.toString((Element) source.getNode());
    Diff diff = new Diff(expectedData, dataFromReply);
    assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) Diff(org.custommonkey.xmlunit.Diff) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Test(org.junit.Test)

Example 28 with DOMSourceAnyxmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.

the class NetconfStateSchemas method findSchemasNode.

private static Optional<? extends NormalizedNode> findSchemasNode(final NormalizedNode result, final EffectiveModelContext schemaContext) {
    if (result == null) {
        return Optional.empty();
    }
    final Optional<DataContainerChild> rpcResultOpt = ((ContainerNode) result).findChildByArg(NETCONF_DATA_NODEID);
    if (rpcResultOpt.isEmpty()) {
        return Optional.empty();
    }
    final DataContainerChild rpcResult = rpcResultOpt.get();
    verify(rpcResult instanceof DOMSourceAnyxmlNode, "Unexpected result %s", rpcResult);
    final NormalizedNode dataNode;
    try {
        dataNode = NetconfUtil.transformDOMSourceToNormalizedNode(schemaContext, ((DOMSourceAnyxmlNode) rpcResult).body()).getResult();
    } catch (XMLStreamException | URISyntaxException | IOException | SAXException e) {
        LOG.warn("Failed to transform {}", rpcResult, e);
        return Optional.empty();
    }
    final Optional<DataContainerChild> nStateNode = ((DataContainerNode) dataNode).findChildByArg(toId(NetconfState.QNAME));
    if (nStateNode.isEmpty()) {
        return Optional.empty();
    }
    return ((DataContainerNode) nStateNode.get()).findChildByArg(toId(Schemas.QNAME));
}
Also used : DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) XMLStreamException(javax.xml.stream.XMLStreamException) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) DataContainerNode(org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode) DataContainerNode(org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) SAXException(org.xml.sax.SAXException)

Example 29 with DOMSourceAnyxmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.

the class NormalizedNodeXmlBodyWriter method writeNormalizedNode.

private static void writeNormalizedNode(final XMLStreamWriter xmlWriter, final InstanceIdentifierContext<?> pathContext, NormalizedNode data, @Nullable final Integer depth) throws IOException {
    final RestconfNormalizedNodeWriter nnWriter;
    final EffectiveModelContext schemaCtx = pathContext.getSchemaContext();
    if (pathContext.getSchemaNode() instanceof SchemaContext) {
        nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, SchemaPath.ROOT, depth);
        if (data instanceof DOMSourceAnyxmlNode) {
            try {
                writeElements(xmlWriter, nnWriter, (ContainerNode) NetconfUtil.transformDOMSourceToNormalizedNode(schemaCtx, ((DOMSourceAnyxmlNode) data).body()).getResult());
            } catch (XMLStreamException | URISyntaxException | SAXException e) {
                throw new IOException("Cannot write anyxml", e);
            }
        } else {
            writeElements(xmlWriter, nnWriter, (ContainerNode) data);
        }
    } else if (pathContext.getSchemaNode() instanceof RpcDefinition) {
        final RpcDefinition rpc = (RpcDefinition) pathContext.getSchemaNode();
        final SchemaPath path = SchemaPath.of(Absolute.of(rpc.getQName(), rpc.getOutput().getQName()));
        nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, path, depth);
        writeElements(xmlWriter, nnWriter, (ContainerNode) data);
    } else {
        final List<QName> qNames = pathContext.getInstanceIdentifier().getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).map(PathArgument::getNodeType).collect(Collectors.toList());
        final SchemaPath path = SchemaPath.of(Absolute.of(qNames)).getParent();
        nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, path, depth);
        if (data instanceof MapEntryNode) {
            // Restconf allows returning one list item. We need to wrap it
            // in map node in order to serialize it properly
            data = ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType()).addChild((MapEntryNode) data).build();
        }
        nnWriter.write(data);
    }
    nnWriter.flush();
}
Also used : SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) Produces(javax.ws.rs.Produces) Provider(javax.ws.rs.ext.Provider) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) URISyntaxException(java.net.URISyntaxException) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) Draft02(org.opendaylight.netconf.sal.rest.api.Draft02) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) NetconfUtil(org.opendaylight.netconf.util.NetconfUtil) Absolute(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute) MediaType(javax.ws.rs.core.MediaType) Nullable(org.eclipse.jdt.annotation.Nullable) XMLStreamException(javax.xml.stream.XMLStreamException) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XMLConstants(javax.xml.XMLConstants) RestconfService(org.opendaylight.netconf.sal.rest.api.RestconfService) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) OutputStream(java.io.OutputStream) RestconfNormalizedNodeWriter(org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter) IOException(java.io.IOException) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) QName(org.opendaylight.yangtools.yang.common.QName) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) FactoryConfigurationError(javax.xml.stream.FactoryConfigurationError) XMLStreamNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) List(java.util.List) IndentingXMLStreamWriter(javanet.staxutils.IndentingXMLStreamWriter) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) Type(java.lang.reflect.Type) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) ImmutableNodes(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes) SAXException(org.xml.sax.SAXException) Annotation(java.lang.annotation.Annotation) Entry(java.util.Map.Entry) WebApplicationException(javax.ws.rs.WebApplicationException) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) RestconfNormalizedNodeWriter(org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) SAXException(org.xml.sax.SAXException) XMLStreamException(javax.xml.stream.XMLStreamException) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) List(java.util.List) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 30 with DOMSourceAnyxmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.

the class NormalizedNodeJsonBodyWriter method writeNormalizedNode.

private static void writeNormalizedNode(final JsonWriter jsonWriter, final InstanceIdentifierContext<SchemaNode> context, NormalizedNode data, @Nullable final Integer depth) throws IOException {
    final RestconfNormalizedNodeWriter nnWriter;
    if (context.getSchemaNode() instanceof SchemaContext) {
        /*
             *  Creates writer without initialNs and we write children of root data container
             *  which is not visible in restconf
             */
        nnWriter = createNormalizedNodeWriter(context, SchemaPath.ROOT, jsonWriter, depth);
        if (data instanceof ContainerNode) {
            writeChildren(nnWriter, (ContainerNode) data);
        } else if (data instanceof DOMSourceAnyxmlNode) {
            try {
                writeChildren(nnWriter, (ContainerNode) NetconfUtil.transformDOMSourceToNormalizedNode(context.getSchemaContext(), ((DOMSourceAnyxmlNode) data).body()).getResult());
            } catch (XMLStreamException | URISyntaxException | SAXException e) {
                throw new IOException("Cannot write anyxml.", e);
            }
        }
    } else if (context.getSchemaNode() instanceof RpcDefinition) {
        /*
             *  RpcDefinition is not supported as initial codec in JSONStreamWriter,
             *  so we need to emit initial output declaratation..
             */
        final RpcDefinition rpc = (RpcDefinition) context.getSchemaNode();
        final SchemaPath path = SchemaPath.of(Absolute.of(rpc.getQName(), rpc.getOutput().getQName()));
        nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth);
        jsonWriter.name("output");
        jsonWriter.beginObject();
        writeChildren(nnWriter, (ContainerNode) data);
        jsonWriter.endObject();
    } else {
        final List<QName> qnames = context.getInstanceIdentifier().getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).map(PathArgument::getNodeType).collect(Collectors.toList());
        final SchemaPath path = SchemaPath.of(Absolute.of(qnames)).getParent();
        if (data instanceof MapEntryNode) {
            data = ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType()).withChild((MapEntryNode) data).build();
        }
        nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth);
        nnWriter.write(data);
    }
    nnWriter.flush();
}
Also used : JSONNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter) JSONCodecFactorySupplier(org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) Produces(javax.ws.rs.Produces) Provider(javax.ws.rs.ext.Provider) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) URISyntaxException(java.net.URISyntaxException) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) Draft02(org.opendaylight.netconf.sal.rest.api.Draft02) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) NetconfUtil(org.opendaylight.netconf.util.NetconfUtil) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) JsonWriterFactory(org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory) Absolute(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute) MediaType(javax.ws.rs.core.MediaType) Nullable(org.eclipse.jdt.annotation.Nullable) XMLStreamException(javax.xml.stream.XMLStreamException) OutputStreamWriter(java.io.OutputStreamWriter) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) RestconfService(org.opendaylight.netconf.sal.rest.api.RestconfService) JsonWriter(com.google.gson.stream.JsonWriter) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) OutputStream(java.io.OutputStream) RestconfNormalizedNodeWriter(org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter) IOException(java.io.IOException) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) QName(org.opendaylight.yangtools.yang.common.QName) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) List(java.util.List) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) Type(java.lang.reflect.Type) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) ImmutableNodes(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) SAXException(org.xml.sax.SAXException) Annotation(java.lang.annotation.Annotation) Entry(java.util.Map.Entry) JSONCodecFactory(org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory) WebApplicationException(javax.ws.rs.WebApplicationException) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) RestconfNormalizedNodeWriter(org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) IOException(java.io.IOException) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) List(java.util.List) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument)

Aggregations

DOMSourceAnyxmlNode (org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode)30 Test (org.junit.Test)16 DOMSource (javax.xml.transform.dom.DOMSource)15 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)15 Diff (org.custommonkey.xmlunit.Diff)10 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)10 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)10 Document (org.w3c.dom.Document)10 DataContainerChild (org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild)9 QName (org.opendaylight.yangtools.yang.common.QName)8 NetconfMessage (org.opendaylight.netconf.api.NetconfMessage)7 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)7 XmlElement (org.opendaylight.netconf.api.xml.XmlElement)6 Element (org.w3c.dom.Element)6 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)5 IOException (java.io.IOException)4 XMLStreamException (javax.xml.stream.XMLStreamException)4 PathArgument (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument)4 NormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter)4 URISyntaxException (java.net.URISyntaxException)3