Eliminate ListenerConstants 88/53188/7
authorRobert Varga <rovarga@cisco.com>
Sat, 11 Mar 2017 23:03:08 +0000 (00:03 +0100)
committerRobert Varga <rovarga@cisco.com>
Sun, 12 Mar 2017 15:57:31 +0000 (16:57 +0100)
These constatns are used only in AbstractNotificationData, inline them
in that class.

lso convert to using Java 8 Time API to fix thread-safety issues with
SimpleDateFormat. Since we need to choose an appropriate API, we choose
to report instants in UTC, so we do not have a dependency on the concept
of the local time zone.

Change-Id: I68153d525960d47e6ff60634df976d472beb2176
Signed-off-by: Robert Varga <rovarga@cisco.com>
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/streams/listeners/AbstractNotificationsData.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/streams/listeners/ListenersConstants.java [deleted file]
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerAdapter.java
restconf/sal-rest-connector/src/test/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerTest.java [moved from restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/streams/listeners/NotificationListenerTest.java with 92% similarity]

index 3b60f677902151521a512eea58f961652379e294..3ffff277036d1b49f6f598294b90648ccca72ba0 100644 (file)
@@ -10,13 +10,17 @@ package org.opendaylight.netconf.sal.streams.listeners;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
-import java.util.Date;
+import java.time.Instant;
+import java.time.OffsetDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMResult;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
@@ -45,8 +49,8 @@ import org.w3c.dom.Element;
  *
  */
 abstract class AbstractNotificationsData {
-
     private static final Logger LOG = LoggerFactory.getLogger(AbstractNotificationsData.class);
+    private static final TransformerFactory TF = TransformerFactory.newInstance();
 
     private TransactionChainHandler transactionChainHandler;
     private SchemaContextHandler schemaHandler;
@@ -91,12 +95,11 @@ abstract class AbstractNotificationsData {
     /**
      * Formats data specified by RFC3339.
      *
-     * @param d
-     *            Date
+     * @param now time stamp
      * @return Data specified by RFC3339.
      */
-    protected static String toRFC3339(final Date d) {
-        return ListenersConstants.RFC3339_PATTERN.matcher(ListenersConstants.RFC3339.format(d)).replaceAll("$1:$2");
+    protected static String toRFC3339(final Instant now) {
+        return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(OffsetDateTime.ofInstant(now, ZoneId.systemDefault()));
     }
 
     /**
@@ -165,7 +168,7 @@ abstract class AbstractNotificationsData {
         doc.appendChild(notificationElement);
 
         final Element eventTimeElement = doc.createElement("eventTime");
-        eventTimeElement.setTextContent(toRFC3339(new Date()));
+        eventTimeElement.setTextContent(toRFC3339(Instant.now()));
         notificationElement.appendChild(eventTimeElement);
 
         return notificationElement;
@@ -182,7 +185,7 @@ abstract class AbstractNotificationsData {
         final ByteArrayOutputStream out = new ByteArrayOutputStream();
 
         try {
-            final Transformer transformer = ListenersConstants.FACTORY.newTransformer();
+            final Transformer transformer = TF.newTransformer();
             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
             transformer.setOutputProperty(OutputKeys.METHOD, "xml");
             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
diff --git a/restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/streams/listeners/ListenersConstants.java b/restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/streams/listeners/ListenersConstants.java
deleted file mode 100644 (file)
index 9d44d0e..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.netconf.sal.streams.listeners;
-
-import java.text.SimpleDateFormat;
-import java.util.regex.Pattern;
-import javax.xml.transform.TransformerFactory;
-
-/**
- * Constants of listeners
- */
-class ListenersConstants {
-
-    static final TransformerFactory FACTORY = TransformerFactory.newInstance();
-    static final Pattern RFC3339_PATTERN = Pattern.compile("(\\d\\d)(\\d\\d)$");
-
-    static final SimpleDateFormat RFC3339 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
-}
index 843edafac688f3086c3fb3a6133fa45c7a224a63..e33be5535106a70ed8a6731ea124831382b96dc9 100644 (file)
@@ -7,12 +7,13 @@
  */
 package org.opendaylight.netconf.sal.streams.listeners;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import java.io.IOException;
 import java.io.StringWriter;
 import java.io.Writer;
+import java.time.Instant;
 import java.util.Collection;
-import java.util.Date;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.transform.dom.DOMResult;
 import org.json.JSONObject;
@@ -134,13 +135,24 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem
      *
      * @return json as {@link String}
      */
-    private String prepareJson() {
+    @VisibleForTesting
+    String prepareJson() {
         final JSONObject json = new JSONObject();
         json.put("ietf-restconf:notification",
-                new JSONObject(writeBodyToString()).put("event-time", ListenerAdapter.toRFC3339(new Date())));
+                new JSONObject(writeBodyToString()).put("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() {
         final Writer writer = new StringWriter();
         final NormalizedNodeStreamWriter jsonStream =
@@ -5,7 +5,7 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-package org.opendaylight.controller.sal.streams.listeners;
+package org.opendaylight.netconf.sal.streams.listeners;
 
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.any;
@@ -15,8 +15,6 @@ import static org.mockito.Mockito.when;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
 import java.net.URI;
 import java.text.ParseException;
 import java.util.ArrayList;
@@ -30,8 +28,6 @@ import org.mockito.MockitoAnnotations;
 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
-import org.opendaylight.netconf.sal.streams.listeners.NotificationListenerAdapter;
-import org.opendaylight.netconf.sal.streams.listeners.Notificator;
 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
 import org.opendaylight.yangtools.util.SingletonSet;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -236,21 +232,9 @@ public class NotificationListenerTest {
         final List<NotificationListenerAdapter> listNotifi =
                 Notificator.createNotificationListener(paths, "stream-name", NotificationOutputType.JSON.toString());
         final NotificationListenerAdapter notifi = listNotifi.get(0);
-
-        final Class<?> vars[] = {};
-        final Method prepareJsonM = notifi.getClass().getDeclaredMethod("prepareJson", vars);
-        prepareJsonM.setAccessible(true);
-
-        final Field notification = notifi.getClass().getDeclaredField("notification");
-        notification.setAccessible(true);
-        notification.set(notifi, notificationData);
-
-        final Field schema = notifi.getClass().getDeclaredField("schemaContext");
-        schema.setAccessible(true);
-        schema.set(notifi, this.schmeaCtx);
-
-        final String result = (String) prepareJsonM.invoke(notifi, null);
-        Preconditions.checkNotNull(result);
-        return result;
+        notifi.setNotification(notificationData);
+        notifi.setSchemaContext(this.schmeaCtx);
+        final String result = notifi.prepareJson();
+        return Preconditions.checkNotNull(result);
     }
 }