Search in sources :

Example 66 with HostPodVO

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

the class ResourceManagerImpl method listByDataCenter.

@Override
public List<PodCluster> listByDataCenter(final long dcId) {
    final List<HostPodVO> pods = _podDao.listByDataCenterId(dcId);
    final ArrayList<PodCluster> pcs = new ArrayList<>();
    for (final HostPodVO pod : pods) {
        final List<ClusterVO> clusters = _clusterDao.listByPodId(pod.getId());
        if (clusters.size() == 0) {
            pcs.add(new PodCluster(pod, null));
        } else {
            for (final ClusterVO cluster : clusters) {
                pcs.add(new PodCluster(pod, cluster));
            }
        }
    }
    return pcs;
}
Also used : ClusterVO(com.cloud.dc.ClusterVO) ArrayList(java.util.ArrayList) HostPodVO(com.cloud.dc.HostPodVO) PodCluster(com.cloud.dc.PodCluster)

Example 67 with HostPodVO

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

the class ResourceManagerImpl method discoverCluster.

@DB
@Override
public List<? extends Cluster> discoverCluster(final AddClusterCmd cmd) throws IllegalArgumentException, DiscoveryException, ResourceInUseException {
    final ResourceChecker resourceChecker = ResourceChecker.builder().dataCenterDao(_dcDao).accountManager(_accountMgr).hostPodDao(_podDao).build();
    final long dcId = cmd.getZoneId();
    final long podId = cmd.getPodId();
    final String clusterName = cmd.getClusterName();
    String url = cmd.getUrl();
    final String username = cmd.getUsername();
    final String password = cmd.getPassword();
    if (url != null) {
        url = URLDecoder.decode(url);
    }
    final URI uri;
    final DataCenterVO zone = resourceChecker.checkIfDataCenterExists(dcId);
    final Account account = CallContext.current().getCallingAccount();
    resourceChecker.checkIfDataCenterIsUsable(zone, account);
    final HostPodVO pod = _podDao.findById(podId);
    if (pod == null) {
        throw new InvalidParameterValueException("Can't find pod with specified podId " + podId);
    }
    // Check if the pod exists in the system
    if (_podDao.findById(podId) == null) {
        throw new InvalidParameterValueException("Can't find pod by id " + podId);
    }
    // check if pod belongs to the zone
    if (!Long.valueOf(pod.getDataCenterId()).equals(dcId)) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Pod with specified id doesn't belong to the zone " + dcId);
        ex.addProxyObject(pod.getUuid(), "podId");
        ex.addProxyObject(zone.getUuid(), "dcId");
        throw ex;
    }
    // Verify cluster information and create a new cluster if needed
    if (clusterName == null || clusterName.isEmpty()) {
        throw new InvalidParameterValueException("Please specify cluster name");
    }
    if (cmd.getHypervisor() == null || cmd.getHypervisor().isEmpty()) {
        throw new InvalidParameterValueException("Please specify a hypervisor");
    }
    final Hypervisor.HypervisorType hypervisorType = Hypervisor.HypervisorType.getType(cmd.getHypervisor());
    if (hypervisorType == null) {
        s_logger.error("Unable to resolve " + cmd.getHypervisor() + " to a valid supported hypervisor type");
        throw new InvalidParameterValueException("Unable to resolve " + cmd.getHypervisor() + " to a supported ");
    }
    Cluster.ClusterType clusterType = null;
    if (cmd.getClusterType() != null && !cmd.getClusterType().isEmpty()) {
        clusterType = Cluster.ClusterType.valueOf(cmd.getClusterType());
    }
    if (clusterType == null) {
        clusterType = Cluster.ClusterType.CloudManaged;
    }
    AllocationState allocationState = null;
    if (cmd.getAllocationState() != null && !cmd.getAllocationState().isEmpty()) {
        try {
            allocationState = AllocationState.valueOf(cmd.getAllocationState());
        } catch (final IllegalArgumentException ex) {
            throw new InvalidParameterValueException("Unable to resolve Allocation State '" + cmd.getAllocationState() + "' to a supported state");
        }
    }
    if (allocationState == null) {
        allocationState = AllocationState.Enabled;
    }
    final Discoverer discoverer = getMatchingDiscover(hypervisorType);
    if (discoverer == null) {
        throw new InvalidParameterValueException("Could not find corresponding resource manager for " + cmd.getHypervisor());
    }
    final List<ClusterVO> result = new ArrayList<>();
    ClusterVO cluster = new ClusterVO(dcId, podId, clusterName);
    cluster.setHypervisorType(hypervisorType.toString());
    cluster.setClusterType(clusterType);
    cluster.setAllocationState(allocationState);
    try {
        cluster = _clusterDao.persist(cluster);
    } catch (final Exception e) {
        // no longer tolerate exception during the cluster creation phase
        final CloudRuntimeException ex = new CloudRuntimeException("Unable to create cluster " + clusterName + " in pod and data center with specified ids", e);
        // Get the pod VO object's table name.
        ex.addProxyObject(pod.getUuid(), "podId");
        ex.addProxyObject(zone.getUuid(), "dcId");
        throw ex;
    }
    result.add(cluster);
    if (clusterType == Cluster.ClusterType.CloudManaged) {
        final Map<String, String> details = new HashMap<>();
        details.put("cpuOvercommitRatio", CapacityManager.CpuOverprovisioningFactor.value().toString());
        details.put("memoryOvercommitRatio", CapacityManager.MemOverprovisioningFactor.value().toString());
        _clusterDetailsDao.persist(cluster.getId(), details);
        return result;
    }
    // save cluster details for later cluster/host cross-checking
    final Map<String, String> details = new HashMap<>();
    details.put("url", url);
    details.put("username", username);
    details.put("password", password);
    details.put("cpuOvercommitRatio", CapacityManager.CpuOverprovisioningFactor.value().toString());
    details.put("memoryOvercommitRatio", CapacityManager.MemOverprovisioningFactor.value().toString());
    _clusterDetailsDao.persist(cluster.getId(), details);
    boolean success = false;
    try {
        try {
            uri = new URI(UriUtils.encodeURIComponent(url));
            if (uri.getScheme() == null) {
                throw new InvalidParameterValueException("uri.scheme is null " + url + ", add http:// as a prefix");
            } else if (uri.getScheme().equalsIgnoreCase("http")) {
                if (uri.getHost() == null || uri.getHost().equalsIgnoreCase("") || uri.getPath() == null || uri.getPath().equalsIgnoreCase("")) {
                    throw new InvalidParameterValueException("Your host and/or path is wrong.  Make sure it's of the format http://hostname/path");
                }
            }
        } catch (final URISyntaxException e) {
            throw new InvalidParameterValueException(url + " is not a valid uri");
        }
        final List<HostVO> hosts = new ArrayList<>();
        final Map<? extends ServerResource, Map<String, String>> resources;
        resources = discoverer.find(dcId, podId, cluster.getId(), uri, username, password, null);
        if (resources != null) {
            for (final Map.Entry<? extends ServerResource, Map<String, String>> entry : resources.entrySet()) {
                final ServerResource resource = entry.getKey();
                final HostVO host = (HostVO) createHostAndAgent(resource, entry.getValue(), true, null, false);
                if (host != null) {
                    hosts.add(host);
                }
                discoverer.postDiscovery(hosts, _nodeId);
            }
            s_logger.info("External cluster has been successfully discovered by " + discoverer.getName());
            success = true;
            return result;
        }
        s_logger.warn("Unable to find the server resources at " + url);
        throw new DiscoveryException("Unable to add the external cluster");
    } finally {
        if (!success) {
            _clusterDetailsDao.deleteDetails(cluster.getId());
            _clusterDao.remove(cluster.getId());
        }
    }
}
Also used : Account(com.cloud.user.Account) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HostPodVO(com.cloud.dc.HostPodVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DiscoveryException(com.cloud.exception.DiscoveryException) DataCenterVO(com.cloud.dc.DataCenterVO) ClusterVO(com.cloud.dc.ClusterVO) Hypervisor(com.cloud.hypervisor.Hypervisor) PodCluster(com.cloud.dc.PodCluster) Cluster(com.cloud.org.Cluster) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceInUseException(com.cloud.exception.ResourceInUseException) URISyntaxException(java.net.URISyntaxException) DiscoveryException(com.cloud.exception.DiscoveryException) SshException(com.cloud.utils.ssh.SshException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO) AllocationState(com.cloud.model.enumeration.AllocationState) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) Map(java.util.Map) HashMap(java.util.HashMap) DB(com.cloud.utils.db.DB)

