Skip to content

Commit 8a35487

Browse files
author
Daan Hoogland
committed
refactor null checks
1 parent d681dfa commit 8a35487

File tree

1 file changed

+57
-46
lines changed

1 file changed

+57
-46
lines changed

server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3211,52 +3211,12 @@ public ServiceOffering createServiceOffering(final CreateServiceOfferingCmd cmd)
32113211
Integer maxMemory = cmd.getMaxMemory();
32123212
Integer minMemory = cmd.getMinMemory();
32133213

3214-
checkSpeedOnConstrainedOffering(cmd.isCustomized(), cpuSpeed, offeringName, maxCPU, minCPU, maxMemory, minMemory);
3215-
32163214
// Check if service offering is Custom,
3217-
// If Customized, the following conditions must hold
3218-
// 1. cpuNumber, cpuSpeed and memory should be all null
3219-
// 2. minCPU, maxCPU, minMemory and maxMemory should all be null or all specified
32203215
boolean isCustomized = cmd.isCustomized();
32213216
if (isCustomized) {
3222-
// validate specs
3223-
//restricting the createserviceoffering to allow setting all or none of the dynamic parameters to null
3224-
if (cpuNumber != null || memory != null) {
3225-
throw new InvalidParameterValueException("For creating a custom compute offering cpu and memory all should be null");
3226-
}
3227-
// if any of them is null, then all of them shoull be null
3228-
if (maxCPU == null || minCPU == null || maxMemory == null || minMemory == null || cpuSpeed == null) {
3229-
if (maxCPU != null || minCPU != null || maxMemory != null || minMemory != null || cpuSpeed != null) {
3230-
throw new InvalidParameterValueException("For creating a custom compute offering min/max cpu and min/max memory/cpu speed should all be null or all specified");
3231-
}
3232-
} else {
3233-
if (cpuSpeed.intValue() < 0 || cpuSpeed.longValue() > Integer.MAX_VALUE) {
3234-
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the cpu speed value between 1 and " + Integer.MAX_VALUE);
3235-
}
3236-
if ((maxCPU <= 0 || maxCPU.longValue() > Integer.MAX_VALUE) || (minCPU <= 0 || minCPU.longValue() > Integer.MAX_VALUE ) ) {
3237-
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the minimum or minimum cpu number value between 1 and " + Integer.MAX_VALUE);
3238-
}
3239-
if (minMemory < 32 || (minMemory.longValue() > Integer.MAX_VALUE) || (maxMemory.longValue() > Integer.MAX_VALUE)) {
3240-
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the memory value between 32 and " + Integer.MAX_VALUE + " MB");
3241-
}
3242-
// Persist min/max CPU and Memory parameters in the service_offering_details table
3243-
details.put(ApiConstants.MIN_MEMORY, minMemory.toString());
3244-
details.put(ApiConstants.MAX_MEMORY, maxMemory.toString());
3245-
details.put(ApiConstants.MIN_CPU_NUMBER, minCPU.toString());
3246-
details.put(ApiConstants.MAX_CPU_NUMBER, maxCPU.toString());
3247-
}
3217+
checkSpeedOnCustomOffering(cpuNumber, memory, maxCPU, minCPU, maxMemory, minMemory, cpuSpeed, offeringName, details);
32483218
} else {
3249-
Integer maxCPUCores = VM_SERVICE_OFFERING_MAX_CPU_CORES.value() == 0 ? Integer.MAX_VALUE: VM_SERVICE_OFFERING_MAX_CPU_CORES.value();
3250-
Integer maxRAMSize = VM_SERVICE_OFFERING_MAX_RAM_SIZE.value() == 0 ? Integer.MAX_VALUE: VM_SERVICE_OFFERING_MAX_RAM_SIZE.value();
3251-
if (cpuNumber != null && (cpuNumber.intValue() <= 0 || cpuNumber.longValue() > maxCPUCores)) {
3252-
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the cpu number value between 1 and " + maxCPUCores);
3253-
}
3254-
if (cpuSpeed == null || (cpuSpeed.intValue() < 0 || cpuSpeed.longValue() > Integer.MAX_VALUE)) {
3255-
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the cpu speed value between 0 and " + Integer.MAX_VALUE);
3256-
}
3257-
if (memory != null && (memory.intValue() < 32 || memory.longValue() > maxRAMSize)) {
3258-
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the memory value between 32 and " + maxRAMSize + " MB");
3259-
}
3219+
checkSpeedOnConstrainedOffering(cpuSpeed, offeringName, maxCPU, minCPU, maxMemory, minMemory, cpuNumber, memory);
32603220
}
32613221

32623222
// check if valid domain
@@ -3378,6 +3338,48 @@ public ServiceOffering createServiceOffering(final CreateServiceOfferingCmd cmd)
33783338
cmd.getDiskOfferingStrictness(), cmd.isCustomized(), cmd.getEncryptRoot(), cmd.isPurgeResources());
33793339
}
33803340

