cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject [21/23] git commit: updated refs/heads/hotfix/CLOUDSTACK-7776 to 9e89a28
Date Thu, 23 Oct 2014 20:12:05 GMT
CLOUDSTACK-7776 extract cidr change from update network method

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

Branch: refs/heads/hotfix/CLOUDSTACK-7776
Commit: 55a07e985b0589b44ff472413449a72de3a50718
Parents: f5036e9
Author: Daan Hoogland <dhoogland@schubergphilis.com>
Authored: Thu Oct 23 17:20:08 2014 +0200
Committer: Daan Hoogland <daan@onecht.net>
Committed: Thu Oct 23 21:14:05 2014 +0200

----------------------------------------------------------------------
 .../com/cloud/network/NetworkServiceImpl.java   | 202 ++++++++++---------
 1 file changed, 107 insertions(+), 95 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/55a07e98/server/src/com/cloud/network/NetworkServiceImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/NetworkServiceImpl.java b/server/src/com/cloud/network/NetworkServiceImpl.java
index 5bf9d5c..4852b9e 100755
--- a/server/src/com/cloud/network/NetworkServiceImpl.java
+++ b/server/src/com/cloud/network/NetworkServiceImpl.java
@@ -2010,101 +2010,7 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService
{
             restartNetwork = true;
         }
 
