Map<Long, Network> networks = new HashMap<Long, Network>();
List<UserVmResponse> vmResponses = new ArrayList<UserVmResponse>();
for (UserVm userVm : userVms) {
UserVmResponse userVmResponse = new UserVmResponse();
Account acct = ApiDBUtils.findAccountById(Long.valueOf(userVm.getAccountId()));
if (acct != null) {
userVmResponse.setAccountName(acct.getAccountName());
userVmResponse.setDomainId(acct.getDomainId());
userVmResponse.setDomainName(ApiDBUtils.findDomainById(acct.getDomainId()).getName());
}
userVmResponse.setId(userVm.getId());
userVmResponse.setName(userVm.getHostName());
userVmResponse.setCreated(userVm.getCreated());
userVmResponse.setHaEnable(userVm.isHaEnabled());
if (userVm.getDisplayName() != null) {
userVmResponse.setDisplayName(userVm.getDisplayName());
} else {
userVmResponse.setDisplayName(userVm.getHostName());
}
if (caller.getType() == Account.ACCOUNT_TYPE_ADMIN) {
userVmResponse.setInstanceName(userVm.getInstanceName());
}
if (userVm.getPassword() != null) {
userVmResponse.setPassword(userVm.getPassword());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.group)) {
InstanceGroupVO group = ApiDBUtils.findInstanceGroupForVM(userVm.getId());
if (group != null) {
userVmResponse.setGroup(group.getName());
userVmResponse.setGroupId(group.getId());
}
}
// Data Center Info
DataCenter zone = dataCenters.get(userVm.getDataCenterIdToDeployIn());
if (zone == null) {
zone = ApiDBUtils.findZoneById(userVm.getDataCenterIdToDeployIn());
dataCenters.put(zone.getId(), zone);
}
userVmResponse.setZoneId(zone.getId());
userVmResponse.setZoneName(zone.getName());
// if user is an admin, display host id
if (((caller == null) || (caller.getType() == Account.ACCOUNT_TYPE_ADMIN)) && (userVm.getHostId() != null)) {
Host host = hosts.get(userVm.getHostId());
if (host == null) {
host = ApiDBUtils.findHostById(userVm.getHostId());
hosts.put(host.getId(), host);
}
userVmResponse.setHostId(host.getId());
userVmResponse.setHostName(host.getName());
}
if (userVm.getState() != null) {
if (userVm.getHostId() != null) {
Host host = hosts.get(userVm.getHostId());
if (host == null) {
host = ApiDBUtils.findHostById(userVm.getHostId());
hosts.put(host.getId(), host);
}
if (host.getStatus() != com.cloud.host.Status.Up) {
userVmResponse.setState(VirtualMachine.State.Unknown.toString());
} else {
userVmResponse.setState(userVm.getState().toString());
}
} else {
userVmResponse.setState(userVm.getState().toString());
}
}
if (userVm.getHypervisorType() != null) {
userVmResponse.setHypervisor(userVm.getHypervisorType().toString());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.tmpl)) {
// Template Info
VMTemplateVO template = templates.get(userVm.getTemplateId());
if (template == null) {
template = ApiDBUtils.findTemplateById(userVm.getTemplateId());
if (template != null) {
templates.put(template.getId(), template);
}
}
if (template != null) {
userVmResponse.setTemplateId(userVm.getTemplateId());
userVmResponse.setTemplateName(template.getName());
userVmResponse.setTemplateDisplayText(template.getDisplayText());
userVmResponse.setPasswordEnabled(template.getEnablePassword());
} else {
userVmResponse.setTemplateId(-1L);
userVmResponse.setTemplateName("ISO Boot");
userVmResponse.setTemplateDisplayText("ISO Boot");
userVmResponse.setPasswordEnabled(false);
}
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.iso)) {
// ISO Info
VMTemplateVO iso = templates.get(userVm.getIsoId());
if (iso == null) {
iso = ApiDBUtils.findTemplateById(userVm.getIsoId());
if (iso != null) {
templates.put(iso.getId(), iso);
}
}
if (iso != null) {
userVmResponse.setIsoId(iso.getId());
userVmResponse.setIsoName(iso.getName());
}
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.servoff)) {
// Service Offering Info
ServiceOffering offering = serviceOfferings.get(userVm.getServiceOfferingId());
if (offering == null) {
offering = ApiDBUtils.findServiceOfferingById(userVm.getServiceOfferingId());
serviceOfferings.put(offering.getId(), offering);
}
userVmResponse.setServiceOfferingId(offering.getId());
userVmResponse.setServiceOfferingName(offering.getName());
userVmResponse.setCpuNumber(offering.getCpu());
userVmResponse.setCpuSpeed(offering.getSpeed());
userVmResponse.setMemory(offering.getRamSize());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.volume)) {
VolumeVO rootVolume = ApiDBUtils.findRootVolume(userVm.getId());
if (rootVolume != null) {
userVmResponse.setRootDeviceId(rootVolume.getDeviceId());
String rootDeviceType = "Not created";
if (rootVolume.getPoolId() != null) {
StoragePoolVO storagePool = ApiDBUtils.findStoragePoolById(rootVolume.getPoolId());
rootDeviceType = storagePool.getPoolType().toString();
}
userVmResponse.setRootDeviceType(rootDeviceType);
}
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.stats)) {
// stats calculation
DecimalFormat decimalFormat = new DecimalFormat("#.##");
String cpuUsed = null;
VmStats vmStats = ApiDBUtils.getVmStatistics(userVm.getId());
if (vmStats != null) {
float cpuUtil = (float) vmStats.getCPUUtilization();
cpuUsed = decimalFormat.format(cpuUtil) + "%";
userVmResponse.setCpuUsed(cpuUsed);
Double networkKbRead = Double.valueOf(vmStats.getNetworkReadKBs());
userVmResponse.setNetworkKbsRead(networkKbRead.longValue());
Double networkKbWrite = Double.valueOf(vmStats.getNetworkWriteKBs());
userVmResponse.setNetworkKbsWrite(networkKbWrite.longValue());
}
}
userVmResponse.setGuestOsId(userVm.getGuestOSId());
if (details.contains(VMDetails.all) || details.contains(VMDetails.secgrp)) {
// security groups - list only when zone is security group enabled
if (zone.isSecurityGroupEnabled()) {
List<SecurityGroupVO> securityGroups = ApiDBUtils.getSecurityGroupsForVm(userVm.getId());
List<SecurityGroupResponse> securityGroupResponse = new ArrayList<SecurityGroupResponse>();
for (SecurityGroupVO grp : securityGroups) {
SecurityGroupResponse resp = new SecurityGroupResponse();
resp.setId(grp.getId());
resp.setName(grp.getName());
resp.setDescription(grp.getDescription());
resp.setObjectName("securitygroup");
securityGroupResponse.add(resp);
}
userVmResponse.setSecurityGroupList(securityGroupResponse);
}
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.nics)) {
List<NicProfile> nicProfiles = ApiDBUtils.getNics(userVm);
List<NicResponse> nicResponses = new ArrayList<NicResponse>();
for (NicProfile singleNicProfile : nicProfiles) {
NicResponse nicResponse = new NicResponse();
nicResponse.setId(singleNicProfile.getId());
nicResponse.setIpaddress(singleNicProfile.getIp4Address());
nicResponse.setGateway(singleNicProfile.getGateway());
nicResponse.setNetmask(singleNicProfile.getNetmask());
nicResponse.setNetworkid(singleNicProfile.getNetworkId());
nicResponse.setNetworkName(ApiDBUtils.findNetworkById(singleNicProfile.getNetworkId()).getName());
if (acct.getType() == Account.ACCOUNT_TYPE_ADMIN) {
if (singleNicProfile.getBroadCastUri() != null) {
nicResponse.setBroadcastUri(singleNicProfile.getBroadCastUri().toString());
}
if (singleNicProfile.getIsolationUri() != null) {
nicResponse.setIsolationUri(singleNicProfile.getIsolationUri().toString());
}
}
// Long networkId = singleNicProfile.getNetworkId();
Network network = networks.get(singleNicProfile.getNetworkId());
if (network == null) {
network = ApiDBUtils.findNetworkById(singleNicProfile.getNetworkId());
networks.put(singleNicProfile.getNetworkId(), network);
}
nicResponse.setTrafficType(network.getTrafficType().toString());
nicResponse.setType(network.getGuestType().toString());
nicResponse.setIsDefault(singleNicProfile.isDefaultNic());
nicResponse.setObjectName("nic");
nicResponses.add(nicResponse);
}
userVmResponse.setNics(nicResponses);
}
//set tag information
List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(TaggedResourceType.UserVm, userVm.getId());
List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
for (ResourceTag tag : tags) {
ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
tagResponses.add(tagResponse);
}
userVmResponse.setTags(tagResponses);
IpAddress ip = ApiDBUtils.findIpByAssociatedVmId(userVm.getId());
if (ip != null) {
userVmResponse.setPublicIpId(ip.getId());
userVmResponse.setPublicIp(ip.getAddress().addr());
}
userVmResponse.setObjectName(objectName);
vmResponses.add(userVmResponse);
}
return vmResponses;
}