Search in sources :

Example 86 with ServerApiException

use of org.apache.cloudstack.api.ServerApiException in project cloudstack by apache.

the class MetricsServiceImpl method listHostMetrics.

@Override
public List<HostMetricsResponse> listHostMetrics(List<HostResponse> hostResponses) {
    final List<HostMetricsResponse> metricsResponses = new ArrayList<>();
    for (final HostResponse hostResponse : hostResponses) {
        HostMetricsResponse metricsResponse = new HostMetricsResponse();
        try {
            BeanUtils.copyProperties(metricsResponse, hostResponse);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate host metrics response");
        }
        final Host host = hostDao.findByUuid(hostResponse.getId());
        if (host == null) {
            continue;
        }
        final Long hostId = host.getId();
        final Long clusterId = host.getClusterId();
        // 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);
        // Over commit ratios
        final Double cpuOvercommitRatio = findRatioValue(ApiDBUtils.findClusterDetails(clusterId, "cpuOvercommitRatio"));
        final Double memoryOvercommitRatio = findRatioValue(ApiDBUtils.findClusterDetails(clusterId, "memoryOvercommitRatio"));
        Long upInstances = 0L;
        Long totalInstances = 0L;
        for (final VMInstanceVO instance : vmInstanceDao.listByHostId(hostId)) {
            if (instance == null) {
                continue;
            }
            if (instance.getType() == VirtualMachine.Type.User) {
                totalInstances++;
                if (instance.getState() == VirtualMachine.State.Running) {
                    upInstances++;
                }
            }
        }
        metricsResponse.setPowerState(hostResponse.getOutOfBandManagementResponse().getPowerState());
        metricsResponse.setInstances(upInstances, totalInstances);
        metricsResponse.setCpuTotal(hostResponse.getCpuNumber(), hostResponse.getCpuSpeed(), cpuOvercommitRatio);
        metricsResponse.setCpuUsed(hostResponse.getCpuUsed(), hostResponse.getCpuNumber(), hostResponse.getCpuSpeed());
        metricsResponse.setCpuAllocated(hostResponse.getCpuAllocated(), hostResponse.getCpuNumber(), hostResponse.getCpuSpeed());
        metricsResponse.setMemTotal(hostResponse.getMemoryTotal(), memoryOvercommitRatio);
        metricsResponse.setMemAllocated(hostResponse.getMemoryAllocated());
        metricsResponse.setMemUsed(hostResponse.getMemoryUsed());
        metricsResponse.setNetworkRead(hostResponse.getNetworkKbsRead());
        metricsResponse.setNetworkWrite(hostResponse.getNetworkKbsWrite());
        // CPU thresholds
        metricsResponse.setCpuUsageThreshold(hostResponse.getCpuUsed(), cpuThreshold);
        metricsResponse.setCpuUsageDisableThreshold(hostResponse.getCpuUsed(), cpuDisableThreshold);
        metricsResponse.setCpuAllocatedThreshold(hostResponse.getCpuAllocated(), cpuOvercommitRatio, cpuThreshold);
        metricsResponse.setCpuAllocatedDisableThreshold(hostResponse.getCpuAllocated(), cpuOvercommitRatio, cpuDisableThreshold);
        // Memory thresholds
        metricsResponse.setMemoryUsageThreshold(hostResponse.getMemoryUsed(), hostResponse.getMemoryTotal(), memoryThreshold);
        metricsResponse.setMemoryUsageDisableThreshold(hostResponse.getMemoryUsed(), hostResponse.getMemoryTotal(), memoryDisableThreshold);
        metricsResponse.setMemoryAllocatedThreshold(hostResponse.getMemoryAllocated(), hostResponse.getMemoryTotal(), memoryOvercommitRatio, memoryThreshold);
        metricsResponse.setMemoryAllocatedDisableThreshold(hostResponse.getMemoryAllocated(), hostResponse.getMemoryTotal(), memoryOvercommitRatio, memoryDisableThreshold);
        metricsResponses.add(metricsResponse);
    }
    return metricsResponses;
}
Also used : ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) Host(com.cloud.host.Host) HostMetricsResponse(org.apache.cloudstack.response.HostMetricsResponse) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServerApiException(org.apache.cloudstack.api.ServerApiException) HostResponse(org.apache.cloudstack.api.response.HostResponse)

