Updated Branches:
  refs/heads/vmsync 1f5c67231 -> f7370196d

Cloudstack-3106 Delete all ips except ipAlias. Cloudstack-3119 Shared network 
removal doesn't cleanup corresponding IP ranges

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


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

Branch: refs/heads/vmsync
Commit: 6b0df2566db34eb87c9603e6effcad2ad2b366c6
Parents: 7f2f25d
Author: Bharat Kumar <bharat.ku...@citrix.com>
Authored: Fri Jun 28 14:14:43 2013 +0530
Committer: Jayapal <jaya...@apache.org>
Committed: Fri Jun 28 19:05:55 2013 +0530

----------------------------------------------------------------------
 engine/schema/src/com/cloud/dc/VlanVO.java      |   4 +
 .../src/com/cloud/network/dao/IPAddressDao.java |   6 +
 .../com/cloud/network/dao/IPAddressDaoImpl.java |  23 +++
 .../configuration/ConfigurationManagerImpl.java | 153 ++++++-------------
 .../VirtualNetworkApplianceManagerImpl.java     |   2 -
 5 files changed, 79 insertions(+), 109 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/6b0df256/engine/schema/src/com/cloud/dc/VlanVO.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/VlanVO.java 
b/engine/schema/src/com/cloud/dc/VlanVO.java
index a2f7a9c..d262409 100644
--- a/engine/schema/src/com/cloud/dc/VlanVO.java
+++ b/engine/schema/src/com/cloud/dc/VlanVO.java
@@ -197,4 +197,8 @@ public class VlanVO implements Vlan {
        public void setIp6Range(String ip6Range) {
                this.ip6Range = ip6Range;
        }
+
+    public void setIpRange(String ipRange) {
+        this.ip6Range = ipRange;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/6b0df256/engine/schema/src/com/cloud/network/dao/IPAddressDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/IPAddressDao.java 
b/engine/schema/src/com/cloud/network/dao/IPAddressDao.java
index fecd44a..3eba6d8 100755
--- a/engine/schema/src/com/cloud/network/dao/IPAddressDao.java
+++ b/engine/schema/src/com/cloud/network/dao/IPAddressDao.java
@@ -17,9 +17,11 @@
 package com.cloud.network.dao;
 
 import com.cloud.dc.Vlan.VlanType;
+import com.cloud.utils.db.DB;
 import com.cloud.utils.db.GenericDao;
 import com.cloud.utils.net.Ip;
 
+import java.sql.SQLException;
 import java.util.List;
 
 public interface IPAddressDao extends GenericDao<IPAddressVO, Long> {
@@ -72,4 +74,8 @@ public interface IPAddressDao extends GenericDao<IPAddressVO, 
Long> {
     IPAddressVO findByIpAndVlanId(String ipAddress, long vlanid);
 
     long countFreeIpsInVlan(long vlanDbId);
+
+    boolean deletePublicIPRangeExceptAliasIP(long vlanDbId, String aliasIp) 
throws SQLException;
+
+    boolean deletePublicIPRange(long vlanDbId) throws SQLException;
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/6b0df256/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java 
b/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java
index 886011e..1051b69 100755
--- a/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java
+++ b/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java
@@ -40,6 +40,7 @@ import javax.ejb.Local;
 import javax.inject.Inject;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.util.Date;
 import java.util.List;
 
@@ -365,6 +366,28 @@ public class IPAddressDaoImpl extends 
GenericDaoBase<IPAddressVO, Long> implemen
     }
 
     @Override
+    public boolean deletePublicIPRangeExceptAliasIP(long vlanDbId, String 
aliasIp) throws SQLException {
+        Transaction txn = Transaction.currentTxn();
+        String deleteSql = "DELETE FROM `cloud`.`user_ip_address` WHERE 
vlan_db_id = ? and public_ip_address!=?";
+
+        txn.start();
+        PreparedStatement stmt = txn.prepareAutoCloseStatement(deleteSql);
+        stmt.setLong(1, vlanDbId);
+        stmt.setString(2, aliasIp);
+        stmt.executeUpdate();
+        txn.commit();
+        return true;
+    }
+
+    @Override
+    public boolean deletePublicIPRange(long vlanDbId) throws SQLException{
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("vlan", vlanDbId);
+        remove(sc);
+        return true;
+    }
+
+    @Override
     @DB
     public boolean remove(Long id) {
         Transaction txn = Transaction.currentTxn();

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/6b0df256/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 041f29a..51c323d 100755
--- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
+++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
@@ -123,7 +123,6 @@ import com.cloud.event.ActionEvent;
 import com.cloud.event.EventTypes;
 import com.cloud.event.UsageEventUtils;
 import com.cloud.exception.ConcurrentOperationException;
-import com.cloud.exception.InsufficientAddressCapacityException;
 import com.cloud.exception.InsufficientCapacityException;
 import com.cloud.exception.InvalidParameterValueException;
 import com.cloud.exception.PermissionDeniedException;
@@ -142,7 +141,6 @@ import com.cloud.network.NetworkService;
 import com.cloud.network.Networks.BroadcastDomainType;
 import com.cloud.network.Networks.TrafficType;
 import com.cloud.network.PhysicalNetwork;
-import com.cloud.network.addr.PublicIp;
 import com.cloud.network.dao.FirewallRulesDao;
 import com.cloud.network.dao.IPAddressDao;
 import com.cloud.network.dao.IPAddressVO;
@@ -3138,11 +3136,13 @@ public class ConfigurationManagerImpl extends 
ManagerBase implements Configurati
         return vlan;
     }
 
-    public boolean removeFromDb (long  vlanDbId){
-        if (!deletePublicIPRange(vlanDbId)) {
-            return false;
-        }
-        return  _vlanDao.expunge(vlanDbId);
+    @DB
+    public void deleteVLANFromDb(long vlanDbId) throws SQLException {
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+        _publicIpAddressDao.deletePublicIPRange(vlanDbId);
+        _vlanDao.expunge(vlanDbId);
+        txn.commit();
     }
 
     @Override
@@ -3226,34 +3226,31 @@ public class ConfigurationManagerImpl extends 
ManagerBase implements Configurati
                             .getVlanType().toString(), ip.getSystem(), 
ip.getClass().getName(), ip.getUuid());
                 }
             }
-            if 
(_networkModel.areServicesSupportedInNetwork(vlanRange.getNetworkId(), 
Service.Dhcp)) {
-                Network network = 
_networkDao.findById(vlanRange.getNetworkId());
-                DhcpServiceProvider dhcpServiceProvider = 
_networkMgr.getDhcpServiceProvider(network);
-                if 
(!dhcpServiceProvider.getProvider().getName().equalsIgnoreCase(Provider.VirtualRouter.getName()))
 {
-                    Transaction txn = Transaction.currentTxn();
-                    txn.start();
-                    if (!removeFromDb(vlanDbId)) {
-                        txn.rollback();
-                        txn.close();
-                        return false;
-                    }
-
-                    else {
-                        txn.commit();
+            try {
+                if 
(_networkModel.areServicesSupportedInNetwork(vlanRange.getNetworkId(), 
Service.Dhcp)) {
+                    Network network = 
_networkDao.findById(vlanRange.getNetworkId());
+                    DhcpServiceProvider dhcpServiceProvider = 
_networkMgr.getDhcpServiceProvider(network);
+                    if 
(!dhcpServiceProvider.getProvider().getName().equalsIgnoreCase(Provider.VirtualRouter.getName()))
 {
+                        deleteVLANFromDb(vlanDbId);
+                    } else {
+                        return  handleIpAliasDeletion(vlanRange, vlanDbId, 
dhcpServiceProvider, network);
                     }
-                    txn.close();
                 }
 
                 else {
-                  return  handleIpAliasDeletion(vlanRange, vlanDbId, 
dhcpServiceProvider, network);
+                    deleteVLANFromDb(vlanDbId);
                 }
             }
+            catch ( SQLException e) {
+               throw  new CloudRuntimeException(e.getMessage());
+            }
+
         }
         return true;
     }
 
-    private boolean handleIpAliasDeletion(VlanVO vlanRange, long vlanDbId, 
DhcpServiceProvider dhcpServiceProvider, Network network) {
-        boolean result_final = false;
+    @DB
+    private boolean handleIpAliasDeletion(VlanVO vlanRange, long vlanDbId, 
DhcpServiceProvider dhcpServiceProvider, Network network) throws SQLException {
         Transaction txn = Transaction.currentTxn();
         txn.start();
         IPAddressVO ip = null;
@@ -3263,87 +3260,48 @@ public class ConfigurationManagerImpl extends 
ManagerBase implements Configurati
             //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.");
+                throw  new InvalidParameterValueException ("Cannot delete this 
range as some of the vlans are in use.");
             }
-            if (allocIpCount == 0){
-                result_final=true;
+            else if (allocIpCount == 0){
+                deleteVLANFromDb(vlanDbId);
             }
             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.
                 ip = 
_publicIpAddressDao.findByIpAndVlanId(ipAlias.getIp4Address(), vlanDbId);
                 if (ip != null && ip.getState() == IpAddress.State.Allocated) {
                     //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;
+                    if (vlanRanges.size() == 1) {
+                        ipAlias.setState(NicIpAlias.state.revoked);
+                        _nicIpAliasDao.update(ipAlias.getId(), ipAlias);
+                        if 
(!dhcpServiceProvider.removeDhcpSupportForSubnet(network)) {
                             s_logger.debug("Failed to delete the vlan range as 
we could not free the ip used to provide the dhcp service.");
-                        } else {
+                            //setting the state back to active
+                            ipAlias.setState(NicIpAlias.state.active);
+                            _nicIpAliasDao.update(ipAlias.getId(), ipAlias);
+                        }
+                        else {
                             _publicIpAddressDao.unassignIpAddress(ip.getId());
-                            result_final = true;
+                            deleteVLANFromDb(vlanDbId);
                         }
                     } else {
-                        // if there are more vlans in the subnet check if there
-                        // are free ips.
-                        List<Long> vlanDbIdList = new ArrayList<Long>();
-                        for (VlanVO vlanrange : vlanRanges) {
-                            if (vlanrange.getId() != vlanDbId) {
-                                vlanDbIdList.add(vlanrange.getId());
-                            }
-                        }
-                        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) {
-
-                                long freeIpsInsubnet =  
_publicIpAddressDao.countFreeIpsInVlan(vlanrange.getId());
-                                if (freeIpsInsubnet > 0){
-                                    //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;
-                                    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");
-                                        result_final = false;
-                                    }
-                                    else {
-                                        
_publicIpAddressDao.unassignIpAddress(ip.getId());
-                                        result_final=true;
-                                    }
-                                }
-                            }
-                        }
+                        // if there are more vlans in the subnet, free all the 
ips in the range except the ip alias.
+                        s_logger.info("vlan Range"+vlanRange.getId()+" id 
being deleted, one of the Ips in this range is used to provide the dhcp 
service, will free the rest of the IPs in range.");
+                        
_publicIpAddressDao.deletePublicIPRangeExceptAliasIP(vlanDbId, 
ipAlias.getIp4Address());
+                        VlanVO vlan = _vlanDao.findById(vlanDbId);
+                        
vlan.setIpRange(ipAlias.getIp4Address()+"-"+ipAlias.getIp4Address());
+                        _vlanDao.update(vlan.getId(), vlan);
                     }
                 }
             }
-
-        } 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();
-                }
-                else {
-                    txn.commit();
-                }
-                txn.close();
-            }
+        } catch (CloudRuntimeException e) {
+            txn.rollback();
+            throw e;
         }
