@@ -63,6 +63,39 @@ public class OvfXmlUtil {
6363 return sdf ;
6464 });
6565
66+ protected enum MemoryAllocationUnit {
67+ Bytes ("byte" , 1 ),
68+ Kilobytes ("byte * 2^10" , 1024 ),
69+ Megabytes ("byte * 2^20" , 1024 * 1024 ),
70+ Gigabytes ("byte * 2^30" , 1024 * 1024 * 1024 );
71+
72+ final String allocationUnitsToken ;
73+ final long bytesMultiplier ;
74+
75+ MemoryAllocationUnit (String allocationUnitsToken , long bytesMultiplier ) {
76+ this .allocationUnitsToken = allocationUnitsToken ;
77+ this .bytesMultiplier = bytesMultiplier ;
78+ }
79+
80+ public String getAllocationUnitsToken () {
81+ return allocationUnitsToken ;
82+ }
83+
84+ public long getBytesMultiplier () {
85+ return bytesMultiplier ;
86+ }
87+
88+ public static MemoryAllocationUnit fromString (String value ) {
89+ for (MemoryAllocationUnit unit : MemoryAllocationUnit .values ()) {
90+ if (StringUtils .isNotBlank (value ) &&
91+ (unit .getAllocationUnitsToken ().equalsIgnoreCase (value ) || unit .name ().equalsIgnoreCase (value ))) {
92+ return unit ;
93+ }
94+ }
95+ return null ;
96+ }
97+ }
98+
6699 public static String toXml (final Vm vm , final UserVmJoinVO vo ) {
67100 final String vmId = vm .getId ();
68101 final String vmName = vm .getName ();
@@ -306,7 +339,7 @@ public static String toXml(final Vm vm, final UserVmJoinVO vo) {
306339 sb .append ("<rasd:Description>Memory Size</rasd:Description>" );
307340 sb .append ("<rasd:InstanceId>2</rasd:InstanceId>" );
308341 sb .append ("<rasd:ResourceType>4</rasd:ResourceType>" );
309- sb .append ("<rasd:AllocationUnits>MegaBytes </rasd:AllocationUnits>" );
342+ sb .append ("<rasd:AllocationUnits>" ). append ( MemoryAllocationUnit . Megabytes . getAllocationUnitsToken ()). append ( " </rasd:AllocationUnits>" );
310343 sb .append ("<rasd:VirtualQuantity>" ).append (memMb ).append ("</rasd:VirtualQuantity>" );
311344 sb .append ("</Item>" );
312345
@@ -493,9 +526,8 @@ private static void updateFromXmlHardwareSection(Vm vm, Node hwSection, XPath xp
493526 if (memItems != null && memItems .getLength () > 0 ) {
494527 Node memItem = memItems .item (0 );
495528 String memStr = childText (memItem , "VirtualQuantity" );
496- if (StringUtils .isNotBlank (memStr )) {
497- vm .setMemory (memStr );
498- }
529+ String memAllocationUnitsStr = childText (memItem , "AllocationUnits" );
530+ updateVmMemory (vm , memStr , memAllocationUnitsStr );
499531 }
500532
501533 // CPU
@@ -529,6 +561,21 @@ private static void updateFromXmlHardwareSection(Vm vm, Node hwSection, XPath xp
529561 }
530562 }
531563
564+ private static void updateVmMemory (Vm vm , String memStr , String memAllocationUnitsStr ) {
565+ if (StringUtils .isAnyBlank (memStr , memAllocationUnitsStr )) {
566+ return ;
567+ }
568+ MemoryAllocationUnit memoryAllocationUnit = MemoryAllocationUnit .fromString (memAllocationUnitsStr );
569+ if (memoryAllocationUnit == null ) {
570+ return ;
571+ }
572+ long memory = parseLong (memStr , 0 );
573+ if (memory == 0 ) {
574+ return ;
575+ }
576+ vm .setMemory (String .valueOf (memory * memoryAllocationUnit .getBytesMultiplier ()));
577+ }
578+
532579 private static void updateFromXmlCloudStackMetadataSection (Vm vm , Node metadataSection , XPath xpath ) {
533580 if (metadataSection == null ) {
534581 return ;
0 commit comments