Search in sources :

Example 6 with NetconfMessage

use of org.opendaylight.netconf.api.NetconfMessage in project lighty-netconf-simulator by PANTHEONtech.

the class DeviceTest method toasterRPCsTest.

@Test
public void toasterRPCsTest() throws ExecutionException, InterruptedException, URISyntaxException, SAXException, TimeoutException, IOException {
    for (SimpleNetconfClientSessionListener listener : SESSION_LISTENERS) {
        final NetconfMessage makeToastResponse = sendRequestToDevice(MAKE_TOAST_REQUEST_XML, listener);
        Assertions.assertTrue(containsOkElement(makeToastResponse));
    }
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) SimpleNetconfClientSessionListener(org.opendaylight.netconf.client.SimpleNetconfClientSessionListener) Test(org.junit.jupiter.api.Test)

Example 7 with NetconfMessage

use of org.opendaylight.netconf.api.NetconfMessage in project lighty-netconf-simulator by PANTHEONtech.

the class DeviceTest method getSchemaTest.

@Test
public void getSchemaTest() throws IOException, URISyntaxException, SAXException, InterruptedException, ExecutionException, TimeoutException {
    for (SimpleNetconfClientSessionListener listener : SESSION_LISTENERS) {
        final NetconfMessage schemaResponse = sendRequestToDevice(GET_SCHEMAS_REQUEST_XML, listener);
        final NodeList schema = schemaResponse.getDocument().getDocumentElement().getElementsByTagName("schema");
        Assertions.assertTrue(schema.getLength() > 0);
        boolean toasterSchemaContained = false;
        for (int i = 0; i < schema.getLength(); i++) {
            if (schema.item(i).getNodeType() == Node.ELEMENT_NODE) {
                final Element item = (Element) schema.item(i);
                final String schemaName = item.getElementsByTagName("identifier").item(0).getTextContent();
                final String schemaNameSpace = item.getElementsByTagName("namespace").item(0).getTextContent();
                if ("toaster".equals(schemaName) && "http://netconfcentral.org/ns/toaster".equals(schemaNameSpace)) {
                    toasterSchemaContained = true;
                }
            }
        }
        Assertions.assertTrue(toasterSchemaContained);
    }
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) SimpleNetconfClientSessionListener(org.opendaylight.netconf.client.SimpleNetconfClientSessionListener) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Test(org.junit.jupiter.api.Test)

Example 8 with NetconfMessage

use of org.opendaylight.netconf.api.NetconfMessage in project lighty-netconf-simulator by PANTHEONtech.

the class NotificationOperation method sendMessage.

public void sendMessage(final Notification notificationMessage, final QName quName) {
    final List<NetconfSession> sessionList = this.sessions.get(quName.getLocalName());
    if (sessionList != null && !sessionList.isEmpty()) {
        final ContainerNode containerNode = this.adapterContext.currentSerializer().toNormalizedNodeNotification(notificationMessage);
        final Optional<? extends NotificationDefinition> notificationDefinition = ConverterUtils.loadNotification(this.effectiveModelContext, quName);
        final XmlNodeConverter xmlNodeConverter = new XmlNodeConverter(this.effectiveModelContext);
        if (notificationDefinition.isEmpty()) {
            throw new UnsupportedOperationException("Cannot load definition for QName: " + quName);
        }
        final Writer writer;
        try {
            writer = xmlNodeConverter.serializeRpc(notificationDefinition.get(), containerNode);
            try (InputStream is = new ByteArrayInputStream(writer.toString().getBytes(StandardCharsets.UTF_8))) {
                final DocumentBuilder builder = UntrustedXML.newDocumentBuilder();
                final Document notification = builder.parse(is);
                final Element body = notification.createElementNS(RPCUtil.CREATE_SUBSCRIPTION_NAMESPACE, "notification");
                final Element notificationElement = notification.getDocumentElement();
                final Element eventTime = notification.createElement("eventTime");
                final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
                eventTime.setTextContent(dateFormat.format(new Date()));
                body.appendChild(eventTime);
                body.appendChild(notificationElement);
                final Document document = builder.newDocument();
                final org.w3c.dom.Node importNode = document.importNode(body, true);
                document.appendChild(importNode);
                final NetconfMessage netconfMessage = new NetconfMessage(document);
                LOG.debug("Sending notification message: {}", netconfMessage.toString());
                sessionList.forEach(session -> session.sendMessage(netconfMessage));
            } catch (IOException | SAXException e) {
                LOG.error("Failed to send notification message", e);
            }
        } catch (final SerializationException e) {
            LOG.error("Failed to serialize notification to xml", e);
        }
    }
}
Also used : NetconfSession(org.opendaylight.netconf.api.NetconfSession) SerializationException(io.lighty.codecs.util.exception.SerializationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) Date(java.util.Date) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) Node(org.w3c.dom.Node) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) XmlNodeConverter(io.lighty.codecs.util.XmlNodeConverter) SimpleDateFormat(java.text.SimpleDateFormat) Writer(java.io.Writer)

