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);
}
}
}
Aggregations