Search in sources :

Example 16 with HostPodVO

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

the class DedicatedResourceManagerImpl method createDedicatePodResponse.

@Override
public DedicatePodResponse createDedicatePodResponse(DedicatedResources resource) {
    DedicatePodResponse dedicatePodResponse = new DedicatePodResponse();
    HostPodVO pod = _podDao.findById(resource.getPodId());
    DomainVO domain = _domainDao.findById(resource.getDomainId());
    AccountVO account = _accountDao.findById(resource.getAccountId());
    AffinityGroup group = _affinityGroupDao.findById(resource.getAffinityGroupId());
    dedicatePodResponse.setId(resource.getUuid());
    dedicatePodResponse.setPodId(pod.getUuid());
    dedicatePodResponse.setPodName(pod.getName());
    dedicatePodResponse.setDomainId(domain.getUuid());
    dedicatePodResponse.setAffinityGroupId(group.getUuid());
    if (account != null) {
        dedicatePodResponse.setAccountId(account.getUuid());
    }
    dedicatePodResponse.setObjectName("dedicatedpod");
    return dedicatePodResponse;
}
Also used : DomainVO(com.cloud.domain.DomainVO) DedicatePodResponse(org.apache.cloudstack.api.response.DedicatePodResponse) HostPodVO(com.cloud.dc.HostPodVO) AccountVO(com.cloud.user.AccountVO) AffinityGroup(org.apache.cloudstack.affinity.AffinityGroup)

Example 17 with HostPodVO

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

