private static final ThreadLocal<SimpleDateFormat> EVENT_TIME_FORMAT = new ThreadLocal<SimpleDateFormat>() {
protected SimpleDateFormat initialValue() {
- return new SimpleDateFormat(NetconfNotification.RFC3339_DATE_FORMAT_BLUEPRINT);
+ final SimpleDateFormat withMillis = new SimpleDateFormat(
+ NetconfNotification.RFC3339_DATE_FORMAT_WITH_MILLIS_BLUEPRINT);
+
+ return new SimpleDateFormat(NetconfNotification.RFC3339_DATE_FORMAT_BLUEPRINT) {
+ @Override public Date parse(final String source) throws ParseException {
+ try {
+ return super.parse(source);
+ } catch (ParseException e) {
+ // In case of failure, try to parse with milliseconds
+ return withMillis.parse(source);
+ }
+ }
+ };
}
public void set(SimpleDateFormat value) {
} catch (NetconfDocumentedException e) {
throw new IllegalArgumentException("Notification payload does not contain " + EVENT_TIME + " " + message);
} catch (ParseException e) {
- throw new IllegalArgumentException("Notification event time in wrong format " + EVENT_TIME + " " + message);
+ LOG.warn("Unable to parse event time from {}. Setting time to {}", eventTimeElement, NetconfNotification.UNKNOWN_EVENT_TIME, e);
+ return new AbstractMap.SimpleEntry<>(NetconfNotification.UNKNOWN_EVENT_TIME, notificationElement);
}
}
assertNotNull(root);
assertEquals(6, Iterables.size(root.getValue()));
assertEquals("user-visited-page", root.getNodeType().getLocalName());
- assertEquals(new SimpleDateFormat(NetconfNotification.RFC3339_DATE_FORMAT_BLUEPRINT).parse("2007-07-08T00:01:00Z"),
+ assertEquals(new SimpleDateFormat(NetconfNotification.RFC3339_DATE_FORMAT_WITH_MILLIS_BLUEPRINT).parse("2015-10-23T09:42:27.67175+00:00"),
((DOMEvent) domNotification).getEventTime());
}
}
<notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">
-<eventTime>2007-07-08T00:01:00Z</eventTime>
+<eventTime>2015-10-23T09:42:27.67175+00:00</eventTime>
<user-visited-page xmlns="org:opendaylight:notification:test:ns:yang:user-notification">
<ui:incoming-user xmlns:ui="org:opendaylight:notification:test:ns:yang:user-notification">ui:public-user</ui:incoming-user>
<ip-address>172.23.29.104</ip-address>
public static final String NOTIFICATION = "notification";
public static final String NOTIFICATION_NAMESPACE = "urn:ietf:params:netconf:capability:notification:1.0";
public static final String RFC3339_DATE_FORMAT_BLUEPRINT = "yyyy-MM-dd'T'HH:mm:ssXXX";
+ // The format with milliseconds is a bit fragile, it cannot be used for timestamps without millis (thats why its a separate format)
+ // + it might not work properly with more than 6 digits
+ // TODO try to find a better solution with Java8
+ public static final String RFC3339_DATE_FORMAT_WITH_MILLIS_BLUEPRINT = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX";
public static final String EVENT_TIME = "eventTime";
+ /**
+ * Used for unknown/un-parse-able event-times
+ */
+ public static final Date UNKNOWN_EVENT_TIME = new Date(0);
+
/**
* Create new notification and capture the timestamp in the constructor
*/
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
+import com.google.common.collect.Lists;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
MockitoAnnotations.initMocks(this);
}
+ @Test public void testEventTime() throws Exception {
+ final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
+ NetconfNotification.RFC3339_DATE_FORMAT_BLUEPRINT);
+ final SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat(
+ NetconfNotification.RFC3339_DATE_FORMAT_WITH_MILLIS_BLUEPRINT);
+
+ for (String time : Lists.newArrayList("2001-07-04T12:08:56.235-07:00", "2015-10-23T09:42:27.67175+00:00",
+ "1970-01-01T17:17:22.229568+00:00", "1937-01-01T12:00:27.87+00:20", "1990-12-31T15:59:60-08:00",
+ "1990-12-31T23:59:60Z", "1996-12-19T16:39:57-08:00"
+ // ,"1985-04-12T23:20:50.52Z"
+ )) {
+ try {
+ simpleDateFormat.parse(time);
+ } catch (ParseException e) {
+ simpleDateFormat2.parse(time);
+ }
+ }
+ }
+
@Test
public void testNotificationListeners() throws Exception {
final NetconfNotificationManager netconfNotificationManager = new NetconfNotificationManager();