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;
}
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;
}
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);
}
}
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());
}
}
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());
}
}
Aggregations