From 27b690c42e6d0dc69c1dfcecc809e3c2b087da3d Mon Sep 17 00:00:00 2001
From: Ryan Izard <ryan.izard@bigswitch.com>
Date: Fri, 5 Aug 2016 18:05:40 -0400
Subject: [PATCH] AppCookie improvements, including freeing up previously
 reserved space for use by users

---
 .../core/util/AppCookie.java                  | 102 +++++-------------
 .../core/util/AppIDInUseException.java        |   6 +-
 .../util/AppIDNotRegisteredException.java     |   2 +-
 3 files changed, 33 insertions(+), 77 deletions(-)

diff --git a/src/main/java/net/floodlightcontroller/core/util/AppCookie.java b/src/main/java/net/floodlightcontroller/core/util/AppCookie.java
index d35e6a9e5..da7997570 100644
--- a/src/main/java/net/floodlightcontroller/core/util/AppCookie.java
+++ b/src/main/java/net/floodlightcontroller/core/util/AppCookie.java
@@ -36,48 +36,37 @@ import org.projectfloodlight.openflow.types.U64;
  *
  * The 64 bit OpenFlow cookie field used in the following way
  * <li> Bit 63 -- 52 (12 bit): the AppId
- * <li> Bit 51 -- 32 (20 bit): currently unused. Set to 0.
- * <li> Bit 31 -- 0  (32 bit): user data
+ * <li> Bit 51 -- 0  (52 bit): user data
  *
  * FIXME: The class should be a singleton. The registration method should
  * return an instance of class. This instance should then be used to generate
  * flow cookies. Ideally, we would also represent a flow cookie as a class
  * instance.
  *
- *
  * @author capveg
