Added ability to configure GUI traffic refresh rate

Change-Id: Iceb1196a20c16f09218218da2e095fbe9644bb04
diff --git a/core/api/src/main/java/org/onosproject/ui/topo/AbstractTopoMonitor.java b/core/api/src/main/java/org/onosproject/ui/topo/AbstractTopoMonitor.java
index ac00e60..2b1dd08 100644
--- a/core/api/src/main/java/org/onosproject/ui/topo/AbstractTopoMonitor.java
+++ b/core/api/src/main/java/org/onosproject/ui/topo/AbstractTopoMonitor.java
@@ -21,6 +21,35 @@
  */
 public class AbstractTopoMonitor {
 
+    /**
+     * Creates a new topo monitor base.
+     */
+    protected AbstractTopoMonitor() {
+    }
+
+    /**
+     * Number of milliseconds between invocations of sending traffic data.
+     */
+    protected static long trafficPeriod = 5000;
+
+    /**
+     * Sets the traffic refresh period in milliseconds.
+     *
+     * @param ms refresh rate in millis
+     */
+    public static void setTrafficPeriod(long ms) {
+        trafficPeriod = ms;
+    }
+
+    /**
+     * Returns the traffic refresh period in milliseconds.
+     *
+     * @return refresh rate in millis
+     */
+    public static long getTrafficPeriod() {
+        return trafficPeriod;
+    }
+
     // TODO: pull common code up into this class
 
     // Note to Andrea:
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/OsgiPropertyConstants.java b/web/gui/src/main/java/org/onosproject/ui/impl/OsgiPropertyConstants.java
new file mode 100644
index 0000000..9a03f1f
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/OsgiPropertyConstants.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.ui.impl;
+
+public final class OsgiPropertyConstants {
+    private OsgiPropertyConstants() {
+    }
+
+    public static final String TRAFFIC_REFRESH_MS = "trafficRefreshMs";
+    public static final int TRAFFIC_REFRESH_MS_DEFAULT = 5000;
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/ProtectedIntentMonitor.java b/web/gui/src/main/java/org/onosproject/ui/impl/ProtectedIntentMonitor.java
index bb4e8ac..2fe3552 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/ProtectedIntentMonitor.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/ProtectedIntentMonitor.java
@@ -96,7 +96,6 @@
         SELECTED_INTENT
     }
 
-    private final long trafficPeriod;
     private final ServicesBundle services;
     private final TopologyViewMessageHandler msgHandler;
 
@@ -110,13 +109,11 @@
     /**
      * Constructs a protected intent monitor.
      *
-     * @param trafficPeriod traffic task period in ms
      * @param services      bundle of services
      * @param msgHandler    our message handler
      */
-    public ProtectedIntentMonitor(long trafficPeriod, ServicesBundle services,
+    public ProtectedIntentMonitor(ServicesBundle services,
                                   TopologyViewMessageHandler msgHandler) {
-        this.trafficPeriod = trafficPeriod;
         this.services = services;
         this.msgHandler = msgHandler;
     }
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/TopologyViewMessageHandler.java b/web/gui/src/main/java/org/onosproject/ui/impl/TopologyViewMessageHandler.java
index cae4760..02bc1ff 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/TopologyViewMessageHandler.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/TopologyViewMessageHandler.java
@@ -173,7 +173,6 @@
 
     private static final String SLASH = "/";
 
-    private static final long TRAFFIC_PERIOD = 5000;
     private static final long SUMMARY_PERIOD = 30000;
 
     private static final Comparator<? super ControllerNode> NODE_COMPARATOR =
@@ -214,8 +213,8 @@
     public void init(UiConnection connection, ServiceDirectory directory) {
         super.init(connection, directory);
         appId = directory.get(CoreService.class).registerApplication(MY_APP_ID);
-        traffic = new TrafficMonitor(TRAFFIC_PERIOD, services, this);
-        protectedIntentMonitor = new ProtectedIntentMonitor(TRAFFIC_PERIOD, services, this);
+        traffic = new TrafficMonitor(services, this);
+        protectedIntentMonitor = new ProtectedIntentMonitor(services, this);
     }
 
     @Override
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/TrafficMonitor.java b/web/gui/src/main/java/org/onosproject/ui/impl/TrafficMonitor.java
index b585ffc..73bda39 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/TrafficMonitor.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/TrafficMonitor.java
@@ -59,13 +59,11 @@
     /**
      * Constructs a traffic monitor.
      *
-     * @param trafficPeriod  traffic task period in ms
      * @param servicesBundle bundle of services
      * @param msgHandler     our message handler
      */
-    public TrafficMonitor(long trafficPeriod, ServicesBundle servicesBundle,
-                          TopologyViewMessageHandler msgHandler) {
-        super(trafficPeriod, servicesBundle, msgHandler);
+    public TrafficMonitor(ServicesBundle servicesBundle, TopologyViewMessageHandler msgHandler) {
+        super(servicesBundle, msgHandler);
         this.msgHandler = msgHandler;
 
     }
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/TrafficMonitorBase.java b/web/gui/src/main/java/org/onosproject/ui/impl/TrafficMonitorBase.java
index ae3c909..c585d92 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/TrafficMonitorBase.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/TrafficMonitorBase.java
@@ -292,10 +292,6 @@
         CUSTOM_TRAFFIC_MONITOR
     }
 
-    /**
-     * Number of milliseconds between invocations of sending traffic data.
-     */
-    protected final long trafficPeriod;
 
     /**
      * Holds references to services.
@@ -314,14 +310,11 @@
      * Constructs the monitor, initializing the task period and
      * services bundle reference.
      *
-     * @param trafficPeriod  traffic task period in ms
      * @param servicesBundle bundle of services
      * @param msgHandler Traffic Message handler
      */
-    protected TrafficMonitorBase(long trafficPeriod,
-                                 ServicesBundle servicesBundle,
+    protected TrafficMonitorBase(ServicesBundle servicesBundle,
                                  TopoologyTrafficMessageHandlerAbstract msgHandler) {
-        this.trafficPeriod = trafficPeriod;
         this.services = servicesBundle;
         this.msgHandler = msgHandler;
         timer = new Timer("uiTopo-" + getClass().getSimpleName());
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java b/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java
index 73de4c1..d47367b 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java
@@ -33,6 +33,7 @@
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import org.onlab.util.Tools;
+import org.onosproject.cfg.ComponentConfigService;
 import org.onosproject.mastership.MastershipService;
 import org.onosproject.store.serializers.KryoNamespaces;
 import org.onosproject.store.service.ConsistentMap;
@@ -61,9 +62,12 @@
 import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
 import org.onosproject.ui.lion.LionBundle;
 import org.onosproject.ui.lion.LionUtils;
+import org.onosproject.ui.topo.AbstractTopoMonitor;
+import org.osgi.service.component.ComponentContext;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Modified;
 import org.osgi.service.component.annotations.Reference;
 import org.osgi.service.component.annotations.ReferenceCardinality;
 import org.slf4j.Logger;
@@ -71,6 +75,7 @@
 
 import java.math.BigInteger;
 import java.security.SecureRandom;
+import java.util.Dictionary;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Locale;
@@ -88,13 +93,23 @@
 import static org.onosproject.security.AppPermission.Type.UI_WRITE;
 import static org.onosproject.ui.UiView.Category.NETWORK;
 import static org.onosproject.ui.UiView.Category.PLATFORM;
+import static org.onosproject.ui.impl.OsgiPropertyConstants.TRAFFIC_REFRESH_MS;
+import static org.onosproject.ui.impl.OsgiPropertyConstants.TRAFFIC_REFRESH_MS_DEFAULT;
 import static org.onosproject.ui.impl.lion.BundleStitcher.generateBundles;
 
 /**
  * Manages the user interface extensions.
  */
-@Component(immediate = true, service = { UiExtensionService.class, UiPreferencesService.class, SpriteService.class,
-        UiTokenService.class })
+@Component(immediate = true,
+        service = {
+                UiExtensionService.class,
+                UiPreferencesService.class,
+                SpriteService.class,
+                UiTokenService.class
+        },
+        property = {
+                TRAFFIC_REFRESH_MS + ":Integer=" + TRAFFIC_REFRESH_MS_DEFAULT,
+        })
 public class UiExtensionManager
         implements UiExtensionService, UiPreferencesService, SpriteService,
         UiTokenService {
@@ -156,6 +171,9 @@
     protected StorageService storageService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected ComponentConfigService cfgService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
     private UiSharedTopologyModel sharedModel;
 
     // User preferences
@@ -178,6 +196,7 @@
 
     private LionBundle navLion;
 
+    protected int trafficRefreshMs = TRAFFIC_REFRESH_MS_DEFAULT;
 
     private String lionNavText(String id) {
         return navLion.getValue("nav_item_" + id);
@@ -281,7 +300,7 @@
 
 
     @Activate
-    public void activate() {
+    public void activate(ComponentContext context) {
         Serializer serializer = Serializer.using(KryoNamespaces.API,
                      ObjectNode.class, ArrayNode.class,
                      JsonNodeFactory.class, LinkedHashMap.class,
@@ -303,6 +322,7 @@
                 .withRelaxedReadConsistency()
                 .build();
         tokens = tokensConsistentMap.asJavaMap();
+        cfgService.registerProperties(getClass());
 
         register(core);
 
@@ -310,7 +330,8 @@
     }
 
     @Deactivate
-    public void deactivate() {
+    public void deactivate(ComponentContext context) {
+        cfgService.unregisterProperties(getClass(), false);
         prefsConsistentMap.removeListener(prefsListener);
         eventHandlingExecutor.shutdown();
         UiWebSocketServlet.closeAll();
@@ -318,6 +339,20 @@
         log.info("Stopped");
     }
 
+    @Modified
+    protected void modified(ComponentContext context) {
+        Dictionary<?, ?> properties = context.getProperties();
+        Integer trafficRefresh = Tools.getIntegerProperty(properties, TRAFFIC_REFRESH_MS);
+
+        if (trafficRefresh != null && trafficRefresh > 10) {
+            AbstractTopoMonitor.setTrafficPeriod(trafficRefresh);
+        } else if (trafficRefresh != null) {
+            log.warn("trafficRefresh must be greater than 10");
+        }
+
+        log.info("Settings: trafficRefresh={}", trafficRefresh);
+    }
+
     @Override
     public synchronized void register(UiExtension extension) {
         checkPermission(UI_WRITE);
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/Topo2TrafficMessageHandler.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/Topo2TrafficMessageHandler.java
index 5f5d84d..a6e0e9d 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/Topo2TrafficMessageHandler.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/Topo2TrafficMessageHandler.java
@@ -80,9 +80,6 @@
     private static final String SRC = "src";
     private static final String DST = "dst";
 
-    // configuration parameters
-    private static final long TRAFFIC_PERIOD = 5000;
-
     protected ServicesBundle services;
     private static final String MY_APP_ID = "org.onosproject.gui";
     private ApplicationId appId;
@@ -96,7 +93,7 @@
         super.init(connection, directory);
         appId = directory.get(CoreService.class).registerApplication(MY_APP_ID);
         services = new ServicesBundle(directory);
-        traffic2 = new Traffic2Monitor(TRAFFIC_PERIOD, services, this);
+        traffic2 = new Traffic2Monitor(services, this);
         topoSession = ((UiWebSocket) connection).topoSession();
     }
 
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/Traffic2Monitor.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/Traffic2Monitor.java
index 6a41cf2..1c0fb81 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/Traffic2Monitor.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/Traffic2Monitor.java
@@ -48,13 +48,12 @@
     /**
      * Constructs a traffic monitor.
      *
-     * @param trafficPeriod  traffic task period in ms
      * @param servicesBundle bundle of services
      * @param msgHandler     our message handler
      */
-    public Traffic2Monitor(long trafficPeriod, ServicesBundle servicesBundle,
+    public Traffic2Monitor(ServicesBundle servicesBundle,
                            Topo2TrafficMessageHandler msgHandler) {
-        super(trafficPeriod, servicesBundle, msgHandler);
+        super(servicesBundle, msgHandler);
         this.msgHandler = msgHandler;
     }