Search in sources :

Example 31 with XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.

the class SchemalessRpcStructureTransformer method selectFromDataStructure.

/**
 * Selects elements in anyxml data node, which matches path arguments QNames. Since class in not context aware,
 * method searches for all elements as they are named in path.
 * @param data data, must be of type {@link DOMSourceAnyxmlNode}
 * @param path path to select
 * @return selected data
 */
@Override
public Optional<NormalizedNode> selectFromDataStructure(final DataContainerChild data, final YangInstanceIdentifier path) {
    Preconditions.checkArgument(data instanceof DOMSourceAnyxmlNode);
    final List<XmlElement> xmlElements = selectMatchingNodes(getSourceElement(((DOMSourceAnyxmlNode) data).body()), path);
    final Document result = XmlUtil.newDocument();
    final Element dataElement = result.createElementNS(NETCONF_DATA_QNAME.getNamespace().toString(), NETCONF_DATA_QNAME.getLocalName());
    result.appendChild(dataElement);
    for (XmlElement xmlElement : xmlElements) {
        dataElement.appendChild(result.importNode(xmlElement.getDomElement(), true));
    }
    final DOMSourceAnyxmlNode resultAnyxml = Builders.anyXmlBuilder().withNodeIdentifier(NETCONF_DATA_NODEID).withValue(new DOMSource(result)).build();
    return Optional.of(resultAnyxml);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Document(org.w3c.dom.Document)

Example 32 with XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.

the class DefaultCloseSessionTest method testDefaultCloseSession2.

@Test
public void testDefaultCloseSession2() throws Exception {
    final NetconfDocumentedException expectedCause = new NetconfDocumentedException("testMessage");
    final AutoCloseable res = mock(AutoCloseable.class);
    doThrow(expectedCause).when(res).close();
    final DefaultCloseSession session = new DefaultCloseSession("testSession", res);
    XmlElement elem = XmlElement.fromDomElement(XmlUtil.readXmlToElement("<elem/>"));
    final DocumentedException ex = assertThrows(DocumentedException.class, () -> session.handleWithNoSubsequentOperations(XmlUtil.newDocument(), elem));
    assertEquals("Unable to properly close session testSession", ex.getMessage());
    assertSame(expectedCause, ex.getCause());
}
Also used : NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) DocumentedException(org.opendaylight.netconf.api.DocumentedException) NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Test(org.junit.Test)

Example 33 with XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.

the class DefaultStopExi method handleWithNoSubsequentOperations.

@Override
protected Element handleWithNoSubsequentOperations(Document document, XmlElement operationElement) {
    LOG.debug("Received stop-exi message {} ", XmlUtil.toString(operationElement));
    netconfSession.stopExiCommunication();
    Element getSchemaResult = document.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, XmlNetconfConstants.OK);
    LOG.trace("{} operation successful", STOP_EXI);
    return getSchemaResult;
}
Also used : XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element)

Example 34 with XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement 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 35 with XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.

the class SettableRpc method handle.

@Override
public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
    final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
    final XmlElement rpcElement = requestElement.getOnlyChildElement();
    final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);
    final Optional<Document> response = mapping.getResponse(rpcElement);
    if (response.isPresent()) {
        final Document document = response.get();
        checkForError(document);
        document.getDocumentElement().setAttribute(XmlNetconfConstants.MESSAGE_ID, msgId);
        return document;
    } else if (subsequentOperation.isExecutionTermination()) {
        throw new DocumentedException("Mapping not found " + XmlUtil.toString(requestMessage), ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED, ErrorSeverity.ERROR);
    } else {
        return subsequentOperation.execute(requestMessage);
    }
}
Also used : DocumentedException(org.opendaylight.netconf.api.DocumentedException) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Document(org.w3c.dom.Document)

Aggregations

XmlElement (org.opendaylight.netconf.api.xml.XmlElement)50 Element (org.w3c.dom.Element)21 Document (org.w3c.dom.Document)16 DocumentedException (org.opendaylight.netconf.api.DocumentedException)13 Test (org.junit.Test)8 QName (org.opendaylight.yangtools.yang.common.QName)7 NodeList (org.w3c.dom.NodeList)6 DOMSource (javax.xml.transform.dom.DOMSource)5 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)4 URISyntaxException (java.net.URISyntaxException)3 DOMSourceAnyxmlNode (org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode)3 NormalizedNodeResult (org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult)3 SAXException (org.xml.sax.SAXException)3 XmlNodeConverter (io.lighty.codecs.util.XmlNodeConverter)2 DeserializationException (io.lighty.codecs.util.exception.DeserializationException)2 Response (io.lighty.netconf.device.response.Response)2 ResponseData (io.lighty.netconf.device.response.ResponseData)2