Search in sources :

Example 11 with AccountVlanMapVO

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

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.isForVirtualNetwork();
    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 (domainId != null) {
        DomainVO domain = ApiDBUtils.findDomainById(domainId);
        if (domain == null) {
            throw new InvalidParameterValueException("Unable to find domain with id " + domainId);
        }
        final SearchBuilder<DomainVlanMapVO> domainVlanMapSearch = _domainVlanMapDao.createSearchBuilder();
        domainVlanMapSearch.and("domainId", domainVlanMapSearch.entity().getDomainId(), SearchCriteria.Op.EQ);
        sb.join("domainVlanMapSearch", domainVlanMapSearch, sb.entity().getId(), domainVlanMapSearch.entity().getVlanDbId(), 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);
        }
        if (domainId != null) {
            sc.setJoinParameters("domainVlanMapSearch", "domainId", domainId);
        }
    }
    final Pair<List<VlanVO>, Integer> result = _vlanDao.searchAndCount(sc, searchFilter);
    return new Pair<List<? extends Vlan>, Integer>(result.first(), result.second());
}
Also used : Account(com.cloud.user.Account) AccountVlanMapVO(com.cloud.dc.AccountVlanMapVO) DomainVlanMapVO(com.cloud.dc.DomainVlanMapVO) NetworkDomainVO(com.cloud.network.dao.NetworkDomainVO) DomainVO(com.cloud.domain.DomainVO) Project(com.cloud.projects.Project) InvalidParameterValueException(com.cloud.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)

Example 12 with AccountVlanMapVO

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

the class ConfigurationManagerImpl method deleteVlanAndPublicIpRange.

