Implement compareTo for TableId to avoid IllegalArgumentException

java.lang.IllegalArgumentException: Comparison method violates its general contract!

Change-Id: I4033e9a6743f134583eab936de2b960d76274919
diff --git a/core/api/src/main/java/org/onosproject/net/flow/IndexTableId.java b/core/api/src/main/java/org/onosproject/net/flow/IndexTableId.java
index fab2463..7b49d98 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/IndexTableId.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/IndexTableId.java
@@ -32,6 +32,16 @@
         return Type.INDEX;
     }
 
+    @Override
+    public int compareTo(TableId other) {
+        if (this.type() != other.type()) {
+            return this.type().compareTo(other.type());
+        } else {
+            IndexTableId indexTableId = (IndexTableId) other;
+            return this.id() - indexTableId.id();
+        }
+    }
+
     /**
      * Returns a table identifier for the given index.
      *
diff --git a/core/api/src/main/java/org/onosproject/net/flow/TableId.java b/core/api/src/main/java/org/onosproject/net/flow/TableId.java
index bb2dda1..fb115f6 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/TableId.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/TableId.java
@@ -36,5 +36,18 @@
         PIPELINE_INDEPENDENT
     }
 
+    /**
+     * Gets type of this table ID.
+     *
+     * @return type
+     */
     Type type();
+
+    /**
+     * Compares table ID.
+     *
+     * @param other table ID to be compared
+     * @return zero if the table IDs are the same. Otherwise, return a non-zero integer
+     */
+    int compareTo(TableId other);
 }
diff --git a/core/api/src/main/java/org/onosproject/net/pi/model/PiTableId.java b/core/api/src/main/java/org/onosproject/net/pi/model/PiTableId.java
index 2e799a9..f817e31 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/model/PiTableId.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/model/PiTableId.java
@@ -49,4 +49,16 @@
     public Type type() {
         return Type.PIPELINE_INDEPENDENT;
     }
+
+    @Override
+    public int compareTo(TableId other) {
+        if (this.type() != other.type()) {
+            return this.type().compareTo(other.type());
+        } else {
+            PiTableId piTableId = (PiTableId) other;
+            checkNotNull(this.identifier, "PiTableId identifier should not be null");
+            checkNotNull(piTableId.identifier, "PiTableId identifier should not be null");
+            return this.identifier.compareTo(piTableId.identifier);
+        }
+    }
 }
diff --git a/core/common/src/main/java/org/onosproject/utils/Comparators.java b/core/common/src/main/java/org/onosproject/utils/Comparators.java
index b509012..20a4497 100644
--- a/core/common/src/main/java/org/onosproject/utils/Comparators.java
+++ b/core/common/src/main/java/org/onosproject/utils/Comparators.java
@@ -62,14 +62,14 @@
     public static final Comparator<FlowRule> FLOW_RULE_COMPARATOR =
             (f1, f2) -> {
                 // Compare table IDs in ascending order
-                int tableCompare = f1.tableId() - f2.tableId();
+                int tableCompare = f1.table().compareTo(f2.table());
                 if (tableCompare != 0) {
                     return tableCompare;
                 }
                 // Compare priorities in descending order
                 int priorityCompare = f2.priority() - f1.priority();
                 return (priorityCompare == 0)
-                        ? Long.valueOf(f1.id().value()).compareTo(f2.id().value())
+                        ? Long.compare(f1.id().value(), f2.id().value())
                         : priorityCompare;
             };