use of com.cloud.dc.DataCenterVnetVO in project cloudstack by apache.
the class NetworkServiceImpl method getVnetsToremove.
private List<String> getVnetsToremove(PhysicalNetworkVO network, List<Pair<Integer, Integer>> vnetRanges) {
int i;
List<String> removeVnets = new ArrayList<String>();
HashSet<String> vnetsInDb = new HashSet<String>();
vnetsInDb.addAll(_datacneterVnet.listVnetsByPhysicalNetworkAndDataCenter(network.getDataCenterId(), network.getId()));
// remove all the vnets not in the list of vnets passed by the user.
if (vnetRanges.size() == 0) {
// this implies remove all vlans.
removeVnets.addAll(vnetsInDb);
int allocated_vnets = _datacneterVnet.countAllocatedVnets(network.getId());
if (allocated_vnets > 0) {
throw new InvalidParameterValueException("physicalnetwork " + network.getId() + " has " + allocated_vnets + " vnets in use");
}
return removeVnets;
}
for (Pair<Integer, Integer> vlan : vnetRanges) {
for (i = vlan.first(); i <= vlan.second(); i++) {
vnetsInDb.remove(Integer.toString(i));
}
}
String vnetRange = null;
if (vnetsInDb.size() != 0) {
removeVnets.addAll(vnetsInDb);
vnetRange = generateVnetString(removeVnets);
} else {
return removeVnets;
}
for (String vnet : vnetRange.split(",")) {
String[] range = vnet.split("-");
Integer start = Integer.parseInt(range[0]);
Integer end = Integer.parseInt(range[1]);
_datacneterVnet.lockRange(network.getDataCenterId(), network.getId(), start, end);
List<DataCenterVnetVO> result = _datacneterVnet.listAllocatedVnetsInRange(network.getDataCenterId(), network.getId(), start, end);
if (!result.isEmpty()) {
throw new InvalidParameterValueException("physicalnetwork " + network.getId() + " has allocated vnets in the range " + start + "-" + end);
}
// If the range is partially dedicated to an account fail the request
List<AccountGuestVlanMapVO> maps = _accountGuestVlanMapDao.listAccountGuestVlanMapsByPhysicalNetwork(network.getId());
for (AccountGuestVlanMapVO map : maps) {
String[] vlans = map.getGuestVlanRange().split("-");
Integer dedicatedStartVlan = Integer.parseInt(vlans[0]);
Integer dedicatedEndVlan = Integer.parseInt(vlans[1]);
if ((start >= dedicatedStartVlan && start <= dedicatedEndVlan) || (end >= dedicatedStartVlan && end <= dedicatedEndVlan)) {
throw new InvalidParameterValueException("Vnet range " + map.getGuestVlanRange() + " is dedicated" + " to an account. The specified range " + start + "-" + end + " overlaps with the dedicated range " + " Please release the overlapping dedicated range before deleting the range");
}
}
}
return removeVnets;
}
use of com.cloud.dc.DataCenterVnetVO in project cloudstack by apache.
the class DedicateGuestVlanRangesTest method runDedicateGuestVlanRangePostiveTest.
void runDedicateGuestVlanRangePostiveTest() throws Exception {
TransactionLegacy txn = TransactionLegacy.open("runDedicateGuestVlanRangePostiveTest");
Field dedicateVlanField = _dedicateGuestVlanRangeClass.getDeclaredField("vlan");
dedicateVlanField.setAccessible(true);
dedicateVlanField.set(dedicateGuestVlanRangesCmd, "2-5");
PhysicalNetworkVO physicalNetwork = new PhysicalNetworkVO(1L, 1L, "2-5", "200", 1L, null, "testphysicalnetwork");
physicalNetwork.addIsolationMethod("VLAN");
AccountGuestVlanMapVO accountGuestVlanMapVO = new AccountGuestVlanMapVO(1L, 1L);
when(networkService._physicalNetworkDao.findById(anyLong())).thenReturn(physicalNetwork);
when(networkService._datacneterVnet.listAllocatedVnetsInRange(anyLong(), anyLong(), anyInt(), anyInt())).thenReturn(null);
when(networkService._accountGuestVlanMapDao.listAccountGuestVlanMapsByPhysicalNetwork(anyLong())).thenReturn(null);
when(networkService._accountGuestVlanMapDao.persist(any(AccountGuestVlanMapVO.class))).thenReturn(accountGuestVlanMapVO);
when(networkService._datacneterVnet.update(anyLong(), any(DataCenterVnetVO.class))).thenReturn(true);
List<DataCenterVnetVO> dataCenterVnetList = new ArrayList<DataCenterVnetVO>();
DataCenterVnetVO dataCenterVnetVO = new DataCenterVnetVO("2-5", 1L, 1L);
dataCenterVnetList.add(dataCenterVnetVO);
when(networkService._datacneterVnet.findVnet(anyLong(), anyString())).thenReturn(dataCenterVnetList);
try {
GuestVlan result = networkService.dedicateGuestVlanRange(dedicateGuestVlanRangesCmd);
Assert.assertNotNull(result);
} catch (Exception e) {
s_logger.info("exception in testing runDedicateGuestVlanRangePostiveTest message: " + e.toString());
} finally {
txn.close("runDedicateGuestRangePostiveTest");
}
}
Aggregations