Updated Branches:
  refs/heads/master d92a01336 -> febe7d9cf

Cloudstack-2621 [Multiple_IP_Ranges] Failed to delete guest IP range from a new 
subnet

Signed-off-by: Abhinandan Prateek <[email protected]>


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

Branch: refs/heads/master
Commit: 4d5c6c8381679ee3fb4ac918ca04eddf09812ab5
Parents: d92a013
Author: Bharat Kumar <[email protected]>
Authored: Mon Jun 17 14:55:13 2013 +0530
Committer: Abhinandan Prateek <[email protected]>
Committed: Thu Jun 20 10:40:23 2013 +0530

----------------------------------------------------------------------
 .../configuration/ConfigurationManagerImpl.java | 133 ++++++++++---------
 .../VirtualNetworkApplianceManagerImpl.java     |   3 +-
 2 files changed, 72 insertions(+), 64 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/4d5c6c83/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java 
b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
index e7e3f74..c117f3a 100755
--- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
+++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
@@ -2931,6 +2931,13 @@ public class ConfigurationManagerImpl extends 
ManagerBase implements Configurati
         return vlan;
     }
 
+    public boolean removeFromDb (long  vlanDbId){
+        if (!deletePublicIPRange(vlanDbId)) {
+            return false;
+        }
+        return  _vlanDao.expunge(vlanDbId);
+    }
+
     @Override
     @DB
     public boolean deleteVlanAndPublicIpRange(long userId, long vlanDbId, 
Account caller)  {
@@ -3004,49 +3011,60 @@ public class ConfigurationManagerImpl extends 
ManagerBase implements Configurati
                 Network network = 
_networkDao.findById(vlanRange.getNetworkId());
                 DhcpServiceProvider dhcpServiceProvider = 
_networkMgr.getDhcpServiceProvider(network);
                 if 
(!dhcpServiceProvider.getProvider().getName().equalsIgnoreCase(Provider.VirtualRouter.getName()))
 {
-                    if (!deletePublicIPRange(vlanDbId)) {
+                    Transaction txn = Transaction.currentTxn();
+                    txn.start();
+                    if (!removeFromDb(vlanDbId)) {
+                        txn.rollback();
                         return false;
                     }
-                    _vlanDao.expunge(vlanDbId);
-                    return  true;
-                }
-                //search if the vlan has any allocated ips.
-                boolean aliasIpBelongsToThisVlan = false;
-                long freeIpsInsubnet = 0;
-                NicIpAliasVO ipAlias = null;
-                allocIpCount = 
_publicIpAddressDao.countIPs(vlanRange.getDataCenterId(), vlanDbId, true);
-                if (allocIpCount > 1) {
-                    throw  new InvalidParameterValueException ("cannot delete 
this range as some of the vlans are in use.");
-                }
-                if (allocIpCount == 0){
-                    //remove the vlan range.
-                    if (!deletePublicIPRange(vlanDbId)) {
-                        return false;
+                    else {
+                        txn.commit();
                     }
-                    _vlanDao.expunge(vlanDbId);
-                    return true;
+                    txn.close();
                 }
-                //check if this allocated ip is being used as an ipAlias on 
the router.
+                else {
+                  return  handleIpAliasDeletion(vlanRange, vlanDbId, 
dhcpServiceProvider, network);
+                }
+            }
+        }
+        return true;
+    }
+
+    private boolean handleIpAliasDeletion(VlanVO vlanRange, long vlanDbId, 
DhcpServiceProvider dhcpServiceProvider, Network network) {
+        boolean result_final = false;
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+        IPAddressVO ip = null;
+        NicIpAliasVO ipAlias = null;
+        try{
+            Integer allocIpCount=0;
+            //search if the vlan has any allocated ips.
+            allocIpCount = 
_publicIpAddressDao.countIPs(vlanRange.getDataCenterId(), vlanDbId, true);
+            if (allocIpCount > 1) {
+                throw  new InvalidParameterValueException ("cannot delete this 
range as some of the vlans are in use.");
+            }
+            if (allocIpCount == 0){
+                result_final=true;
+            }
+            else {
                 ipAlias = 
_nicIpAliasDao.findByGatewayAndNetworkIdAndState(vlanRange.getVlanGateway(), 
vlanRange.getNetworkId(),  NicIpAlias.state.active);
+                ipAlias.setState(NicIpAlias.state.revoked);
+                _nicIpAliasDao.update(ipAlias.getId(), ipAlias);
                 //check if this ip belongs to this vlan and is allocated.
-                IPAddressVO ip = 
_publicIpAddressDao.findByIpAndVlanId(ipAlias.getIp4Address(), vlanDbId);
+                ip = 
_publicIpAddressDao.findByIpAndVlanId(ipAlias.getIp4Address(), vlanDbId);
                 if (ip != null && ip.getState() == IpAddress.State.Allocated) {
-                    aliasIpBelongsToThisVlan =true;
                     //check if there any other vlan ranges in the same subnet 
having free ips
                     List<VlanVO> vlanRanges = 
_vlanDao.listVlansByNetworkIdAndGateway(vlanRange.getNetworkId(), 
vlanRange.getVlanGateway());
                     //if there is no other vlanrage in this subnet. free the 
ip and delete the vlan.
                     if (vlanRanges.size() == 1){
                         boolean result = 
dhcpServiceProvider.removeDhcpSupportForSubnet(network);
                         if (result == false) {
+                            result_final = false;
                             s_logger.debug("Failed to delete the vlan range as 
we could not free the ip used to provide the dhcp service.");
                         }
                         else {
                             _publicIpAddressDao.unassignIpAddress(ip.getId());
-                            if (!deletePublicIPRange(vlanDbId)) {
-                                return false;
-                            }
-                            _vlanDao.expunge(vlanDbId);
-                            _nicIpAliasDao.expunge(ipAlias.getId());
+                            result_final = true;
                         }
                     } else {
                         // if there are more vlans in the subnet check if 
there are free ips.
@@ -3059,62 +3077,50 @@ public class ConfigurationManagerImpl extends 
ManagerBase implements Configurati
                         s_logger.info("vlan Range"+vlanRange.getId()+" id 
being deleted, one of the Ips in this range is used to provide the dhcp 
service, trying to free this ip and allocate a new one.");
                         for (VlanVO vlanrange : vlanRanges) {
                             if (vlanrange.getId() != vlanDbId) {
-                                freeIpsInsubnet =  
_publicIpAddressDao.countFreeIpsInVlan(vlanrange.getId());
+                                long freeIpsInsubnet =  
_publicIpAddressDao.countFreeIpsInVlan(vlanrange.getId());
                                 if (freeIpsInsubnet > 0){
-                                    //assign one free ip to the router for 
creating ip Alias.
-                                    Transaction txn = Transaction.currentTxn();
-                                    //changing the state to revoked so that 
removeDhcpSupport for subnet sses it.
-                                    ipAlias.setState(NicIpAlias.state.revoked);
-                                    _nicIpAliasDao.update(ipAlias.getId(), 
ipAlias);
+                                    //assign one free ip to the router for 
creating ip Alias. The ipalias is system managed ip so we are using the system 
account to allocate the ip not the caller.
                                     boolean result = false;
-                                    try {
-                                        PublicIp routerPublicIP = 
_networkMgr.assignPublicIpAddressFromVlans(network.getDataCenterId(), null, 
caller, Vlan.VlanType.DirectAttached, vlanDbIdList, network.getId(), null, 
false);
-                                        s_logger.info("creating a db entry for 
the new ip alias.");
-                                        NicIpAliasVO newipAlias = new 
NicIpAliasVO(ipAlias.getNicId(), routerPublicIP.getAddress().addr(), 
ipAlias.getVmId(), ipAlias.getAccountId(), network.getDomainId(), 
network.getId(), ipAlias.getGateway(), ipAlias.getNetmask());
-                                        
newipAlias.setAliasCount(routerPublicIP.getIpMacAddress());
-                                        _nicIpAliasDao.persist(newipAlias);
-                                        //we revoke all the rules and apply 
all the rules as a part of the removedhcp config. so the new ip will get 
configured when we delete the old ip.
-
-                                    }
-                                    catch 
(InsufficientAddressCapacityException e) {
-                                        txn.rollback();
-                                        txn.close();
-                                        throw new 
InvalidParameterValueException("cannot delete  vlan range"+ 
vlanRange.getId()+"one of the ips in this range is benig used to provide dhcp 
service. Cannot use some other ip as there are no free ips in this subnet");
-                                    }
+                                    PublicIp routerPublicIP = 
_networkMgr.assignPublicIpAddressFromVlans(network.getDataCenterId(), null, 
_accountDao.findById(Account.ACCOUNT_ID_SYSTEM), Vlan.VlanType.DirectAttached, 
vlanDbIdList, network.getId(), null, false);
+                                    s_logger.info("creating a db entry for the 
new ip alias.");
+                                    NicIpAliasVO newipAlias = new 
NicIpAliasVO(ipAlias.getNicId(), routerPublicIP.getAddress().addr(), 
ipAlias.getVmId(), ipAlias.getAccountId(), network.getDomainId(), 
network.getId(), ipAlias.getGateway(), ipAlias.getNetmask());
+                                    
newipAlias.setAliasCount(routerPublicIP.getIpMacAddress());
+                                    _nicIpAliasDao.persist(newipAlias);
+                                    //we revoke all the rules and apply all 
the rules as a part of the removedhcp config. so the new ip will get configured 
when we delete the old ip.
                                     s_logger.info("removing the old ip alias 
on router");
                                     result = 
dhcpServiceProvider.removeDhcpSupportForSubnet(network);
                                     if (result == false) {
                                         s_logger.debug("could't delete the ip 
alias on the router");
-                                        txn.rollback();
-                                        txn.close();
-                                        return false;
+                                        result_final = false;
                                     }
-                                    
_publicIpAddressDao.unassignIpAddress(ip.getId());
-                                    if (!deletePublicIPRange(vlanDbId)) {
-                                        return false;
+                                    else {
+                                        
_publicIpAddressDao.unassignIpAddress(ip.getId());
+                                        result_final=true;
                                     }
-                                    _vlanDao.expunge(vlanDbId);
-                                    txn.commit();
-                                    txn.close();
                                 }
                             }
                         }
                     }
                 }
+            }
 
-            } else {
-                // when there is no dhcp support in the network.
-                if (!deletePublicIPRange(vlanDbId)) {
-                    return false;
+        } catch (InsufficientAddressCapacityException e) {
+            throw new InvalidParameterValueException("cannot delete  vlan 
range"+ vlanRange.getId()+"one of the ips in this range is benig used to 
provide dhcp service. Cannot use some other ip as there are no free ips in this 
subnet");
+        }
+        finally {
+            if (result_final) {
+                if (!removeFromDb(vlanDbId)) {
+                    txn.rollback();
                 }
-                _vlanDao.expunge(vlanDbId);
-                return  true;
+                else {
+                    txn.commit();
+                }
+                txn.close();
             }
         }
-        return false;
+        return result_final;
     }
 
-
     @Override
     @DB
     @ActionEvent(eventType = EventTypes.EVENT_VLAN_IP_RANGE_DEDICATE, 
eventDescription = "dedicating vlan ip range", async = false)
@@ -3332,6 +3338,7 @@ public class ConfigurationManagerImpl extends ManagerBase 
implements Configurati
             stmt.setLong(1, vlanDbId);
             stmt.executeUpdate();
         } catch (Exception ex) {
+            s_logger.error(ex.getMessage());
             return false;
         }
         txn.commit();

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/4d5c6c83/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
----------------------------------------------------------------------
diff --git 
a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java 
b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
index 752a4f3..2e75277 100755
--- 
a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
+++ 
b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
@@ -2766,7 +2766,7 @@ public class VirtualNetworkApplianceManagerImpl extends 
ManagerBase implements V
         //Asuming we have only one router per network For Now.
         DomainRouterVO router = routers.get(0);
         if (router.getState() != State.Running) {
-            s_logger.warn("Failed to add/remove VPN users: router not in 
running state");
+            s_logger.warn("Failed to configure dhcp: router not in running 
state");
             throw new ResourceUnavailableException("Unable to assign ip 
addresses, domR is not in right state " +
                     router.getState(), DataCenter.class, 
network.getDataCenterId());
         }
@@ -2876,6 +2876,7 @@ public class VirtualNetworkApplianceManagerImpl extends 
ManagerBase implements V
             if (result) {
                 for (NicIpAliasVO revokedAliasVO : revokedIpAliasVOs) {
                     _nicIpAliasDao.expunge(revokedAliasVO.getId());
+                    return true;
                 }
             }
         }

Reply via email to