Search in sources :

Example 16 with AccountVlanMapVO

use of com.cloud.dc.AccountVlanMapVO in project cloudstack by apache.

the class ConfigurationManagerImpl method updateVlanAndPublicIpRange.

@DB
@ActionEvent(eventType = EventTypes.EVENT_VLAN_IP_RANGE_UPDATE, eventDescription = "update vlan ip Range", async = false)
public Vlan updateVlanAndPublicIpRange(final long id, String startIp, String endIp, String gateway, String netmask, String startIpv6, String endIpv6, String ip6Gateway, String ip6Cidr, Boolean forSystemVms) throws ConcurrentOperationException {
    VlanVO vlanRange = _vlanDao.findById(id);
    if (vlanRange == null) {
        throw new InvalidParameterValueException("Please specify a valid IP range id.");
    }
    final boolean ipv4 = vlanRange.getVlanGateway() != null;
    final boolean ipv6 = vlanRange.getIp6Gateway() != null;
    if (!ipv4) {
        if (startIp != null || endIp != null || gateway != null || netmask != null) {
            throw new InvalidParameterValueException("IPv4 is not support in this IP range.");
        }
    }
    if (!ipv6) {
        if (startIpv6 != null || endIpv6 != null || ip6Gateway != null || ip6Cidr != null) {
            throw new InvalidParameterValueException("IPv6 is not support in this IP range.");
        }
    }
    final Boolean isRangeForSystemVM = checkIfVlanRangeIsForSystemVM(id);
    if (forSystemVms != null && isRangeForSystemVM != forSystemVms) {
        if (VlanType.DirectAttached.equals(vlanRange.getVlanType())) {
            throw new InvalidParameterValueException("forSystemVms is not available for this IP range with vlan type: " + VlanType.DirectAttached);
        }
        // Check if range has already been dedicated
        final List<AccountVlanMapVO> maps = _accountVlanMapDao.listAccountVlanMapsByVlan(id);
        if (maps != null && !maps.isEmpty()) {
            throw new InvalidParameterValueException("Specified Public IP range has already been dedicated to an account");
        }
        List<DomainVlanMapVO> domainmaps = _domainVlanMapDao.listDomainVlanMapsByVlan(id);
        if (domainmaps != null && !domainmaps.isEmpty()) {
            throw new InvalidParameterValueException("Specified Public IP range has already been dedicated to a domain");
        }
    }
    if (ipv4) {
        updateVlanAndIpv4Range(id, vlanRange, startIp, endIp, gateway, netmask, isRangeForSystemVM, forSystemVms);
    }
    if (ipv6) {
        updateVlanAndIpv6Range(id, vlanRange, startIpv6, endIpv6, ip6Gateway, ip6Cidr, isRangeForSystemVM, forSystemVms);
    }
    return _vlanDao.findById(id);
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) AccountVlanMapVO(com.cloud.dc.AccountVlanMapVO) VlanVO(com.cloud.dc.VlanVO) DomainVlanMapVO(com.cloud.dc.DomainVlanMapVO) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 17 with AccountVlanMapVO

use of com.cloud.dc.AccountVlanMapVO in project cloudstack by apache.

