Simplify NotificationListenerAdapter
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / streams / listeners / NotificationListenerAdapter.java
index 54e25a7c189b27863f042d3123940795e57fe56a..653e0bfbc12e9c8f83fcfcc9d62e99acecd3c504 100644 (file)
@@ -15,20 +15,16 @@ 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.ContainerNode;
 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,6 +44,7 @@ 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;
@@ -65,12 +62,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,8 +83,9 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem
     }
 
     @Override
+    @SuppressWarnings("checkstyle:hiddenField")
     public void onNotification(final DOMNotification notification) {
-        this.schemaContext = ControllerContext.getInstance().getGlobalSchema();
+        this.schemaContext = controllerContext.getGlobalSchema();
         this.notification = notification;
 
         final String xml = prepareXml();
@@ -154,9 +154,9 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem
 
     private String writeBodyToString() {
         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(this.schemaContext),
+            this.notification.getType(), null, JsonWriterFactory.createJsonWriter(writer));
         final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
         try {
             nodeWriter.write(this.notification.getBody());
@@ -173,20 +173,14 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem
 
         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);
         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();
+    private void addValuesToNotificationEventElement(final Document doc, final Element element) {
+        final ContainerNode body = notification.getBody();
         try {
 
             final DOMResult domResult = writeNormalizedNode(body, schemaContext, this.path);