Search in sources :

Example 1 with AccountGuestVlanMapVO

use of com.cloud.network.dao.AccountGuestVlanMapVO in project cosmic by MissionCriticalCloud.

the class DedicateGuestVlanRangesTest method runDedicateGuestVlanRangePostiveTest.

void runDedicateGuestVlanRangePostiveTest() throws Exception {
    final TransactionLegacy txn = TransactionLegacy.open("runDedicateGuestVlanRangePostiveTest");
    final Field dedicateVlanField = _dedicateGuestVlanRangeClass.getDeclaredField("vlan");
    dedicateVlanField.setAccessible(true);
    dedicateVlanField.set(dedicateGuestVlanRangesCmd, "2-5");
    final PhysicalNetworkVO physicalNetwork = new PhysicalNetworkVO(1L, 1L, "2-5", "200", 1L, null, "testphysicalnetwork");
    physicalNetwork.addIsolationMethod("VLAN");
    final 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);
    final List<DataCenterVnetVO> dataCenterVnetList = new ArrayList<>();
    final DataCenterVnetVO dataCenterVnetVO = new DataCenterVnetVO("2-5", 1L, 1L);
    dataCenterVnetList.add(dataCenterVnetVO);
    when(networkService._datacneterVnet.findVnet(anyLong(), anyString())).thenReturn(dataCenterVnetList);
    try {
        final GuestVlan result = networkService.dedicateGuestVlanRange(dedicateGuestVlanRangesCmd);
        Assert.assertNotNull(result);
    } catch (final 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)

Example 2 with AccountGuestVlanMapVO

use of com.cloud.network.dao.AccountGuestVlanMapVO in project cosmic by MissionCriticalCloud.

the class DedicateGuestVlanRangesTest method runDedicateGuestVlanRangeDedicatedRange.

