Bump versions to 4.0.0-SNAPSHOT
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / streams / listeners / NotificationListenerAdapter.java
index a5999ddfacc1aefef9b1e9f955988a40d4c80924..3061285483696254235963f3a01cbe7007358805 100644 (file)
@@ -7,32 +7,30 @@
  */
 package org.opendaylight.netconf.sal.streams.listeners;
 
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 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.netconf.sal.restconf.impl.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.restconf.common.errors.RestconfDocumentedException;
 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;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
+import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -40,21 +38,16 @@ import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 
 /**
- * {@link NotificationListenerAdapter} is responsible to track events on
- * notifications.
- *
+ * {@link NotificationListenerAdapter} is responsible to track events on notifications.
  */
-public class NotificationListenerAdapter extends AbstractCommonSubscriber implements DOMNotificationListener {
-
+public final class NotificationListenerAdapter extends AbstractCommonSubscriber implements DOMNotificationListener {
     private static final Logger LOG = LoggerFactory.getLogger(NotificationListenerAdapter.class);
 
+    private final ControllerContext controllerContext;
     private final String streamName;
-    private final SchemaPath path;
+    private final Absolute path;
     private final String outputType;
 
-    private SchemaContext schemaContext;
-    private DOMNotification notification;
-
     /**
      * Set path of listener and stream name, register event bus.
      *
@@ -65,15 +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) {
-        super();
+    NotificationListenerAdapter(final Absolute path, final String streamName, final String outputType,
+            final ControllerContext controllerContext) {
         register(this);
-        setLocalNameOfPath(path.getLastComponent().getLocalName());
-
-        this.outputType = Preconditions.checkNotNull(outputType);
-        this.path = Preconditions.checkNotNull(path);
-        Preconditions.checkArgument(streamName != null && !streamName.isEmpty());
+        this.outputType = requireNonNull(outputType);
+        this.path = requireNonNull(path);
+        checkArgument(streamName != null && !streamName.isEmpty());
         this.streamName = streamName;
+        this.controllerContext = controllerContext;
     }
 
     /**
@@ -83,17 +75,20 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem
      */
     @Override
     public String getOutputType() {
-        return this.outputType;
+        return outputType;
     }
 
     @Override
     public void onNotification(final DOMNotification notification) {
-        this.schemaContext = ControllerContext.getInstance().getGlobalSchema();
-        this.notification = notification;
+        final Instant now = Instant.now();
+        if (!checkStartStop(now, this)) {
+            return;
+        }
 
-        final String xml = prepareXml();
-        if (checkQueryParams(xml, this)) {
-            prepareAndPostData(xml);
+        final EffectiveModelContext schemaContext = controllerContext.getGlobalSchema();
+        final String xml = prepareXml(schemaContext, notification);
+        if (checkFilter(xml)) {
+            prepareAndPostData(outputType.equals("JSON") ? prepareJson(schemaContext, notification) : xml);
         }
     }
 
@@ -104,30 +99,26 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem
      */
     @Override
     public String getStreamName() {
-        return this.streamName;
+        return streamName;
     }
 
     /**
      * Get schema path of notification.
      *
-     * @return {@link SchemaPath}
+     * @return {@link Absolute} SchemaNodeIdentifier
      */
-    public SchemaPath getSchemaPath() {
-        return this.path;
+    public Absolute getSchemaPath() {
+        return path;
     }
 
     /**
      * 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);
     }
 
@@ -137,32 +128,22 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem
      * @return json as {@link String}
      */
     @VisibleForTesting
-    String prepareJson() {
-        JsonParser jsonParser = new JsonParser();
-        JsonObject json = new JsonObject();
-        json.add("ietf-restconf:notification", jsonParser.parse(writeBodyToString()));
+    String prepareJson(final EffectiveModelContext schemaContext, final DOMNotification notification) {
+        final JsonObject json = new JsonObject();
+        json.add("ietf-restconf:notification", JsonParser.parseString(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 EffectiveModelContext 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);
@@ -170,29 +151,23 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem
         return writer.toString();
     }
 
-    private String prepareXml() {
+    private String prepareXml(final EffectiveModelContext 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, this.notification, this.schemaContext);
+        addValuesToNotificationEventElement(doc, notificationEventElement, schemaContext, notification);
         notificationElement.appendChild(notificationEventElement);
 
         return transformDoc(doc);
     }
 
     private void addValuesToNotificationEventElement(final Document doc, final Element element,
-            final DOMNotification notification, final SchemaContext schemaContext) {
-        if (notification == null) {
-            return;
-        }
-
-        final NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> body =
-                notification.getBody();
+            final EffectiveModelContext schemaContext, final DOMNotification notification) {
         try {
-
-            final DOMResult domResult = writeNormalizedNode(body, schemaContext, this.path);
+            final DOMResult domResult = writeNormalizedNode(notification.getBody(),
+                SchemaInferenceStack.of(schemaContext, path).toInference());
             final Node result = doc.importNode(domResult.getNode().getFirstChild(), true);
             final Element dataElement = doc.createElement("notification");
             dataElement.appendChild(result);