Skip to content

Commit afb97f1

Browse files
author
Alena Prokharchyk
committed
Account specific vlan ranges - fixed deleteVlanRange
Conflicts: api/src/com/cloud/api/commands/DeleteVlanIpRangeCmd.java server/src/com/cloud/configuration/ConfigurationManagerImpl.java
1 parent 8d2a008 commit afb97f1

File tree

4 files changed

+78
-19
lines changed

4 files changed

+78
-19
lines changed

api/src/com/cloud/api/commands/DeleteVlanIpRangeCmd.java

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
// Automatically generated by addcopyright.py at 04/03/2012
1313
package com.cloud.api.commands;
1414

15-
import java.util.UUID;
16-
1715
import org.apache.log4j.Logger;
1816

1917
import com.cloud.api.ApiConstants;

server/src/com/cloud/configuration/ConfigurationManager.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ DataCenterVO createZone(long userId, String zoneName, String dns1, String dns2,
137137
*
138138
* @param userId
139139
* @param vlanDbId
140+
* @param caller TODO
140141
* @return success/failure
141142
*/
142-
boolean deleteVlanAndPublicIpRange(long userId, long vlanDbId);
143+
boolean deleteVlanAndPublicIpRange(long userId, long vlanDbId, Account caller);
143144

144145
/**
145146
* Converts a comma separated list of tags to a List

server/src/com/cloud/configuration/ConfigurationManagerImpl.java

+72-12
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838

3939
import com.cloud.acl.SecurityChecker;
4040
import com.cloud.alert.AlertManager;
41-
import com.cloud.api.ApiDBUtils;
4241
import com.cloud.api.ApiConstants.LDAPParams;
42+
import com.cloud.api.ApiDBUtils;
4343
import com.cloud.api.commands.CreateDiskOfferingCmd;
4444
import com.cloud.api.commands.CreateNetworkOfferingCmd;
4545
import com.cloud.api.commands.CreateServiceOfferingCmd;
@@ -110,6 +110,7 @@
110110
import com.cloud.network.Networks.TrafficType;
111111
import com.cloud.network.PhysicalNetwork;
112112
import com.cloud.network.PhysicalNetworkVO;
113+
import com.cloud.network.dao.FirewallRulesDao;
113114
import com.cloud.network.dao.IPAddressDao;
114115
import com.cloud.network.dao.NetworkDao;
115116
import com.cloud.network.dao.PhysicalNetworkDao;
@@ -155,6 +156,7 @@
155156
import com.cloud.utils.exception.CloudRuntimeException;
156157
import com.cloud.utils.net.NetUtils;
157158
import com.cloud.vm.VirtualMachine;
159+
import com.cloud.vm.dao.NicDao;
158160

159161
import edu.emory.mathcs.backport.java.util.Arrays;
160162

@@ -219,6 +221,10 @@ public class ConfigurationManagerImpl implements ConfigurationManager, Configura
219221
SwiftManager _swiftMgr;
220222
@Inject
221223
PhysicalNetworkTrafficTypeDao _trafficTypeDao;
224+
@Inject
225+
NicDao _nicDao;
226+
@Inject
227+
FirewallRulesDao _firewallDao;
222228

223229
// FIXME - why don't we have interface for DataCenterLinkLocalIpAddressDao?
224230
protected static final DataCenterLinkLocalIpAddressDaoImpl _LinkLocalIpAllocDao = ComponentLocator.inject(DataCenterLinkLocalIpAddressDaoImpl.class);
@@ -2425,24 +2431,78 @@ public Vlan createVlanAndPublicIpRange(long zoneId, long networkId, long physica
24252431
}
24262432

24272433
@Override
2428-
public boolean deleteVlanAndPublicIpRange(long userId, long vlanDbId) {
2434+
@DB
2435+
public boolean deleteVlanAndPublicIpRange(long userId, long vlanDbId, Account caller) {
24292436
VlanVO vlan = _vlanDao.findById(vlanDbId);
24302437
if (vlan == null) {
24312438
throw new InvalidParameterValueException("Please specify a valid IP range id.");
24322439
}
2440+
2441+
boolean isAccountSpecific = false;
2442+
List<AccountVlanMapVO> acctVln = _accountVlanMapDao.listAccountVlanMapsByVlan(vlan.getId());
2443+
// Check for account wide pool. It will have an entry for account_vlan_map.
2444+
if (acctVln != null && !acctVln.isEmpty()) {
2445+
isAccountSpecific = true;
2446+
}
24332447

24342448
// Check if the VLAN has any allocated public IPs
2435-
if (_publicIpAddressDao.countIPs(vlan.getDataCenterId(), vlanDbId, true) > 0) {
2436-
throw new InvalidParameterValueException("The IP range can't be deleted because it has allocated public IP addresses.");
2449+
long allocIpCount = _publicIpAddressDao.countIPs(vlan.getDataCenterId(), vlanDbId, true);
2450+
boolean success = true;
2451+
if (allocIpCount > 0) {
2452+
if (isAccountSpecific) {
2453+
try {
2454+
vlan = _vlanDao.acquireInLockTable(vlanDbId, 30);
2455+
if (vlan == null) {
2456+
throw new CloudRuntimeException("Unable to acquire vlan configuration: " + vlanDbId);
2457+
}
2458+
2459+
if (s_logger.isDebugEnabled()) {
2460+
s_logger.debug("lock vlan " + vlanDbId + " is acquired");
2461+
}
2462+
2463+
List<IPAddressVO> ips = _publicIpAddressDao.listByVlanId(vlanDbId);
2464+
2465+
for (IPAddressVO ip : ips) {
2466+
if (ip.isOneToOneNat()) {
2467+
throw new InvalidParameterValueException("Can't delete account specific vlan " + vlanDbId +
2468+
" as ip " + ip + " belonging to the range is used for static nat purposes. Cleanup the rules first");
2469+
}
2470+
2471+
if (ip.isSourceNat() && _nicDao.findByIp4AddressAndNetworkId(ip.getAddress().addr(), ip.getSourceNetworkId()) != null) {
2472+
throw new InvalidParameterValueException("Can't delete account specific vlan " + vlanDbId +
2473+
" as ip " + ip + " belonging to the range is a source nat ip for the network id=" + ip.getSourceNetworkId() +
2474+
". Either delete the network, or Virtual Router instance using this ip address");
2475+
}
2476+
2477+
if (_firewallDao.countRulesByIpId(ip.getId()) > 0) {
2478+
throw new InvalidParameterValueException("Can't delete account specific vlan " + vlanDbId +
2479+
" as ip " + ip + " belonging to the range has firewall rules applied. Cleanup the rules first");
2480+
}
2481+
//release public ip address here
2482+
success = success && _networkMgr.releasePublicIpAddress(ip.getId(), userId, caller);
2483+
}
2484+
if (!success) {
2485+
s_logger.warn("Some ip addresses failed to be released as a part of vlan " + vlanDbId + " removal");
2486+
}
2487+
} finally {
2488+
_vlanDao.releaseFromLockTable(vlanDbId);
2489+
}
2490+
} else {
2491+
throw new InvalidParameterValueException("The IP range can't be deleted because it has allocated public IP addresses.");
2492+
}
24372493
}
24382494

2439-
// Delete all public IPs in the VLAN
2440-
if (!deletePublicIPRange(vlanDbId)) {
2495+
if (success) {
2496+
// Delete all public IPs in the VLAN
2497+
if (!deletePublicIPRange(vlanDbId)) {
2498+
return false;
2499+
}
2500+
2501+
// Delete the VLAN
2502+
return _vlanDao.expunge(vlanDbId);
2503+
} else {
24412504
return false;
24422505
}
2443-
2444-
// Delete the VLAN
2445-
return _vlanDao.expunge(vlanDbId);
24462506
}
24472507

24482508
@Override
@@ -2766,8 +2826,7 @@ public boolean deleteVlanIpRange(DeleteVlanIpRangeCmd cmd) {
27662826
throw new InvalidParameterValueException("Please specify a valid IP range id.");
27672827
}
27682828

2769-
return deleteVlanAndPublicIpRange(UserContext.current().getCallerUserId(), vlanDbId);
2770-
2829+
return deleteVlanAndPublicIpRange(UserContext.current().getCallerUserId(), vlanDbId, UserContext.current().getCaller());
27712830
}
27722831

27732832
@Override
@@ -3620,7 +3679,8 @@ public boolean deleteAccountSpecificVirtualRanges(long accountId) {
36203679
Transaction txn = Transaction.currentTxn();
36213680
txn.start();
36223681
for (AccountVlanMapVO map : maps) {
3623-
if (!deleteVlanAndPublicIpRange(_accountMgr.getSystemUser().getId(), map.getVlanDbId())) {
3682+
if (!deleteVlanAndPublicIpRange(_accountMgr.getSystemUser().getId(), map.getVlanDbId(),
3683+
_accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM))) {
36243684
result = false;
36253685
}
36263686
}

server/src/com/cloud/network/NetworkManagerImpl.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ public boolean applyIpAssociations(Network network, boolean continueOnError) thr
614614

615615
} else if (addr.getState() == IpAddress.State.Releasing) {
616616
// Cleanup all the resources for ip address if there are any, and only then un-assign ip in the
617-
// system
617+
// system
618618
if (cleanupIpResources(addr.getId(), Account.ACCOUNT_ID_SYSTEM, _accountMgr.getSystemAccount())) {
619619
_ipAddressDao.unassignIpAddress(addr.getId());
620620
} else {
@@ -3144,7 +3144,7 @@ public boolean destroyNetwork(long networkId, ReservationContext context) {
31443144
txn.start();
31453145
guru.trash(network, _networkOfferingDao.findById(network.getNetworkOfferingId()), owner);
31463146

3147-
if (!deleteVlansInNetwork(network.getId(), context.getCaller().getId())) {
3147+
if (!deleteVlansInNetwork(network.getId(), context.getCaller().getId(), callerAccount)) {
31483148
success = false;
31493149
s_logger.warn("Failed to delete network " + network + "; was unable to cleanup corresponding ip ranges");
31503150
} else {
@@ -3159,11 +3159,11 @@ public boolean destroyNetwork(long networkId, ReservationContext context) {
31593159
return success;
31603160
}
31613161

3162-
private boolean deleteVlansInNetwork(long networkId, long userId) {
3162+
private boolean deleteVlansInNetwork(long networkId, long userId, Account callerAccount) {
31633163
List<VlanVO> vlans = _vlanDao.listVlansByNetworkId(networkId);
31643164
boolean result = true;
31653165
for (VlanVO vlan : vlans) {
3166-
if (!_configMgr.deleteVlanAndPublicIpRange(_accountMgr.getSystemUser().getId(), vlan.getId())) {
3166+
if (!_configMgr.deleteVlanAndPublicIpRange(_accountMgr.getSystemUser().getId(), vlan.getId(), callerAccount)) {
31673167
s_logger.warn("Failed to delete vlan " + vlan.getId() + ");");
31683168
result = false;
31693169
}

0 commit comments

Comments
 (0)