Search in sources :

Example 1 with NetconfSession

use of org.opendaylight.netconf.api.NetconfSession 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)

Aggregations

XmlNodeConverter (io.lighty.codecs.util.XmlNodeConverter)1 SerializationException (io.lighty.codecs.util.exception.SerializationException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Writer (java.io.Writer)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 NetconfMessage (org.opendaylight.netconf.api.NetconfMessage)1 NetconfSession (org.opendaylight.netconf.api.NetconfSession)1 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 SAXException (org.xml.sax.SAXException)1