the class DedicatedResourceManagerImpl method dedicateZone.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_DEDICATE_RESOURCE, eventDescription = "dedicating a Zone")
public List<DedicatedResourceVO> dedicateZone(final Long zoneId, final Long domainId, final String accountName) {
    Long accountId = null;
    List<HostVO> hosts = null;
    if (accountName != null) {
        Account caller = CallContext.current().getCallingAccount();
        Account owner = _accountMgr.finalizeOwner(caller, accountName, domainId, null);
        accountId = owner.getId();
    }
    List<Long> childDomainIds = getDomainChildIds(domainId);
    childDomainIds.add(domainId);
    checkAccountAndDomain(accountId, domainId);
    final DataCenterVO dc = _zoneDao.findById(zoneId);
    if (dc == null) {
        throw new InvalidParameterValueException("Unable to find zone by id " + zoneId);
    } else {
        DedicatedResourceVO dedicatedZone = _dedicatedDao.findByZoneId(zoneId);
        //check if zone is dedicated
        if (dedicatedZone != null) {
            s_logger.error("Zone " + dc.getName() + " is already dedicated");
            throw new CloudRuntimeException("Zone  " + dc.getName() + " is already dedicated");
        }
        //check if any resource under this zone is dedicated to different account or sub-domain
        List<HostPodVO> pods = _podDao.listByDataCenterId(dc.getId());
        List<DedicatedResourceVO> podsToRelease = new ArrayList<DedicatedResourceVO>();
        List<DedicatedResourceVO> clustersToRelease = new ArrayList<DedicatedResourceVO>();
        List<DedicatedResourceVO> hostsToRelease = new ArrayList<DedicatedResourceVO>();
        for (HostPodVO pod : pods) {
            DedicatedResourceVO dPod = _dedicatedDao.findByPodId(pod.getId());
            if (dPod != null) {
                if (!(childDomainIds.contains(dPod.getDomainId()))) {
                    throw new CloudRuntimeException("Pod " + pod.getName() + " under this Zone " + dc.getName() + " is dedicated to different account/domain");
                }
                if (accountId != null) {
                    if (dPod.getAccountId().equals(accountId)) {
                        podsToRelease.add(dPod);
                    } else {
                        s_logger.error("Pod " + pod.getName() + " under this Zone " + dc.getName() + " is dedicated to different account/domain");
                        throw new CloudRuntimeException("Pod " + pod.getName() + " under this Zone " + dc.getName() + " is dedicated to different account/domain");
                    }
                } else {
                    if (dPod.getAccountId() == null && dPod.getDomainId().equals(domainId)) {
                        podsToRelease.add(dPod);
                    }
                }
            }
        }
        for (DedicatedResourceVO dr : podsToRelease) {
            releaseDedicatedResource(null, dr.getPodId(), null, null);
        }
        List<ClusterVO> clusters = _clusterDao.listClustersByDcId(dc.getId());
        for (ClusterVO cluster : clusters) {
            DedicatedResourceVO dCluster = _dedicatedDao.findByClusterId(cluster.getId());
            if (dCluster != null) {
                if (!(childDomainIds.contains(dCluster.getDomainId()))) {
                    throw new CloudRuntimeException("Cluster " + cluster.getName() + " under this Zone " + dc.getName() + " is dedicated to different account/domain");
                }
                if (accountId != null) {
                    if (dCluster.getAccountId().equals(accountId)) {
                        clustersToRelease.add(dCluster);
                    } else {
                        s_logger.error("Cluster " + cluster.getName() + " under this Zone " + dc.getName() + " is dedicated to different account/domain");
                        throw new CloudRuntimeException("Cluster " + cluster.getName() + " under this Zone " + dc.getName() + " is dedicated to different account/domain");
                    }
                } else {
                    if (dCluster.getAccountId() == null && dCluster.getDomainId().equals(domainId)) {
                        clustersToRelease.add(dCluster);
                    }
                }
            }
        }
        for (DedicatedResourceVO dr : clustersToRelease) {
            releaseDedicatedResource(null, null, dr.getClusterId(), null);
        }
        hosts = _hostDao.listByDataCenterId(dc.getId());
        for (HostVO host : hosts) {
            DedicatedResourceVO dHost = _dedicatedDao.findByHostId(host.getId());
            if (dHost != null) {
                if (!(childDomainIds.contains(dHost.getDomainId()))) {
                    throw new CloudRuntimeException("Host " + host.getName() + " under this Zone " + dc.getName() + " is dedicated to different account/domain");
                }
                if (accountId != null) {
                    if (dHost.getAccountId().equals(accountId)) {
                        hostsToRelease.add(dHost);
                    } else {
                        s_logger.error("Host " + host.getName() + " under this Zone " + dc.getName() + " is dedicated to different account/domain");
                        throw new CloudRuntimeException("Host " + host.getName() + " under this Zone " + dc.getName() + " is dedicated to different account/domain");
                    }
                } else {
                    if (dHost.getAccountId() == null && dHost.getDomainId().equals(domainId)) {
                        hostsToRelease.add(dHost);
                    }
                }
            }
        }
        for (DedicatedResourceVO dr : hostsToRelease) {
            releaseDedicatedResource(null, null, null, dr.getHostId());
        }
    }
    checkHostsSuitabilityForExplicitDedication(accountId, childDomainIds, hosts);
    final Long accountIdFinal = accountId;
    return Transaction.execute(new TransactionCallback<List<DedicatedResourceVO>>() {

        @Override
        public List<DedicatedResourceVO> doInTransaction(TransactionStatus status) {
            // find or create the affinity group by name under this account/domain
            AffinityGroup group = findOrCreateDedicatedAffinityGroup(domainId, accountIdFinal);
            if (group == null) {
                s_logger.error("Unable to dedicate zone due to, failed to create dedication affinity group");
                throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support.");
            }
            DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(zoneId, null, null, null, null, null, group.getId());
            try {
                dedicatedResource.setDomainId(domainId);
                if (accountIdFinal != null) {
                    dedicatedResource.setAccountId(accountIdFinal);
                }
                dedicatedResource = _dedicatedDao.persist(dedicatedResource);
                // save the domainId in the zone
                dc.setDomainId(domainId);
                if (!_zoneDao.update(zoneId, dc)) {
                    throw new CloudRuntimeException("Failed to dedicate zone, could not set domainId. Please contact Cloud Support.");
                }
            } catch (Exception e) {
                s_logger.error("Unable to dedicate zone due to " + e.getMessage(), e);
                throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support.");
            }
            List<DedicatedResourceVO> result = new ArrayList<DedicatedResourceVO>();
            result.add(dedicatedResource);
            return result;
        }
    });
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) ClusterVO(com.cloud.dc.ClusterVO) ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) HostPodVO(com.cloud.dc.HostPodVO) HostVO(com.cloud.host.HostVO) AffinityGroup(org.apache.cloudstack.affinity.AffinityGroup) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) List(java.util.List) ArrayList(java.util.ArrayList) DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 18 with HostPodVO

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

the class ConfigurationManagerImpl method podHasAllocatedPrivateIPs.

private boolean podHasAllocatedPrivateIPs(final long podId) {
    final HostPodVO pod = _podDao.findById(podId);
    final int count = _privateIpAddressDao.countIPs(podId, pod.getDataCenterId(), true);
    return count > 0;
}
Also used : HostPodVO(com.cloud.dc.HostPodVO)

Example 19 with HostPodVO

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

the class ConfigurationManagerImpl method editPod.