-        return result_final;
+        txn.commit();
+        return true;
     }
 
     @Override
@@ -3566,25 +3524,6 @@ public class ConfigurationManagerImpl extends 
ManagerBase implements Configurati
     }
 
     @DB
-    protected boolean deletePublicIPRange(long vlanDbId) {
-        Transaction txn = Transaction.currentTxn();
-        String deleteSql = "DELETE FROM `cloud`.`user_ip_address` WHERE 
vlan_db_id = ?";
-
-        txn.start();
-        try {
-            PreparedStatement stmt = txn.prepareAutoCloseStatement(deleteSql);
-            stmt.setLong(1, vlanDbId);
-            stmt.executeUpdate();
-        } catch (Exception ex) {
-            s_logger.error(ex.getMessage());
-            return false;
-        }
-        txn.commit();
-
-        return true;
-    }
-
-    @DB
     protected boolean savePublicIPRange(String startIP, String endIP, long 
zoneId, long vlanDbId, long sourceNetworkid,
             long physicalNetworkId) {
         long startIPLong = NetUtils.ip2Long(startIP);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/6b0df256/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 5327f0b..ddfa998 100755
--- 
a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
+++ 
b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
@@ -2849,8 +2849,6 @@ public class VirtualNetworkApplianceManagerImpl extends 
ManagerBase implements V
                     network.getId(), DataCenter.class, 
network.getDataCenterId());
         }
 
-        boolean agentResults = true;
-
         for (DomainRouterVO router : routers) {
             if (router.getState() != State.Running) {
                 s_logger.warn("Failed to add/remove VPN users: router not in 
running state");

Reply via email to