Search in sources :

Example 1 with IpRangeResponse

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

the class ApiResponseHelper method createPodResponse.

@Override
public PodResponse createPodResponse(Pod pod, Boolean showCapacities) {
    String[] ipRange = new String[2];
    List<String> startIps = new ArrayList<String>();
    List<String> endIps = new ArrayList<String>();
    List<String> forSystemVms = new ArrayList<String>();
    List<String> vlanIds = new ArrayList<String>();
    List<IpRangeResponse> ipRanges = new ArrayList<>();
    if (pod.getDescription() != null && pod.getDescription().length() > 0) {
        final String[] existingPodIpRanges = pod.getDescription().split(",");
        for (String podIpRange : existingPodIpRanges) {
            IpRangeResponse ipRangeResponse = new IpRangeResponse();
            final String[] existingPodIpRange = podIpRange.split("-");
            String startIp = ((existingPodIpRange.length > 0) && (existingPodIpRange[0] != null)) ? existingPodIpRange[0] : "";
            ipRangeResponse.setStartIp(startIp);
            startIps.add(startIp);
            String endIp = ((existingPodIpRange.length > 1) && (existingPodIpRange[1] != null)) ? existingPodIpRange[1] : "";
            ipRangeResponse.setEndIp(endIp);
            endIps.add(endIp);
            String forSystemVm = (existingPodIpRange.length > 2) && (existingPodIpRange[2] != null) ? existingPodIpRange[2] : "0";
            ipRangeResponse.setForSystemVms(forSystemVm);
            forSystemVms.add(forSystemVm);
            String vlanId = (existingPodIpRange.length > 3) && (existingPodIpRange[3] != null && !existingPodIpRange[3].equals("untagged")) ? BroadcastDomainType.Vlan.toUri(existingPodIpRange[3]).toString() : BroadcastDomainType.Vlan.toUri(Vlan.UNTAGGED).toString();
            ipRangeResponse.setVlanId(vlanId);
            vlanIds.add(vlanId);
            ipRanges.add(ipRangeResponse);
        }
    }
    PodResponse podResponse = new PodResponse();
    podResponse.setId(pod.getUuid());
    podResponse.setName(pod.getName());
    DataCenter zone = ApiDBUtils.findZoneById(pod.getDataCenterId());
    if (zone != null) {
        podResponse.setZoneId(zone.getUuid());
        podResponse.setZoneName(zone.getName());
    }
    podResponse.setNetmask(NetUtils.getCidrNetmask(pod.getCidrSize()));
    podResponse.setIpRanges(ipRanges);
    podResponse.setStartIp(startIps);
    podResponse.setEndIp(endIps);
    podResponse.setForSystemVms(forSystemVms);
    podResponse.setVlanId(vlanIds);
    podResponse.setGateway(pod.getGateway());
    podResponse.setAllocationState(pod.getAllocationState().toString());
    if (showCapacities != null && showCapacities) {
        List<SummedCapacity> capacities = ApiDBUtils.getCapacityByClusterPodZone(null, pod.getId(), null);
        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, pod.getId(), null);
                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, null, pod.getId(), pod.getDataCenterId()));
        podResponse.setCapacities(new ArrayList<CapacityResponse>(capacityResponses));
    }
    podResponse.setHasAnnotation(annotationDao.hasAnnotations(pod.getUuid(), AnnotationService.EntityType.POD.name(), _accountMgr.isRootAdmin(CallContext.current().getCallingAccount().getId())));
    podResponse.setObjectName("pod");
    return podResponse;
}
Also used : ArrayList(java.util.ArrayList) SummedCapacity(com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity) DataCenter(com.cloud.dc.DataCenter) StorageNetworkIpRangeResponse(org.apache.cloudstack.api.response.StorageNetworkIpRangeResponse) IpRangeResponse(org.apache.cloudstack.api.response.IpRangeResponse) PortableIpRangeResponse(org.apache.cloudstack.api.response.PortableIpRangeResponse) VlanIpRangeResponse(org.apache.cloudstack.api.response.VlanIpRangeResponse) CapacityResponse(org.apache.cloudstack.api.response.CapacityResponse) PodResponse(org.apache.cloudstack.api.response.PodResponse) HashSet(java.util.HashSet)

Aggregations

SummedCapacity (com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity)1 DataCenter (com.cloud.dc.DataCenter)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CapacityResponse (org.apache.cloudstack.api.response.CapacityResponse)1 IpRangeResponse (org.apache.cloudstack.api.response.IpRangeResponse)1 PodResponse (org.apache.cloudstack.api.response.PodResponse)1 PortableIpRangeResponse (org.apache.cloudstack.api.response.PortableIpRangeResponse)1 StorageNetworkIpRangeResponse (org.apache.cloudstack.api.response.StorageNetworkIpRangeResponse)1 VlanIpRangeResponse (org.apache.cloudstack.api.response.VlanIpRangeResponse)1