Search in sources :

Example 1 with ClusterResponse

use of org.apache.cloudstack.api.response.ClusterResponse in project cloudstack by apache.

the class AddClusterCmd method execute.

@Override
public void execute() {
    try {
        List<? extends Cluster> result = _resourceService.discoverCluster(this);
        ListResponse<ClusterResponse> response = new ListResponse<ClusterResponse>();
        List<ClusterResponse> clusterResponses = new ArrayList<ClusterResponse>();
        if (result != null && result.size() > 0) {
            for (Cluster cluster : result) {
                ClusterResponse clusterResponse = _responseGenerator.createClusterResponse(cluster, false);
                clusterResponses.add(clusterResponse);
            }
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add cluster");
        }
        response.setResponses(clusterResponses);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } catch (DiscoveryException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    } catch (ResourceInUseException ex) {
        s_logger.warn("Exception: ", ex);
        ServerApiException e = new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
        for (String proxyObj : ex.getIdProxyList()) {
            e.addProxyObject(proxyObj);
        }
        throw e;
    }
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) ResourceInUseException(com.cloud.exception.ResourceInUseException) ArrayList(java.util.ArrayList) ClusterResponse(org.apache.cloudstack.api.response.ClusterResponse) Cluster(com.cloud.org.Cluster) DiscoveryException(com.cloud.exception.DiscoveryException)

Example 2 with ClusterResponse

use of org.apache.cloudstack.api.response.ClusterResponse in project cloudstack by apache.

the class UpdateClusterCmd method execute.