Example 68 with HostPodVO

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

the class ResourceManagerImpl method discoverHostsFull.

private List<HostVO> discoverHostsFull(final Long dcId, final Long podId, Long clusterId, final String clusterName, final String url, final String username, final String password, final String hypervisorType, final List<String> hostTags, final Map<String, String> params, final boolean deferAgentCreation) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException {
    final ResourceChecker resourceChecker = ResourceChecker.builder().dataCenterDao(_dcDao).accountManager(_accountMgr).hostPodDao(_podDao).build();
    s_logger.info("Trying to discover host for " + hypervisorType);
    final DataCenterVO zone = resourceChecker.checkIfDataCenterExists(dcId);
    final Account account = CallContext.current().getCallingAccount();
    resourceChecker.checkIfDataCenterIsUsable(zone, account);
    final HostPodVO pod = resourceChecker.checkIfPodExists(podId);
    resourceChecker.checkIfPodIsUsable(zone, pod);
    verifyClusterInfo(podId, clusterId, clusterName, hypervisorType);
    clusterId = getOrCreateCluster(zone, pod, clusterId, clusterName, hypervisorType);
    final URI uri = generateUri(url);
    final List<HostVO> hosts = new ArrayList<>();
    s_logger.info("Trying to add a new host at " + url + " in data center " + dcId);
    boolean isHypervisorTypeSupported = false;
    for (final Discoverer discoverer : _discoverers) {
        if (params != null) {
            discoverer.putParam(params);
        }
        if (!discoverer.matchHypervisor(hypervisorType)) {
            s_logger.debug("Discoverer " + discoverer.getName() + " for " + discoverer.getHypervisorType() + " is not a match for " + hypervisorType);
            continue;
        }
        s_logger.debug("Discoverer " + discoverer.getName() + " for " + discoverer.getHypervisorType() + " is a match for " + hypervisorType);
        isHypervisorTypeSupported = true;
        Map<? extends ServerResource, Map<String, String>> resources = null;
        processResourceEvent(ResourceListener.EVENT_DISCOVER_BEFORE, dcId, podId, clusterId, uri, username, password, hostTags);
        try {
            resources = discoverer.find(dcId, podId, clusterId, uri, username, password, hostTags);
        } catch (final DiscoveryException e) {
            throw e;
        } catch (final Exception e) {
            s_logger.info("Exception in host discovery process with discoverer: " + discoverer.getName() + ", skip to another discoverer if there is any");
        }
        processResourceEvent(ResourceListener.EVENT_DISCOVER_AFTER, resources);
        if (resources != null) {
            for (final Map.Entry<? extends ServerResource, Map<String, String>> entry : resources.entrySet()) {
                final ServerResource resource = entry.getKey();
                /*
                     * For KVM, if we go to here, that means kvm agent is
                     * already connected to mgt svr.
                     */
                if (resource instanceof KvmDummyResourceBase) {
                    final Map<String, String> details = entry.getValue();
                    final String guid = details.get("guid");
                    final List<HostVO> kvmHosts = listAllUpAndEnabledHosts(Host.Type.Routing, clusterId, podId, dcId);
                    for (final HostVO host : kvmHosts) {
                        if (host.getGuid().equalsIgnoreCase(guid)) {
                            if (hostTags != null) {
                                if (s_logger.isTraceEnabled()) {
                                    s_logger.trace("Adding Host Tags for KVM host, tags:  :" + hostTags);
                                }
                                _hostTagsDao.persist(host.getId(), hostTags);
                            }
                            hosts.add(host);
                            return hosts;
                        }
                    }
                    return null;
                }
                final HostVO host;
                if (deferAgentCreation) {
                    host = (HostVO) createHostAndAgentDeferred(resource, entry.getValue(), true, hostTags, false);
                } else {
                    host = (HostVO) createHostAndAgent(resource, entry.getValue(), true, hostTags, false);
                }
                if (host != null) {
                    hosts.add(host);
                }
                discoverer.postDiscovery(hosts, _nodeId);
            }
            s_logger.info("server resources successfully discovered by " + discoverer.getName());
            return hosts;
        }
    }
    if (!isHypervisorTypeSupported) {
        final String msg = "Do not support HypervisorType " + hypervisorType + " for " + url;
        s_logger.warn(msg);
        throw new DiscoveryException(msg);
    }
    s_logger.warn("Unable to find the server resources at " + url);
    throw new DiscoveryException("Unable to add the host");
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) HostPodVO(com.cloud.dc.HostPodVO) URI(java.net.URI) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceInUseException(com.cloud.exception.ResourceInUseException) URISyntaxException(java.net.URISyntaxException) DiscoveryException(com.cloud.exception.DiscoveryException) SshException(com.cloud.utils.ssh.SshException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) KvmDummyResourceBase(com.cloud.hypervisor.kvm.discoverer.KvmDummyResourceBase) Map(java.util.Map) HashMap(java.util.HashMap) DiscoveryException(com.cloud.exception.DiscoveryException)

