Search in sources :

Example 21 with AccountVlanMapVO

use of com.cloud.dc.AccountVlanMapVO in project cosmic by MissionCriticalCloud.

the class ConfigurationManagerImpl method releasePublicIpRange.

@DB
public boolean releasePublicIpRange(final long vlanDbId, final long userId, final Account caller) {
    VlanVO vlan = _vlanDao.findById(vlanDbId);
    // Verify range is dedicated
    boolean isAccountSpecific = false;
    final List<AccountVlanMapVO> acctVln = _accountVlanMapDao.listAccountVlanMapsByVlan(vlanDbId);
    // Verify range is dedicated
    if (acctVln != null && !acctVln.isEmpty()) {
        isAccountSpecific = true;
    }
    boolean isDomainSpecific = false;
    final List<DomainVlanMapVO> domainVln = _domainVlanMapDao.listDomainVlanMapsByVlan(vlanDbId);
    // Check for domain wide pool. It will have an entry for domain_vlan_map.
    if (domainVln != null && !domainVln.isEmpty()) {
        isDomainSpecific = true;
    }
    if (!isAccountSpecific && !isDomainSpecific) {
        throw new InvalidParameterValueException("Can't release Public IP range " + vlanDbId + " as it not dedicated to any domain and any account");
    }
    // Check if range has any allocated public IPs
    final long allocIpCount = _publicIpAddressDao.countIPs(vlan.getDataCenterId(), vlanDbId, true);
    final List<IPAddressVO> ips = _publicIpAddressDao.listByVlanId(vlanDbId);
    boolean success = true;
    final List<IPAddressVO> ipsInUse = new ArrayList<>();
    if (allocIpCount > 0) {
        try {
            vlan = _vlanDao.acquireInLockTable(vlanDbId, 30);
            if (vlan == null) {
                throw new CloudRuntimeException("Unable to acquire vlan configuration: " + vlanDbId);
            }
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("lock vlan " + vlanDbId + " is acquired");
            }
            for (final IPAddressVO ip : ips) {
                // Disassociate allocated IP's that are not in use
                if (!ip.isOneToOneNat() && !ip.isSourceNat() && !(_firewallDao.countRulesByIpId(ip.getId()) > 0)) {
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Releasing Public IP addresses" + ip + " of vlan " + vlanDbId + " as part of Public IP" + " range release to the system pool");
                    }
                    success = success && _ipAddrMgr.disassociatePublicIpAddress(ip.getId(), userId, caller);
                } else {
                    ipsInUse.add(ip);
                }
            }
            if (!success) {
                s_logger.warn("Some Public IP addresses that were not in use failed to be released as a part of" + " vlan " + vlanDbId + "release to the system pool");
            }
        } finally {
            _vlanDao.releaseFromLockTable(vlanDbId);
        }
    }
    // A Public IP range can only be dedicated to one account at a time
    if (isAccountSpecific && _accountVlanMapDao.remove(acctVln.get(0).getId())) {
        // decrement resource count for dedicated public ip's
        _resourceLimitMgr.decrementResourceCount(acctVln.get(0).getAccountId(), ResourceType.public_ip, new Long(ips.size()));
        return true;
    } else if (isDomainSpecific && _domainVlanMapDao.remove(domainVln.get(0).getId())) {
        s_logger.debug("Remove the vlan from domain_vlan_map successfully.");
        return true;
    } else {
        return false;
    }
}
Also used : AccountVlanMapVO(com.cloud.dc.AccountVlanMapVO) ArrayList(java.util.ArrayList) DomainVlanMapVO(com.cloud.dc.DomainVlanMapVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IPAddressVO(com.cloud.network.dao.IPAddressVO) VlanVO(com.cloud.dc.VlanVO) DB(com.cloud.utils.db.DB)

Example 22 with AccountVlanMapVO

use of com.cloud.dc.AccountVlanMapVO in project cosmic by MissionCriticalCloud.

the class ConfigurationManagerTest method runReleasePublicIpRangePostiveTest2.

void runReleasePublicIpRangePostiveTest2() throws Exception {
    final TransactionLegacy txn = TransactionLegacy.open("runReleasePublicIpRangePostiveTest2");
    when(configurationMgr._vlanDao.findById(anyLong())).thenReturn(vlan);
    final List<AccountVlanMapVO> accountVlanMaps = new ArrayList<>();
    final AccountVlanMapVO accountVlanMap = new AccountVlanMapVO(1, 1);
    accountVlanMaps.add(accountVlanMap);
    when(configurationMgr._accountVlanMapDao.listAccountVlanMapsByVlan(anyLong())).thenReturn(accountVlanMaps);
    when(configurationMgr._publicIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(1);
    final List<IPAddressVO> ipAddressList = new ArrayList<>();
    final IPAddressVO ipAddress = new IPAddressVO(new Ip("75.75.75.75"), 1, 0xaabbccddeeffL, 10, false);
    ipAddressList.add(ipAddress);
    when(configurationMgr._publicIpAddressDao.listByVlanId(anyLong())).thenReturn(ipAddressList);
    when(configurationMgr._firewallDao.countRulesByIpId(anyLong())).thenReturn(0L);
    when(configurationMgr._ipAddrMgr.disassociatePublicIpAddress(anyLong(), anyLong(), any(Account.class))).thenReturn(true);
    when(configurationMgr._vlanDao.releaseFromLockTable(anyLong())).thenReturn(true);
    when(configurationMgr._accountVlanMapDao.remove(anyLong())).thenReturn(true);
    try {
        final Boolean result = configurationMgr.releasePublicIpRange(releasePublicIpRangesCmd);
        Assert.assertTrue(result);
    } catch (final Exception e) {
        s_logger.info("exception in testing runReleasePublicIpRangePostiveTest2 message: " + e.toString());
    } finally {
        txn.close("runReleasePublicIpRangePostiveTest2");
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) Account(com.cloud.user.Account) Ip(com.cloud.utils.net.Ip) AccountVlanMapVO(com.cloud.dc.AccountVlanMapVO) ArrayList(java.util.ArrayList) IPAddressVO(com.cloud.network.dao.IPAddressVO) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 23 with AccountVlanMapVO

use of com.cloud.dc.AccountVlanMapVO in project cosmic by MissionCriticalCloud.

the class ConfigurationManagerTest method runReleasePublicIpRangePostiveTest1.

void runReleasePublicIpRangePostiveTest1() throws Exception {
    final TransactionLegacy txn = TransactionLegacy.open("runReleasePublicIpRangePostiveTest1");
    when(configurationMgr._vlanDao.findById(anyLong())).thenReturn(vlan);
    final List<AccountVlanMapVO> accountVlanMaps = new ArrayList<>();
    final AccountVlanMapVO accountVlanMap = new AccountVlanMapVO(1, 1);
    accountVlanMaps.add(accountVlanMap);
    when(configurationMgr._accountVlanMapDao.listAccountVlanMapsByVlan(anyLong())).thenReturn(accountVlanMaps);
    // no allocated ip's
    when(configurationMgr._publicIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(0);
    when(configurationMgr._accountVlanMapDao.remove(anyLong())).thenReturn(true);
    try {
        final Boolean result = configurationMgr.releasePublicIpRange(releasePublicIpRangesCmd);
        Assert.assertTrue(result);
    } catch (final Exception e) {
        s_logger.info("exception in testing runReleasePublicIpRangePostiveTest1 message: " + e.toString());
    } finally {
        txn.close("runReleasePublicIpRangePostiveTest1");
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) AccountVlanMapVO(com.cloud.dc.AccountVlanMapVO) ArrayList(java.util.ArrayList) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Aggregations

AccountVlanMapVO (com.cloud.dc.AccountVlanMapVO)23 ArrayList (java.util.ArrayList)16 VlanVO (com.cloud.dc.VlanVO)15 IPAddressVO (com.cloud.network.dao.IPAddressVO)13 DomainVlanMapVO (com.cloud.dc.DomainVlanMapVO)12 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)8 Account (com.cloud.user.Account)8 DB (com.cloud.utils.db.DB)8 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)7 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)6 TransactionStatus (com.cloud.utils.db.TransactionStatus)6 List (java.util.List)6 DataCenterVO (com.cloud.dc.DataCenterVO)4 PodVlanMapVO (com.cloud.dc.PodVlanMapVO)4 Project (com.cloud.projects.Project)4 Filter (com.cloud.utils.db.Filter)4 Ip (com.cloud.utils.net.Ip)4 ActionEvent (com.cloud.event.ActionEvent)3 SearchCriteria (com.cloud.utils.db.SearchCriteria)3