@Override
@DB
public Pod editPod(final long id, String name, String startIp, String endIp, String gateway, String netmask, String allocationStateStr) {
    // verify parameters
    final HostPodVO pod = _podDao.findById(id);
    if (pod == null) {
        throw new InvalidParameterValueException("Unable to find pod by id " + id);
    }
    final String[] existingPodIpRange = pod.getDescription().split("-");
    String[] leftRangeToAdd = null;
    String[] rightRangeToAdd = null;
    boolean allowToDownsize = false;
    // pod has allocated private IP addresses
    if (podHasAllocatedPrivateIPs(id)) {
        if (netmask != null) {
            final long newCidr = NetUtils.getCidrSize(netmask);
            final long oldCidr = pod.getCidrSize();
            if (newCidr > oldCidr) {
                throw new CloudRuntimeException("The specified pod has allocated private IP addresses, so its IP address range can be extended only");
            }
        }
        if (startIp != null && !startIp.equals(existingPodIpRange[0])) {
            if (NetUtils.ipRangesOverlap(startIp, null, existingPodIpRange[0], existingPodIpRange[1])) {
                throw new CloudRuntimeException("The specified pod has allocated private IP addresses, so its IP address range can be extended only");
            } else {
                leftRangeToAdd = new String[2];
                final long endIpForUpdate = NetUtils.ip2Long(existingPodIpRange[0]) - 1;
                leftRangeToAdd[0] = startIp;
                leftRangeToAdd[1] = NetUtils.long2Ip(endIpForUpdate);
            }
        }
        if (endIp != null && !endIp.equals(existingPodIpRange[1])) {
            if (NetUtils.ipRangesOverlap(endIp, endIp, existingPodIpRange[0], existingPodIpRange[1])) {
                throw new CloudRuntimeException("The specified pod has allocated private IP addresses, so its IP address range can be extended only");
            } else {
                rightRangeToAdd = new String[2];
                final long startIpForUpdate = NetUtils.ip2Long(existingPodIpRange[1]) + 1;
                rightRangeToAdd[0] = NetUtils.long2Ip(startIpForUpdate);
                rightRangeToAdd[1] = endIp;
            }
        }
    } else {
        allowToDownsize = true;
    }
    if (gateway == null) {
        gateway = pod.getGateway();
    }
    if (netmask == null) {
        netmask = NetUtils.getCidrNetmask(pod.getCidrSize());
    }
    final String oldPodName = pod.getName();
    if (name == null) {
        name = oldPodName;
    }
    if (gateway == null) {
        gateway = pod.getGateway();
    }
    if (startIp == null) {
        startIp = existingPodIpRange[0];
    }
    if (endIp == null) {
        endIp = existingPodIpRange[1];
    }
    if (allocationStateStr == null) {
        allocationStateStr = pod.getAllocationState().toString();
    }
    // Verify pod's attributes
    final String cidr = NetUtils.ipAndNetMaskToCidr(gateway, netmask);
    final boolean checkForDuplicates = !oldPodName.equals(name);
    checkPodAttributes(id, name, pod.getDataCenterId(), gateway, cidr, startIp, endIp, allocationStateStr, checkForDuplicates, false);
    try {
        final String[] existingPodIpRangeFinal = existingPodIpRange;
        final String[] leftRangeToAddFinal = leftRangeToAdd;
        final String[] rightRangeToAddFinal = rightRangeToAdd;
        final boolean allowToDownsizeFinal = allowToDownsize;
        final String allocationStateStrFinal = allocationStateStr;
        final String startIpFinal = startIp;
        final String endIpFinal = endIp;
        final String nameFinal = name;
        final String gatewayFinal = gateway;
        Transaction.execute(new TransactionCallbackNoReturn() {

            @Override
            public void doInTransactionWithoutResult(final TransactionStatus status) {
                final long zoneId = pod.getDataCenterId();
                String startIp = startIpFinal;
                String endIp = endIpFinal;
                if (!allowToDownsizeFinal) {
                    if (leftRangeToAddFinal != null) {
                        _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), leftRangeToAddFinal[0], leftRangeToAddFinal[1]);
                    }
                    if (rightRangeToAddFinal != null) {
                        _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), rightRangeToAddFinal[0], rightRangeToAddFinal[1]);
                    }
                } else {
                    // delete the old range
                    _zoneDao.deletePrivateIpAddressByPod(pod.getId());
                    // add the new one
                    if (startIp == null) {
                        startIp = existingPodIpRangeFinal[0];
                    }
                    if (endIp == null) {
                        endIp = existingPodIpRangeFinal[1];
                    }
                    _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), startIp, endIp);
                }
                pod.setName(nameFinal);
                pod.setDataCenterId(zoneId);
                pod.setGateway(gatewayFinal);
                pod.setCidrAddress(getCidrAddress(cidr));
                pod.setCidrSize(getCidrSize(cidr));
                final String ipRange = startIp + "-" + endIp;
                pod.setDescription(ipRange);
                Grouping.AllocationState allocationState = null;
                if (allocationStateStrFinal != null && !allocationStateStrFinal.isEmpty()) {
                    allocationState = Grouping.AllocationState.valueOf(allocationStateStrFinal);
                    pod.setAllocationState(allocationState);
                }
                _podDao.update(id, pod);
            }
        });
    } catch (final Exception e) {
        s_logger.error("Unable to edit pod due to " + e.getMessage(), e);
        throw new CloudRuntimeException("Failed to edit pod. Please contact Cloud Support.");
    }
    return pod;
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AllocationState(com.cloud.org.Grouping.AllocationState) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) HostPodVO(com.cloud.dc.HostPodVO) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConfigurationException(javax.naming.ConfigurationException) DB(com.cloud.utils.db.DB)