Example 69 with HostPodVO

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

the class DedicatedResourceManagerImpl method dedicateCluster.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_DEDICATE_RESOURCE, eventDescription = "dedicating a Cluster")
public List<DedicatedResourceVO> dedicateCluster(final Long clusterId, final Long domainId, final String accountName) {
    Long accountId = null;
    List<HostVO> hosts = null;
    if (accountName != null) {
        final Account caller = CallContext.current().getCallingAccount();
        final Account owner = _accountMgr.finalizeOwner(caller, accountName, domainId, null);
        accountId = owner.getId();
    }
    final List<Long> childDomainIds = getDomainChildIds(domainId);
    childDomainIds.add(domainId);
    checkAccountAndDomain(accountId, domainId);
    final ClusterVO cluster = _clusterDao.findById(clusterId);
    if (cluster == null) {
        throw new InvalidParameterValueException("Unable to find cluster by id " + clusterId);
    } else {
        final DedicatedResourceVO dedicatedCluster = _dedicatedDao.findByClusterId(clusterId);
        final DedicatedResourceVO dedicatedPodOfCluster = _dedicatedDao.findByPodId(cluster.getPodId());
        final DedicatedResourceVO dedicatedZoneOfCluster = _dedicatedDao.findByZoneId(cluster.getDataCenterId());
        // check if cluster is dedicated
        if (dedicatedCluster != null) {
            s_logger.error("Cluster " + cluster.getName() + " is already dedicated");
            throw new CloudRuntimeException("Cluster " + cluster.getName() + " is already dedicated");
        }
        if (dedicatedPodOfCluster != null) {
            final boolean domainIdInChildreanList = getDomainChildIds(dedicatedPodOfCluster.getDomainId()).contains(domainId);
            // can dedicate a cluster to an account/domain if pod is dedicated to parent-domain
            if (dedicatedPodOfCluster.getAccountId() != null || (accountId == null && !domainIdInChildreanList) || (accountId != null && !(dedicatedPodOfCluster.getDomainId().equals(domainId) || domainIdInChildreanList))) {
                s_logger.error("Cannot dedicate Cluster. Its Pod is already dedicated");
                final HostPodVO pod = _podDao.findById(cluster.getPodId());
                throw new CloudRuntimeException("Cluster's Pod " + pod.getName() + " is already dedicated");
            }
        }
        if (dedicatedZoneOfCluster != null) {
            final boolean domainIdInChildreanList = getDomainChildIds(dedicatedZoneOfCluster.getDomainId()).contains(domainId);
            // can dedicate a cluster to an account/domain if zone is dedicated to parent-domain
            if (dedicatedZoneOfCluster.getAccountId() != null || (accountId == null && !domainIdInChildreanList) || (accountId != null && !(dedicatedZoneOfCluster.getDomainId().equals(domainId) || domainIdInChildreanList))) {
                s_logger.error("Cannot dedicate Cluster. Its zone is already dedicated");
                final Zone zone = zoneRepository.findOne(cluster.getDataCenterId());
                throw new CloudRuntimeException("Cluster's Zone " + zone.getName() + " is already dedicated");
            }
        }
        // check if any resource under this cluster is dedicated to different account or sub-domain
        hosts = _hostDao.findByClusterId(cluster.getId());
        final List<DedicatedResourceVO> hostsToRelease = new ArrayList<>();
        for (final HostVO host : hosts) {
            final DedicatedResourceVO dHost = _dedicatedDao.findByHostId(host.getId());
            if (dHost != null) {
                if (!(childDomainIds.contains(dHost.getDomainId()))) {
                    throw new CloudRuntimeException("Host " + host.getName() + " under this Cluster " + cluster.getName() + " is dedicated to different account/domain");
                }
                /*if all dedicated resources belongs to same account and domain then we should release dedication
                    and make new entry for this cluster */
                if (accountId != null) {
                    if (dHost.getAccountId().equals(accountId)) {
                        hostsToRelease.add(dHost);
                    } else {
                        s_logger.error("Cannot dedicate Cluster " + cluster.getName() + " to account" + accountName);
                        throw new CloudRuntimeException("Cannot dedicate Cluster " + cluster.getName() + " to account" + accountName);
                    }
                } else {
                    if (dHost.getAccountId() == null && dHost.getDomainId().equals(domainId)) {
                        hostsToRelease.add(dHost);
                    }
                }
            }
        }
        for (final 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(final TransactionStatus status) {
            // find or create the affinity group by name under this account/domain
            final 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(null, null, clusterId, null, null, null, group.getId());
            try {
                dedicatedResource.setDomainId(domainId);
                if (accountIdFinal != null) {
                    dedicatedResource.setAccountId(accountIdFinal);
                }
                dedicatedResource = _dedicatedDao.persist(dedicatedResource);
            } catch (final Exception e) {
                s_logger.error("Unable to dedicate host due to " + e.getMessage(), e);
                throw new CloudRuntimeException("Failed to dedicate cluster. Please contact Cloud Support.");
            }
            final List<DedicatedResourceVO> result = new ArrayList<>();
            result.add(dedicatedResource);
            return result;
        }
    });
}
Also used : Account(com.cloud.user.Account) ClusterVO(com.cloud.dc.ClusterVO) Zone(com.cloud.db.model.Zone) ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) HostPodVO(com.cloud.dc.HostPodVO) HostVO(com.cloud.host.HostVO) AffinityGroup(com.cloud.affinity.AffinityGroup) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.utils.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 70 with HostPodVO

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