@Override
public void execute() {
    Cluster cluster = _resourceService.getCluster(getId());
    if (cluster == null) {
        throw new InvalidParameterValueException("Unable to find the cluster by id=" + getId());
    }
    Cluster result = _resourceService.updateCluster(this);
    if (result != null) {
        ClusterResponse clusterResponse = _responseGenerator.createClusterResponse(cluster, false);
        clusterResponse.setResponseName(getCommandName());
        this.setResponseObject(clusterResponse);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update cluster");
    }
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Cluster(com.cloud.org.Cluster) ClusterResponse(org.apache.cloudstack.api.response.ClusterResponse)

Example 3 with ClusterResponse

use of org.apache.cloudstack.api.response.ClusterResponse in project cloudstack by apache.

the class MetricsServiceImpl method listClusterMetrics.

@Override
public List<ClusterMetricsResponse> listClusterMetrics(List<ClusterResponse> clusterResponses) {
    final List<ClusterMetricsResponse> metricsResponses = new ArrayList<>();
    for (final ClusterResponse clusterResponse : clusterResponses) {
        ClusterMetricsResponse metricsResponse = new ClusterMetricsResponse();
        try {
            BeanUtils.copyProperties(metricsResponse, clusterResponse);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate cluster metrics response");
        }
        final Cluster cluster = clusterDao.findByUuid(clusterResponse.getId());
        if (cluster == null) {
            continue;
        }
        final Long clusterId = cluster.getId();
        // Thresholds
        final Double cpuThreshold = AlertManager.CPUCapacityThreshold.valueIn(clusterId);
        final Double memoryThreshold = AlertManager.MemoryCapacityThreshold.valueIn(clusterId);
        final Float cpuDisableThreshold = DeploymentClusterPlanner.ClusterCPUCapacityDisableThreshold.valueIn(clusterId);
        final Float memoryDisableThreshold = DeploymentClusterPlanner.ClusterMemoryCapacityDisableThreshold.valueIn(clusterId);
        final Double cpuOvercommitRatio = findRatioValue(ApiDBUtils.findClusterDetails(clusterId, "cpuOvercommitRatio"));
        final Double memoryOvercommitRatio = findRatioValue(ApiDBUtils.findClusterDetails(clusterId, "memoryOvercommitRatio"));
        // CPU and memory capacities
        final CapacityDaoImpl.SummedCapacity cpuCapacity = getCapacity((int) Capacity.CAPACITY_TYPE_CPU, null, clusterId);
        final CapacityDaoImpl.SummedCapacity memoryCapacity = getCapacity((int) Capacity.CAPACITY_TYPE_MEMORY, null, clusterId);
        final Metrics metrics = new Metrics(cpuCapacity, memoryCapacity);
        for (final HostJoinVO host : hostJoinDao.findByClusterId(clusterId, Host.Type.Routing)) {
            if (host.getStatus() == Status.Up) {
                metrics.incrUpResources();
            }
            metrics.incrTotalResources();
            updateHostMetrics(metrics, host);
        }
        metricsResponse.setState(clusterResponse.getAllocationState(), clusterResponse.getManagedState());
        metricsResponse.setResources(metrics.getUpResources(), metrics.getTotalResources());
        // CPU
        metricsResponse.setCpuTotal(metrics.getTotalCpu());
        metricsResponse.setCpuAllocated(metrics.getCpuAllocated(), metrics.getTotalCpu());
        if (metrics.getCpuUsedPercentage() > 0L) {
            metricsResponse.setCpuUsed(metrics.getCpuUsedPercentage(), metrics.getTotalHosts());
            metricsResponse.setCpuMaxDeviation(metrics.getMaximumCpuUsage(), metrics.getCpuUsedPercentage(), metrics.getTotalHosts());
        }
        // Memory
        metricsResponse.setMemTotal(metrics.getTotalMemory());
        metricsResponse.setMemAllocated(metrics.getMemoryAllocated(), metrics.getTotalMemory());
        if (metrics.getMemoryUsed() > 0L) {
            metricsResponse.setMemUsed(metrics.getMemoryUsed(), metrics.getTotalMemory());
            metricsResponse.setMemMaxDeviation(metrics.getMaximumMemoryUsage(), metrics.getMemoryUsed(), metrics.getTotalHosts());
        }
        // CPU thresholds
        metricsResponse.setCpuUsageThreshold(metrics.getCpuUsedPercentage(), metrics.getTotalHosts(), cpuThreshold);
        metricsResponse.setCpuUsageDisableThreshold(metrics.getCpuUsedPercentage(), metrics.getTotalHosts(), cpuDisableThreshold);
        metricsResponse.setCpuAllocatedThreshold(metrics.getCpuAllocated(), metrics.getTotalCpu(), cpuOvercommitRatio, cpuThreshold);
        metricsResponse.setCpuAllocatedDisableThreshold(metrics.getCpuAllocated(), metrics.getTotalCpu(), cpuOvercommitRatio, cpuDisableThreshold);
        // Memory thresholds
        metricsResponse.setMemoryUsageThreshold(metrics.getMemoryUsed(), metrics.getTotalMemory(), memoryThreshold);
        metricsResponse.setMemoryUsageDisableThreshold(metrics.getMemoryUsed(), metrics.getTotalMemory(), memoryDisableThreshold);
        metricsResponse.setMemoryAllocatedThreshold(metrics.getMemoryAllocated(), metrics.getTotalMemory(), memoryOvercommitRatio, memoryThreshold);
        metricsResponse.setMemoryAllocatedDisableThreshold(metrics.getMemoryAllocated(), metrics.getTotalMemory(), memoryOvercommitRatio, memoryDisableThreshold);
        metricsResponses.add(metricsResponse);
    }
    return metricsResponses;
}
Also used : ArrayList(java.util.ArrayList) ClusterResponse(org.apache.cloudstack.api.response.ClusterResponse) Cluster(com.cloud.org.Cluster) HostJoinVO(com.cloud.api.query.vo.HostJoinVO) InvocationTargetException(java.lang.reflect.InvocationTargetException) ClusterMetricsResponse(org.apache.cloudstack.response.ClusterMetricsResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) CapacityDaoImpl(com.cloud.capacity.dao.CapacityDaoImpl)

Example 4 with ClusterResponse

use of org.apache.cloudstack.api.response.ClusterResponse in project cloudstack by apache.

the class ApiResponseHelper method createClusterResponse.

@Override
public ClusterResponse createClusterResponse(Cluster cluster, Boolean showCapacities) {
    ClusterResponse clusterResponse = new ClusterResponse();
    clusterResponse.setId(cluster.getUuid());
    clusterResponse.setName(cluster.getName());
    HostPodVO pod = ApiDBUtils.findPodById(cluster.getPodId());
    if (pod != null) {
        clusterResponse.setPodId(pod.getUuid());
        clusterResponse.setPodName(pod.getName());
    }
    DataCenter dc = ApiDBUtils.findZoneById(cluster.getDataCenterId());
    if (dc != null) {
        clusterResponse.setZoneId(dc.getUuid());
        clusterResponse.setZoneName(dc.getName());
    }
    clusterResponse.setHypervisorType(cluster.getHypervisorType().toString());
    clusterResponse.setClusterType(cluster.getClusterType().toString());
    clusterResponse.setAllocationState(cluster.getAllocationState().toString());
    clusterResponse.setManagedState(cluster.getManagedState().toString());
    String cpuOvercommitRatio = ApiDBUtils.findClusterDetails(cluster.getId(), "cpuOvercommitRatio");
    String memoryOvercommitRatio = ApiDBUtils.findClusterDetails(cluster.getId(), "memoryOvercommitRatio");
    clusterResponse.setCpuOvercommitRatio(cpuOvercommitRatio);
    clusterResponse.setMemoryOvercommitRatio(memoryOvercommitRatio);
    clusterResponse.setResourceDetails(_clusterDetailsDao.findDetails(cluster.getId()));
    if (showCapacities != null && showCapacities) {
        List<SummedCapacity> capacities = ApiDBUtils.getCapacityByClusterPodZone(null, null, cluster.getId());
        Set<CapacityResponse> capacityResponses = new HashSet<CapacityResponse>();
        for (SummedCapacity capacity : capacities) {
            CapacityResponse capacityResponse = new CapacityResponse();
            capacityResponse.setCapacityType(capacity.getCapacityType());
            capacityResponse.setCapacityName(CapacityVO.getCapacityName(capacity.getCapacityType()));
            capacityResponse.setCapacityUsed(capacity.getUsedCapacity() + capacity.getReservedCapacity());
            if (capacity.getCapacityType() == Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED) {
                List<SummedCapacity> c = ApiDBUtils.findNonSharedStorageForClusterPodZone(null, null, cluster.getId());
                capacityResponse.setCapacityTotal(capacity.getTotalCapacity() - c.get(0).getTotalCapacity());
                capacityResponse.setCapacityUsed(capacity.getUsedCapacity() - c.get(0).getUsedCapacity());
            } else {
                capacityResponse.setCapacityTotal(capacity.getTotalCapacity());
            }
            if (capacityResponse.getCapacityTotal() != 0) {
                capacityResponse.setPercentUsed(s_percentFormat.format((float) capacityResponse.getCapacityUsed() / (float) capacityResponse.getCapacityTotal() * 100f));
            } else {
                capacityResponse.setPercentUsed(s_percentFormat.format(0L));
            }
            capacityResponses.add(capacityResponse);
        }
        // Do it for stats as well.
        capacityResponses.addAll(getStatsCapacityresponse(null, cluster.getId(), pod.getId(), pod.getDataCenterId()));
        clusterResponse.setCapacitites(new ArrayList<CapacityResponse>(capacityResponses));
    }
    clusterResponse.setHasAnnotation(annotationDao.hasAnnotations(cluster.getUuid(), AnnotationService.EntityType.CLUSTER.name(), _accountMgr.isRootAdmin(CallContext.current().getCallingAccount().getId())));
    clusterResponse.setObjectName("cluster");
    return clusterResponse;
}
Also used : DataCenter(com.cloud.dc.DataCenter) CapacityResponse(org.apache.cloudstack.api.response.CapacityResponse) ClusterResponse(org.apache.cloudstack.api.response.ClusterResponse) SummedCapacity(com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity) HostPodVO(com.cloud.dc.HostPodVO) HashSet(java.util.HashSet)

Example 5 with ClusterResponse

use of org.apache.cloudstack.api.response.ClusterResponse in project cloudstack by apache.

the class MetricsServiceImpl method listClusterMetrics.

@Override
public List<ClusterMetricsResponse> listClusterMetrics(Pair<List<ClusterResponse>, Integer> clusterResponses) {
    final List<ClusterMetricsResponse> metricsResponses = new ArrayList<>();
    for (final ClusterResponse clusterResponse : clusterResponses.first()) {
        ClusterMetricsResponse metricsResponse = new ClusterMetricsResponse();
        try {
            BeanUtils.copyProperties(metricsResponse, clusterResponse);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate cluster metrics response");
        }
        final Cluster cluster = clusterDao.findByUuid(clusterResponse.getId());
        if (cluster == null) {
            continue;
        }
        final Long clusterId = cluster.getId();
        // Thresholds
        final Double cpuThreshold = AlertManager.CPUCapacityThreshold.valueIn(clusterId);
        final Double memoryThreshold = AlertManager.MemoryCapacityThreshold.valueIn(clusterId);
        final Float cpuDisableThreshold = DeploymentClusterPlanner.ClusterCPUCapacityDisableThreshold.valueIn(clusterId);
        final Float memoryDisableThreshold = DeploymentClusterPlanner.ClusterMemoryCapacityDisableThreshold.valueIn(clusterId);
        // CPU and memory capacities
        final CapacityDaoImpl.SummedCapacity cpuCapacity = getCapacity((int) Capacity.CAPACITY_TYPE_CPU, null, clusterId);
        final CapacityDaoImpl.SummedCapacity memoryCapacity = getCapacity((int) Capacity.CAPACITY_TYPE_MEMORY, null, clusterId);
        final Metrics metrics = new Metrics(cpuCapacity, memoryCapacity);
        for (final Host host : hostDao.findByClusterId(clusterId)) {
            if (host == null || host.getType() != Host.Type.Routing) {
                continue;
            }
            if (host.getStatus() == Status.Up) {
                metrics.incrUpResources();
            }
            metrics.incrTotalResources();
            updateHostMetrics(metrics, hostJoinDao.findById(host.getId()));
        }
        metricsResponse.setState(clusterResponse.getAllocationState(), clusterResponse.getManagedState());
        metricsResponse.setResources(metrics.getUpResources(), metrics.getTotalResources());
        // CPU
        metricsResponse.setCpuTotal(metrics.getTotalCpu());
        metricsResponse.setCpuAllocated(metrics.getCpuAllocated(), metrics.getTotalCpu());
        if (metrics.getCpuUsedPercentage() > 0L) {
            metricsResponse.setCpuUsed(metrics.getCpuUsedPercentage(), metrics.getTotalHosts());
            metricsResponse.setCpuMaxDeviation(metrics.getMaximumCpuUsage(), metrics.getCpuUsedPercentage(), metrics.getTotalHosts());
        }
        // Memory
        metricsResponse.setMemTotal(metrics.getTotalMemory());
        metricsResponse.setMemAllocated(metrics.getMemoryAllocated(), metrics.getTotalMemory());
        if (metrics.getMemoryUsed() > 0L) {
            metricsResponse.setMemUsed(metrics.getMemoryUsed(), metrics.getTotalMemory());
            metricsResponse.setMemMaxDeviation(metrics.getMaximumMemoryUsage(), metrics.getMemoryUsed(), metrics.getTotalHosts());
        }
        // CPU thresholds
        metricsResponse.setCpuUsageThreshold(metrics.getCpuUsedPercentage(), metrics.getTotalHosts(), cpuThreshold);
        metricsResponse.setCpuUsageDisableThreshold(metrics.getCpuUsedPercentage(), metrics.getTotalHosts(), cpuDisableThreshold);
        metricsResponse.setCpuAllocatedThreshold(metrics.getCpuAllocated(), metrics.getTotalCpu(), cpuThreshold);
        metricsResponse.setCpuAllocatedDisableThreshold(metrics.getCpuAllocated(), metrics.getTotalCpu(), cpuDisableThreshold);
        // Memory thresholds
        metricsResponse.setMemoryUsageThreshold(metrics.getMemoryUsed(), metrics.getTotalMemory(), memoryThreshold);
        metricsResponse.setMemoryUsageDisableThreshold(metrics.getMemoryUsed(), metrics.getTotalMemory(), memoryDisableThreshold);
        metricsResponse.setMemoryAllocatedThreshold(metrics.getMemoryAllocated(), metrics.getTotalMemory(), memoryThreshold);
        metricsResponse.setMemoryAllocatedDisableThreshold(metrics.getMemoryAllocated(), metrics.getTotalMemory(), memoryDisableThreshold);
        metricsResponse.setHasAnnotation(clusterResponse.hasAnnotation());
        metricsResponses.add(metricsResponse);
    }
    return metricsResponses;
}
Also used : ArrayList(java.util.ArrayList) ClusterResponse(org.apache.cloudstack.api.response.ClusterResponse) Cluster(com.cloud.org.Cluster) Host(com.cloud.host.Host) InvocationTargetException(java.lang.reflect.InvocationTargetException) ClusterMetricsResponse(org.apache.cloudstack.response.ClusterMetricsResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) CapacityDaoImpl(com.cloud.capacity.dao.CapacityDaoImpl)

Aggregations

ClusterResponse (org.apache.cloudstack.api.response.ClusterResponse)6 Cluster (com.cloud.org.Cluster)5 ArrayList (java.util.ArrayList)4 ServerApiException (org.apache.cloudstack.api.ServerApiException)4 CapacityDaoImpl (com.cloud.capacity.dao.CapacityDaoImpl)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ClusterMetricsResponse (org.apache.cloudstack.response.ClusterMetricsResponse)2 HostJoinVO (com.cloud.api.query.vo.HostJoinVO)1 SummedCapacity (com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity)1 DataCenter (com.cloud.dc.DataCenter)1 HostPodVO (com.cloud.dc.HostPodVO)1 DiscoveryException (com.cloud.exception.DiscoveryException)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 ResourceInUseException (com.cloud.exception.ResourceInUseException)1 Host (com.cloud.host.Host)1 Pair (com.cloud.utils.Pair)1 HashSet (java.util.HashSet)1 List (java.util.List)1 CapacityResponse (org.apache.cloudstack.api.response.CapacityResponse)1 ListResponse (org.apache.cloudstack.api.response.ListResponse)1