- *
  */
 
 public class AppCookie {
-    static final int APP_ID_BITS = 12;
-    static final long APP_ID_MASK = (1L << APP_ID_BITS) - 1;
-    static final int APP_ID_SHIFT = (64 - APP_ID_BITS);
-
-    static final long USER_MASK = 0x00000000FFFFFFFFL;
+    private static final int APP_ID_BITS = 12;
+    private static final long APP_ID_MASK = (1L << APP_ID_BITS) - 1;
+    private static final int APP_ID_SHIFT = 64 - APP_ID_BITS;
 
-    /**the following bit will be set accordingly if the field is rewritten by application. e.g. VRS or floating IP
-     * FIXME: these should not be in AppCookie and they shoul not use
-     * the reserved bit range*/
-    static final int SRC_MAC_REWRITE_BIT=33;
-    static final int DEST_MAC_REWRITE_BIT=34;
-    static final int SRC_IP_REWRITE_BIT=35;
-    static final int DEST_IP_REWRITE_BIT=36;
+    private static final long USER_MASK = 0x000FFFFFFFFFFFFFL;
 
-
-    static final long REWRITE_MASK= 0x000f00000000L;
-    private static ConcurrentMap<Integer, String> appIdMap =
-            new ConcurrentHashMap<Integer, String>();
+    private static ConcurrentMap<Long, String> appIdMap =
+            new ConcurrentHashMap<Long, String>();
 
     /**
-     * Returns a mask suitable for matching the App ID within a cookie.
+     * Returns a mask suitable for matching the app ID within a cookie.
+     * @return a mask representing the bits used for the app ID in the cookie
      */
     static public U64 getAppFieldMask() {
         return U64.of(APP_ID_MASK << APP_ID_SHIFT);
     }
 
     /**
-     * Returns a mask suitable for matching the User field within a cookie.
+     * Returns a mask suitable for matching the user field within a cookie.
+     * @return a mask representing the bits used for user data in the cookie
      */
     static public U64 getUserFieldMask() {
         return U64.of(USER_MASK);
@@ -92,76 +81,43 @@ public class AppCookie {
      * @throws IllegalStateException if the application has not been registered
      */
 
-    static public U64 makeCookie(int application, int user) {
+    static public U64 makeCookie(long application, long user) {
         if (!appIdMap.containsKey(application)) {
             throw new AppIDNotRegisteredException(application);
         }
-        long longApp = application;
-        long longUser = user & USER_MASK; // mask to prevent sign extend
-        return U64.of((longApp << APP_ID_SHIFT) | longUser);
+        user = user & USER_MASK; // mask to prevent sign extend
+        return U64.of((application << APP_ID_SHIFT) | user);
     }
 
     /**
-     * Extract the application id from a flow cookie. Does <em>not</em> check
-     * whether the application id is registered
+     * Extract the application id from a flow cookie. Does <em>not</em>
+     * check whether the application id is registered. The app ID is 
+     * defined by the {@link #getAppFieldMask()} bits
      * @param cookie
      * @return
      */
-    static public int extractApp(U64 cookie) {
-        return (int)((cookie.getValue() >>> APP_ID_SHIFT) & APP_ID_MASK);
+    static public long extractApp(U64 cookie) {
+        return (cookie.getValue() >>> APP_ID_SHIFT) & APP_ID_MASK;
     }
 
-    static public int extractUser(U64 cookie) {
-        return (int)(cookie.getValue() & USER_MASK);
+    /**
+     * Extract the user portion from a flow cookie, defined
+     * by the {@link #getUserFieldMask()} bits
+     * @param cookie
+     * @return
+     */
+    static public long extractUser(U64 cookie) {
+        return cookie.getValue() & USER_MASK;
     }
 
-    static public boolean isRewriteFlagSet(U64 cookie) {
-        if ((cookie.getValue() & REWRITE_MASK) !=0L)
-            return true;
-        return false;
-    }
-    static public boolean isSrcMacRewriteFlagSet(U64 cookie) {
-        if ((cookie.getValue() & (1L << (SRC_MAC_REWRITE_BIT-1))) !=0L)
-            return true;
-        return false;
-    }
-    static public boolean isDestMacRewriteFlagSet(U64 cookie) {
-        if ((cookie.getValue() & (1L << (DEST_MAC_REWRITE_BIT-1))) !=0L)
-            return true;
-        return false;
-    }
-    static public boolean isSrcIpRewriteFlagSet(U64 cookie) {
-        if ((cookie.getValue() & (1L << (SRC_IP_REWRITE_BIT-1))) !=0L)
-            return true;
-        return false;
-    }
-    static public boolean isDestIpRewriteFlagSet(U64 cookie) {
-        if ((cookie.getValue() & (1L << (DEST_IP_REWRITE_BIT-1))) !=0L)
-            return true;
-        return false;
-    }
-    static public U64 setSrcMacRewriteFlag(U64 cookie) {
-        return U64.of(cookie.getValue() | (1L << (SRC_MAC_REWRITE_BIT-1)));
-    }
-    static public U64 setDestMacRewriteFlag(U64 cookie) {
-        return U64.of(cookie.getValue() | (1L << (DEST_MAC_REWRITE_BIT-1)));
-    }
-    static public U64 setSrcIpRewriteFlag(U64 cookie) {
-        return U64.of(cookie.getValue() | (1L << (SRC_IP_REWRITE_BIT-1)));
-    }
-    static public U64 setDestIpRewriteFlag(U64 cookie) {
-        return U64.of(cookie.getValue() | (1L << (DEST_IP_REWRITE_BIT-1)));
-    }
     /**
      * A lame attempt to prevent duplicate application ID.
-     * TODO: We should expose appID->appName map
-     *       via REST API so CLI doesn't need a separate copy of the map.
      *
      * @param application
      * @param appName
      * @throws AppIDInUseException
      */
-    public static void registerApp(int application, String appName)
+    public static void registerApp(long application, String appName)
         throws AppIDException
     {
         if ((application & APP_ID_MASK) != application) {
@@ -179,7 +135,7 @@ public class AppCookie {
      * @param application
      * @return
      */
-    public static String getAppName(int application) {
+    public static String getAppName(long application) {
         return appIdMap.get(application);
     }
 }
diff --git a/src/main/java/net/floodlightcontroller/core/util/AppIDInUseException.java b/src/main/java/net/floodlightcontroller/core/util/AppIDInUseException.java
index 077def6a3..073fa941b 100644
--- a/src/main/java/net/floodlightcontroller/core/util/AppIDInUseException.java
+++ b/src/main/java/net/floodlightcontroller/core/util/AppIDInUseException.java
@@ -3,9 +3,9 @@ package net.floodlightcontroller.core.util;
 public class AppIDInUseException extends AppIDException {
     private static final long serialVersionUID = 3167241821651094997L;
 
-    public AppIDInUseException(int appId, String oldAppName,
+    public AppIDInUseException(long application, String oldAppName,
                                String newAppName) {
-        super(String.format("Tried to register application IdD %s for %s, but" +
-                "already registered for %s.", appId, oldAppName, newAppName));
+        super(String.format("Tried to register application ID %s for %s, but" +
+                "already registered for %s.", application, oldAppName, newAppName));
     }
 }
\ No newline at end of file
diff --git a/src/main/java/net/floodlightcontroller/core/util/AppIDNotRegisteredException.java b/src/main/java/net/floodlightcontroller/core/util/AppIDNotRegisteredException.java
index 4dab96101..b53638d26 100644
--- a/src/main/java/net/floodlightcontroller/core/util/AppIDNotRegisteredException.java
+++ b/src/main/java/net/floodlightcontroller/core/util/AppIDNotRegisteredException.java
@@ -3,7 +3,7 @@ package net.floodlightcontroller.core.util;
 public class AppIDNotRegisteredException extends AppIDException {
     private static final long serialVersionUID = -9195312786292237763L;
 
-    public AppIDNotRegisteredException(int application) {
+    public AppIDNotRegisteredException(long application) {
         super("Application Id " + application + " has not been registered");
     }
 
-- 
GitLab