package org.ovirt.mobile.movirt.rest.dto; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import org.ovirt.mobile.movirt.rest.ParseUtils; import org.ovirt.mobile.movirt.rest.RestEntityWrapper; import org.ovirt.mobile.movirt.util.ObjectUtils; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.List; @JsonIgnoreProperties(ignoreUnknown = true) public class Vm implements RestEntityWrapper<org.ovirt.mobile.movirt.model.Vm> { private static final String CPU_PERCENTAGE_STAT = "cpu.current.total"; private static final String TOTAL_MEMORY_STAT = "memory.installed"; private static final String USED_MEMORY_STAT = "memory.used"; // public for json mapping public String id; public String name; public Statistics statistics; public String memory; public Os os; public Cpu cpu; @JsonIgnoreProperties(ignoreUnknown = true) public static class Os { public String type; } @JsonIgnoreProperties(ignoreUnknown = true) public static class Cpu { public Topology topology; } @Override public String toString() { return String.format("Vm: name=%s, id=%s", name, id); } public org.ovirt.mobile.movirt.model.Vm toEntity() { org.ovirt.mobile.movirt.model.Vm vm = new org.ovirt.mobile.movirt.model.Vm(); vm.setId(id); vm.setName(name); if (statistics != null && statistics.statistic != null) { BigDecimal cpu = getStatisticValueByName(CPU_PERCENTAGE_STAT, statistics.statistic); BigDecimal totalMemory = getStatisticValueByName(TOTAL_MEMORY_STAT, statistics.statistic); BigDecimal usedMemory = getStatisticValueByName(USED_MEMORY_STAT, statistics.statistic); vm.setCpuUsage(cpu.doubleValue()); if (BigDecimal.ZERO.equals(totalMemory)) { vm.setMemoryUsage(0); } else { vm.setMemoryUsage(100 * usedMemory.divide(totalMemory, 3, RoundingMode.HALF_UP).doubleValue()); } vm.setUsedMemorySize(usedMemory.longValue()); } vm.setMemorySize(ObjectUtils.parseLong(memory)); if (cpu != null && cpu.topology != null) { vm.setSockets(ParseUtils.intOrDefault(cpu.topology.sockets)); vm.setCoresPerSocket(ParseUtils.intOrDefault(cpu.topology.cores)); } else { vm.setSockets(-1); vm.setCoresPerSocket(-1); } if (os != null) { vm.setOsType(os.type); } return vm; } private static BigDecimal getStatisticValueByName(String name, List<Statistic> statistics) { for (Statistic statistic : statistics) { if (name.equals(statistic.name)) { return new BigDecimal(statistic.values.value.get(0).datum); } } return BigDecimal.ZERO; } }