3341+
/**
3342+
* If Customized, the following conditions must hold
3343+
* 1. cpuNumber, cpuSpeed and memory should be all null
3344+
* 2. minCPU, maxCPU, minMemory and maxMemory should all be null or all specified
3345+
* @param cpuNumber
3346+
* @param memory
3347+
* @param maxCPU
3348+
* @param minCPU
3349+
* @param maxMemory
3350+
* @param minMemory
3351+
* @param cpuSpeed
3352+
* @param offeringName
3353+
*/
3354+
private static void checkSpeedOnCustomOffering(Integer cpuNumber, Integer memory, Integer maxCPU, Integer minCPU, Integer maxMemory, Integer minMemory, Integer cpuSpeed, String offeringName, Map<String, String> details) {
3355+
// validate specs
3356+
//restricting the createserviceoffering to allow setting all or none of the dynamic parameters to null
3357+
if (cpuNumber != null || memory != null) {
3358+
throw new InvalidParameterValueException("For creating a custom compute offering cpu and memory all should be null");
3359+
}
3360+
// if any of them is null, then all of them should be null
3361+
if (maxCPU == null || minCPU == null || maxMemory == null || minMemory == null || cpuSpeed == null) {
3362+
if (maxCPU != null || minCPU != null || maxMemory != null || minMemory != null || cpuSpeed != null) {
3363+
throw new InvalidParameterValueException("For creating a custom compute offering min/max cpu and min/max memory/cpu speed should all be null or all specified");
3364+
}
3365+
} else {
3366+
if (cpuSpeed.intValue() < 0 || cpuSpeed.longValue() > Integer.MAX_VALUE) {
3367+
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the cpu speed value between 1 and " + Integer.MAX_VALUE);
3368+
}
3369+
if ((maxCPU <= 0 || maxCPU.longValue() > Integer.MAX_VALUE) || (minCPU <= 0 || minCPU.longValue() > Integer.MAX_VALUE ) ) {
3370+
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the minimum or minimum cpu number value between 1 and " + Integer.MAX_VALUE);
3371+
}
3372+
if (minMemory < 32 || (minMemory.longValue() > Integer.MAX_VALUE) || (maxMemory.longValue() > Integer.MAX_VALUE)) {
3373+
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the memory value between 32 and " + Integer.MAX_VALUE + " MB");
3374+
}
3375+
// Persist min/max CPU and Memory parameters in the service_offering_details table
3376+
details.put(ApiConstants.MIN_MEMORY, minMemory.toString());
3377+
details.put(ApiConstants.MAX_MEMORY, maxMemory.toString());
3378+
details.put(ApiConstants.MIN_CPU_NUMBER, minCPU.toString());
3379+
details.put(ApiConstants.MAX_CPU_NUMBER, maxCPU.toString());
3380+
}
3381+
}
3382+
33813383
private static void checkNameAndText(String name, String displayText) {
33823384
if (name == null || name.length() == 0) {
33833385
throw new InvalidParameterValueException("Failed to create service offering: specify the name that has non-zero length");
@@ -3388,16 +3390,25 @@ private static void checkNameAndText(String name, String displayText) {
33883390
}
33893391
}
33903392

3391-
private static void checkSpeedOnConstrainedOffering(boolean customised, Integer cpuSpeed, String offeringName, Integer maxCPU, Integer minCPU, Integer maxMemory, Integer minMemory) {
3393+
private static void checkSpeedOnConstrainedOffering(Integer cpuSpeed, String offeringName, Integer maxCPU, Integer minCPU, Integer maxMemory, Integer minMemory, Integer cpuNumber, Integer memory) {
33923394
// Check for the combination of zero speed and custom or constrained offering
33933395
if (cpuSpeed != null && cpuSpeed.intValue() == 0) {
3394-
if (customised) {
3395-
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": cpu speed cannot be zero for custom offerings");
3396-
}
33973396
if (maxCPU != null || minCPU != null || maxMemory != null || minMemory != null) {
33983397
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": cpu speed cannot be zero for constrained offerings");
33993398
}
34003399
}
3400+
Integer maxCPUCores = VM_SERVICE_OFFERING_MAX_CPU_CORES.value() == 0 ? Integer.MAX_VALUE: VM_SERVICE_OFFERING_MAX_CPU_CORES.value();
3401+
Integer maxRAMSize = VM_SERVICE_OFFERING_MAX_RAM_SIZE.value() == 0 ? Integer.MAX_VALUE: VM_SERVICE_OFFERING_MAX_RAM_SIZE.value();
3402+
if (cpuNumber != null && (cpuNumber.intValue() <= 0 || cpuNumber.longValue() > maxCPUCores)) {
3403+
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the cpu number value between 1 and " + maxCPUCores);
3404+
}
3405+
if (cpuSpeed == null || (cpuSpeed.intValue() < 0 || cpuSpeed.longValue() > Integer.MAX_VALUE)) {
3406+
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the cpu speed value between 0 and " + Integer.MAX_VALUE);
3407+
}
3408+
if (memory != null && (memory.intValue() < 32 || memory.longValue() > maxRAMSize)) {
3409+
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the memory value between 32 and " + maxRAMSize + " MB");
3410+
}
3411+
34013412
}
34023413

34033414
protected ServiceOfferingVO createServiceOffering(final long userId, final boolean isSystem, final VirtualMachine.Type vmType,

0 commit comments

Comments
 (0)