void runDedicateGuestVlanRangeDedicatedRange() throws Exception {
    final TransactionLegacy txn = TransactionLegacy.open("runDedicateGuestVlanRangeDedicatedRange");
    final Field dedicateVlanField = _dedicateGuestVlanRangeClass.getDeclaredField("vlan");
    dedicateVlanField.setAccessible(true);
    dedicateVlanField.set(dedicateGuestVlanRangesCmd, "2-5");
    final PhysicalNetworkVO physicalNetwork = new PhysicalNetworkVO(1L, 1L, "2-5", "200", 1L, null, "testphysicalnetwork");
    physicalNetwork.addIsolationMethod("VLAN");
    when(networkService._physicalNetworkDao.findById(anyLong())).thenReturn(physicalNetwork);
    when(networkService._datacneterVnet.listAllocatedVnetsInRange(anyLong(), anyLong(), anyInt(), anyInt())).thenReturn(null);
    final List<AccountGuestVlanMapVO> guestVlanMaps = new ArrayList<>();
    final AccountGuestVlanMapVO accountGuestVlanMap = new AccountGuestVlanMapVO(1L, 1L);
    accountGuestVlanMap.setGuestVlanRange("2-5");
    guestVlanMaps.add(accountGuestVlanMap);
    when(networkService._accountGuestVlanMapDao.listAccountGuestVlanMapsByPhysicalNetwork(anyLong())).thenReturn(guestVlanMaps);
    try {
        networkService.dedicateGuestVlanRange(dedicateGuestVlanRangesCmd);
    } catch (final Exception e) {
        Assert.assertTrue(e.getMessage().contains("Vlan range is already dedicated"));
    } finally {
        txn.close("runDedicateGuestVlanRangeDedicatedRange");
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) Field(java.lang.reflect.Field) AccountGuestVlanMapVO(com.cloud.network.dao.AccountGuestVlanMapVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) ArrayList(java.util.ArrayList)

Example 3 with AccountGuestVlanMapVO

use of com.cloud.network.dao.AccountGuestVlanMapVO in project cosmic by MissionCriticalCloud.

the class DedicateGuestVlanRangesTest method runReleaseDedicatedGuestVlanRangePostiveTest.

void runReleaseDedicatedGuestVlanRangePostiveTest() throws Exception {
    final TransactionLegacy txn = TransactionLegacy.open("runReleaseDedicatedGuestVlanRangePostiveTest");
    final AccountGuestVlanMapVO accountGuestVlanMap = new AccountGuestVlanMapVO(1L, 1L);
    when(networkService._accountGuestVlanMapDao.findById(anyLong())).thenReturn(accountGuestVlanMap);
    doNothing().when(networkService._datacneterVnet).releaseDedicatedGuestVlans(anyLong());
    when(networkService._accountGuestVlanMapDao.remove(anyLong())).thenReturn(true);
    try {
        final Boolean result = networkService.releaseDedicatedGuestVlanRange(releaseDedicatedGuestVlanRangesCmd.getId());
        Assert.assertTrue(result);
    } catch (final Exception e) {
        s_logger.info("exception in testing runReleaseGuestVlanRangePostiveTest1 message: " + e.toString());
    } finally {
        txn.close("runReleaseDedicatedGuestVlanRangePostiveTest");
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) AccountGuestVlanMapVO(com.cloud.network.dao.AccountGuestVlanMapVO)

Example 4 with AccountGuestVlanMapVO

use of com.cloud.network.dao.AccountGuestVlanMapVO in project cloudstack by apache.

the class NetworkServiceImpl method releaseDedicatedGuestVlanRange.

@Override
@ActionEvent(eventType = EventTypes.EVENT_DEDICATED_GUEST_VLAN_RANGE_RELEASE, eventDescription = "releasing" + " dedicated guest vlan range", async = true)
@DB
public boolean releaseDedicatedGuestVlanRange(Long dedicatedGuestVlanRangeId) {
    // Verify dedicated range exists
    AccountGuestVlanMapVO dedicatedGuestVlan = _accountGuestVlanMapDao.findById(dedicatedGuestVlanRangeId);
    if (dedicatedGuestVlan == null) {
        throw new InvalidParameterValueException("Dedicated guest vlan with specified" + " id doesn't exist in the system");
    }
    // Remove dedication for the guest vlan
    _datacneterVnet.releaseDedicatedGuestVlans(dedicatedGuestVlan.getId());
    if (_accountGuestVlanMapDao.remove(dedicatedGuestVlanRangeId)) {
        return true;
    } else {
        return false;
    }
}
Also used : AccountGuestVlanMapVO(com.cloud.network.dao.AccountGuestVlanMapVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 5 with AccountGuestVlanMapVO

use of com.cloud.network.dao.AccountGuestVlanMapVO in project cloudstack by apache.

the class NetworkServiceImpl method dedicateGuestVlanRange.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_GUEST_VLAN_RANGE_DEDICATE, eventDescription = "dedicating guest vlan range", async = false)
public GuestVlan dedicateGuestVlanRange(DedicateGuestVlanRangeCmd cmd) {
    String vlan = cmd.getVlan();
    String accountName = cmd.getAccountName();
    Long domainId = cmd.getDomainId();
    Long physicalNetworkId = cmd.getPhysicalNetworkId();
    Long projectId = cmd.getProjectId();
    int startVlan, endVlan;
    String updatedVlanRange = null;
    long guestVlanMapId = 0;
    long guestVlanMapAccountId = 0;
    long vlanOwnerId = 0;
    // Verify account is valid
    Account vlanOwner = null;
    if (projectId != null) {
        if (accountName != null) {
            throw new InvalidParameterValueException("accountName and projectId are mutually exclusive");
        }
        Project project = _projectMgr.getProject(projectId);
        if (project == null) {
            throw new InvalidParameterValueException("Unable to find project by id " + projectId);
        }
        vlanOwner = _accountMgr.getAccount(project.getProjectAccountId());
    }
    if ((accountName != null) && (domainId != null)) {
        vlanOwner = _accountDao.findActiveAccount(accountName, domainId);
    }
    if (vlanOwner == null) {
        throw new InvalidParameterValueException("Unable to find account by name " + accountName);
    }
    vlanOwnerId = vlanOwner.getAccountId();
    // Verify physical network isolation type is VLAN
    PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
    if (physicalNetwork == null) {
        throw new InvalidParameterValueException("Unable to find physical network by id " + physicalNetworkId);
    } else if (!physicalNetwork.getIsolationMethods().isEmpty() && !physicalNetwork.getIsolationMethods().contains("VLAN")) {
        throw new InvalidParameterValueException("Cannot dedicate guest vlan range. " + "Physical isolation type of network " + physicalNetworkId + " is not VLAN");
    }
    // Get the start and end vlan
    String[] vlanRange = vlan.split("-");
    if (vlanRange.length != 2) {
        throw new InvalidParameterValueException("Invalid format for parameter value vlan " + vlan + " .Vlan should be specified as 'startvlan-endvlan'");
    }
    try {
        startVlan = Integer.parseInt(vlanRange[0]);
        endVlan = Integer.parseInt(vlanRange[1]);
    } catch (NumberFormatException e) {
        s_logger.warn("Unable to parse guest vlan range:", e);
        throw new InvalidParameterValueException("Please provide valid guest vlan range");
    }
    // Verify guest vlan range exists in the system
    List<Pair<Integer, Integer>> existingRanges = physicalNetwork.getVnet();
    Boolean exists = false;
    if (!existingRanges.isEmpty()) {
        for (int i = 0; i < existingRanges.size(); i++) {
            int existingStartVlan = existingRanges.get(i).first();
            int existingEndVlan = existingRanges.get(i).second();
            if (startVlan <= endVlan && startVlan >= existingStartVlan && endVlan <= existingEndVlan) {
                exists = true;
                break;
            }
        }
        if (!exists) {
            throw new InvalidParameterValueException("Unable to find guest vlan by range " + vlan);
        }
    }
    // Verify guest vlans in the range don't belong to a network of a different account
    for (int i = startVlan; i <= endVlan; i++) {
        List<DataCenterVnetVO> allocatedVlans = _datacneterVnet.listAllocatedVnetsInRange(physicalNetwork.getDataCenterId(), physicalNetwork.getId(), startVlan, endVlan);
        if (allocatedVlans != null && !allocatedVlans.isEmpty()) {
            for (DataCenterVnetVO allocatedVlan : allocatedVlans) {
                if (allocatedVlan.getAccountId() != vlanOwner.getAccountId()) {
                    throw new InvalidParameterValueException("Guest vlan from this range " + allocatedVlan.getVnet() + " is allocated to a different account." + " Can only dedicate a range which has no allocated vlans or has vlans allocated to the same account ");
                }
            }
        }
    }
    List<AccountGuestVlanMapVO> guestVlanMaps = _accountGuestVlanMapDao.listAccountGuestVlanMapsByPhysicalNetwork(physicalNetworkId);
    // Verify if vlan range is already dedicated
    for (AccountGuestVlanMapVO guestVlanMap : guestVlanMaps) {
        List<Integer> vlanTokens = getVlanFromRange(guestVlanMap.getGuestVlanRange());
        int dedicatedStartVlan = vlanTokens.get(0).intValue();
        int dedicatedEndVlan = vlanTokens.get(1).intValue();
        if ((startVlan < dedicatedStartVlan & endVlan >= dedicatedStartVlan) || (startVlan >= dedicatedStartVlan & startVlan <= dedicatedEndVlan)) {
            throw new InvalidParameterValueException("Vlan range is already dedicated. Cannot" + " dedicate guest vlan range " + vlan);
        }
    }
    // Sort the existing dedicated vlan ranges
    Collections.sort(guestVlanMaps, new Comparator<AccountGuestVlanMapVO>() {

        @Override
        public int compare(AccountGuestVlanMapVO obj1, AccountGuestVlanMapVO obj2) {
            List<Integer> vlanTokens1 = getVlanFromRange(obj1.getGuestVlanRange());
            List<Integer> vlanTokens2 = getVlanFromRange(obj2.getGuestVlanRange());
            return vlanTokens1.get(0).compareTo(vlanTokens2.get(0));
        }
    });
    // Verify if vlan range extends an already dedicated range
    for (int i = 0; i < guestVlanMaps.size(); i++) {
        guestVlanMapId = guestVlanMaps.get(i).getId();
        guestVlanMapAccountId = guestVlanMaps.get(i).getAccountId();
        List<Integer> vlanTokens1 = getVlanFromRange(guestVlanMaps.get(i).getGuestVlanRange());
        // Range extends a dedicated vlan range to the left
        if (endVlan == (vlanTokens1.get(0).intValue() - 1)) {
            if (guestVlanMapAccountId == vlanOwnerId) {
                updatedVlanRange = startVlan + "-" + vlanTokens1.get(1).intValue();
            }
            break;
        }
        // Range extends a dedicated vlan range to the right
        if (startVlan == (vlanTokens1.get(1).intValue() + 1) & guestVlanMapAccountId == vlanOwnerId) {
            if (i != (guestVlanMaps.size() - 1)) {
                List<Integer> vlanTokens2 = getVlanFromRange(guestVlanMaps.get(i + 1).getGuestVlanRange());
                // Range extends 2 vlan ranges, both to the right and left
                if (endVlan == (vlanTokens2.get(0).intValue() - 1) && guestVlanMaps.get(i + 1).getAccountId() == vlanOwnerId) {
                    _datacneterVnet.releaseDedicatedGuestVlans(guestVlanMaps.get(i + 1).getId());
                    _accountGuestVlanMapDao.remove(guestVlanMaps.get(i + 1).getId());
                    updatedVlanRange = vlanTokens1.get(0).intValue() + "-" + vlanTokens2.get(1).intValue();
                    break;
                }
            }
            updatedVlanRange = vlanTokens1.get(0).intValue() + "-" + endVlan;
            break;
        }
    }
    // Dedicate vlan range
    AccountGuestVlanMapVO accountGuestVlanMapVO;
    if (updatedVlanRange != null) {
        accountGuestVlanMapVO = _accountGuestVlanMapDao.findById(guestVlanMapId);
        accountGuestVlanMapVO.setGuestVlanRange(updatedVlanRange);
        _accountGuestVlanMapDao.update(guestVlanMapId, accountGuestVlanMapVO);
    } else {
        accountGuestVlanMapVO = new AccountGuestVlanMapVO(vlanOwner.getAccountId(), physicalNetworkId);
        accountGuestVlanMapVO.setGuestVlanRange(startVlan + "-" + endVlan);
        _accountGuestVlanMapDao.persist(accountGuestVlanMapVO);
    }
    // For every guest vlan set the corresponding account guest vlan map id
    List<Integer> finaVlanTokens = getVlanFromRange(accountGuestVlanMapVO.getGuestVlanRange());
    for (int i = finaVlanTokens.get(0).intValue(); i <= finaVlanTokens.get(1).intValue(); i++) {
        List<DataCenterVnetVO> dataCenterVnet = _datacneterVnet.findVnet(physicalNetwork.getDataCenterId(), physicalNetworkId, Integer.toString(i));
        dataCenterVnet.get(0).setAccountGuestVlanMapId(accountGuestVlanMapVO.getId());
        _datacneterVnet.update(dataCenterVnet.get(0).getId(), dataCenterVnet.get(0));
    }
    return accountGuestVlanMapVO;
}
Also used : Account(com.cloud.user.Account) AccountGuestVlanMapVO(com.cloud.network.dao.AccountGuestVlanMapVO) Project(com.cloud.projects.Project) DataCenterVnetVO(com.cloud.dc.DataCenterVnetVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.utils.Pair) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Aggregations

AccountGuestVlanMapVO (com.cloud.network.dao.AccountGuestVlanMapVO)23 ArrayList (java.util.ArrayList)17 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)13 DataCenterVnetVO (com.cloud.dc.DataCenterVnetVO)11 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)8 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)7 DB (com.cloud.utils.db.DB)7 List (java.util.List)7 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)6 Field (java.lang.reflect.Field)6 NetworkVO (com.cloud.network.dao.NetworkVO)5 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)5 ActionEvent (com.cloud.event.ActionEvent)4 Project (com.cloud.projects.Project)4 Account (com.cloud.user.Account)4 Pair (com.cloud.utils.Pair)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