Segment Routing ONS demo:Traffic visualization fixes

Change-Id: Ib4960a0fe4666b8027614c8dd04df7883746b9b9
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/TopologyViewMessageHandlerBase.java b/web/gui/src/main/java/org/onosproject/ui/impl/TopologyViewMessageHandlerBase.java
index 75d2994..0c466f8 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/TopologyViewMessageHandlerBase.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/TopologyViewMessageHandlerBase.java
@@ -117,17 +117,20 @@
             new ProviderId("core", "org.onosproject.core", true);
     private static final String COMPACT = "%s/%s-%s/%s";
 
-    private static final double KB = 1024;
-    private static final double MB = 1024 * KB;
-    private static final double GB = 1024 * MB;
+    private static final double KILO = 1024;
+    private static final double MEGA = 1024 * KILO;
+    private static final double GIGA = 1024 * MEGA;
 
-    // TODO: change GB to Gb (when we compute bits/second)
-    private static final String GB_UNIT = "GB";
-    private static final String MB_UNIT = "MB";
-    private static final String KB_UNIT = "KB";
-    private static final String B_UNIT = "B";
-
-    private static final double BPS_THRESHOLD = 4 * KB;
+    private static final String GBITS_UNIT = "Gb";
+    private static final String MBITS_UNIT = "Mb";
+    private static final String KBITS_UNIT = "Kb";
+    private static final String BITS_UNIT = "b";
+    private static final String GBYTES_UNIT = "GB";
+    private static final String MBYTES_UNIT = "MB";
+    private static final String KBYTES_UNIT = "KB";
+    private static final String BYTES_UNIT = "B";
+    //4 Kilo Bytes as threshold
+    private static final double BPS_THRESHOLD = 4 * KILO;
 
     protected ServiceDirectory directory;
     protected ClusterService clusterService;
@@ -576,13 +579,17 @@
                 link.addLoad(getLinkLoad(link.one));
                 link.addLoad(bi ? getLinkLoad(link.two) : null);
             } else if (type == PORT) {
-                link.addLoad(portStatsService.load(link.one.src()), BPS_THRESHOLD);
-                link.addLoad(portStatsService.load(link.one.dst()), BPS_THRESHOLD);
+                //For a bi-directional traffic links, use
+                //the max link rate of either direction
+                link.addLoad(portStatsService.load(link.one.src()),
+                             BPS_THRESHOLD,
+                             portStatsService.load(link.one.dst()),
+                             BPS_THRESHOLD);
             }
             if (link.hasTraffic) {
                 linksNodeT.add(compactLinkString(link.one));
                 labelsT.add(type == PORT ?
-                                    formatBytes(link.rate) + "ps" :
+                                    formatBitRate(link.rate) + "ps" :
                                     formatBytes(link.bytes));
             } else {
                 linksNodeN.add(compactLinkString(link.one));
@@ -745,21 +752,43 @@
 
     // Poor-mans formatting to get the labels with byte counts looking nice.
     private String formatBytes(long bytes) {
-        // TODO: multiply everything by 8 to compute bits/second
         String unit;
         double value;
-        if (bytes > GB) {
-            value = bytes / GB;
-            unit = GB_UNIT;
-        } else if (bytes > MB) {
-            value = bytes / MB;
-            unit = MB_UNIT;
-        } else if (bytes > KB) {
-            value = bytes / KB;
-            unit = KB_UNIT;
+        if (bytes > GIGA) {
+            value = bytes / GIGA;
+            unit = GBYTES_UNIT;
+        } else if (bytes > MEGA) {
+            value = bytes / MEGA;
+            unit = MBYTES_UNIT;
+        } else if (bytes > KILO) {
+            value = bytes / KILO;
+            unit = KBYTES_UNIT;
         } else {
             value = bytes;
-            unit = B_UNIT;
+            unit = BYTES_UNIT;
+        }
+        DecimalFormat format = new DecimalFormat("#,###.##");
+        return format.format(value) + " " + unit;
+    }
+
+    // Poor-mans formatting to get the labels with byte counts looking nice.
+    private String formatBitRate(long bytes) {
+        String unit;
+        double value;
+        //Convert to bits
+        long bits = bytes * 8;
+        if (bits > GIGA) {
+            value = bits / GIGA;
+            unit = GBITS_UNIT;
+        } else if (bits > MEGA) {
+            value = bits / MEGA;
+            unit = MBITS_UNIT;
+        } else if (bits > KILO) {
+            value = bits / KILO;
+            unit = KBITS_UNIT;
+        } else {
+            value = bits;
+            unit = BITS_UNIT;
         }
         DecimalFormat format = new DecimalFormat("#,###.##");
         return format.format(value) + " " + unit;
@@ -832,6 +861,26 @@
             }
         }
 
+        void addLoad(Load srcLinkLoad,
+                     double srcLinkThreshold,
+                     Load dstLinkLoad,
+                     double dstLinkThreshold) {
+            //use the max of link load at source or destination
+            if (srcLinkLoad != null) {
+                this.hasTraffic = hasTraffic || srcLinkLoad.rate() > srcLinkThreshold;
+                this.bytes = srcLinkLoad.latest();
+                this.rate = srcLinkLoad.rate();
+            }
+
+            if (dstLinkLoad != null) {
+                if (dstLinkLoad.rate() > this.rate) {
+                    this.bytes = dstLinkLoad.latest();
+                    this.rate = dstLinkLoad.rate();
+                    this.hasTraffic = hasTraffic || dstLinkLoad.rate() > dstLinkThreshold;
+                }
+            }
+        }
+
         void addClass(String trafficClass) {
             classes.add(trafficClass);
         }
diff --git a/web/gui/src/main/webapp/app/view/topo/topo.css b/web/gui/src/main/webapp/app/view/topo/topo.css
index 515db25..b987e77 100644
--- a/web/gui/src/main/webapp/app/view/topo/topo.css
+++ b/web/gui/src/main/webapp/app/view/topo/topo.css
@@ -462,40 +462,40 @@
     stroke: rgba(121,231,158,0.5);
 }
 
