From be1fff9d605aa73518084c9f2971e7cec424ec2a Mon Sep 17 00:00:00 2001
From: Gregor Maier <gregor.maier@bigswitch.com>
Date: Thu, 20 Jun 2013 11:05:50 -0700
Subject: [PATCH] BSC-3926. Set switch's connectedSince when the switch is
 actually connected. Set to null otherwise.

---
 .../java/net/floodlightcontroller/core/OFSwitchBase.java | 9 +++++++--
 .../core/web/ControllerSwitchesResource.java             | 2 ++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/main/java/net/floodlightcontroller/core/OFSwitchBase.java b/src/main/java/net/floodlightcontroller/core/OFSwitchBase.java
index 6cdcf712c..75d900131 100644
--- a/src/main/java/net/floodlightcontroller/core/OFSwitchBase.java
+++ b/src/main/java/net/floodlightcontroller/core/OFSwitchBase.java
@@ -95,7 +95,8 @@ public abstract class OFSwitchBase implements IOFSwitch {
     protected IFloodlightProviderService floodlightProvider;
     protected IThreadPoolService threadPool;
     protected IDebugCounterService debugCounters;
-    protected Date connectedSince;
+    // FIXME: Don't use java.util.Date
+    protected volatile Date connectedSince;
 
     /* Switch features from initial featuresReply */
     protected int capabilities;
@@ -170,7 +171,7 @@ public abstract class OFSwitchBase implements IOFSwitch {
     public OFSwitchBase() {
         this.stringId = null;
         this.attributes = new ConcurrentHashMap<Object, Object>();
-        this.connectedSince = new Date();
+        this.connectedSince = null;
         this.transactionIdSource = new AtomicInteger();
         this.connected = false;
         this.statsFutureMap = new ConcurrentHashMap<Integer,OFStatisticsFuture>();
@@ -1094,6 +1095,10 @@ public abstract class OFSwitchBase implements IOFSwitch {
     @JsonIgnore
     public void setConnected(boolean connected) {
         // No lock needed since we use volatile
+        if (connected && this.connectedSince == null)
+            this.connectedSince = new Date();
+        else if (!connected)
+            this.connectedSince = null;
         this.connected = connected;
     }
 
diff --git a/src/main/java/net/floodlightcontroller/core/web/ControllerSwitchesResource.java b/src/main/java/net/floodlightcontroller/core/web/ControllerSwitchesResource.java
index b8d8e0bf9..eeaebe22c 100644
--- a/src/main/java/net/floodlightcontroller/core/web/ControllerSwitchesResource.java
+++ b/src/main/java/net/floodlightcontroller/core/web/ControllerSwitchesResource.java
@@ -92,6 +92,8 @@ public class ControllerSwitchesResource extends ServerResource {
         }
 
         public long getConnectedSince() {
+            if (sw.getConnectedSince() == null)
+                return 0;
             return sw.getConnectedSince().getTime();
         }
 
-- 
GitLab