@Override
@DB
public boolean deleteVlanAndPublicIpRange(final long userId, final long vlanDbId, final Account caller) {
    VlanVO vlanRange = _vlanDao.findById(vlanDbId);
    if (vlanRange == null) {
        throw new InvalidParameterValueException("Please specify a valid IP range id.");
    }
    boolean isAccountSpecific = false;
    final List<AccountVlanMapVO> acctVln = _accountVlanMapDao.listAccountVlanMapsByVlan(vlanRange.getId());
    // account_vlan_map.
    if (acctVln != null && !acctVln.isEmpty()) {
        isAccountSpecific = true;
    }
    boolean isDomainSpecific = false;
    List<DomainVlanMapVO> domainVlan = _domainVlanMapDao.listDomainVlanMapsByVlan(vlanRange.getId());
    // Check for domain wide pool. It will have an entry for domain_vlan_map.
    if (domainVlan != null && !domainVlan.isEmpty()) {
        isDomainSpecific = true;
    }
    // Check if the VLAN has any allocated public IPs
    final List<IPAddressVO> ips = _publicIpAddressDao.listByVlanId(vlanDbId);
    if (isAccountSpecific) {
        int resourceCountToBeDecrement = 0;
        try {
            vlanRange = _vlanDao.acquireInLockTable(vlanDbId, 30);
            if (vlanRange == 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) {
                boolean success = true;
                if (ip.isOneToOneNat()) {
                    throw new InvalidParameterValueException("Can't delete account specific vlan " + vlanDbId + " as ip " + ip + " belonging to the range is used for static nat purposes. Cleanup the rules first");
                }
                if (ip.isSourceNat()) {
                    throw new InvalidParameterValueException("Can't delete account specific vlan " + vlanDbId + " as ip " + ip + " belonging to the range is a source nat ip for the network id=" + ip.getSourceNetworkId() + ". IP range with the source nat ip address can be removed either as a part of Network, or account removal");
                }
                if (_firewallDao.countRulesByIpId(ip.getId()) > 0) {
                    throw new InvalidParameterValueException("Can't delete account specific vlan " + vlanDbId + " as ip " + ip + " belonging to the range has firewall rules applied. Cleanup the rules first");
                }
                if (ip.getAllocatedTime() != null) {
                    // This means IP is allocated
                    // release public ip address here
                    success = _ipAddrMgr.disassociatePublicIpAddress(ip.getId(), userId, caller);
                }
                if (!success) {
                    s_logger.warn("Some ip addresses failed to be released as a part of vlan " + vlanDbId + " removal");
                } else {
                    resourceCountToBeDecrement++;
                    final boolean usageHidden = _ipAddrMgr.isUsageHidden(ip);
                    UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_RELEASE, acctVln.get(0).getAccountId(), ip.getDataCenterId(), ip.getId(), ip.getAddress().toString(), ip.isSourceNat(), vlanRange.getVlanType().toString(), ip.getSystem(), usageHidden, ip.getClass().getName(), ip.getUuid());
                }
            }
        } finally {
            _vlanDao.releaseFromLockTable(vlanDbId);
            if (resourceCountToBeDecrement > 0) {
                // Making sure to decrement the count of only success operations above. For any reaason if disassociation fails then this number will vary from original range length.
                _resourceLimitMgr.decrementResourceCount(acctVln.get(0).getAccountId(), ResourceType.public_ip, new Long(resourceCountToBeDecrement));
            }
        }
    } else {
        // !isAccountSpecific
        final NicIpAliasVO ipAlias = _nicIpAliasDao.findByGatewayAndNetworkIdAndState(vlanRange.getVlanGateway(), vlanRange.getNetworkId(), NicIpAlias.State.active);
        // check if the ipalias belongs to the vlan range being deleted.
        if (ipAlias != null && vlanDbId == _publicIpAddressDao.findByIpAndSourceNetworkId(vlanRange.getNetworkId(), ipAlias.getIp4Address()).getVlanId()) {
            throw new InvalidParameterValueException("Cannot delete vlan range " + vlanDbId + " as " + ipAlias.getIp4Address() + "is being used for providing dhcp service in this subnet. Delete all VMs in this subnet and try again");
        }
        final long allocIpCount = _publicIpAddressDao.countIPs(vlanRange.getDataCenterId(), vlanDbId, true);
        if (allocIpCount > 0) {
            throw new InvalidParameterValueException(allocIpCount + "  Ips are in use. Cannot delete this vlan");
        }
    }
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            _publicIpAddressDao.deletePublicIPRange(vlanDbId);
            s_logger.debug(String.format("Delete Public IP Range (from user_ip_address, where vlan_db_d=%s)", vlanDbId));
            _vlanDao.remove(vlanDbId);
            s_logger.debug(String.format("Mark vlan as Remove vlan (vlan_db_id=%s)", vlanDbId));
            SearchBuilder<PodVlanMapVO> sb = podVlanMapDao.createSearchBuilder();
            sb.and("vlan_db_id", sb.entity().getVlanDbId(), SearchCriteria.Op.EQ);
            SearchCriteria<PodVlanMapVO> sc = sb.create();
            sc.setParameters("vlan_db_id", vlanDbId);
            podVlanMapDao.remove(sc);
            s_logger.debug(String.format("Delete vlan_db_id=%s in pod_vlan_map", vlanDbId));
        }
    });
    return true;
}
Also used : AccountVlanMapVO(com.cloud.dc.AccountVlanMapVO) SearchBuilder(com.cloud.utils.db.SearchBuilder) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) NicIpAliasVO(com.cloud.vm.dao.NicIpAliasVO) DomainVlanMapVO(com.cloud.dc.DomainVlanMapVO) SearchCriteria(com.cloud.utils.db.SearchCriteria) InvalidParameterValueException(com.cloud.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 13 with AccountVlanMapVO

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

the class ConfigurationManagerImpl method commitVlanAndIpRange.

private VlanVO commitVlanAndIpRange(final long zoneId, final long networkId, final long physicalNetworkId, final Long podId, final String startIP, final String endIP, final String vlanGateway, final String vlanNetmask, final String vlanId, final Domain domain, final Account vlanOwner, final String vlanIp6Gateway, final String vlanIp6Cidr, final boolean ipv4, final DataCenterVO zone, final VlanType vlanType, final String ipv6Range, final String ipRange, final boolean forSystemVms) {
    return Transaction.execute(new TransactionCallback<VlanVO>() {

        @Override
        public VlanVO doInTransaction(final TransactionStatus status) {
            VlanVO vlan = new VlanVO(vlanType, vlanId, vlanGateway, vlanNetmask, zone.getId(), ipRange, networkId, physicalNetworkId, vlanIp6Gateway, vlanIp6Cidr, ipv6Range);
            s_logger.debug("Saving vlan range " + vlan);
            vlan = _vlanDao.persist(vlan);
            // public ip range
            if (ipv4) {
                if (!savePublicIPRange(startIP, endIP, zoneId, vlan.getId(), networkId, physicalNetworkId, forSystemVms)) {
                    throw new CloudRuntimeException("Failed to save IPv4 range. Please contact Cloud Support.");
                }
            }
            if (vlanOwner != null) {
                // This VLAN is account-specific, so 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
                final List<IPAddressVO> ips = _publicIpAddressDao.listByVlanId(vlan.getId());
                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());
                }
                // increment resource count for dedicated public ip's
                _resourceLimitMgr.incrementResourceCount(vlanOwner.getId(), ResourceType.public_ip, new Long(ips.size()));
            } else if (domain != null && !forSystemVms) {
                // This VLAN is domain-wide, so create a DomainVlanMapVO entry
                final DomainVlanMapVO domainVlanMapVO = new DomainVlanMapVO(domain.getId(), vlan.getId());
                _domainVlanMapDao.persist(domainVlanMapVO);
            } else if (podId != null) {
                // This VLAN is pod-wide, so create a PodVlanMapVO entry
                final PodVlanMapVO podVlanMapVO = new PodVlanMapVO(podId, vlan.getId());
                podVlanMapDao.persist(podVlanMapVO);
            }
            return vlan;
        }
    });
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AccountVlanMapVO(com.cloud.dc.AccountVlanMapVO) PodVlanMapVO(com.cloud.dc.PodVlanMapVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) ArrayList(java.util.ArrayList) List(java.util.List) IPAddressVO(com.cloud.network.dao.IPAddressVO) VlanVO(com.cloud.dc.VlanVO) DomainVlanMapVO(com.cloud.dc.DomainVlanMapVO)