Example 9 with NetconfMessage

use of org.opendaylight.netconf.api.NetconfMessage in project lighty-netconf-simulator by PANTHEONtech.

the class ActionDeviceTest method actionsTest.

@Test
public void actionsTest() throws IOException, URISyntaxException, SAXException, InterruptedException, ExecutionException, TimeoutException {
    final SimpleNetconfClientSessionListener sessionListener = new SimpleNetconfClientSessionListener();
    try (NetconfClientSession session = dispatcher.createClient(createSHHConfig(sessionListener)).get(TimeoutUtil.TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) {
        final NetconfMessage startActionResponse = sentRequesttoDevice(sessionListener, START_ACTION_REQUEST_XML);
        final String startResultTag = startActionResponse.getDocument().getDocumentElement().getElementsByTagName(START_TAG).item(0).getTextContent();
        assertEquals(startResultTag, START_ACTION_EXPECTED_VALUE);
        final NetconfMessage resetActionResponse = sentRequesttoDevice(sessionListener, RESET_ACTION_REQUEST_XML);
        final String resetResultTag = resetActionResponse.getDocument().getDocumentElement().getElementsByTagName(RESET_TAG).item(0).getTextContent();
        assertEquals(resetResultTag, RESET_ACTION_EXPECTED_VALUE);
    }
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) SimpleNetconfClientSessionListener(org.opendaylight.netconf.client.SimpleNetconfClientSessionListener) NetconfClientSession(org.opendaylight.netconf.client.NetconfClientSession) Test(org.junit.jupiter.api.Test)

Example 10 with NetconfMessage

use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.

the class NetconfServerSessionListenerTest method testOnMessageRuntimeFail.

@Test
public void testOnMessageRuntimeFail() throws Exception {
    doThrow(new RuntimeException("runtime fail")).when(router).onNetconfMessage(any(), any());
    final Document reply = XmlUtil.readXmlToDocument("<rpc message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" + "<example/></rpc>");
    final NetconfMessage msg = new NetconfMessage(reply);
    final IllegalStateException ex = assertThrows(IllegalStateException.class, () -> listener.onMessage(session, msg));
    verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.IN_RPC_FAIL)));
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) Document(org.w3c.dom.Document) Test(org.junit.Test)

Aggregations

NetconfMessage (org.opendaylight.netconf.api.NetconfMessage)125 Test (org.junit.Test)72 AbstractBaseSchemasTest (org.opendaylight.netconf.sal.connect.netconf.AbstractBaseSchemasTest)40 Document (org.w3c.dom.Document)28 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)23 QName (org.opendaylight.yangtools.yang.common.QName)17 DOMSourceAnyxmlNode (org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode)15 Test (org.junit.jupiter.api.Test)13 SimpleNetconfClientSessionListener (org.opendaylight.netconf.client.SimpleNetconfClientSessionListener)13 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)13 Node (org.w3c.dom.Node)13 NetconfClientSession (org.opendaylight.netconf.client.NetconfClientSession)12 ArrayList (java.util.ArrayList)11 Element (org.w3c.dom.Element)11 DOMRpcResult (org.opendaylight.mdsal.dom.api.DOMRpcResult)10 UnkeyedListNode (org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode)10 RpcResult (org.opendaylight.yangtools.yang.common.RpcResult)9 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)9 ChannelFuture (io.netty.channel.ChannelFuture)8 MapNode (org.opendaylight.yangtools.yang.data.api.schema.MapNode)8