Improve performance of notification emitting in netconf testtool 26/23826/2
authorMaros Marsalek <mmarsale@cisco.com>
Mon, 8 Jun 2015 15:22:45 +0000 (17:22 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 22 Jul 2015 08:13:53 +0000 (08:13 +0000)
- pre-parse all notifications beforehand
- remove unwanted outputs

Change-Id: I6dd823786fd0d5f9ab04943633f8aa3b61a540e8
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
(cherry picked from commit 7392728edfe0b150833c7af47c3bc8d4b0ce101a)

opendaylight/netconf/netconf-testtool/src/main/java/org/opendaylight/controller/netconf/test/tool/rpc/SimulatedCreateSubscription.java

index abf51c476462996e9e4c709626893412c3a1995c..2aa92b8339386950aa486b6cf22511dde076cad8 100644 (file)
@@ -10,11 +10,14 @@ package org.opendaylight.controller.netconf.test.tool.rpc;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps;
 import java.io.File;
 import java.io.IOException;
 import java.text.SimpleDateFormat;
+import java.util.Collections;
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
@@ -36,18 +39,33 @@ import org.xml.sax.SAXException;
 
 public class SimulatedCreateSubscription extends AbstractLastNetconfOperation implements DefaultNetconfOperation {
 
+    private final Map<Notification, NetconfMessage> notifications;
     private NetconfServerSession session;
-    private final Optional<Notifications> notifications;
     private ScheduledExecutorService scheduledExecutorService;
 
     public SimulatedCreateSubscription(final String id, final Optional<File> notificationsFile) {
         super(id);
+
+        Optional<Notifications> notifications;
+
         if(notificationsFile.isPresent()) {
             notifications = Optional.of(loadNotifications(notificationsFile.get()));
             scheduledExecutorService = Executors.newScheduledThreadPool(1);
         } else {
             notifications = Optional.absent();
         }
+
+        if(notifications.isPresent()) {
+            Map<Notification, NetconfMessage> preparedMessages = Maps.newHashMapWithExpectedSize(notifications.get().getNotificationList().size());
+            for (final Notification notification : notifications.get().getNotificationList()) {
+                final NetconfMessage parsedNotification = parseNetconfNotification(notification.getContent());
+                preparedMessages.put(notification, parsedNotification);
+            }
+            this.notifications = preparedMessages;
+        } else {
+            this.notifications = Collections.emptyMap();
+        }
+
     }
 
     private Notifications loadNotifications(final File file) {
@@ -72,37 +90,26 @@ public class SimulatedCreateSubscription extends AbstractLastNetconfOperation im
 
     @Override
     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws NetconfDocumentedException {
+        long delayAggregator = 0;
 
+        for (final Map.Entry<Notification, NetconfMessage> notification : notifications.entrySet()) {
+            for (int i = 0; i <= notification.getKey().getTimes(); i++) {
 
-        if(notifications.isPresent()) {
-            long delayAggregator = 0;
-            System.console().writer().println("Scheduling notifications " + notifications.get());
+                delayAggregator += notification.getKey().getDelayInSeconds();
 
-            for (final Notification notification : notifications.get().getNotificationList()) {
-                for (int i = 0; i <= notification.getTimes(); i++) {
-
-                    delayAggregator += notification.getDelayInSeconds();
-
-                    System.console().writer().println("Times " + notification.getTimes());
-                    scheduledExecutorService.schedule(new Runnable() {
-                        @Override
-                        public void run() {
-                            try {
-                                System.console().writer().println("Sending actual notification " + notification);
-                                Preconditions.checkState(session != null, "Session is not set, cannot process notifications");
-                                session.sendMessage(parseNetconfNotification(notification.getContent()));
-                            } catch (IOException | SAXException e) {
-                                throw new IllegalStateException("Unable to process notification " + notification, e);
-                            }
-                        }
-                    }, delayAggregator, TimeUnit.SECONDS);
-                }
+                scheduledExecutorService.schedule(new Runnable() {
+                    @Override
+                    public void run() {
+                        Preconditions.checkState(session != null, "Session is not set, cannot process notifications");
+                        session.sendMessage(notification.getValue());
+                    }
+                }, delayAggregator, TimeUnit.SECONDS);
             }
         }
         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
     }
 
-    private static NetconfMessage parseNetconfNotification(String content) throws IOException, SAXException {
+    private static NetconfMessage parseNetconfNotification(String content) {
         final int startEventTime = content.indexOf("<eventTime>") + "<eventTime>".length();
         final int endEventTime = content.indexOf("</eventTime>");
         final String eventTime = content.substring(startEventTime, endEventTime);
@@ -110,7 +117,11 @@ public class SimulatedCreateSubscription extends AbstractLastNetconfOperation im
             content = content.replace(eventTime, new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").format(new Date()));
         }
 
-        return new NetconfMessage(XmlUtil.readXmlToDocument(content));
+        try {
+            return new NetconfMessage(XmlUtil.readXmlToDocument(content));
+        } catch (SAXException | IOException e) {
+            throw new IllegalArgumentException("Cannot parse notifications", e);
+        }
     }
 
     @Override