Example 87 with ServerApiException

use of org.apache.cloudstack.api.ServerApiException in project cloudstack by apache.

the class MetricsServiceImpl method listZoneMetrics.

@Override
public List<ZoneMetricsResponse> listZoneMetrics(List<ZoneResponse> zoneResponses) {
    final List<ZoneMetricsResponse> metricsResponses = new ArrayList<>();
    for (final ZoneResponse zoneResponse : zoneResponses) {
        ZoneMetricsResponse metricsResponse = new ZoneMetricsResponse();
        try {
            BeanUtils.copyProperties(metricsResponse, zoneResponse);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate zone metrics response");
        }
        final DataCenter zone = dataCenterDao.findByUuid(zoneResponse.getId());
        if (zone == null) {
            continue;
        }
        final Long zoneId = zone.getId();
        // Thresholds
        final Double cpuThreshold = AlertManager.CPUCapacityThreshold.value();
        final Double memoryThreshold = AlertManager.MemoryCapacityThreshold.value();
        final Float cpuDisableThreshold = DeploymentClusterPlanner.ClusterCPUCapacityDisableThreshold.value();
        final Float memoryDisableThreshold = DeploymentClusterPlanner.ClusterMemoryCapacityDisableThreshold.value();
        // CPU and memory capacities
        final CapacityDaoImpl.SummedCapacity cpuCapacity = getCapacity((int) Capacity.CAPACITY_TYPE_CPU, zoneId, null);
        final CapacityDaoImpl.SummedCapacity memoryCapacity = getCapacity((int) Capacity.CAPACITY_TYPE_MEMORY, zoneId, null);
        final Metrics metrics = new Metrics(cpuCapacity, memoryCapacity);
        for (final Cluster cluster : clusterDao.listClustersByDcId(zoneId)) {
            metrics.incrTotalResources();
            if (cluster.getAllocationState() == Grouping.AllocationState.Enabled && cluster.getManagedState() == Managed.ManagedState.Managed) {
                metrics.incrUpResources();
            }
            for (final HostJoinVO host : hostJoinDao.findByClusterId(cluster.getId(), Host.Type.Routing)) {
                updateHostMetrics(metrics, host);
            }
        }
        metricsResponse.setState(zoneResponse.getAllocationState());
        metricsResponse.setResource(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);
        metricsResponses.add(metricsResponse);
    }
    return metricsResponses;
}
Also used : ArrayList(java.util.ArrayList) Cluster(com.cloud.org.Cluster) HostJoinVO(com.cloud.api.query.vo.HostJoinVO) InvocationTargetException(java.lang.reflect.InvocationTargetException) ZoneResponse(org.apache.cloudstack.api.response.ZoneResponse) DataCenter(com.cloud.dc.DataCenter) ServerApiException(org.apache.cloudstack.api.ServerApiException) ZoneMetricsResponse(org.apache.cloudstack.response.ZoneMetricsResponse) CapacityDaoImpl(com.cloud.capacity.dao.CapacityDaoImpl)

Example 88 with ServerApiException

use of org.apache.cloudstack.api.ServerApiException in project cloudstack by apache.

the class ListBigSwitchBcfDevicesCmd method execute.

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException {
    try {
        final List<BigSwitchBcfDeviceVO> bigswitchDevices = bigswitchBcfElementService.listBigSwitchBcfDevices(this);
        final ListResponse<BigSwitchBcfDeviceResponse> response = new ListResponse<BigSwitchBcfDeviceResponse>();
        final List<BigSwitchBcfDeviceResponse> bigswitchDevicesResponse = new ArrayList<BigSwitchBcfDeviceResponse>();
        if (bigswitchDevices != null && !bigswitchDevices.isEmpty()) {
            for (final BigSwitchBcfDeviceVO bigswitchDeviceVO : bigswitchDevices) {
                final BigSwitchBcfDeviceResponse bigswitchDeviceResponse = bigswitchBcfElementService.createBigSwitchBcfDeviceResponse(bigswitchDeviceVO);
                bigswitchDevicesResponse.add(bigswitchDeviceResponse);
            }
        }
        response.setResponses(bigswitchDevicesResponse);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } catch (InvalidParameterValueException invalidParamExcp) {
        throw new ServerApiException(ApiErrorCode.PARAM_ERROR, invalidParamExcp.getMessage(), invalidParamExcp);
    } catch (CloudRuntimeException runtimeExcp) {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, runtimeExcp.getMessage(), runtimeExcp);
    }
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) BigSwitchBcfDeviceResponse(com.cloud.api.response.BigSwitchBcfDeviceResponse) BigSwitchBcfDeviceVO(com.cloud.network.BigSwitchBcfDeviceVO)