Example 14 with AccountVlanMapVO

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

the class ConfigurationManagerTest method runDedicatePublicIpRangeDedicatedRange.

void runDedicatePublicIpRangeDedicatedRange() throws Exception {
    TransactionLegacy txn = TransactionLegacy.open("runDedicatePublicIpRangeDedicatedRange");
    when(configurationMgr._vlanDao.findById(anyLong())).thenReturn(vlan);
    // public ip range is already dedicated
    List<AccountVlanMapVO> accountVlanMaps = new ArrayList<AccountVlanMapVO>();
    AccountVlanMapVO accountVlanMap = new AccountVlanMapVO(1, 1);
    accountVlanMaps.add(accountVlanMap);
    when(configurationMgr._accountVlanMapDao.listAccountVlanMapsByVlan(anyLong())).thenReturn(accountVlanMaps);
    DataCenterVO dc = new DataCenterVO(UUID.randomUUID().toString(), "test", "8.8.8.8", null, "10.0.0.1", null, "10.0.0.1/24", null, null, NetworkType.Advanced, null, null, true, true, null, null);
    when(configurationMgr._zoneDao.findById(anyLong())).thenReturn(dc);
    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);
    try {
        configurationMgr.dedicatePublicIpRange(dedicatePublicIpRangesCmd);
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("Public IP range has already been dedicated"));
    } finally {
        txn.close("runDedicatePublicIpRangePublicIpRangeDedicated");
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) Ip(com.cloud.utils.net.Ip) AccountVlanMapVO(com.cloud.dc.AccountVlanMapVO) ArrayList(java.util.ArrayList) IPAddressVO(com.cloud.network.dao.IPAddressVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 15 with AccountVlanMapVO

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

the class VlanDaoImpl method listVlansForAccountByType.

@Override
public List<VlanVO> listVlansForAccountByType(Long zoneId, long accountId, VlanType vlanType) {
    // FIXME: use a join statement to improve the performance (should be minor since we expect only one or two)
    List<AccountVlanMapVO> vlanMaps = _accountVlanMapDao.listAccountVlanMapsByAccount(accountId);
    List<VlanVO> result = new ArrayList<VlanVO>();
    for (AccountVlanMapVO acvmvo : vlanMaps) {
        VlanVO vlan = findById(acvmvo.getVlanDbId());
        if (vlan.getVlanType() == vlanType && (zoneId == null || vlan.getDataCenterId() == zoneId)) {
            result.add(vlan);
        }
    }
    return result;
}
Also used : AccountVlanMapVO(com.cloud.dc.AccountVlanMapVO) ArrayList(java.util.ArrayList) VlanVO(com.cloud.dc.VlanVO)

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