-        //IP reservation checks
-        // allow reservation only to Isolated Guest networks
-        DataCenter dc = _dcDao.findById(network.getDataCenterId());
-        String networkCidr = network.getNetworkCidr();
-
-        if (guestVmCidr != null) {
-            if (dc.getNetworkType() == NetworkType.Basic) {
-                throw new InvalidParameterValueException("Guest VM CIDR can't be specified
for zone with " + NetworkType.Basic  + " networking");
-            }
-            if (network.getGuestType() != GuestType.Isolated) {
-                throw new InvalidParameterValueException("Can only allow IP Reservation in
networks with guest type " + GuestType.Isolated);
-            }
-            if (networkOfferingChanged == true) {
-                throw new InvalidParameterValueException("Cannot specify this nework offering
change and guestVmCidr at same time. Specify only one.");
-            }
-            if (!(network.getState() == Network.State.Implemented)) {
-                throw new InvalidParameterValueException("The network must be in " + Network.State.Implemented
+ " state. IP Reservation cannot be applied in "
-                        + network.getState() + " state");
-            }
-            if (!NetUtils.isValidCIDR(guestVmCidr)) {
-                throw new InvalidParameterValueException("Invalid format of Guest VM CIDR.");
-            }
-            if (!NetUtils.validateGuestCidr(guestVmCidr)) {
-                throw new InvalidParameterValueException("Invalid format of Guest VM CIDR.
Make sure it is RFC1918 compliant. ");
-            }
-
-            // If networkCidr is null it implies that there was no prior IP reservation,
so the network cidr is network.getCidr()
-            // But in case networkCidr is a non null value (IP reservation already exists),
it implies network cidr is networkCidr
-            if (networkCidr != null) {
-                if (!NetUtils.isNetworkAWithinNetworkB(guestVmCidr, networkCidr)) {
-                    throw new InvalidParameterValueException("Invalid value of Guest VM CIDR.
For IP Reservation, Guest VM CIDR  should be a subset of network CIDR : "
-                            + networkCidr);
-                }
-            } else {
-                if (!NetUtils.isNetworkAWithinNetworkB(guestVmCidr, network.getCidr())) {
-                    throw new InvalidParameterValueException("Invalid value of Guest VM CIDR.
For IP Reservation, Guest VM CIDR  should be a subset of network CIDR :  "
-                            + network.getCidr());
-                }
-            }
-
-            // This check makes sure there are no active IPs existing outside the guestVmCidr
in the network
-                String[] guestVmCidrPair = guestVmCidr.split("\\/");
-                Long size = Long.valueOf(guestVmCidrPair[1]);
-                List<NicVO> nicsPresent = _nicDao.listByNetworkId(networkId);
-
-                String cidrIpRange[] = NetUtils.getIpRangeFromCidr(guestVmCidrPair[0], size);
-            s_logger.info("The start IP of the specified guest vm cidr is: " + cidrIpRange[0]
+ " and end IP is: " + cidrIpRange[1]);
-                long startIp = NetUtils.ip2Long(cidrIpRange[0]);
-                long endIp = NetUtils.ip2Long(cidrIpRange[1]);
-                long range =  endIp - startIp + 1;
-                s_logger.info("The specified guest vm cidr has " +  range + " IPs");
-
-                for (NicVO nic : nicsPresent) {
-                    long nicIp = NetUtils.ip2Long(nic.getIp4Address());
-                    //check if nic IP is outside the guest vm cidr
-                    if (nicIp < startIp || nicIp > endIp) {
-                    if (!(nic.getState() == Nic.State.Deallocating)) {
-                            throw new InvalidParameterValueException("Active IPs like " +
nic.getIp4Address() + " exist outside the Guest VM CIDR. Cannot apply reservation ");
-                            }
-                        }
-                    }
-
-                // In some scenarios even though guesVmCidr and network CIDR do not appear
similar but
-                // the IP ranges exactly matches, in these special cases make sure no Reservation
gets applied
-                if (network.getNetworkCidr() == null) {
-                    if (NetUtils.isSameIpRange(guestVmCidr, network.getCidr()) &&
!guestVmCidr.equals(network.getCidr())) {
-                    throw new InvalidParameterValueException("The Start IP and End IP of
guestvmcidr: " + guestVmCidr + " and CIDR: " + network.getCidr() + " are same, "
-                            + "even though both the cidrs appear to be different. As a precaution
no IP Reservation will be applied.");
-                    }
-                } else {
-                if (NetUtils.isSameIpRange(guestVmCidr, network.getNetworkCidr()) &&
!guestVmCidr.equals(network.getNetworkCidr())) {
-                    throw new InvalidParameterValueException("The Start IP and End IP of
guestvmcidr: " + guestVmCidr + " and Network CIDR: " + network.getNetworkCidr()
-                            + " are same, "
-                            + "even though both the cidrs appear to be different. As a precaution
IP Reservation will not be affected. If you want to reset IP Reservation, "
-                            + "specify guestVmCidr to be: " + network.getNetworkCidr());
-                    }
-                }
-
-                // When reservation is applied for the first time, network_cidr will be null
-                // Populate it with the actual network cidr
-                if (network.getNetworkCidr() == null) {
-                    network.setNetworkCidr(network.getCidr());
-                }
-
-                // Condition for IP Reservation reset : guestVmCidr and network CIDR are
same
-                if (network.getNetworkCidr().equals(guestVmCidr)) {
-                    s_logger.warn("Guest VM CIDR and Network CIDR both are same, reservation
will reset.");
-                    network.setNetworkCidr(null);
-                }
-                // Finally update "cidr" with the guestVmCidr
-                // which becomes the effective address space for CloudStack guest VMs
-                network.setCidr(guestVmCidr);
-                _networksDao.update(networkId, network);
-                s_logger.info("IP Reservation has been applied. The new CIDR for Guests Vms
is " + guestVmCidr);
-            }
+        changeCidrWhenValid(guestVmCidr, networkOfferingChanged, network);
 
         ReservationContext context = new ReservationContextImpl(null, null, callerUser, callerAccount);
         // 1) Shutdown all the elements and cleanup all the rules. Don't allow to shutdown
network in intermediate
@@ -2237,6 +2143,112 @@ public class NetworkServiceImpl extends ManagerBase implements  NetworkService
{
     }
 
     /**
+     * @param guestVmCidr
+     * @param networkOfferingChanged
+     * @param network
+     * @throws InvalidParameterValueException
+     * @throws NumberFormatException
+     */
+    private void changeCidrWhenValid(String guestVmCidr, boolean networkOfferingChanged,
final NetworkVO network)
+            throws InvalidParameterValueException, NumberFormatException {
+        //IP reservation checks
+        // allow reservation only to Isolated Guest networks
+        DataCenter dc = _dcDao.findById(network.getDataCenterId());
+        String networkCidr = network.getNetworkCidr();
+
+        if (guestVmCidr != null) {
+            if (dc.getNetworkType() == NetworkType.Basic) {
+                throw new InvalidParameterValueException("Guest VM CIDR can't be specified
for zone with " + NetworkType.Basic  + " networking");
+            }
+            if (network.getGuestType() != GuestType.Isolated) {
+                throw new InvalidParameterValueException("Can only allow IP Reservation in
networks with guest type " + GuestType.Isolated);
+            }
+            if (networkOfferingChanged == true) {
+                throw new InvalidParameterValueException("Cannot specify this nework offering
change and guestVmCidr at same time. Specify only one.");
+            }
+            if (!(network.getState() == Network.State.Implemented)) {
+                throw new InvalidParameterValueException("The network must be in " + Network.State.Implemented
+ " state. IP Reservation cannot be applied in "
+                        + network.getState() + " state");
+            }
+            if (!NetUtils.isValidCIDR(guestVmCidr)) {
+                throw new InvalidParameterValueException("Invalid format of Guest VM CIDR.");
+            }
+            if (!NetUtils.validateGuestCidr(guestVmCidr)) {
+                throw new InvalidParameterValueException("Invalid format of Guest VM CIDR.
Make sure it is RFC1918 compliant. ");
+            }
+
+            // If networkCidr is null it implies that there was no prior IP reservation,
so the network cidr is network.getCidr()
+            // But in case networkCidr is a non null value (IP reservation already exists),
it implies network cidr is networkCidr
+            if (networkCidr != null) {
+                if (!NetUtils.isNetworkAWithinNetworkB(guestVmCidr, networkCidr)) {
+                    throw new InvalidParameterValueException("Invalid value of Guest VM CIDR.
For IP Reservation, Guest VM CIDR  should be a subset of network CIDR : "
+                            + networkCidr);
+                }
+            } else {
+                if (!NetUtils.isNetworkAWithinNetworkB(guestVmCidr, network.getCidr())) {
+                    throw new InvalidParameterValueException("Invalid value of Guest VM CIDR.
For IP Reservation, Guest VM CIDR  should be a subset of network CIDR :  "
+                            + network.getCidr());
+                }
+            }
+
+            // This check makes sure there are no active IPs existing outside the guestVmCidr
in the network
+            String[] guestVmCidrPair = guestVmCidr.split("\\/");
+            Long size = Long.valueOf(guestVmCidrPair[1]);
+            List<NicVO> nicsPresent = _nicDao.listByNetworkId(network.getId());
+
+            String cidrIpRange[] = NetUtils.getIpRangeFromCidr(guestVmCidrPair[0], size);
+            s_logger.info("The start IP of the specified guest vm cidr is: " + cidrIpRange[0]
+ " and end IP is: " + cidrIpRange[1]);
+            long startIp = NetUtils.ip2Long(cidrIpRange[0]);
+            long endIp = NetUtils.ip2Long(cidrIpRange[1]);
+            long range =  endIp - startIp + 1;
+            s_logger.info("The specified guest vm cidr has " +  range + " IPs");
+
+            for (NicVO nic : nicsPresent) {
+                long nicIp = NetUtils.ip2Long(nic.getIp4Address());
+                //check if nic IP is outside the guest vm cidr
+                if (nicIp < startIp || nicIp > endIp) {
+                    if (!(nic.getState() == Nic.State.Deallocating)) {
+                        throw new InvalidParameterValueException("Active IPs like " + nic.getIp4Address()
+ " exist outside the Guest VM CIDR. Cannot apply reservation ");
+                    }
+                }
+            }
+
+            // In some scenarios even though guesVmCidr and network CIDR do not appear similar
but
+            // the IP ranges exactly matches, in these special cases make sure no Reservation
gets applied
+            if (network.getNetworkCidr() == null) {
+                if (NetUtils.isSameIpRange(guestVmCidr, network.getCidr()) && !guestVmCidr.equals(network.getCidr()))
{
+                    throw new InvalidParameterValueException("The Start IP and End IP of
guestvmcidr: " + guestVmCidr + " and CIDR: " + network.getCidr() + " are same, "
+                            + "even though both the cidrs appear to be different. As a precaution
no IP Reservation will be applied.");
+                }
+            } else {
+                if (NetUtils.isSameIpRange(guestVmCidr, network.getNetworkCidr()) &&
!guestVmCidr.equals(network.getNetworkCidr())) {
+                    throw new InvalidParameterValueException("The Start IP and End IP of
guestvmcidr: " + guestVmCidr + " and Network CIDR: " + network.getNetworkCidr()
+                            + " are same, "
+                            + "even though both the cidrs appear to be different. As a precaution
IP Reservation will not be affected. If you want to reset IP Reservation, "
+                            + "specify guestVmCidr to be: " + network.getNetworkCidr());
+                }
+            }
+
+            // When reservation is applied for the first time, network_cidr will be null
+            // Populate it with the actual network cidr
+            if (network.getNetworkCidr() == null) {
+                network.setNetworkCidr(network.getCidr());
+            }
+
+            // Condition for IP Reservation reset : guestVmCidr and network CIDR are same
+            if (network.getNetworkCidr().equals(guestVmCidr)) {
+                s_logger.warn("Guest VM CIDR and Network CIDR both are same, reservation
will reset.");
+                network.setNetworkCidr(null);
+            }
+            // Finally update "cidr" with the guestVmCidr
+            // which becomes the effective address space for CloudStack guest VMs
+            network.setCidr(guestVmCidr);
+            _networksDao.update(network.getId(), network);
+            s_logger.info("IP Reservation has been applied. The new CIDR for Guests Vms is
" + guestVmCidr);
+        }
+    }
+
+    /**
      * @param networkOffering
      * @param oldNtwkOff
      * @param network


Mime
View raw message