Segment Routing ONS demo:Traffic visualization fixes

Change-Id: Ib4960a0fe4666b8027614c8dd04df7883746b9b9
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/impl/PortStatisticsManager.java b/incubator/net/src/main/java/org/onosproject/incubator/net/impl/PortStatisticsManager.java
index c403bb9..1a61548 100644
--- a/incubator/net/src/main/java/org/onosproject/incubator/net/impl/PortStatisticsManager.java
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/impl/PortStatisticsManager.java
@@ -80,10 +80,19 @@
         long now = System.currentTimeMillis();
 
         if (c != null && p != null && (now - c.time < STALE_LIMIT)) {
-            if ((c.time > p.time + SECOND) &&
-                (c.stats.bytesSent() >= p.stats.bytesSent())) {
-                return new DefaultLoad(c.stats.bytesSent(), p.stats.bytesSent(),
-                                       (int) (c.time - p.time) / SECOND);
+            if (c.time > p.time + SECOND) {
+                //Use max of either Tx or Rx load as the total load of a port
+                Load load = null;
+                if (c.stats.bytesSent() >= p.stats.bytesSent()) {
+                    load = new DefaultLoad(c.stats.bytesSent(), p.stats.bytesSent(),
+                                                    (int) (c.time - p.time) / SECOND);
+                }
+                if (c.stats.bytesReceived() >= p.stats.bytesReceived()) {
+                    Load rcvLoad = new DefaultLoad(c.stats.bytesReceived(), p.stats.bytesReceived(),
+                                                    (int) (c.time - p.time) / SECOND);
+                    load = ((load == null) || (rcvLoad.rate() > load.rate())) ? rcvLoad : load;
+                }
+                return load;
             }
         }
         return null;
diff --git a/tools/test/topos/cord.json b/tools/test/topos/cord.json
index aba8276..418a6b4 100644
--- a/tools/test/topos/cord.json
+++ b/tools/test/topos/cord.json
@@ -10,13 +10,15 @@
         { "uri": "of:00010001e88b27e3", "mac": "0001e88b27e3", "annotations": { "name": "s108", "latitude": 41.996877, "longitude":  -84.169273 }}
     ],
     "hosts": [
-        { "id": "00:1f:29:e1:2c:36/-1", "mac": "00:1f:29:e1:2c:36", "vlan": -1, "location": "of:00010001e88b9368/46", "ip": "10.200.1.11", "annotations": { "name": "h1", "latitude": 34.384830, "longitude": -104.019262 }},
-        { "id": "00:1e:0b:cb:9d:64/-1", "mac": "00:1e:0b:cb:9d:64", "vlan": -1, "location": "of:00010001e88b9368/45", "ip": "10.200.1.12", "annotations": { "name": "h2", "latitude": 34.384830, "longitude": -104.019262 }},
-        { "id": "00:1c:c4:6b:d8:12/-1", "mac": "00:1c:c4:6b:d8:12", "vlan": -1, "location": "of:00010001e88b939b/46", "ip": "10.200.2.21", "annotations": { "name": "h3", "latitude": 34.133376, "longitude":  -89.442032 }},
-        { "id": "00:1e:0b:ca:90:44/-1", "mac": "00:1e:0b:ca:90:44", "vlan": -1, "location": "of:00010001e88b939b/45", "ip": "10.200.2.22", "annotations": { "name": "h4", "latitude": 34.133376, "longitude":  -89.442032 }},
-        { "id": "00:1e:0b:ca:e5:1a/-1", "mac": "00:1e:0b:ca:e5:1a", "vlan": -1, "location": "of:00010001e88b938c/46", "ip": "10.200.3.31", "annotations": { "name": "h5", "latitude": 34.166625, "longitude":  -91.983293 }},
-        { "id": "52:1C:5E:A5:8C:E0/-1", "mac": "52:1C:5E:A5:8C:E0", "vlan": -1, "location": "of:00010001e88b938c/45", "ip": "10.200.3.32", "annotations": { "name": "h6", "latitude": 34.166625, "longitude":  -91.983293 }},
-        { "id": "00:1e:0b:cb:3f:ac/-1", "mac": "00:1e:0b:cb:3f:ac", "vlan": -1, "location": "of:00010001e88b93ad/46", "ip": "10.200.4.41", "annotations": { "name": "h7", "latitude": 34.225065, "longitude":  -97.492882 }},
-        { "id": "EA:11:0C:F7:49:D8/-1", "mac": "EA:11:0C:F7:49:D8", "vlan": -1, "location": "of:00010001e88b93ad/45", "ip": "10.200.4.42", "annotations": { "name": "h8", "latitude": 34.249236, "longitude":  -84.253025 }}
+        { "id": "00:1f:29:e1:2c:36/-1", "mac": "00:1f:29:e1:2c:36", "vlan": -1, "location": "of:00010001e88b9368/46", "ip": "10.200.1.11", "annotations": { "name": "h11", "latitude": 34.384830, "longitude": -104.019262 }},
+        { "id": "00:1e:0b:cb:9d:64/-1", "mac": "00:1e:0b:cb:9d:64", "vlan": -1, "location": "of:00010001e88b9368/45", "ip": "10.200.1.12", "annotations": { "name": "h12", "latitude": 34.384830, "longitude": -104.019262 }},
+        { "id": "00:1c:c4:6b:d8:12/-1", "mac": "00:1c:c4:6b:d8:12", "vlan": -1, "location": "of:00010001e88b939b/46", "ip": "10.200.2.21", "annotations": { "name": "h21", "latitude": 34.133376, "longitude":  -89.442032 }},
+        { "id": "00:1e:0b:ca:90:44/-1", "mac": "00:1e:0b:ca:90:44", "vlan": -1, "location": "of:00010001e88b939b/45", "ip": "10.200.2.22", "annotations": { "name": "h22", "latitude": 34.133376, "longitude":  -89.442032 }},
+        { "id": "00:1e:0b:ca:e5:1a/-1", "mac": "00:1e:0b:ca:e5:1a", "vlan": -1, "location": "of:00010001e88b938c/46", "ip": "10.200.3.31", "annotations": { "name": "h31", "latitude": 34.166625, "longitude":  -91.983293 }},
+        { "id": "2c:44:fd:e0:90:40/-1", "mac": "2c:44:fd:e0:90:40", "vlan": -1, "location": "of:00010001e88b938c/45", "ip": "10.200.3.32", "annotations": { "name": "h32", "latitude": 34.166625, "longitude":  -91.983293 }},
+        { "id": "2c:44:fd:e0:90:44/-1", "mac": "2c:44:fd:e0:90:44", "vlan": -1, "location": "of:00010001e88b938c/47", "ip": "10.200.3.33", "annotations": { "name": "h33", "latitude": 34.166625, "longitude":  -91.983293 }},
+        { "id": "00:1e:0b:cb:3f:ac/-1", "mac": "00:1e:0b:cb:3f:ac", "vlan": -1, "location": "of:00010001e88b93ad/46", "ip": "10.200.4.41", "annotations": { "name": "h41", "latitude": 34.225065, "longitude":  -97.492882 }},
+        { "id": "c8:1f:66:e2:9b:d0/-1", "mac": "c8:1f:66:e2:9b:d0", "vlan": -1, "location": "of:00010001e88b93ad/45", "ip": "10.200.4.42", "annotations": { "name": "h42", "latitude": 34.249236, "longitude":  -84.253025 }},
+        { "id": "c8:1f:66:e2:9b:d2/-1", "mac": "c8:1f:66:e2:9b:d2", "vlan": -1, "location": "of:00010001e88b93ad/47", "ip": "10.200.4.43", "annotations": { "name": "h43", "latitude": 34.249236, "longitude":  -84.253025 }}
     ]
 }
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 }
     ]
   }
 }