Fixing an issue where stale port stats could be reported when collector stops or negative load reported when device counters overflow or glitch.

Change-Id: I9c7238758555a08fc3f02e6cded15fb4e211bf69
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 19b093c..ea2d2e3 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
@@ -49,7 +49,8 @@
 
     private final Logger log = getLogger(getClass());
 
-    private static final int SECOND = 1_000;
+    private static final int SECOND = 1_000; // milliseconds
+    private static final long STALE_LIMIT = 15_000; // milliseconds
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected DeviceService deviceService;
@@ -63,7 +64,6 @@
     public void activate() {
         deviceService.addListener(deviceListener);
         log.info("Started");
-
     }
 
     @Deactivate
@@ -76,7 +76,10 @@
     public Load load(ConnectPoint connectPoint) {
         DataPoint c = current.get(connectPoint);
         DataPoint p = previous.get(connectPoint);
-        if (c != null && p != null && (c.time > p.time + SECOND)) {
+        long now = System.currentTimeMillis();
+        if (c != null && p != null && (now - c.time < STALE_LIMIT) &&
+                (c.time > p.time + SECOND) &&
+                (c.stats.bytesSent() - p.stats.bytesSent() >= 0)) {
             return new DefaultLoad(c.stats.bytesSent(), p.stats.bytesSent(),
                                    (int) (c.time - p.time) / SECOND);
         }