-/* Port traffic color visualization for KBps, MBps, and GBps */
+/* Port traffic color visualization for Kbps, Mbps, and Gbps */
 
-.light #ov-topo svg .link.secondary.port-traffic-KBps {
+.light #ov-topo svg .link.secondary.port-traffic-Kbps {
     stroke: rgb(0,153,51);
     stroke-width: 5.0;
 }
-.dark #ov-topo svg .link.secondary.port-traffic-KBps {
+.dark #ov-topo svg .link.secondary.port-traffic-Kbps {
     stroke: rgb(98, 153, 118);
     stroke-width: 5.0;
 }
 
-.light #ov-topo svg .link.secondary.port-traffic-MBps {
+.light #ov-topo svg .link.secondary.port-traffic-Mbps {
     stroke: rgb(128,145,27);
     stroke-width: 6.5;
 }
-.dark #ov-topo svg .link.secondary.port-traffic-MBps {
+.dark #ov-topo svg .link.secondary.port-traffic-Mbps {
     stroke: rgb(91, 109, 54);
     stroke-width: 6.5;
 }
 
-.light #ov-topo svg .link.secondary.port-traffic-GBps {
+.light #ov-topo svg .link.secondary.port-traffic-Gbps {
     stroke: rgb(255, 137, 3);
     stroke-width: 8.0;
 }
-.dark #ov-topo svg .link.secondary.port-traffic-GBps {
+.dark #ov-topo svg .link.secondary.port-traffic-Gbps {
     stroke: rgb(174, 119, 55);
     stroke-width: 8.0;
 }
 
-.light #ov-topo svg .link.secondary.port-traffic-GBps-choked {
+.light #ov-topo svg .link.secondary.port-traffic-Gbps-choked {
     stroke: rgb(183, 30, 21);
     stroke-width: 8.0;
 }
-.dark #ov-topo svg .link.secondary.port-traffic-GBps-choked {
+.dark #ov-topo svg .link.secondary.port-traffic-Gbps-choked {
     stroke: rgb(127, 40, 39);
     stroke-width: 8.0;
 }
diff --git a/web/gui/src/main/webapp/app/view/topo/topoTraffic.js b/web/gui/src/main/webapp/app/view/topo/topoTraffic.js
index 80d0b4b..7332ad0 100644
--- a/web/gui/src/main/webapp/app/view/topo/topoTraffic.js
+++ b/web/gui/src/main/webapp/app/view/topo/topoTraffic.js
@@ -69,8 +69,7 @@
                     ldata.el.classed(p.class, true);
                     ldata.label = lab;
 
-                    // TODO: change this to 'bps' when we measure bits/sec
-                    if (fs.endsWith(lab, 'Bps')) {
+                    if (fs.endsWith(lab, 'bps')) {
                         // inject additional styling for port-based traffic
                         units = lab.substring(lab.length-4);
                         portcls = 'port-traffic-' + units;
diff --git a/web/gui/src/main/webapp/data/sprites/segmentRouting.json b/web/gui/src/main/webapp/data/sprites/segmentRouting.json
index 6b56b2f..58d4702 100644
--- a/web/gui/src/main/webapp/data/sprites/segmentRouting.json
+++ b/web/gui/src/main/webapp/data/sprites/segmentRouting.json
@@ -46,7 +46,7 @@
       { "id": "rack", "pos":[800,600], "class":"blue1" }
     ],
     "labels": [
-      { "pos":[550,80], "text":"Segment Routing Demo", "class":"blue1", "size":1.4 }
+      { "pos":[550,80], "text":"", "class":"blue1", "size":1.4 }
     ]
   }
 }