Skip to content

Commit 6d4e8bb

Browse files
shwstpprneogismm
authored andcommitted
server: fix error when dedicating guest vlan range for physical nw without vlan range (apache#6655)
Fixes apache#6648 If any of the VLAN from the given range is not found in the database (cloud.op_dc_vnet_alloc) then an InvalidParameterValueException will be thrown. Also, refactors and fixes account check.
1 parent 25ce8de commit 6d4e8bb

2 files changed

Lines changed: 25 additions & 22 deletions

File tree

server/src/main/java/com/cloud/network/NetworkServiceImpl.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,20 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService, C
370370

371371
private Map<String, String> _configs;
372372

373+
private void verifyDedicatedGuestVlansWithExistingDatacenterVlans(PhysicalNetwork physicalNetwork, Account vlanOwner, int startVlan, int endVlan) {
374+
for (int i = startVlan; i <= endVlan; i++) {
375+
List<DataCenterVnetVO> dataCenterVnet = _dcVnetDao.findVnet(physicalNetwork.getDataCenterId(), physicalNetwork.getId(), Integer.toString(i));
376+
if (CollectionUtils.isEmpty(dataCenterVnet)) {
377+
throw new InvalidParameterValueException(String.format("Guest vlan %d from this range %d-%d is not present in the system for physical network ID: %s", i, startVlan, endVlan, physicalNetwork.getUuid()));
378+
}
379+
// Verify guest vlans in the range don't belong to a network of a different account
380+
if (dataCenterVnet.get(0).getAccountId() != null && dataCenterVnet.get(0).getAccountId() != vlanOwner.getAccountId()) {
381+
throw new InvalidParameterValueException("Guest vlan from this range " + dataCenterVnet.get(0).getVnet() + " is allocated to a different account."
382+
+ " Can only dedicate a range which has no allocated vlans or has vlans allocated to the same account ");
383+
}
384+
}
385+
}
386+
373387
/* Get a list of IPs, classify them by service */
374388
protected Map<PublicIp, Set<Service>> getIpToServices(List<PublicIp> publicIps, boolean rulesRevoked, boolean includingFirewall) {
375389
Map<PublicIp, Set<Service>> ipToServices = new HashMap<PublicIp, Set<Service>>();
@@ -4073,18 +4087,7 @@ public GuestVlanRange dedicateGuestVlanRange(DedicateGuestVlanRangeCmd cmd) {
40734087
}
40744088
}
40754089

4076-
// Verify guest vlans in the range don't belong to a network of a different account
4077-
for (int i = startVlan; i <= endVlan; i++) {
4078-
List<DataCenterVnetVO> allocatedVlans = _dcVnetDao.listAllocatedVnetsInRange(physicalNetwork.getDataCenterId(), physicalNetwork.getId(), startVlan, endVlan);
4079-
if (allocatedVlans != null && !allocatedVlans.isEmpty()) {
4080-
for (DataCenterVnetVO allocatedVlan : allocatedVlans) {
4081-
if (allocatedVlan.getAccountId() != vlanOwner.getAccountId()) {
4082-
throw new InvalidParameterValueException("Guest vlan from this range " + allocatedVlan.getVnet() + " is allocated to a different account."
4083-
+ " Can only dedicate a range which has no allocated vlans or has vlans allocated to the same account ");
4084-
}
4085-
}
4086-
}
4087-
}
4090+
verifyDedicatedGuestVlansWithExistingDatacenterVlans(physicalNetwork, vlanOwner, startVlan, endVlan);
40884091

40894092
List<AccountGuestVlanMapVO> guestVlanMaps = _accountGuestVlanMapDao.listAccountGuestVlanMapsByPhysicalNetwork(physicalNetworkId);
40904093
// Verify if vlan range is already dedicated

server/src/test/java/com/cloud/network/DedicateGuestVlanRangesTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,17 @@
2929
import java.util.List;
3030
import java.util.UUID;
3131

32-
import com.cloud.user.User;
33-
import junit.framework.Assert;
34-
32+
import org.apache.cloudstack.api.command.admin.network.DedicateGuestVlanRangeCmd;
33+
import org.apache.cloudstack.api.command.admin.network.ListDedicatedGuestVlanRangesCmd;
34+
import org.apache.cloudstack.api.command.admin.network.ReleaseDedicatedGuestVlanRangeCmd;
35+
import org.apache.cloudstack.context.CallContext;
3536
import org.apache.log4j.Logger;
3637
import org.junit.After;
3738
import org.junit.Before;
3839
import org.junit.Test;
3940
import org.mockito.Mock;
4041
import org.mockito.MockitoAnnotations;
4142

42-
import org.apache.cloudstack.api.command.admin.network.DedicateGuestVlanRangeCmd;
43-
import org.apache.cloudstack.api.command.admin.network.ListDedicatedGuestVlanRangesCmd;
44-
import org.apache.cloudstack.api.command.admin.network.ReleaseDedicatedGuestVlanRangeCmd;
45-
import org.apache.cloudstack.context.CallContext;
46-
4743
import com.cloud.dc.DataCenterVnetVO;
4844
import com.cloud.dc.dao.DataCenterVnetDao;
4945
import com.cloud.network.dao.AccountGuestVlanMapDao;
@@ -54,10 +50,13 @@
5450
import com.cloud.user.Account;
5551
import com.cloud.user.AccountManager;
5652
import com.cloud.user.AccountVO;
53+
import com.cloud.user.User;
5754
import com.cloud.user.UserVO;
5855
import com.cloud.user.dao.AccountDao;
5956
import com.cloud.utils.db.TransactionLegacy;
6057

58+
import junit.framework.Assert;
59+
6160
public class DedicateGuestVlanRangesTest {
6261

6362
private static final Logger s_logger = Logger.getLogger(DedicateGuestVlanRangesTest.class);
@@ -275,7 +274,7 @@ void runDedicateGuestVlanRangeAllocatedVlans() throws Exception {
275274
DataCenterVnetVO dataCenter = new DataCenterVnetVO("2-5", 1L, 1L);
276275
dataCenter.setAccountId(1L);
277276
dataCenterList.add(dataCenter);
278-
when(networkService._dcVnetDao.listAllocatedVnetsInRange(anyLong(), anyLong(), anyInt(), anyInt())).thenReturn(dataCenterList);
277+
when(networkService._dcVnetDao.findVnet(anyLong(), anyLong(), anyString())).thenReturn(dataCenterList);
279278

280279
try {
281280
networkService.dedicateGuestVlanRange(dedicateGuestVlanRangesCmd);
@@ -298,7 +297,8 @@ void runDedicateGuestVlanRangeDedicatedRange() throws Exception {
298297

299298
when(networkService._physicalNetworkDao.findById(anyLong())).thenReturn(physicalNetwork);
300299

301-
when(networkService._dcVnetDao.listAllocatedVnetsInRange(anyLong(), anyLong(), anyInt(), anyInt())).thenReturn(null);
300+
DataCenterVnetVO dataCenterVnetVO = new DataCenterVnetVO("2-5", 1L, 1L);
301+
when(networkService._dcVnetDao.findVnet(anyLong(), anyLong(), anyString())).thenReturn(List.of(dataCenterVnetVO));
302302

303303
List<AccountGuestVlanMapVO> guestVlanMaps = new ArrayList<AccountGuestVlanMapVO>();
304304
AccountGuestVlanMapVO accountGuestVlanMap = new AccountGuestVlanMapVO(1L, 1L);

0 commit comments

Comments
 (0)