the class ConfigurationManagerImpl method dedicatePublicIpRange.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_VLAN_IP_RANGE_DEDICATE, eventDescription = "dedicating vlan ip range", async = false)
public Vlan dedicatePublicIpRange(final DedicatePublicIpRangeCmd cmd) throws ResourceAllocationException {
    final Long vlanDbId = cmd.getId();
    final String accountName = cmd.getAccountName();
    final Long domainId = cmd.getDomainId();
    final Long projectId = cmd.getProjectId();
    // Check if account is valid
    Account vlanOwner = null;
    if (projectId != null) {
        if (accountName != null) {
            throw new InvalidParameterValueException("accountName and projectId are mutually exclusive");
        }
        final Project project = _projectMgr.getProject(projectId);
        if (project == null) {
            throw new InvalidParameterValueException("Unable to find project by id " + projectId);
        }
        vlanOwner = _accountMgr.getAccount(project.getProjectAccountId());
        if (vlanOwner == null) {
            throw new InvalidParameterValueException("Please specify a valid projectId");
        }
    }
    Domain domain = null;
    if (accountName != null && domainId != null) {
        vlanOwner = _accountDao.findActiveAccount(accountName, domainId);
        if (vlanOwner == null) {
            throw new InvalidParameterValueException("Unable to find account by name " + accountName);
        } else if (vlanOwner.getId() == Account.ACCOUNT_ID_SYSTEM) {
            throw new InvalidParameterValueException("Please specify a valid account. Cannot dedicate IP range to system account");
        }
    } else if (domainId != null) {
        domain = _domainDao.findById(domainId);
        if (domain == null) {
            throw new InvalidParameterValueException("Please specify a valid domain id");
        }
    }
    // Check if range is valid
    final VlanVO vlan = _vlanDao.findById(vlanDbId);
    if (vlan == null) {
        throw new InvalidParameterValueException("Unable to find vlan by id " + vlanDbId);
    }
    // Check if range has already been dedicated
    final List<AccountVlanMapVO> maps = _accountVlanMapDao.listAccountVlanMapsByVlan(vlanDbId);
    if (maps != null && !maps.isEmpty()) {
        throw new InvalidParameterValueException("Specified Public IP range has already been dedicated");
    }
    List<DomainVlanMapVO> domainmaps = _domainVlanMapDao.listDomainVlanMapsByVlan(vlanDbId);
    if (domainmaps != null && !domainmaps.isEmpty()) {
        throw new InvalidParameterValueException("Specified Public IP range has already been dedicated to a domain");
    }
    // Verify that zone exists and is advanced
    final Long zoneId = vlan.getDataCenterId();
    final DataCenterVO zone = _zoneDao.findById(zoneId);
    if (zone == null) {
        throw new InvalidParameterValueException("Unable to find zone by id " + zoneId);
    }
    if (zone.getNetworkType() == NetworkType.Basic) {
        throw new InvalidParameterValueException("Public IP range can be dedicated to an account only in the zone of type " + NetworkType.Advanced);
    }
    // Check Public IP resource limits
    if (vlanOwner != null) {
        final int accountPublicIpRange = _publicIpAddressDao.countIPs(zoneId, vlanDbId, false);
        _resourceLimitMgr.checkResourceLimit(vlanOwner, ResourceType.public_ip, accountPublicIpRange);
    }
    // Check if any of the Public IP addresses is allocated to another
    // account
    boolean forSystemVms = false;
    final List<IPAddressVO> ips = _publicIpAddressDao.listByVlanId(vlanDbId);
    for (final IPAddressVO ip : ips) {
        forSystemVms = ip.isForSystemVms();
        final Long allocatedToAccountId = ip.getAllocatedToAccountId();
        if (allocatedToAccountId != null) {
            if (vlanOwner != null && allocatedToAccountId != vlanOwner.getId()) {
                throw new InvalidParameterValueException(ip.getAddress() + " Public IP address in range is allocated to another account ");
            }
            final Account accountAllocatedTo = _accountMgr.getActiveAccountById(allocatedToAccountId);
            if (vlanOwner == null && domain != null && domain.getId() != accountAllocatedTo.getDomainId()) {
                throw new InvalidParameterValueException(ip.getAddress() + " Public IP address in range is allocated to another domain/account ");
            }
        }
    }
    if (vlanOwner != null) {
        // Create an AccountVlanMapVO entry
        final AccountVlanMapVO accountVlanMapVO = new AccountVlanMapVO(vlanOwner.getId(), vlan.getId());
        _accountVlanMapDao.persist(accountVlanMapVO);
        // generate usage event for dedication of every ip address in the range
        for (final IPAddressVO ip : ips) {
            final boolean usageHidden = _ipAddrMgr.isUsageHidden(ip);
            UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_ASSIGN, vlanOwner.getId(), ip.getDataCenterId(), ip.getId(), ip.getAddress().toString(), ip.isSourceNat(), vlan.getVlanType().toString(), ip.getSystem(), usageHidden, ip.getClass().getName(), ip.getUuid());
        }
    } else if (domain != null && !forSystemVms) {
        // Create an DomainVlanMapVO entry
        DomainVlanMapVO domainVlanMapVO = new DomainVlanMapVO(domain.getId(), vlan.getId());
        _domainVlanMapDao.persist(domainVlanMapVO);
    }
    // increment resource count for dedicated public ip's
    if (vlanOwner != null) {
        _resourceLimitMgr.incrementResourceCount(vlanOwner.getId(), ResourceType.public_ip, new Long(ips.size()));
    }
    return vlan;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) AccountVlanMapVO(com.cloud.dc.AccountVlanMapVO) DomainVlanMapVO(com.cloud.dc.DomainVlanMapVO) Project(com.cloud.projects.Project) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) IPAddressVO(com.cloud.network.dao.IPAddressVO) Domain(com.cloud.domain.Domain) VlanVO(com.cloud.dc.VlanVO) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 18 with AccountVlanMapVO

