Search in sources :

Example 21 with DataCenterVnetVO

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;
}
Also used : AccountGuestVlanMapVO(com.cloud.network.dao.AccountGuestVlanMapVO) ArrayList(java.util.ArrayList) DataCenterVnetVO(com.cloud.dc.DataCenterVnetVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) HashSet(java.util.HashSet)

Example 22 with DataCenterVnetVO

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");
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) Field(java.lang.reflect.Field) AccountGuestVlanMapVO(com.cloud.network.dao.AccountGuestVlanMapVO) DataCenterVnetVO(com.cloud.dc.DataCenterVnetVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) ArrayList(java.util.ArrayList)

Aggregations

DataCenterVnetVO (com.cloud.dc.DataCenterVnetVO)22 ArrayList (java.util.ArrayList)16 AccountGuestVlanMapVO (com.cloud.network.dao.AccountGuestVlanMapVO)11 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)9 DB (com.cloud.utils.db.DB)7 List (java.util.List)7 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)6 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 Pair (com.cloud.utils.Pair)4 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)4 Field (java.lang.reflect.Field)4 DataCenterDeployment (com.cloud.deploy.DataCenterDeployment)3 Network (com.cloud.network.Network)3 Capability (com.cloud.network.Network.Capability)3 PhysicalNetwork (com.cloud.network.PhysicalNetwork)3 NetworkVO (com.cloud.network.dao.NetworkVO)3 NetworkOffering (com.cloud.offering.NetworkOffering)3 NetworkOfferingVO (com.cloud.offerings.NetworkOfferingVO)3 TransactionStatus (com.cloud.utils.db.TransactionStatus)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3