X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=protocol%2Fnetconf-api%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fapi%2Fmessages%2FNotificationMessage.java;h=66da0ffe79f8ecba336020c8fd45f70983b9f0ff;hb=894d3e240e70c339f15cf73eabcff646d58051cc;hp=f06db1a26ae5676f98de02d139fff39b227e554d;hpb=c61f223369842b18df5f8c724b7f77b0f7cefc26;p=netconf.git diff --git a/protocol/netconf-api/src/main/java/org/opendaylight/netconf/api/messages/NotificationMessage.java b/protocol/netconf-api/src/main/java/org/opendaylight/netconf/api/messages/NotificationMessage.java index f06db1a26a..66da0ffe79 100644 --- a/protocol/netconf-api/src/main/java/org/opendaylight/netconf/api/messages/NotificationMessage.java +++ b/protocol/netconf-api/src/main/java/org/opendaylight/netconf/api/messages/NotificationMessage.java @@ -9,7 +9,6 @@ package org.opendaylight.netconf.api.messages; import static java.util.Objects.requireNonNull; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.text.ParsePosition; import java.time.Instant; import java.time.LocalDateTime; @@ -20,8 +19,9 @@ import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.time.temporal.ChronoField; import java.time.temporal.TemporalAccessor; -import java.util.Date; import java.util.function.Function; +import org.eclipse.jdt.annotation.NonNull; +import org.opendaylight.netconf.api.NamespaceURN; import org.opendaylight.netconf.api.NetconfMessage; import org.opendaylight.netconf.api.xml.XmlNetconfConstants; import org.slf4j.Logger; @@ -34,14 +34,12 @@ import org.w3c.dom.Element; */ public final class NotificationMessage extends NetconfMessage { private static final Logger LOG = LoggerFactory.getLogger(NotificationMessage.class); - private static final String NOTIFICATION_TAG = "notification"; - private static final String EVENT_TIME_TAG = "eventTime"; /** * Used for unknown/un-parse-able event-times. */ - public static final Date UNKNOWN_EVENT_TIME = new Date(0); - + // FIXME: we should differentiate unknown and invalid event times + public static final Instant UNKNOWN_EVENT_TIME = Instant.EPOCH; /** * The ISO-like date-time formatter that formats or parses a date-time with @@ -51,14 +49,14 @@ public final class NotificationMessage extends NetconfMessage { private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ISO_DATE_TIME; /** - * Provide a {@link String} representation of a {@link Date} object, + * Provide a {@link String} representation of a {@link Instant} object, * using the time-zone offset for UTC, {@code ZoneOffset.UTC}. */ - public static final Function RFC3339_DATE_FORMATTER = date -> - DATE_TIME_FORMATTER.format(date.toInstant().atOffset(ZoneOffset.UTC)); + public static final Function RFC3339_DATE_FORMATTER = date -> + DATE_TIME_FORMATTER.format(date.atOffset(ZoneOffset.UTC)); /** - * Parse a {@link String} object into a {@link Date} using the time-zone + * Parse a {@link String} object into a {@link Instant} using the time-zone * offset for UTC, {@code ZoneOffset.UTC}, and the system default time-zone, * {@code ZoneId.systemDefault()}. *

@@ -69,7 +67,7 @@ public final class NotificationMessage extends NetconfMessage { * caller. *

*/ - public static final Function RFC3339_DATE_PARSER = time -> { + public static final Function RFC3339_DATE_PARSER = time -> { try { final ZonedDateTime localDateTime = ZonedDateTime.parse(time, DATE_TIME_FORMATTER); final int startAt = 0; @@ -95,9 +93,9 @@ public final class NotificationMessage extends NetconfMessage { reminderBuilder.toString()); } - return Date.from(Instant.from(localDateTime)); + return Instant.from(localDateTime); } catch (DateTimeParseException exception) { - Date res = handlePotentialLeapSecond(time); + final var res = handlePotentialLeapSecond(time); if (res != null) { return res; } @@ -111,10 +109,10 @@ public final class NotificationMessage extends NetconfMessage { * conversion is applied, replacing the second-of-minute of 60 with 59. * * @param time {@link String} representation of a time - * @return {@code null} if time isn't ISO compliant or if the time doesn't have a leap second - * else a {@link Date} as per as the RFC3339_DATE_PARSER. + * @return {@code null} if time isn't ISO compliant or if the time doesn't have a leap second else an + * {@link Instant} as per as the RFC3339_DATE_PARSER. */ - private static Date handlePotentialLeapSecond(final String time) { + private static Instant handlePotentialLeapSecond(final String time) { // Parse the string from offset 0, so we get the whole value. final int offset = 0; final TemporalAccessor parsed = DATE_TIME_FORMATTER.parseUnresolved(time, new ParsePosition(offset)); @@ -166,22 +164,21 @@ public final class NotificationMessage extends NetconfMessage { return accessor.isSupported(field) ? (int) accessor.getLong(field) : 0; } - private final Date eventTime; + private final @NonNull Instant eventTime; /** * Create new notification and capture the timestamp in the constructor. */ public NotificationMessage(final Document notificationContent) { - this(notificationContent, new Date()); + this(notificationContent, Instant.now()); } /** * Create new notification with provided timestamp. */ - @SuppressFBWarnings("EI_EXPOSE_REP2") // stores a reference to an externally mutable Date object - public NotificationMessage(final Document notificationContent, final Date eventTime) { + public NotificationMessage(final Document notificationContent, final Instant eventTime) { super(wrapNotification(notificationContent, eventTime)); - this.eventTime = eventTime; + this.eventTime = requireNonNull(eventTime); } /** @@ -189,30 +186,25 @@ public final class NotificationMessage extends NetconfMessage { * * @return notification event time */ - @SuppressFBWarnings("EI_EXPOSE_REP") - public Date getEventTime() { + public @NonNull Instant getEventTime() { return eventTime; } - private static Document wrapNotification(final Document notificationContent, final Date eventTime) { + private static Document wrapNotification(final Document notificationContent, final Instant eventTime) { requireNonNull(notificationContent); requireNonNull(eventTime); final Element baseNotification = notificationContent.getDocumentElement(); final Element entireNotification = notificationContent.createElementNS( - XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_CAPABILITY_NOTIFICATION_1_0, NOTIFICATION_TAG); + NamespaceURN.NOTIFICATION, XmlNetconfConstants.NOTIFICATION_ELEMENT_NAME); entireNotification.appendChild(baseNotification); final Element eventTimeElement = notificationContent.createElementNS( - XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_CAPABILITY_NOTIFICATION_1_0, EVENT_TIME_TAG); - eventTimeElement.setTextContent(getSerializedEventTime(eventTime)); + NamespaceURN.NOTIFICATION, XmlNetconfConstants.EVENT_TIME); + eventTimeElement.setTextContent(RFC3339_DATE_FORMATTER.apply(eventTime)); entireNotification.appendChild(eventTimeElement); notificationContent.appendChild(entireNotification); return notificationContent; } - - private static String getSerializedEventTime(final Date eventTime) { - return RFC3339_DATE_FORMATTER.apply(eventTime); - } }