- /**
- * Prepare data of notification and data to client.
- *
- * @param data data
- */
- private void prepareAndPostData(final String data) {
- final Event event = new Event(EventType.NOTIFY);
- event.setData(data);
- post(event);
- }
-
- /**
- * Prepare json from notification data.
- *
- * @return json as {@link String}
- */
- @VisibleForTesting
- String prepareJson(final SchemaContext schemaContext, final DOMNotification notification) {
- final JsonParser jsonParser = new JsonParser();
- final JsonObject json = new JsonObject();
- json.add("ietf-restconf:notification", jsonParser.parse(writeBodyToString(schemaContext, notification)));
- json.addProperty("event-time", ListenerAdapter.toRFC3339(Instant.now()));
- return json.toString();
- }
-
- private static String writeBodyToString(final SchemaContext schemaContext, final DOMNotification notification) {
- final Writer writer = new StringWriter();
- final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
- JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), notification.getType(),
- null, JsonWriterFactory.createJsonWriter(writer));
- final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
- try {
- nodeWriter.write(notification.getBody());
- nodeWriter.close();
- } catch (final IOException e) {
- throw new RestconfDocumentedException("Problem while writing body of notification to JSON. ", e);
- }
- return writer.toString();
- }
-
- private String prepareXml(final SchemaContext schemaContext, final DOMNotification notification) {
- final Document doc = createDocument();
- final Element notificationElement = basePartDoc(doc);
-
- final Element notificationEventElement = doc.createElementNS(
- "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "create-notification-stream");
- addValuesToNotificationEventElement(doc, notificationEventElement, schemaContext, notification);
- notificationElement.appendChild(notificationEventElement);
-
- return transformDoc(doc);
- }
-
- private void addValuesToNotificationEventElement(final Document doc, final Element element,
- final SchemaContext schemaContext, final DOMNotification notification) {
- try {
-
- final DOMResult domResult = writeNormalizedNode(notification.getBody(), schemaContext, this.path);
- final Node result = doc.importNode(domResult.getNode().getFirstChild(), true);
- final Element dataElement = doc.createElement("notification");
- dataElement.appendChild(result);
- element.appendChild(dataElement);
- } catch (final IOException e) {
- LOG.error("Error in writer ", e);
- } catch (final XMLStreamException e) {
- LOG.error("Error processing stream", e);