X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=restconf%2Frestconf-nb-bierman02%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fsal%2Fstreams%2Flisteners%2FNotificationListenerAdapter.java;h=f3193e7993d9f81e4a69cc9bf9c6253ed93ca59a;hb=2c69dd7eb97b9e5a8cc7fd17d12bf792e9113ce0;hp=9775e344f6018361eb4b7d07bbafe5117136edeb;hpb=2295d50e7212d80a9bc752f655fa66790ad45022;p=netconf.git diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerAdapter.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerAdapter.java index 9775e344f6..f3193e7993 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerAdapter.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerAdapter.java @@ -15,20 +15,15 @@ import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import java.time.Instant; -import java.util.Collection; import javax.xml.stream.XMLStreamException; import javax.xml.transform.dom.DOMResult; -import org.opendaylight.controller.md.sal.dom.api.DOMNotification; -import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener; +import org.opendaylight.mdsal.dom.api.DOMNotification; +import org.opendaylight.mdsal.dom.api.DOMNotificationListener; import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.restconf.common.errors.RestconfDocumentedException; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; -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.JSONCodecFactorySupplier; import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory; import org.opendaylight.yangtools.yang.model.api.SchemaContext; @@ -48,13 +43,11 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem private static final Logger LOG = LoggerFactory.getLogger(NotificationListenerAdapter.class); + private final ControllerContext controllerContext; private final String streamName; private final SchemaPath path; private final String outputType; - private SchemaContext schemaContext; - private DOMNotification notification; - /** * Set path of listener and stream name, register event bus. * @@ -65,12 +58,14 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem * @param outputType * type of output on notification (JSON, XML) */ - NotificationListenerAdapter(final SchemaPath path, final String streamName, final String outputType) { + NotificationListenerAdapter(final SchemaPath path, final String streamName, final String outputType, + final ControllerContext controllerContext) { register(this); this.outputType = Preconditions.checkNotNull(outputType); this.path = Preconditions.checkNotNull(path); Preconditions.checkArgument(streamName != null && !streamName.isEmpty()); this.streamName = streamName; + this.controllerContext = controllerContext; } /** @@ -84,14 +79,11 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem } @Override - @SuppressWarnings("checkstyle:hiddenField") public void onNotification(final DOMNotification notification) { - this.schemaContext = ControllerContext.getInstance().getGlobalSchema(); - this.notification = notification; - - final String xml = prepareXml(); + final SchemaContext schemaContext = controllerContext.getGlobalSchema(); + final String xml = prepareXml(schemaContext, notification); if (checkQueryParams(xml, this)) { - prepareAndPostData(xml); + prepareAndPostData(outputType.equals("JSON") ? prepareJson(schemaContext, notification) : xml); } } @@ -117,15 +109,11 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem /** * Prepare data of notification and data to client. * - * @param xml data + * @param data data */ - private void prepareAndPostData(final String xml) { + private void prepareAndPostData(final String data) { final Event event = new Event(EventType.NOTIFY); - if (this.outputType.equals("JSON")) { - event.setData(prepareJson()); - } else { - event.setData(xml); - } + event.setData(data); post(event); } @@ -135,32 +123,22 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem * @return json as {@link String} */ @VisibleForTesting - String prepareJson() { + 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())); + json.add("ietf-restconf:notification", jsonParser.parse(writeBodyToString(schemaContext, notification))); json.addProperty("event-time", ListenerAdapter.toRFC3339(Instant.now())); return json.toString(); } - @VisibleForTesting - void setNotification(final DOMNotification notification) { - this.notification = Preconditions.checkNotNull(notification); - } - - @VisibleForTesting - void setSchemaContext(final SchemaContext schemaContext) { - this.schemaContext = Preconditions.checkNotNull(schemaContext); - } - - private String writeBodyToString() { + private static String writeBodyToString(final SchemaContext schemaContext, final DOMNotification notification) { final Writer writer = new StringWriter(); - final NormalizedNodeStreamWriter jsonStream = - JSONNormalizedNodeStreamWriter.createExclusiveWriter(JSONCodecFactory.getShared(this.schemaContext), - this.notification.getType(), null, JsonWriterFactory.createJsonWriter(writer)); + 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(this.notification.getBody()); + nodeWriter.write(notification.getBody()); nodeWriter.close(); } catch (final IOException e) { throw new RestconfDocumentedException("Problem while writing body of notification to JSON. ", e); @@ -168,24 +146,23 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem return writer.toString(); } - private String prepareXml() { + 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); + addValuesToNotificationEventElement(doc, notificationEventElement, schemaContext, notification); notificationElement.appendChild(notificationEventElement); return transformDoc(doc); } - private void addValuesToNotificationEventElement(final Document doc, final Element element) { - final NormalizedNode>> body = - notification.getBody(); + private void addValuesToNotificationEventElement(final Document doc, final Element element, + final SchemaContext schemaContext, final DOMNotification notification) { try { - final DOMResult domResult = writeNormalizedNode(body, schemaContext, this.path); + 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);