Example 89 with ServerApiException

use of org.apache.cloudstack.api.ServerApiException in project cloudstack by apache.

the class CreateServiceInstanceCmd method create.

@Override
public void create() throws ResourceAllocationException {
    // Parameter validation
    try {
        DataCenter zone = _entityMgr.findById(DataCenter.class, zoneId);
        if (zone == null) {
            throw new InvalidParameterValueException("Unable to find zone ID " + zoneId);
        }
        Account owner = _accountService.getActiveAccountById(getEntityOwnerId());
        VirtualMachineTemplate template = _entityMgr.findById(VirtualMachineTemplate.class, templateId);
        if (template == null) {
            throw new InvalidParameterValueException("Invalid template ID " + templateId);
        }
        ServiceOffering serviceOffering = _entityMgr.findById(ServiceOffering.class, serviceOfferingId);
        if (serviceOffering == null) {
            throw new InvalidParameterValueException("Invalid service offering ID " + serviceOfferingId);
        }
        Network left = _networkService.getNetwork(leftNetworkId);
        if (left == null) {
            throw new InvalidParameterValueException("Invalid ID for left network " + leftNetworkId);
        }
        Network right = _networkService.getNetwork(rightNetworkId);
        if (right == null) {
            throw new InvalidParameterValueException("Invalid ID for right network " + rightNetworkId);
        }
        if (name.isEmpty()) {
            throw new InvalidParameterValueException("service instance name is empty");
        }
        ServiceVirtualMachine svm = _vrouterService.createServiceInstance(zone, owner, template, serviceOffering, name, left, right);
        if (svm == null) {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Unable to create service instance");
        }
        setEntityId(svm.getId());
        setEntityUuid(svm.getUuid());
    } catch (Exception ex) {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    }
}
Also used : Account(com.cloud.user.Account) DataCenter(com.cloud.dc.DataCenter) VirtualMachineTemplate(com.cloud.template.VirtualMachineTemplate) ServerApiException(org.apache.cloudstack.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ServiceOffering(com.cloud.offering.ServiceOffering) Network(com.cloud.network.Network) ServiceVirtualMachine(org.apache.cloudstack.network.contrail.management.ServiceVirtualMachine) ServerApiException(org.apache.cloudstack.api.ServerApiException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException)

Example 90 with ServerApiException

use of org.apache.cloudstack.api.ServerApiException in project cloudstack by apache.

the class DeleteNuageVspDeviceCmd method execute.

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException {
    try {
        boolean result = _nuageVspManager.deleteNuageVspDevice(this);
        if (result) {
            SuccessResponse response = new SuccessResponse(getCommandName());
            response.setResponseName(getCommandName());
            this.setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete Nuage device.");
        }
    } catch (InvalidParameterValueException invalidParamExcp) {
        throw new ServerApiException(ApiErrorCode.PARAM_ERROR, invalidParamExcp.getMessage());
    } catch (CloudRuntimeException runtimeExcp) {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, runtimeExcp.getMessage());
    }
}
Also used : SuccessResponse(org.apache.cloudstack.api.response.SuccessResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Aggregations

ServerApiException (org.apache.cloudstack.api.ServerApiException)513 SuccessResponse (org.apache.cloudstack.api.response.SuccessResponse)125 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)116 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)61 ArrayList (java.util.ArrayList)58 UserVm (com.cloud.uservm.UserVm)44 ListResponse (org.apache.cloudstack.api.response.ListResponse)44 UserVmResponse (org.apache.cloudstack.api.response.UserVmResponse)42 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)39 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)33 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)26 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)25 Account (com.cloud.user.Account)24 Host (com.cloud.host.Host)20 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)18 Volume (com.cloud.storage.Volume)16 Test (org.junit.Test)16 VirtualMachineTemplate (com.cloud.template.VirtualMachineTemplate)15 VolumeResponse (org.apache.cloudstack.api.response.VolumeResponse)15 UserAccount (com.cloud.user.UserAccount)13