Add uptimes to device and cluster REST APIs

Change-Id: I0ccdf4e33135be4bcfd1674a76ff4b39e992268b
diff --git a/core/api/src/main/java/org/onosproject/cluster/ClusterService.java b/core/api/src/main/java/org/onosproject/cluster/ClusterService.java
index 1a75e5f..54f23cd 100644
--- a/core/api/src/main/java/org/onosproject/cluster/ClusterService.java
+++ b/core/api/src/main/java/org/onosproject/cluster/ClusterService.java
@@ -20,6 +20,7 @@
 import java.util.Set;
 
 import org.joda.time.DateTime;
+import org.onlab.util.Tools;
 import org.onosproject.core.Version;
 import org.onosproject.event.ListenerService;
 
@@ -82,6 +83,21 @@
     }
 
     /**
+     * Returns a human readable form of the system time when the availability state was last updated.
+     *
+     * @param nodeId controller node identifier
+     * @return human readable string for system time when the availability state was last updated.
+     */
+    default String localStatus(NodeId nodeId) {
+        Instant lastUpdated = getLastUpdatedInstant(nodeId);
+        String timeAgo = "Never";
+        if (lastUpdated != null) {
+            timeAgo = Tools.timeAgo(lastUpdated.toEpochMilli());
+        }
+        return timeAgo;
+    }
+
+    /**
      * Returns the system time when the availability state was last updated.
      *
      * @param nodeId controller node identifier
diff --git a/core/api/src/main/java/org/onosproject/net/device/DeviceService.java b/core/api/src/main/java/org/onosproject/net/device/DeviceService.java
index 232809b..f86a98a 100644
--- a/core/api/src/main/java/org/onosproject/net/device/DeviceService.java
+++ b/core/api/src/main/java/org/onosproject/net/device/DeviceService.java
@@ -185,4 +185,14 @@
      */
     String localStatus(DeviceId deviceId);
 
+
+    /**
+     * Indicates how long ago the device connected or disconnected from this
+     * controller instance as a time offset.
+     *
+     * @param deviceId device identifier
+     * @return time offset in miliseconds
+     */
+    long getLastUpdatedInstant(DeviceId deviceId);
+
 }
diff --git a/core/api/src/main/java/org/onosproject/net/device/DeviceServiceAdapter.java b/core/api/src/main/java/org/onosproject/net/device/DeviceServiceAdapter.java
index 125cbcc..f85d198 100644
--- a/core/api/src/main/java/org/onosproject/net/device/DeviceServiceAdapter.java
+++ b/core/api/src/main/java/org/onosproject/net/device/DeviceServiceAdapter.java
@@ -142,4 +142,9 @@
         return null;
     }
 
+    @Override
+    public long getLastUpdatedInstant(DeviceId deviceId) {
+        return 0;
+    }
+
 }
diff --git a/core/api/src/main/java/org/onosproject/net/utils/ForwardingDeviceService.java b/core/api/src/main/java/org/onosproject/net/utils/ForwardingDeviceService.java
index acca07f..3a3eea2 100644
--- a/core/api/src/main/java/org/onosproject/net/utils/ForwardingDeviceService.java
+++ b/core/api/src/main/java/org/onosproject/net/utils/ForwardingDeviceService.java
@@ -127,4 +127,9 @@
         return delegate.localStatus(deviceId);
     }
 
+    @Override
+    public long getLastUpdatedInstant(DeviceId deviceId) {
+        return delegate.getLastUpdatedInstant(deviceId);
+    }
+
 }
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/ControllerNodeCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/ControllerNodeCodec.java
index f6206b9..07d8e45 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/ControllerNodeCodec.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/ControllerNodeCodec.java
@@ -40,7 +40,9 @@
                 .put("id", node.id().toString())
                 .put("ip", node.ip().toString())
                 .put("tcpPort", node.tcpPort())
-                .put("status", service.getState(node.id()).toString());
+                .put("status", service.getState(node.id()).toString())
+                .put("lastUpdate", Long.toString(service.getLastUpdatedInstant(node.id()).toEpochMilli()))
+                .put("humanReadableLastUpdate", service.localStatus(node.id()));
     }
 
 
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/DeviceCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/DeviceCodec.java
index 029549e..d34092a 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/DeviceCodec.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/DeviceCodec.java
@@ -47,7 +47,8 @@
     private static final String SERIAL = "serial";
     private static final String CHASSIS_ID = "chassisId";
     private static final String DRIVER = "driver";
-
+    private static final String LAST_UPDATE = "lastUpdate";
+    private static final String HUMAN_READABLE_LAST_UPDATE = "humanReadableLastUpdate";
 
     @Override
     public ObjectNode encode(Device device, CodecContext context) {
@@ -64,7 +65,9 @@
                 .put(SW, device.swVersion())
                 .put(SERIAL, device.serialNumber())
                 .put(DRIVER, driveService.getDriver(device.id()).name())
-                .put(CHASSIS_ID, device.chassisId().toString());
+                .put(CHASSIS_ID, device.chassisId().toString())
+                .put(LAST_UPDATE, Long.toString(service.getLastUpdatedInstant(device.id())))
+                .put(HUMAN_READABLE_LAST_UPDATE, service.localStatus(device.id()));
         return annotate(result, device, context);
     }
 
diff --git a/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java b/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java
index 02290e6..8359bdf 100644
--- a/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java
+++ b/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java
@@ -341,6 +341,15 @@
         return (ls.connected) ? "connected " + timeAgo : "disconnected " + timeAgo;
     }
 
+    @Override
+    public long getLastUpdatedInstant(DeviceId deviceId) {
+        LocalStatus ls = deviceLocalStatus.get(deviceId);
+        if (ls == null) {
+            return 0;
+        }
+        return ls.dateTime.toEpochMilli();
+    }
+
     // Check a device for control channel connectivity.
     private boolean isReachable(DeviceId deviceId) {
         if (deviceId == null) {