the class DedicatedResourceManagerImpl method dedicatePod.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_DEDICATE_RESOURCE, eventDescription = "dedicating a Pod")
public List<DedicatedResourceVO> dedicatePod(final Long podId, final Long domainId, final String accountName) {
    Long accountId = null;
    if (accountName != null) {
        final Account caller = CallContext.current().getCallingAccount();
        final Account owner = _accountMgr.finalizeOwner(caller, accountName, domainId, null);
        accountId = owner.getId();
    }
    final List<Long> childDomainIds = getDomainChildIds(domainId);
    childDomainIds.add(domainId);
    checkAccountAndDomain(accountId, domainId);
    final HostPodVO pod = _podDao.findById(podId);
    List<HostVO> hosts = null;
    if (pod == null) {
        throw new InvalidParameterValueException("Unable to find pod by id " + podId);
    } else {
        final DedicatedResourceVO dedicatedPod = _dedicatedDao.findByPodId(podId);
        final DedicatedResourceVO dedicatedZoneOfPod = _dedicatedDao.findByZoneId(pod.getDataCenterId());
        // check if pod is dedicated
        if (dedicatedPod != null) {
            s_logger.error("Pod " + pod.getName() + " is already dedicated");
            throw new CloudRuntimeException("Pod " + pod.getName() + " is already dedicated");
        }
        if (dedicatedZoneOfPod != null) {
            final boolean domainIdInChildreanList = getDomainChildIds(dedicatedZoneOfPod.getDomainId()).contains(domainId);
            // can dedicate a pod to an account/domain if zone is dedicated to parent-domain
            if (dedicatedZoneOfPod.getAccountId() != null || (accountId == null && !domainIdInChildreanList) || (accountId != null && !(dedicatedZoneOfPod.getDomainId().equals(domainId) || domainIdInChildreanList))) {
                final Zone zone = zoneRepository.findOne(pod.getDataCenterId());
                s_logger.error("Cannot dedicate Pod. Its zone is already dedicated");
                throw new CloudRuntimeException("Pod's Zone " + zone.getName() + " is already dedicated");
            }
        }
        // check if any resource under this pod is dedicated to different account or sub-domain
        final List<ClusterVO> clusters = _clusterDao.listByPodId(pod.getId());
        final List<DedicatedResourceVO> clustersToRelease = new ArrayList<>();
        final List<DedicatedResourceVO> hostsToRelease = new ArrayList<>();
        for (final ClusterVO cluster : clusters) {
            final DedicatedResourceVO dCluster = _dedicatedDao.findByClusterId(cluster.getId());
            if (dCluster != null) {
                if (!(childDomainIds.contains(dCluster.getDomainId()))) {
                    throw new CloudRuntimeException("Cluster " + cluster.getName() + " under this Pod " + pod.getName() + " is dedicated to different account/domain");
                }
                /*if all dedicated resources belongs to same account and domain then we should release dedication
                    and make new entry for this Pod*/
                if (accountId != null) {
                    if (dCluster.getAccountId().equals(accountId)) {
                        clustersToRelease.add(dCluster);
                    } else {
                        s_logger.error("Cluster " + cluster.getName() + " under this Pod " + pod.getName() + " is dedicated to different account/domain");
                        throw new CloudRuntimeException("Cluster " + cluster.getName() + " under this Pod " + pod.getName() + " is dedicated to different account/domain");
                    }
                } else {
                    if (dCluster.getAccountId() == null && dCluster.getDomainId().equals(domainId)) {
                        clustersToRelease.add(dCluster);
                    }
                }
            }
        }
        for (final DedicatedResourceVO dr : clustersToRelease) {
            releaseDedicatedResource(null, null, dr.getClusterId(), null);
        }
        hosts = _hostDao.findByPodId(pod.getId());
        for (final HostVO host : hosts) {
            final DedicatedResourceVO dHost = _dedicatedDao.findByHostId(host.getId());
            if (dHost != null) {
                if (!(getDomainChildIds(domainId).contains(dHost.getDomainId()))) {
                    throw new CloudRuntimeException("Host " + host.getName() + " under this Pod " + pod.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 Pod " + pod.getName() + " is dedicated to different account/domain");
                        throw new CloudRuntimeException("Host " + host.getName() + " under this Pod " + pod.getName() + " is dedicated to different account/domain");
                    }
                } else {
                    if (dHost.getAccountId() == null && dHost.getDomainId().equals(domainId)) {
                        hostsToRelease.add(dHost);
                    }
                }
            }
        }
        for (final 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(final TransactionStatus status) {
            // find or create the affinity group by name under this account/domain
            final 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(null, podId, null, null, null, null, group.getId());
            try {
                dedicatedResource.setDomainId(domainId);
                if (accountIdFinal != null) {
                    dedicatedResource.setAccountId(accountIdFinal);
                }
                dedicatedResource = _dedicatedDao.persist(dedicatedResource);
            } catch (final Exception e) {
                s_logger.error("Unable to dedicate pod due to " + e.getMessage(), e);
                throw new CloudRuntimeException("Failed to dedicate pod. Please contact Cloud Support.");
            }
            final List<DedicatedResourceVO> result = new ArrayList<>();
            result.add(dedicatedResource);
            return result;
        }
    });
}
Also used : Account(com.cloud.user.Account) ClusterVO(com.cloud.dc.ClusterVO) Zone(com.cloud.db.model.Zone) ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) HostPodVO(com.cloud.dc.HostPodVO) HostVO(com.cloud.host.HostVO) AffinityGroup(com.cloud.affinity.AffinityGroup) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.utils.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)

Aggregations

HostPodVO (com.cloud.dc.HostPodVO)126 ArrayList (java.util.ArrayList)52 HostVO (com.cloud.host.HostVO)47 ClusterVO (com.cloud.dc.ClusterVO)46 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)46 DataCenterVO (com.cloud.dc.DataCenterVO)39 Account (com.cloud.user.Account)25 DB (com.cloud.utils.db.DB)25 Test (org.junit.Test)23 ConfigurationException (javax.naming.ConfigurationException)22 TransactionStatus (com.cloud.utils.db.TransactionStatus)21 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)20 VMInstanceVO (com.cloud.vm.VMInstanceVO)18 Random (java.util.Random)18 VolumeVO (com.cloud.storage.VolumeVO)17 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)16 HashMap (java.util.HashMap)16 List (java.util.List)16 Zone (com.cloud.db.model.Zone)15 DataCenter (com.cloud.dc.DataCenter)14