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;
*
*/
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;
/**
* 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()));
}
/**
doc.appendChild(notificationElement);
final Element eventTimeElement = doc.createElement("eventTime");
- eventTimeElement.setTextContent(toRFC3339(new Date()));
+ eventTimeElement.setTextContent(toRFC3339(Instant.now()));
notificationElement.appendChild(eventTimeElement);
return notificationElement;
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");
+++ /dev/null
-/*
- * 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");
-}
*/
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;
*
* @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 =
* 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;
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;
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;
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);
}
}