cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jaya...@apache.org
Subject [1/2] git commit: updated refs/heads/4.3 to d825b3d
Date Thu, 26 Dec 2013 11:26:57 GMT
Updated Branches:
  refs/heads/4.3 fd831ff2c -> d825b3dd1


CLOUDSTACK-4622:If a VM from guest network is added to network tier of VPC then IP reservation
allows the CIDR to be a superset of Network CIDR for that VPC tier

Signed-off-by: Jayapal <jayapal@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/d825b3dd
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/d825b3dd
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/d825b3dd

Branch: refs/heads/4.3
Commit: d825b3dd141c76c0af8d02bac2147378a3506e96
Parents: 749c77a
Author: Saksham Srivastava <saksham.srivastava@citrix.com>
Authored: Fri Sep 13 14:45:35 2013 +0530
Committer: Jayapal <jayapal@apache.org>
Committed: Thu Dec 26 16:40:00 2013 +0530

----------------------------------------------------------------------
 utils/src/com/cloud/utils/net/NetUtils.java     | 23 ++++++++++++++++++--
 .../test/com/cloud/utils/net/NetUtilsTest.java  | 15 +++++++++++++
 2 files changed, 36 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d825b3dd/utils/src/com/cloud/utils/net/NetUtils.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/net/NetUtils.java b/utils/src/com/cloud/utils/net/NetUtils.java
index a315b93..db5365a 100755
--- a/utils/src/com/cloud/utils/net/NetUtils.java
+++ b/utils/src/com/cloud/utils/net/NetUtils.java
@@ -845,13 +845,32 @@ public class NetUtils {
     }
 
     public static boolean isNetworkAWithinNetworkB(String cidrA, String cidrB) {
+        // This utility returns true if IP range of cidrA is same or lies completely in cidrB
+        // Returns true if networkA is same as networkB or networkA is a subset of networkB
         Long[] cidrALong = cidrToLong(cidrA);
         Long[] cidrBLong = cidrToLong(cidrB);
         if (cidrALong == null || cidrBLong == null) {
             return false;
         }
-        long shift = 32 - cidrBLong[1];
-        return ((cidrALong[0] >> shift) == (cidrBLong[0] >> shift));
+        if (isSameIpRange(cidrA, cidrB)) {
+            return true;
+        }
+        String[] cidrPairFirst = cidrA.split("\\/");
+        String[] cidrPairSecond = cidrB.split("\\/");
+
+        Long networkSizeFirst = Long.valueOf(cidrPairFirst[1]);
+        Long networkSizeSecond = Long.valueOf(cidrPairSecond[1]);
+        String ipRangeFirst [] = NetUtils.getIpRangeFromCidr(cidrPairFirst[0], networkSizeFirst);
+        String ipRangeSecond [] = NetUtils.getIpRangeFromCidr(cidrPairFirst[0], networkSizeSecond);
+
+        long startIpFirst = NetUtils.ip2Long(ipRangeFirst[0]);
+        long endIpFirst = NetUtils.ip2Long(ipRangeFirst[1]);
+        long startIpSecond = NetUtils.ip2Long(ipRangeSecond[0]);
+        long endIpSecond = NetUtils.ip2Long(ipRangeSecond[1]);
+
+        if((startIpFirst >= startIpSecond) && (endIpFirst <= endIpSecond))
+            return true;
+        return false;
     }
 
     public static Long[] cidrToLong(String cidr) {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d825b3dd/utils/test/com/cloud/utils/net/NetUtilsTest.java
----------------------------------------------------------------------
diff --git a/utils/test/com/cloud/utils/net/NetUtilsTest.java b/utils/test/com/cloud/utils/net/NetUtilsTest.java
index 3cfc98f..121d489 100644
--- a/utils/test/com/cloud/utils/net/NetUtilsTest.java
+++ b/utils/test/com/cloud/utils/net/NetUtilsTest.java
@@ -173,4 +173,19 @@ public class NetUtilsTest extends TestCase {
     public void testGetLocalIPString() {
         assertNotNull(NetUtils.getLocalIPString());
     }
+
+    public void testSubnet() {
+        //Test to check if a cidr is a part of another cidr
+        //Test 2 same cidrs
+        assertTrue(NetUtils.isNetworkAWithinNetworkB("10.1.1.0/25", "10.1.1.0/25"));
+        //Tests when cidrA is smaller than cidrB
+        assertTrue(NetUtils.isNetworkAWithinNetworkB("10.1.1.0/26", "10.1.1.0/25"));
+        assertTrue(NetUtils.isNetworkAWithinNetworkB("10.1.1.0/25", "10.1.1.0/24"));
+        assertTrue(NetUtils.isNetworkAWithinNetworkB("10.1.1.0/23", "10.1.1.0/22"));
+        assertTrue(NetUtils.isNetworkAWithinNetworkB("192.168.0.0/16" , "192.168.0.0/15"));
+        //Tests when cidrA is larger than cidrB
+        assertFalse(NetUtils.isNetworkAWithinNetworkB("10.1.1.0/26", "10.1.1.0/27"));
+        assertFalse(NetUtils.isNetworkAWithinNetworkB("10.1.1.0/24", "10.1.1.0/25"));
+        assertFalse(NetUtils.isNetworkAWithinNetworkB("10.1.1.0/22", "10.1.1.0/23"));
+    }
 }


Mime
View raw message