Skip to content

Commit e68bc29

Browse files
committed
Add support for functionality in hypervisor class
1 parent bf2cede commit e68bc29

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

api/src/main/java/com/cloud/hypervisor/Hypervisor.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,52 @@
2323
import java.util.Locale;
2424
import java.util.Map;
2525
import java.util.Objects;
26+
import java.util.Set;
27+
import java.util.EnumSet;
2628

2729
public class Hypervisor {
2830
public static class HypervisorType {
31+
public static enum Functionality {
32+
DirectDownloadTemplate,
33+
RootDiskSizeOverride;
34+
}
35+
2936
private static final Map<String, HypervisorType> hypervisorTypeMap = new LinkedHashMap<>();
3037
public static final HypervisorType None = new HypervisorType("None"); //for storage hosts
31-
public static final HypervisorType XenServer = new HypervisorType("XenServer", ImageFormat.VHD);
32-
public static final HypervisorType KVM = new HypervisorType("KVM", ImageFormat.QCOW2);
33-
public static final HypervisorType VMware = new HypervisorType("VMware", ImageFormat.OVA);
38+
public static final HypervisorType XenServer = new HypervisorType("XenServer", ImageFormat.VHD,
39+
EnumSet.of(Functionality.RootDiskSizeOverride));
40+
public static final HypervisorType KVM = new HypervisorType("KVM", ImageFormat.QCOW2,
41+
EnumSet.of(Functionality.DirectDownloadTemplate, Functionality.RootDiskSizeOverride));
42+
public static final HypervisorType VMware = new HypervisorType("VMware", ImageFormat.OVA,
43+
EnumSet.of(Functionality.RootDiskSizeOverride));
3444
public static final HypervisorType Hyperv = new HypervisorType("Hyperv");
3545
public static final HypervisorType VirtualBox = new HypervisorType("VirtualBox");
3646
public static final HypervisorType Parralels = new HypervisorType("Parralels");
3747
public static final HypervisorType BareMetal = new HypervisorType("BareMetal");
38-
public static final HypervisorType Simulator = new HypervisorType("Simulator");
48+
public static final HypervisorType Simulator = new HypervisorType("Simulator", null,
49+
EnumSet.of(Functionality.RootDiskSizeOverride));
3950
public static final HypervisorType Ovm = new HypervisorType("Ovm", ImageFormat.RAW);
4051
public static final HypervisorType Ovm3 = new HypervisorType("Ovm3", ImageFormat.RAW);
4152
public static final HypervisorType LXC = new HypervisorType("LXC");
42-
public static final HypervisorType Custom = new HypervisorType("Custom");
53+
public static final HypervisorType Custom = new HypervisorType("Custom", null,
54+
EnumSet.of(Functionality.RootDiskSizeOverride));
4355
public static final HypervisorType Any = new HypervisorType("Any"); /*If you don't care about the hypervisor type*/
4456
private final String name;
4557
private final ImageFormat imageFormat;
58+
private final Set<Functionality> supportedFunctionalities;
4659

4760
public HypervisorType(String name) {
48-
this(name, null);
61+
this(name, null, EnumSet.noneOf(Functionality.class));
4962
}
5063

5164
public HypervisorType(String name, ImageFormat imageFormat) {
65+
this(name, imageFormat, EnumSet.noneOf(Functionality.class));
66+
}
67+
68+
public HypervisorType(String name, ImageFormat imageFormat, Set<Functionality> supportedFunctionalities) {
5269
this.name = name;
5370
this.imageFormat = imageFormat;
71+
this.supportedFunctionalities = supportedFunctionalities;
5472
if (name.equals("Parralels")){ // typo in the original code
5573
hypervisorTypeMap.put("parallels", this);
5674
} else {
@@ -102,6 +120,15 @@ public String name() {
102120
return name;
103121
}
104122

123+
/**
124+
* Make this method to be part of the properties of the hypervisor type itself.
125+
*
126+
* @return true if the hypervisor plugin support the specified functionality
127+
*/
128+
public boolean isFunctionalitySupported(Functionality functionality) {
129+
return supportedFunctionalities.contains(functionality);
130+
}
131+
105132
@Override
106133
public int hashCode() {
107134
return Objects.hash(name);

api/src/main/java/org/apache/cloudstack/api/command/user/template/RegisterTemplateCmd.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,9 @@ protected void validateParameters() {
362362
"Parameter zoneids cannot combine all zones (-1) option with other zones");
363363

364364
String customHypervisor = HypervisorGuru.HypervisorCustomDisplayName.value();
365-
if (isDirectDownload() && !(getHypervisor().equalsIgnoreCase(Hypervisor.HypervisorType.KVM.toString())
365+
if (isDirectDownload() &&
366+
!(Hypervisor.HypervisorType.getType(getHypervisor())
367+
.isFunctionalitySupported(Hypervisor.HypervisorType.Functionality.DirectDownloadTemplate)
366368
|| getHypervisor().equalsIgnoreCase(customHypervisor))) {
367369
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, String.format("Parameter directdownload " +
368370
"is only allowed for KVM or %s templates", customHypervisor));

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -686,14 +686,6 @@ public void setKubernetesServiceHelpers(final List<KubernetesServiceHelper> kube
686686
HypervisorType.Simulator
687687
));
688688

689-
protected static final List<HypervisorType> ROOT_DISK_SIZE_OVERRIDE_SUPPORTING_HYPERVISORS = Arrays.asList(
690-
HypervisorType.KVM,
691-
HypervisorType.XenServer,
692-
HypervisorType.VMware,
693-
HypervisorType.Simulator,
694-
HypervisorType.Custom
695-
);
696-
697689
private static final List<HypervisorType> HYPERVISORS_THAT_CAN_DO_STORAGE_MIGRATION_ON_NON_USER_VMS = Arrays.asList(HypervisorType.KVM, HypervisorType.VMware);
698690

699691
@Override
@@ -4563,7 +4555,7 @@ protected long configureCustomRootDiskSize(Map<String, String> customParameters,
45634555
* @throws InvalidParameterValueException if the hypervisor does not support rootdisksize override
45644556
*/
45654557
protected void verifyIfHypervisorSupportsRootdiskSizeOverride(HypervisorType hypervisorType) {
4566-
if (!ROOT_DISK_SIZE_OVERRIDE_SUPPORTING_HYPERVISORS.contains(hypervisorType)) {
4558+
if (!hypervisorType.isFunctionalitySupported(HypervisorType.Functionality.RootDiskSizeOverride)) {
45674559
throw new InvalidParameterValueException("Hypervisor " + hypervisorType + " does not support rootdisksize override");
45684560
}
45694561
}

server/src/test/java/com/cloud/vm/UserVmManagerImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ public void verifyIfHypervisorSupportRootdiskSizeOverrideTest() {
635635
int expectedExceptionCounter = hypervisorTypeArray.length - 5;
636636

637637
for(int i = 0; i < hypervisorTypeArray.length; i++) {
638-
if (UserVmManagerImpl.ROOT_DISK_SIZE_OVERRIDE_SUPPORTING_HYPERVISORS.contains(hypervisorTypeArray[i])) {
638+
if (hypervisorTypeArray[i].isFunctionalitySupported(Hypervisor.HypervisorType.Functionality.RootDiskSizeOverride)) {
639639
userVmManagerImpl.verifyIfHypervisorSupportsRootdiskSizeOverride(hypervisorTypeArray[i]);
640640
} else {
641641
try {

0 commit comments

Comments
 (0)