Add integer representation of version numbers for use in primitive revisions.

Change-Id: I16e29e3a2f0495e5135a5387249f042b82a466bc
diff --git a/core/api/src/main/java/org/onosproject/core/Version.java b/core/api/src/main/java/org/onosproject/core/Version.java
index a5cad64..ec0ea6d 100644
--- a/core/api/src/main/java/org/onosproject/core/Version.java
+++ b/core/api/src/main/java/org/onosproject/core/Version.java
@@ -24,7 +24,7 @@
 /**
  * Representation of the product version.
  */
-public final class Version {
+public final class Version implements Comparable<Version> {
 
     public static final String FORMAT_MINIMAL = "%d.%d";
     public static final String FORMAT_SHORT = "%d.%d.%s";
@@ -85,6 +85,26 @@
     }
 
     /**
+     * Returns an version from integer.
+     * <p>
+     * The version integer must be in the following format (big endian):
+     * <ul>
+     *     <li>8-bit unsigned major version</li>
+     *     <li>8-bit unsigned minor version</li>
+     *     <li>16-bit unsigned patch version</li>
+     * </ul>
+     *
+     * @param version the version integer
+     * @return the version instance
+     */
+    public static Version fromInt(int version) {
+        int major = (version >> 24) & 0xff;
+        int minor = (version >> 16) & 0xff;
+        int patch = (version) & 0xffff;
+        return new Version(major, minor, String.valueOf(patch), null);
+    }
+
+    /**
      * Returns the major version number.
      *
      * @return major version number
@@ -120,6 +140,37 @@
         return build;
     }
 
+    /**
+     * Returns an integer representation of the version.
+     * <p>
+     * The version integer can be used to compare two versions to one another.
+     * The integer representation of the version number is in the following format (big endian):
+     * <ul>
+     *     <li>8-bit unsigned major version</li>
+     *     <li>8-bit unsigned minor version</li>
+     *     <li>16-bit unsigned patch version</li>
+     * </ul>
+     * If the {@link #patch()} is not a number, it will default to {@code 0}.
+     *
+     * @return an integer representation of the version
+     */
+    public int toInt() {
+        byte major = (byte) this.major;
+        byte minor = (byte) this.minor;
+        short patch;
+        try {
+            patch = (short) Integer.parseInt(this.patch);
+        } catch (NumberFormatException e) {
+            patch = 0;
+        }
+        return major << 24 | (minor & 0xff) << 16 | (patch & 0xffff);
+    }
+
+    @Override
+    public int compareTo(Version other) {
+        return Integer.compare(toInt(), other.toInt());
+    }
+
     @Override
     public String toString() {
         return format;
diff --git a/core/api/src/test/java/org/onosproject/VersionTest.java b/core/api/src/test/java/org/onosproject/VersionTest.java
index cf5bb32..1c972ee 100644
--- a/core/api/src/test/java/org/onosproject/VersionTest.java
+++ b/core/api/src/test/java/org/onosproject/VersionTest.java
@@ -15,6 +15,10 @@
  */
 package org.onosproject;
 
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
 import com.google.common.testing.EqualsTester;
 import org.junit.Test;
 import org.onosproject.core.Version;
@@ -71,6 +75,52 @@
         assertEquals("wrong patch", null, v.patch());
         assertEquals("wrong build", null, v.build());
     }
+
+    @Test
+    public void testToFromInt() {
+        Version version1;
+        Version version2;
+
+        version1 = version("1.2");
+        version2 = Version.fromInt(version1.toInt());
+        assertEquals(version2, version(1, 2, "0", null));
+
+        version1 = version("1.2.foo.bar");
+        version2 = Version.fromInt(version1.toInt());
+        assertEquals(version2, version(1, 2, "0", null));
+
+        version1 = version("1.2.3");
+        version2 = Version.fromInt(version1.toInt());
+        assertEquals(version2, version(1, 2, "3", null));
+
+        version1 = version("255.254.65535.252");
+        version2 = Version.fromInt(version1.toInt());
+        assertEquals(version2, version(255, 254, "65535", null));
+
+        assertTrue(version("0.0.2").toInt() > version("0.0.1").toInt());
+        assertTrue(version("0.1.0").toInt() > version("0.0.1").toInt());
+        assertTrue(version("1.0.0").toInt() > version("0.1.0").toInt());
+        assertTrue(version("1.1.0").toInt() > version("1.0.1").toInt());
+        assertTrue(version("2.1.1").toInt() > version("1.10.10").toInt());
+    }
+
+    @Test
+    public void testOrder() {
+        List<Version> versions = Arrays.asList(
+            version("0.1.0"),
+            version("1.0.1"),
+            version("0.0.1"),
+            version("1.0.0"),
+            version("1.1.1"));
+        Collections.sort(versions);
+        assertEquals(versions, Arrays.asList(
+            version("0.0.1"),
+            version("0.1.0"),
+            version("1.0.0"),
+            version("1.0.1"),
+            version("1.1.1")));
+    }
+
     @Test
     public void testEquals() {
         new EqualsTester()