Search in sources :

Example 6 with NicResponse

use of com.cloud.api.response.NicResponse in project cosmic by MissionCriticalCloud.

the class UserVmJoinDaoImpl method newUserVmResponse.

@Override
public UserVmResponse newUserVmResponse(final ResponseView view, final String objectName, final UserVmJoinVO userVm, final EnumSet<VMDetails> details, final Account caller) {
    final UserVmResponse userVmResponse = new UserVmResponse();
    if (userVm.getHypervisorType() != null) {
        userVmResponse.setHypervisor(userVm.getHypervisorType().toString());
    }
    userVmResponse.setId(userVm.getUuid());
    userVmResponse.setName(userVm.getName());
    if (userVm.getDisplayName() != null) {
        userVmResponse.setDisplayName(userVm.getDisplayName());
    } else {
        userVmResponse.setDisplayName(userVm.getName());
    }
    if (userVm.getAccountType() == Account.ACCOUNT_TYPE_PROJECT) {
        userVmResponse.setProjectId(userVm.getProjectUuid());
        userVmResponse.setProjectName(userVm.getProjectName());
    } else {
        userVmResponse.setAccountName(userVm.getAccountName());
    }
    final User user = _userDao.getUser(userVm.getUserId());
    if (user != null) {
        userVmResponse.setUserId(user.getUuid());
        userVmResponse.setUserName(user.getUsername());
    }
    userVmResponse.setDomainId(userVm.getDomainUuid());
    userVmResponse.setDomainName(userVm.getDomainName());
    userVmResponse.setCreated(userVm.getCreated());
    userVmResponse.setDisplayVm(userVm.isDisplayVm());
    if (userVm.getState() != null) {
        userVmResponse.setState(userVm.getState().toString());
    }
    userVmResponse.setHaEnable(userVm.isHaEnabled());
    if (details.contains(VMDetails.all) || details.contains(VMDetails.group)) {
        userVmResponse.setGroupId(userVm.getInstanceGroupUuid());
        userVmResponse.setGroup(userVm.getInstanceGroupName());
    }
    userVmResponse.setZoneId(userVm.getDataCenterUuid());
    userVmResponse.setZoneName(userVm.getDataCenterName());
    if (view == ResponseView.Full) {
        userVmResponse.setHostId(userVm.getHostUuid());
    }
    userVmResponse.setInstanceName(userVm.getInstanceName());
    userVmResponse.setHostName(userVm.getHostName());
    if (details.contains(VMDetails.all) || details.contains(VMDetails.tmpl)) {
        userVmResponse.setTemplateId(userVm.getTemplateUuid());
        userVmResponse.setTemplateName(userVm.getTemplateName());
        userVmResponse.setTemplateDisplayText(userVm.getTemplateDisplayText());
        userVmResponse.setPasswordEnabled(userVm.isPasswordEnabled());
    }
    if (details.contains(VMDetails.all) || details.contains(VMDetails.iso)) {
        userVmResponse.setIsoId(userVm.getIsoUuid());
        userVmResponse.setIsoName(userVm.getIsoName());
        userVmResponse.setIsoDisplayText(userVm.getIsoDisplayText());
    }
    if (details.contains(VMDetails.all) || details.contains(VMDetails.servoff)) {
        userVmResponse.setServiceOfferingId(userVm.getServiceOfferingUuid());
        userVmResponse.setServiceOfferingName(userVm.getServiceOfferingName());
    }
    if (details.contains(VMDetails.all) || details.contains(VMDetails.diskoff)) {
        userVmResponse.setDiskOfferingId(userVm.getDiskOfferingUuid());
        userVmResponse.setDiskOfferingName(userVm.getDiskOfferingName());
    }
    if (details.contains(VMDetails.all) || details.contains(VMDetails.servoff) || details.contains(VMDetails.stats)) {
        userVmResponse.setCpuNumber(userVm.getCpu());
        userVmResponse.setMemory(userVm.getRamSize());
        final ServiceOfferingDetailsVO serviceOfferingDetail = ApiDBUtils.findServiceOfferingDetail(userVm.getServiceOfferingId(), GPU.Keys.vgpuType.toString());
        if (serviceOfferingDetail != null) {
            userVmResponse.setVgpu(serviceOfferingDetail.getValue());
        }
    }
    userVmResponse.setGuestOsId(userVm.getGuestOsUuid());
    if (details.contains(VMDetails.all) || details.contains(VMDetails.volume)) {
        userVmResponse.setRootDeviceId(userVm.getVolumeDeviceId());
        if (userVm.getVolumeType() != null) {
            userVmResponse.setRootDeviceType(userVm.getVolumeType().toString());
        }
    }
    userVmResponse.setPassword(userVm.getPassword());
    if (userVm.getJobId() != null) {
        userVmResponse.setJobId(userVm.getJobUuid());
        userVmResponse.setJobStatus(userVm.getJobStatus());
    }
    // userVmResponse.setForVirtualNetwork(userVm.getForVirtualNetwork());
    userVmResponse.setPublicIpId(userVm.getPublicIpUuid());
    userVmResponse.setPublicIp(userVm.getPublicIpAddress());
    userVmResponse.setKeyPairName(userVm.getKeypairName());
    userVmResponse.setOsTypeId(userVm.getGuestOsId());
    if (details.contains(VMDetails.all) || details.contains(VMDetails.stats)) {
        // stats calculation
        final VmStats vmStats = ApiDBUtils.getVmStatistics(userVm.getId());
        if (vmStats != null) {
            userVmResponse.setCpuUsed(new DecimalFormat("#.##").format(vmStats.getCPUUtilization()) + "%");
            userVmResponse.setNetworkKbsRead((long) vmStats.getNetworkReadKBs());
            userVmResponse.setNetworkKbsWrite((long) vmStats.getNetworkWriteKBs());
            if ((userVm.getHypervisorType() != null) && (userVm.getHypervisorType().equals(HypervisorType.KVM) || userVm.getHypervisorType().equals(HypervisorType.XenServer))) {
                // support KVM and XenServer only util 2013.06.25
                userVmResponse.setDiskKbsRead((long) vmStats.getDiskReadKBs());
                userVmResponse.setDiskKbsWrite((long) vmStats.getDiskWriteKBs());
                userVmResponse.setDiskIORead((long) vmStats.getDiskReadIOs());
                userVmResponse.setDiskIOWrite((long) vmStats.getDiskWriteIOs());
            }
        }
    }
    if (details.contains(VMDetails.all) || details.contains(VMDetails.nics)) {
        final long nic_id = userVm.getNicId();
        if (nic_id > 0) {
            final NicResponse nicResponse = new NicResponse();
            nicResponse.setId(userVm.getNicUuid());
            nicResponse.setIpaddress(userVm.getIpAddress());
            nicResponse.setGateway(userVm.getGateway());
            nicResponse.setNetmask(userVm.getNetmask());
            nicResponse.setNetworkid(userVm.getNetworkUuid());
            nicResponse.setNetworkName(userVm.getNetworkName());
            nicResponse.setMacAddress(userVm.getMacAddress());
            nicResponse.setIp6Address(userVm.getIp6Address());
            nicResponse.setIp6Gateway(userVm.getIp6Gateway());
            nicResponse.setIp6Cidr(userVm.getIp6Cidr());
            if (userVm.getBroadcastUri() != null) {
                nicResponse.setBroadcastUri(userVm.getBroadcastUri().toString());
            }
            if (userVm.getIsolationUri() != null) {
                nicResponse.setIsolationUri(userVm.getIsolationUri().toString());
            }
            if (userVm.getTrafficType() != null) {
                nicResponse.setTrafficType(userVm.getTrafficType().toString());
            }
            if (userVm.getGuestType() != null) {
                nicResponse.setType(userVm.getGuestType().toString());
            }
            nicResponse.setIsDefault(userVm.isDefaultNic());
            final List<NicSecondaryIpVO> secondaryIps = ApiDBUtils.findNicSecondaryIps(userVm.getNicId());
            if (secondaryIps != null) {
                final List<NicSecondaryIpResponse> ipList = new ArrayList<>();
                for (final NicSecondaryIpVO ip : secondaryIps) {
                    final NicSecondaryIpResponse ipRes = new NicSecondaryIpResponse();
                    ipRes.setId(ip.getUuid());
                    ipRes.setIpAddr(ip.getIp4Address());
                    ipList.add(ipRes);
                }
                nicResponse.setSecondaryIps(ipList);
            }
            nicResponse.setObjectName("nic");
            userVmResponse.addNic(nicResponse);
        }
    }
    // update tag information
    final long tag_id = userVm.getTagId();
    if (tag_id > 0 && !userVmResponse.containTag(tag_id)) {
        final ResourceTagJoinVO vtag = ApiDBUtils.findResourceTagViewById(tag_id);
        if (vtag != null) {
            userVmResponse.addTag(ApiDBUtils.newResourceTagResponse(vtag, false));
        }
    }
    if (details.contains(VMDetails.all) || details.contains(VMDetails.affgrp)) {
        final Long affinityGroupId = userVm.getAffinityGroupId();
        if (affinityGroupId != null && affinityGroupId.longValue() != 0) {
            final AffinityGroupResponse resp = new AffinityGroupResponse();
            resp.setId(userVm.getAffinityGroupUuid());
            resp.setName(userVm.getAffinityGroupName());
            resp.setDescription(userVm.getAffinityGroupDescription());
            resp.setObjectName("affinitygroup");
            resp.setAccountName(userVm.getAccountName());
            userVmResponse.addAffinityGroup(resp);
        }
    }
    // set resource details map
    // only hypervisortoolsversion can be returned to the end user
    final UserVmDetailVO hypervisorToolsVersion = _userVmDetailsDao.findDetail(userVm.getId(), VmDetailConstants.HYPERVISOR_TOOLS_VERSION);
    if (hypervisorToolsVersion != null) {
        final Map<String, String> resourceDetails = new HashMap<>();
        resourceDetails.put(hypervisorToolsVersion.getName(), hypervisorToolsVersion.getValue());
        userVmResponse.setDetails(resourceDetails);
    }
    userVmResponse.setObjectName(objectName);
    if (userVm.isDynamicallyScalable() == null) {
        userVmResponse.setDynamicallyScalable(false);
    } else {
        userVmResponse.setDynamicallyScalable(userVm.isDynamicallyScalable());
    }
    return userVmResponse;
}
Also used : User(com.cloud.user.User) HashMap(java.util.HashMap) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) ServiceOfferingDetailsVO(com.cloud.service.ServiceOfferingDetailsVO) UserVmResponse(com.cloud.api.response.UserVmResponse) NicSecondaryIpResponse(com.cloud.api.response.NicSecondaryIpResponse) UserVmDetailVO(com.cloud.vm.UserVmDetailVO) NicSecondaryIpVO(com.cloud.vm.dao.NicSecondaryIpVO) AffinityGroupResponse(com.cloud.affinity.AffinityGroupResponse) VmStats(com.cloud.vm.VmStats) ResourceTagJoinVO(com.cloud.api.query.vo.ResourceTagJoinVO) NicResponse(com.cloud.api.response.NicResponse)

Aggregations

NicResponse (com.cloud.api.response.NicResponse)6 NicSecondaryIpResponse (com.cloud.api.response.NicSecondaryIpResponse)3 NicSecondaryIpVO (com.cloud.vm.dao.NicSecondaryIpVO)3 ArrayList (java.util.ArrayList)3 AffinityGroupResponse (com.cloud.affinity.AffinityGroupResponse)2 ResourceTagJoinVO (com.cloud.api.query.vo.ResourceTagJoinVO)2 TrafficType (com.cloud.network.Networks.TrafficType)2 ServerApiException (com.cloud.api.ServerApiException)1 DomainRouterResponse (com.cloud.api.response.DomainRouterResponse)1 ListResponse (com.cloud.api.response.ListResponse)1 UserVmResponse (com.cloud.api.response.UserVmResponse)1 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)1 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 NetworkVO (com.cloud.network.dao.NetworkVO)1 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)1 ServiceOfferingDetailsVO (com.cloud.service.ServiceOfferingDetailsVO)1 User (com.cloud.user.User)1 Nic (com.cloud.vm.Nic)1