X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=restconf%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fsal%2Fstreams%2Flisteners%2FNotificationListenerAdapter.java;h=608ddcbca1f8d12e572536b4fba4f2f5b1c4a4a6;hb=7c43e0b0e18fcda26907903b15659b2cdb2bb5e1;hp=1d32b39e23bb077c59120d7bcd5a78c09db0fe35;hpb=68f482715ca404aaaffec1eb76b4a37b67f8f21b;p=netconf.git diff --git a/restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerAdapter.java b/restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerAdapter.java index 1d32b39e23..608ddcbca1 100644 --- a/restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerAdapter.java +++ b/restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerAdapter.java @@ -19,7 +19,9 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.StringReader; +import java.io.StringWriter; import java.io.UnsupportedEncodingException; +import java.io.Writer; import java.util.Collection; import java.util.Date; import java.util.Set; @@ -40,7 +42,6 @@ import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; import org.json.JSONObject; -import org.json.XML; import org.opendaylight.controller.md.sal.dom.api.DOMNotification; import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener; import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; @@ -53,6 +54,9 @@ import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter; +import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory; +import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter; +import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory; import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils; import org.opendaylight.yangtools.yang.model.api.SchemaContext; @@ -75,8 +79,6 @@ public class NotificationListenerAdapter implements DOMNotificationListener { private static final TransformerFactory FACTORY = TransformerFactory.newInstance(); private final String streamName; - private ListenerRegistration registration; - private Set subscribers = new ConcurrentSet<>(); private final EventBus eventBus; private final EventBusChangeRecorder eventBusChangeRecorder; @@ -86,6 +88,11 @@ public class NotificationListenerAdapter implements DOMNotificationListener { private Date stop = null; private String filter; + private SchemaContext schemaContext; + private DOMNotification notification; + private ListenerRegistration registration; + private Set subscribers = new ConcurrentSet<>(); + /** * Set path of listener and stream name, register event bus. * @@ -109,10 +116,12 @@ public class NotificationListenerAdapter implements DOMNotificationListener { @Override public void onNotification(final DOMNotification notification) { + this.schemaContext = ControllerContext.getInstance().getGlobalSchema(); + this.notification = notification; final Date now = new Date(); if (this.stop != null) { if ((this.start.compareTo(now) < 0) && (this.stop.compareTo(now) > 0)) { - checkFilter(notification); + checkFilter(); } if (this.stop.compareTo(now) < 0) { try { @@ -124,21 +133,19 @@ public class NotificationListenerAdapter implements DOMNotificationListener { } else if (this.start != null) { if (this.start.compareTo(now) < 0) { this.start = null; - checkFilter(notification); + checkFilter(); } } else { - checkFilter(notification); + checkFilter(); } } /** * Check if is filter used and then prepare and post data do client * - * @param notification - * - data of notification */ - private void checkFilter(final DOMNotification notification) { - final String xml = prepareXmlFrom(notification); + private void checkFilter() { + final String xml = prepareXml(); if (this.filter == null) { prepareAndPostData(xml); } else { @@ -177,14 +184,40 @@ public class NotificationListenerAdapter implements DOMNotificationListener { private void prepareAndPostData(final String xml) { final Event event = new Event(EventType.NOTIFY); if (this.outputType.equals("JSON")) { - final JSONObject jsonObject = XML.toJSONObject(xml); - event.setData(jsonObject.toString()); + event.setData(prepareJson()); } else { event.setData(xml); } this.eventBus.post(event); } + /** + * Prepare json from notification data + * + * @return json as {@link String} + */ + private String prepareJson() { + final JSONObject json = new JSONObject(); + json.put("ietf-restconf:notification", + new JSONObject(writeBodyToString()).put("event-time", ListenerAdapter.toRFC3339(new Date()))); + return json.toString(); + } + + private String writeBodyToString() { + final Writer writer = new StringWriter(); + final NormalizedNodeStreamWriter jsonStream = + JSONNormalizedNodeStreamWriter.createExclusiveWriter(JSONCodecFactory.create(this.schemaContext), + this.notification.getType(), null, JsonWriterFactory.createJsonWriter(writer)); + final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream); + try { + nodeWriter.write(this.notification.getBody()); + nodeWriter.close(); + } catch (final IOException e) { + throw new RestconfDocumentedException("Problem while writing body of notification to JSON. ", e); + } + return writer.toString(); + } + /** * Checks if exists at least one {@link Channel} subscriber. * @@ -273,8 +306,7 @@ public class NotificationListenerAdapter implements DOMNotificationListener { this.eventBus.post(event); } - private String prepareXmlFrom(final DOMNotification notification) { - final SchemaContext schemaContext = ControllerContext.getInstance().getGlobalSchema(); + private String prepareXml() { final Document doc = ListenerAdapter.createDocument(); final Element notificationElement = doc.createElementNS("urn:ietf:params:xml:ns:netconf:notification:1.0", @@ -284,10 +316,10 @@ public class NotificationListenerAdapter implements DOMNotificationListener { final Element eventTimeElement = doc.createElement("eventTime"); eventTimeElement.setTextContent(ListenerAdapter.toRFC3339(new Date())); notificationElement.appendChild(eventTimeElement); - final String notificationNamespace = notification.getType().getLastComponent().getNamespace().toString(); + final Element notificationEventElement = doc.createElementNS( - notificationNamespace, "event"); - addValuesToNotificationEventElement(doc, notificationEventElement, notification, schemaContext); + "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "create-notification-stream"); + addValuesToNotificationEventElement(doc, notificationEventElement, this.notification, this.schemaContext); notificationElement.appendChild(notificationEventElement); try { @@ -320,7 +352,9 @@ public class NotificationListenerAdapter implements DOMNotificationListener { final DOMResult domResult = writeNormalizedNode(body, YangInstanceIdentifier.create(body.getIdentifier()), schemaContext); final Node result = doc.importNode(domResult.getNode().getFirstChild(), true); - element.appendChild(result); + 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) {