use of com.cloud.dc.AccountVlanMapVO in project cloudstack by apache.

the class ConfigurationManagerTest method runReleasePublicIpRangePostiveTest1.

void runReleasePublicIpRangePostiveTest1() throws Exception {
    TransactionLegacy txn = TransactionLegacy.open("runReleasePublicIpRangePostiveTest1");
    when(configurationMgr._vlanDao.findById(anyLong())).thenReturn(vlan);
    List<AccountVlanMapVO> accountVlanMaps = new ArrayList<AccountVlanMapVO>();
    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 {
        Boolean result = configurationMgr.releasePublicIpRange(releasePublicIpRangesCmd);
        Assert.assertTrue(result);
    } catch (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) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 19 with AccountVlanMapVO

use of com.cloud.dc.AccountVlanMapVO in project cloudstack by apache.

the class ConfigurationManagerTest method runReleasePublicIpRangePostiveTest2.

void runReleasePublicIpRangePostiveTest2() throws Exception {
    TransactionLegacy txn = TransactionLegacy.open("runReleasePublicIpRangePostiveTest2");
    when(configurationMgr._vlanDao.findById(anyLong())).thenReturn(vlan);
    List<AccountVlanMapVO> accountVlanMaps = new ArrayList<AccountVlanMapVO>();
    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);
    List<IPAddressVO> ipAddressList = new ArrayList<IPAddressVO>();
    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 {
        Boolean result = configurationMgr.releasePublicIpRange(releasePublicIpRangesCmd);
        Assert.assertTrue(result);
    } catch (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) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 20 with AccountVlanMapVO

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

the class ManagementServerImpl method searchForVlans.

@Override
public Pair<List<? extends Vlan>, Integer> searchForVlans(final ListVlanIpRangesCmd cmd) {
    // If an account name and domain ID are specified, look up the account
    final String accountName = cmd.getAccountName();
    final Long domainId = cmd.getDomainId();
    Long accountId = null;
    final Long networkId = cmd.getNetworkId();
    final Boolean forVirtual = cmd.getForVirtualNetwork();
    String vlanType = null;
    final Long projectId = cmd.getProjectId();
    final Long physicalNetworkId = cmd.getPhysicalNetworkId();
    if (accountName != null && domainId != null) {
        if (projectId != null) {
            throw new InvalidParameterValueException("Account and projectId can't be specified together");
        }
        final Account account = _accountDao.findActiveAccount(accountName, domainId);
        if (account == null) {
            final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find account " + accountName + " in specified domain");
            // Since we don't have a DomainVO object here, we directly set
            // tablename to "domain".
            final DomainVO domain = ApiDBUtils.findDomainById(domainId);
            String domainUuid = domainId.toString();
            if (domain != null) {
                domainUuid = domain.getUuid();
            }
            ex.addProxyObject(domainUuid, "domainId");
            throw ex;
        } else {
            accountId = account.getId();
        }
    }
    if (forVirtual != null) {
        if (forVirtual) {
            vlanType = VlanType.VirtualNetwork.toString();
        } else {
            vlanType = VlanType.DirectAttached.toString();
        }
    }
    // set project information
    if (projectId != null) {
        final Project project = _projectMgr.getProject(projectId);
        if (project == null) {
            final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find project by id " + projectId);
            ex.addProxyObject(projectId.toString(), "projectId");
            throw ex;
        }
        accountId = project.getProjectAccountId();
    }
    final Filter searchFilter = new Filter(VlanVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    final Object id = cmd.getId();
    final Object vlan = cmd.getVlan();
    final Object dataCenterId = cmd.getZoneId();
    final Object podId = cmd.getPodId();
    final Object keyword = cmd.getKeyword();
    final SearchBuilder<VlanVO> sb = _vlanDao.createSearchBuilder();
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("vlan", sb.entity().getVlanTag(), SearchCriteria.Op.EQ);
    sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    sb.and("vlan", sb.entity().getVlanTag(), SearchCriteria.Op.EQ);
    sb.and("networkId", sb.entity().getNetworkId(), SearchCriteria.Op.EQ);
    sb.and("vlanType", sb.entity().getVlanType(), SearchCriteria.Op.EQ);
    sb.and("physicalNetworkId", sb.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ);
    if (accountId != null) {
        final SearchBuilder<AccountVlanMapVO> accountVlanMapSearch = _accountVlanMapDao.createSearchBuilder();
        accountVlanMapSearch.and("accountId", accountVlanMapSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
        sb.join("accountVlanMapSearch", accountVlanMapSearch, sb.entity().getId(), accountVlanMapSearch.entity().getVlanDbId(), JoinBuilder.JoinType.INNER);
    }
    if (podId != null) {
        final SearchBuilder<PodVlanMapVO> podVlanMapSearch = _podVlanMapDao.createSearchBuilder();
        podVlanMapSearch.and("podId", podVlanMapSearch.entity().getPodId(), SearchCriteria.Op.EQ);
        sb.join("podVlanMapSearch", podVlanMapSearch, sb.entity().getId(), podVlanMapSearch.entity().getVlanDbId(), JoinBuilder.JoinType.INNER);
    }
    final SearchCriteria<VlanVO> sc = sb.create();
    if (keyword != null) {
        final SearchCriteria<VlanVO> ssc = _vlanDao.createSearchCriteria();
        ssc.addOr("vlanTag", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("ipRange", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("vlanTag", SearchCriteria.Op.SC, ssc);
    } else {
        if (id != null) {
            sc.setParameters("id", id);
        }
        if (vlan != null) {
            sc.setParameters("vlan", vlan);
        }
        if (dataCenterId != null) {
            sc.setParameters("dataCenterId", dataCenterId);
        }
        if (networkId != null) {
            sc.setParameters("networkId", networkId);
        }
        if (accountId != null) {
            sc.setJoinParameters("accountVlanMapSearch", "accountId", accountId);
        }
        if (podId != null) {
            sc.setJoinParameters("podVlanMapSearch", "podId", podId);
        }
        if (vlanType != null) {
            sc.setParameters("vlanType", vlanType);
        }
        if (physicalNetworkId != null) {
            sc.setParameters("physicalNetworkId", physicalNetworkId);
        }
    }
    final Pair<List<VlanVO>, Integer> result = _vlanDao.searchAndCount(sc, searchFilter);
    return new Pair<>(result.first(), result.second());
}
Also used : Account(com.cloud.user.Account) AccountVlanMapVO(com.cloud.dc.AccountVlanMapVO) DomainVO(com.cloud.domain.DomainVO) Project(com.cloud.projects.Project) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) Filter(com.cloud.utils.db.Filter) PodVlanMapVO(com.cloud.dc.PodVlanMapVO) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) VlanVO(com.cloud.dc.VlanVO) Pair(com.cloud.utils.Pair) SSHKeyPair(com.cloud.user.SSHKeyPair)

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