Example 20 with HostPodVO

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

the class ConsoleProxyManagerImpl method createProxyInstance.

protected Map<String, Object> createProxyInstance(long dataCenterId, VMTemplateVO template) throws ConcurrentOperationException {
    long id = _consoleProxyDao.getNextInSequence(Long.class, "id");
    String name = VirtualMachineName.getConsoleProxyName(id, _instance);
    DataCenterVO dc = _dcDao.findById(dataCenterId);
    Account systemAcct = _accountMgr.getSystemAccount();
    DataCenterDeployment plan = new DataCenterDeployment(dataCenterId);
    NetworkVO defaultNetwork = getDefaultNetworkForCreation(dc);
    List<? extends NetworkOffering> offerings = _networkModel.getSystemAccountNetworkOfferings(NetworkOffering.SystemControlNetwork, NetworkOffering.SystemManagementNetwork);
    LinkedHashMap<Network, List<? extends NicProfile>> networks = new LinkedHashMap<Network, List<? extends NicProfile>>(offerings.size() + 1);
    NicProfile defaultNic = new NicProfile();
    defaultNic.setDefaultNic(true);
    defaultNic.setDeviceId(2);
    networks.put(_networkMgr.setupNetwork(systemAcct, _networkOfferingDao.findById(defaultNetwork.getNetworkOfferingId()), plan, null, null, false).get(0), new ArrayList<NicProfile>(Arrays.asList(defaultNic)));
    for (NetworkOffering offering : offerings) {
        networks.put(_networkMgr.setupNetwork(systemAcct, offering, plan, null, null, false).get(0), new ArrayList<NicProfile>());
    }
    ServiceOfferingVO serviceOffering = _serviceOffering;
    if (serviceOffering == null) {
        serviceOffering = _offeringDao.findDefaultSystemOffering(ServiceOffering.consoleProxyDefaultOffUniqueName, ConfigurationManagerImpl.SystemVMUseLocalStorage.valueIn(dataCenterId));
    }
    ConsoleProxyVO proxy = new ConsoleProxyVO(id, serviceOffering.getId(), name, template.getId(), template.getHypervisorType(), template.getGuestOSId(), dataCenterId, systemAcct.getDomainId(), systemAcct.getId(), _accountMgr.getSystemUser().getId(), 0, serviceOffering.getOfferHA());
    proxy.setDynamicallyScalable(template.isDynamicallyScalable());
    proxy = _consoleProxyDao.persist(proxy);
    try {
        _itMgr.allocate(name, template, serviceOffering, networks, plan, null);
    } catch (InsufficientCapacityException e) {
        s_logger.warn("InsufficientCapacity", e);
        throw new CloudRuntimeException("Insufficient capacity exception", e);
    }
    Map<String, Object> context = new HashMap<String, Object>();
    context.put("dc", dc);
    HostPodVO pod = _podDao.findById(proxy.getPodIdToDeployIn());
    context.put("pod", pod);
    context.put("proxyVmId", proxy.getId());
    return context;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) NetworkVO(com.cloud.network.dao.NetworkVO) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) NetworkOffering(com.cloud.offering.NetworkOffering) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) NicProfile(com.cloud.vm.NicProfile) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) HostPodVO(com.cloud.dc.HostPodVO) LinkedHashMap(java.util.LinkedHashMap) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) ArrayList(java.util.ArrayList) List(java.util.List) ConsoleProxyVO(com.cloud.vm.ConsoleProxyVO) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException)

Aggregations

HostPodVO (com.cloud.dc.HostPodVO)68 DataCenterVO (com.cloud.dc.DataCenterVO)32 HostVO (com.cloud.host.HostVO)32 ClusterVO (com.cloud.dc.ClusterVO)31 ArrayList (java.util.ArrayList)29 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)23 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)16 VolumeVO (com.cloud.storage.VolumeVO)14 VMInstanceVO (com.cloud.vm.VMInstanceVO)14 Account (com.cloud.user.Account)13 DB (com.cloud.utils.db.DB)12 Test (org.junit.Test)12 Random (java.util.Random)11 TransactionStatus (com.cloud.utils.db.TransactionStatus)10 ConfigurationException (javax.naming.ConfigurationException)10 HashMap (java.util.HashMap)8 List (java.util.List)8 DataCenter (com.cloud.dc.DataCenter)7 DedicatedResourceVO (com.cloud.dc.DedicatedResourceVO)7 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)6