fix parsing ovf memory

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
This commit is contained in:
Abhishek Kumar 2026-05-05 17:22:53 +05:30
parent b452d20a3e
commit 83490a9bdb
2 changed files with 52 additions and 5 deletions

View File

@ -63,6 +63,39 @@ public class OvfXmlUtil {
return sdf;
});
protected enum MemoryAllocationUnit {
Bytes("byte", 1),
Kilobytes("byte * 2^10", 1024),
Megabytes("byte * 2^20", 1024 * 1024),
Gigabytes("byte * 2^30", 1024 * 1024 * 1024);
final String allocationUnitsToken;
final long bytesMultiplier;
MemoryAllocationUnit(String allocationUnitsToken, long bytesMultiplier) {
this.allocationUnitsToken = allocationUnitsToken;
this.bytesMultiplier = bytesMultiplier;
}
public String getAllocationUnitsToken() {
return allocationUnitsToken;
}
public long getBytesMultiplier() {
return bytesMultiplier;
}
public static MemoryAllocationUnit fromString(String value) {
for (MemoryAllocationUnit unit : MemoryAllocationUnit.values()) {
if (StringUtils.isNotBlank(value) &&
(unit.getAllocationUnitsToken().equalsIgnoreCase(value) || unit.name().equalsIgnoreCase(value))) {
return unit;
}
}
return null;
}
}
public static String toXml(final Vm vm, final UserVmJoinVO vo) {
final String vmId = vm.getId();
final String vmName = vm.getName();
@ -306,7 +339,7 @@ public class OvfXmlUtil {
sb.append("<rasd:Description>Memory Size</rasd:Description>");
sb.append("<rasd:InstanceId>2</rasd:InstanceId>");
sb.append("<rasd:ResourceType>4</rasd:ResourceType>");
sb.append("<rasd:AllocationUnits>MegaBytes</rasd:AllocationUnits>");
sb.append("<rasd:AllocationUnits>").append(MemoryAllocationUnit.Megabytes.getAllocationUnitsToken()).append("</rasd:AllocationUnits>");
sb.append("<rasd:VirtualQuantity>").append(memMb).append("</rasd:VirtualQuantity>");
sb.append("</Item>");
@ -493,9 +526,8 @@ public class OvfXmlUtil {
if (memItems != null && memItems.getLength() > 0) {
Node memItem = memItems.item(0);
String memStr = childText(memItem, "VirtualQuantity");
if (StringUtils.isNotBlank(memStr)) {
vm.setMemory(memStr);
}
String memAllocationUnitsStr = childText(memItem, "AllocationUnits");
updateVmMemory(vm, memStr, memAllocationUnitsStr);
}
// CPU
@ -529,6 +561,21 @@ public class OvfXmlUtil {
}
}
private static void updateVmMemory(Vm vm, String memStr, String memAllocationUnitsStr) {
if (StringUtils.isAnyBlank(memStr, memAllocationUnitsStr)) {
return;
}
MemoryAllocationUnit memoryAllocationUnit = MemoryAllocationUnit.fromString(memAllocationUnitsStr);
if (memoryAllocationUnit == null) {
return;
}
long memory = parseLong(memStr, 0);
if (memory == 0) {
return;
}
vm.setMemory(String.valueOf(memory * memoryAllocationUnit.getBytesMultiplier()));
}
private static void updateFromXmlCloudStackMetadataSection(Vm vm, Node metadataSection, XPath xpath) {
if (metadataSection == null) {
return;

View File

@ -37,7 +37,7 @@ public class OvfXmlUtilTest {
Vm vm = new Vm();
OvfXmlUtil.updateFromXml(vm, configuration);
assertEquals(String.valueOf(512L), vm.getMemory());
assertEquals(String.valueOf(512 * OvfXmlUtil.MemoryAllocationUnit.Megabytes.getBytesMultiplier()), vm.getMemory());
assertEquals("1", vm.getCpu().getTopology().getSockets());
assertEquals("1", vm.getCpu().getTopology().getCores());
assertEquals("1", vm.getCpu().getTopology().getThreads());