From 8a912034eb219c38f0343c60b055ee7f8fead231 Mon Sep 17 00:00:00 2001
From: Kanzhe Jiang <kanzhe.jiang@bigswitch.com>
Date: Tue, 6 Mar 2012 15:07:26 -0800
Subject: [PATCH] Add per-port broadcast cache hit counter

---
 .../floodlightcontroller/core/IOFSwitch.java  | 12 +++++++++---
 .../core/internal/OFSwitchImpl.java           | 19 +++++++++++++++++--
 2 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/src/main/java/net/floodlightcontroller/core/IOFSwitch.java b/src/main/java/net/floodlightcontroller/core/IOFSwitch.java
index 5c6b7b62d..d3781cb58 100644
--- a/src/main/java/net/floodlightcontroller/core/IOFSwitch.java
+++ b/src/main/java/net/floodlightcontroller/core/IOFSwitch.java
@@ -300,10 +300,16 @@ public interface IOFSwitch {
     public void clearAllFlowMods();
     
     /**
-     * Return a TimedHashMap associated with the switch
+     * Update broadcast cache
      * @param data
+     * @return true if there is a cache hit
+     *         false if there is no cache hit.
+     */
+    public boolean updateBroadcastCache(Long entry, Short port);
+    
+    /**
+     * Get the portBroadcastCacheHits
      * @return
      */
-    public TimedCache<Long> getTimedCache();
-
+    public Map<Short, Long> getPortBroadcastHits();
 }
diff --git a/src/main/java/net/floodlightcontroller/core/internal/OFSwitchImpl.java b/src/main/java/net/floodlightcontroller/core/internal/OFSwitchImpl.java
index f623b2b21..96016b09d 100644
--- a/src/main/java/net/floodlightcontroller/core/internal/OFSwitchImpl.java
+++ b/src/main/java/net/floodlightcontroller/core/internal/OFSwitchImpl.java
@@ -74,6 +74,7 @@ public class OFSwitchImpl implements IOFSwitch {
     protected Map<Integer,OFStatisticsFuture> statsFutureMap;
     protected boolean connected;
     protected TimedCache<Long> timedCache;
+    protected ConcurrentMap<Short, Long> portBroadcastCacheHitMap;
     protected ReentrantReadWriteLock lock;
     
     public static IOFSwitchFeatures switchFeatures;
@@ -90,6 +91,7 @@ public class OFSwitchImpl implements IOFSwitch {
         this.connected = true;
         this.statsFutureMap = new ConcurrentHashMap<Integer,OFStatisticsFuture>();
         this.timedCache = new TimedCache<Long>(100, 5*1000 );  // 5 seconds interval
+        this.portBroadcastCacheHitMap = new ConcurrentHashMap<Short, Long>();
         this.lock = new ReentrantReadWriteLock();
         
         // Defaults properties for an ideal switch
@@ -361,10 +363,23 @@ public class OFSwitchImpl implements IOFSwitch {
     }
 
     @Override
-	public TimedCache<Long> getTimedCache() {
-        return timedCache;
+	public boolean updateBroadcastCache(Long entry, Short port) {
+        if (timedCache.update(entry)) {
+        	Long count = portBroadcastCacheHitMap.putIfAbsent(port, new Long(1));
+        	if (count != null) {
+        		count++;
+        	}
+        	return true;
+        } else {
+        	return false;
+        }
 	}
 
+    @Override
+    public Map<Short, Long> getPortBroadcastHits() {
+    	return this.portBroadcastCacheHitMap;
+    }
+    
     /**
      * Return a lock that need to be held while processing a message. Multiple threads
      * can hold this lock. 
-- 
GitLab