Skip to content

Commit b9dd4da

Browse files
author
Henrique Sato
committed
Refactor
1 parent aa4271b commit b9dd4da

File tree

7 files changed

+97
-65
lines changed

7 files changed

+97
-65
lines changed

engine/api/src/main/java/org/apache/cloudstack/engine/orchestration/service/NetworkOrchestrationService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,10 @@ void implementNetworkElementsAndResources(DeployDestination dest, ReservationCon
361361
* Returns the maximum number of NICs that the given virtual machine can have considering its hypervisor.
362362
* <br/><br/>
363363
* First we try to retrieve the setting value from the cluster where the virtual machine is deployed. If the cluster does not exist, we try to retrieve the setting value from the virtual machine hypervisor type.
364-
* In last case, if the virtual machine does not have a hypervisor type, we retrieve the smallest value between the {@link NetworkOrchestrationService#VirtualMachineMaxNicsKvm}, {@link NetworkOrchestrationService#VirtualMachineMaxNicsVmware}
365-
* and {@link NetworkOrchestrationService#VirtualMachineMaxNicsXenserver} global settings.
364+
* Returns null if the setting value could not be extracted.
366365
*
367366
* @param virtualMachine Virtual machine to get the maximum number of NICs.
368367
* @return The maximum number of NICs that the virtual machine can have.
369368
*/
370-
int getVirtualMachineMaxNicsValue(VirtualMachine virtualMachine);
369+
Integer getVirtualMachineMaxNicsValue(VirtualMachine virtualMachine);
371370
}

engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@
257257
import com.cloud.vm.dao.UserVmDao;
258258
import com.cloud.vm.dao.VMInstanceDao;
259259
import com.googlecode.ipv6.IPv6Address;
260-
import org.apache.commons.lang3.math.NumberUtils;
261260
import org.jetbrains.annotations.NotNull;
262261

263262
/**
@@ -1041,10 +1040,10 @@ public void saveExtraDhcpOptions(final String networkUuid, final Long nicId, fin
10411040
public Pair<NicProfile, Integer> allocateNic(final NicProfile requested, final Network network, final Boolean isDefaultNic, int deviceId, final VirtualMachineProfile vm)
10421041
throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, ConcurrentOperationException {
10431042

1044-
int virtualMachineMaxNicsValue = getVirtualMachineMaxNicsValue(vm.getVirtualMachine());
1043+
Integer virtualMachineMaxNicsValue = getVirtualMachineMaxNicsValue(vm.getVirtualMachine());
10451044
List<NicVO> nics = _nicDao.listByVmId(vm.getId());
10461045

1047-
if (nics.size() >= virtualMachineMaxNicsValue) {
1046+
if (virtualMachineMaxNicsValue != null && nics.size() >= virtualMachineMaxNicsValue) {
10481047
throw new CloudRuntimeException(String.format("Failed to allocate NIC on network [%s] because VM [%s] has reached its maximum NIC capacity [%s].", network.getUuid(), vm.getUuid(), virtualMachineMaxNicsValue));
10491048
}
10501049

@@ -1099,16 +1098,16 @@ public Pair<NicProfile, Integer> allocateNic(final NicProfile requested, final N
10991098
}
11001099

11011100
@Override
1102-
public int getVirtualMachineMaxNicsValue(VirtualMachine virtualMachine) {
1101+
public Integer getVirtualMachineMaxNicsValue(VirtualMachine virtualMachine) {
11031102
Integer virtualMachineMaxNicsValue = getVirtualMachineMaxNicsValueFromCluster(virtualMachine);
11041103

11051104
if (virtualMachineMaxNicsValue != null) {
11061105
return virtualMachineMaxNicsValue;
11071106
}
11081107

11091108
if (virtualMachine.getHypervisorType() == null) {
1110-
logger.debug("Using the smallest setting global value between {}, {} and {} as the VM {} does not have a hypervisor type and is not deployed on either a host or a cluster.", VirtualMachineMaxNicsKvm, VirtualMachineMaxNicsVmware, VirtualMachineMaxNicsXenserver, virtualMachine.getUuid());
1111-
return NumberUtils.min(VirtualMachineMaxNicsKvm.value(), VirtualMachineMaxNicsVmware.value(), VirtualMachineMaxNicsXenserver.value());
1109+
logger.debug("Not considering the hypervisor maximum limits as we were unable to find a compatible hypervisor on the VM and VM cluster for virtual machine maximum NICs settings.");
1110+
return null;
11121111
}
11131112

11141113
return getVirtualMachineMaxNicsValueFromVmHypervisorType(virtualMachine);
@@ -1140,22 +1139,22 @@ protected Integer getVirtualMachineMaxNicsValueFromCluster(VirtualMachine virtua
11401139
* @param cluster Cluster to get the virtual machine max NICs value from.
11411140
* @return The maximum number of NICs that the virtual machine can have.
11421141
*/
1143-
protected int getVirtualMachineMaxNicsValueFromCluster(ClusterVO cluster) {
1144-
int virtualMachineMaxNicsValue = 0;
1145-
HypervisorType hypervisor = cluster.getHypervisorType();
1146-
1147-
if (HypervisorType.KVM.equals(hypervisor)) {
1148-
virtualMachineMaxNicsValue = VirtualMachineMaxNicsKvm.valueIn(cluster.getId());
1149-
logger.debug("The cluster {} where the VM is deployed uses the {} hypervisor. Therefore, the {} setting value [{}] will be used.", cluster.getName(), hypervisor, VirtualMachineMaxNicsKvm, virtualMachineMaxNicsValue);
1150-
} else if (HypervisorType.VMware.equals(hypervisor)) {
1151-
virtualMachineMaxNicsValue = VirtualMachineMaxNicsVmware.valueIn(cluster.getId());
1152-
logger.debug("The cluster {} where the VM is deployed uses the {} hypervisor. Therefore, the {} setting value [{}] will be used.", cluster.getName(), hypervisor, VirtualMachineMaxNicsVmware, virtualMachineMaxNicsValue);
1153-
} else {
1154-
virtualMachineMaxNicsValue = VirtualMachineMaxNicsXenserver.valueIn(cluster.getId());
1155-
logger.debug("The cluster {} where the VM is deployed uses the {} hypervisor. Therefore, the {} setting value [{}] will be used.", cluster.getName(), hypervisor, VirtualMachineMaxNicsXenserver, virtualMachineMaxNicsValue);
1142+
protected Integer getVirtualMachineMaxNicsValueFromCluster(ClusterVO cluster) {
1143+
HypervisorType clusterHypervisor = cluster.getHypervisorType();
1144+
1145+
switch (clusterHypervisor) {
1146+
case KVM:
1147+
logger.debug("The cluster {} where the VM is deployed uses the {} hypervisor. Therefore, the {} setting value [{}] will be used.", cluster.getName(), clusterHypervisor, VirtualMachineMaxNicsKvm, VirtualMachineMaxNicsKvm.valueIn(cluster.getId()));
1148+
return VirtualMachineMaxNicsKvm.valueIn(cluster.getId());
1149+
case VMware:
1150+
logger.debug("The cluster {} where the VM is deployed uses the {} hypervisor. Therefore, the {} setting value [{}] will be used.", cluster.getName(), clusterHypervisor, VirtualMachineMaxNicsKvm, VirtualMachineMaxNicsVmware.valueIn(cluster.getId()));
1151+
return VirtualMachineMaxNicsVmware.valueIn(cluster.getId());
1152+
case XenServer:
1153+
logger.debug("The cluster {} where the VM is deployed uses the {} hypervisor. Therefore, the {} setting value [{}] will be used.", cluster.getName(), clusterHypervisor, VirtualMachineMaxNicsXenserver, VirtualMachineMaxNicsXenserver.valueIn(cluster.getId()));
1154+
return VirtualMachineMaxNicsXenserver.valueIn(cluster.getId());
1155+
default:
1156+
return null;
11561157
}
1157-
1158-
return virtualMachineMaxNicsValue;
11591158
}
11601159

11611160
/**
@@ -1164,19 +1163,23 @@ protected int getVirtualMachineMaxNicsValueFromCluster(ClusterVO cluster) {
11641163
* @param virtualMachine Virtual machine to get the hypervisor type.
11651164
* @return The maximum number of NICs that the virtual machine can have.
11661165
*/
1167-
protected int getVirtualMachineMaxNicsValueFromVmHypervisorType(VirtualMachine virtualMachine) {
1166+
protected Integer getVirtualMachineMaxNicsValueFromVmHypervisorType(VirtualMachine virtualMachine) {
11681167
HypervisorType virtualMachineHypervisorType = virtualMachine.getHypervisorType();
11691168

1170-
if (HypervisorType.KVM.equals(virtualMachineHypervisorType)) {
1171-
logger.debug("Using the {} setting global value {} as the VM {} has the {} hypervisor type and is not deployed on either a host or a cluster.", VirtualMachineMaxNicsKvm, VirtualMachineMaxNicsKvm.value(), virtualMachine.getUuid(), virtualMachineHypervisorType);
1172-
return VirtualMachineMaxNicsKvm.value();
1173-
} else if (HypervisorType.VMware.equals(virtualMachineHypervisorType)) {
1174-
logger.debug("Using the {} setting global value {} as the VM {} has the {} hypervisor type and is not deployed on either a host or a cluster.", VirtualMachineMaxNicsVmware, VirtualMachineMaxNicsVmware.value(), virtualMachine.getUuid(), virtualMachineHypervisorType);
1175-
return VirtualMachineMaxNicsVmware.value();
1169+
switch (virtualMachineHypervisorType) {
1170+
case KVM:
1171+
logger.debug("Using the {} setting global value {} as the VM {} has the {} hypervisor type and is not deployed on either a host or a cluster.", VirtualMachineMaxNicsKvm, VirtualMachineMaxNicsKvm.value(), virtualMachine.getUuid(), virtualMachineHypervisorType);
1172+
return VirtualMachineMaxNicsKvm.value();
1173+
case VMware:
1174+
logger.debug("Using the {} setting global value {} as the VM {} has the {} hypervisor type and is not deployed on either a host or a cluster.", VirtualMachineMaxNicsVmware, VirtualMachineMaxNicsVmware.value(), virtualMachine.getUuid(), virtualMachineHypervisorType);
1175+
return VirtualMachineMaxNicsVmware.value();
1176+
case XenServer:
1177+
logger.debug("Using the {} setting global value {} as the VM {} has the {} hypervisor type and is not deployed on either a host or a cluster.", VirtualMachineMaxNicsXenserver, VirtualMachineMaxNicsXenserver.value(), virtualMachine.getUuid(), virtualMachineHypervisorType);
1178+
return VirtualMachineMaxNicsXenserver.value();
1179+
default:
1180+
logger.debug("Not considering the hypervisor maximum limits as we were unable to find a compatible hypervisor on the VM and VM cluster for virtual machine maximum NICs configurations.");
1181+
return null;
11761182
}
1177-
1178-
logger.debug("Using the {} setting global value {} as the VM {} has the {} hypervisor type and is not deployed on either a host or a cluster.", VirtualMachineMaxNicsXenserver, VirtualMachineMaxNicsXenserver.value(), virtualMachine.getUuid(), virtualMachineHypervisorType);
1179-
return VirtualMachineMaxNicsXenserver.value();
11801183
}
11811184

11821185
private boolean isNicAllocatedForNsxPublicNetworkOnVR(Network network, NicProfile requested, VirtualMachineProfile vm) {

engine/orchestration/src/test/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestratorTest.java

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -855,32 +855,23 @@ public void getVirtualMachineMaxNicsValueTestVirtualMachineDeployedReturnsVirtua
855855
VirtualMachine virtualMachineMock = Mockito.mock(VirtualMachine.class);
856856
Mockito.doReturn(44).when(testOrchastrator).getVirtualMachineMaxNicsValueFromCluster(virtualMachineMock);
857857

858-
int virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValue(virtualMachineMock);
858+
Integer virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValue(virtualMachineMock);
859859

860860
Mockito.verify(testOrchastrator, Mockito.times(1)).getVirtualMachineMaxNicsValueFromCluster((VirtualMachine) Mockito.any());
861-
Assert.assertEquals(44, virtualMachineMaxNicsValue);
861+
Assert.assertEquals((Integer) 44, virtualMachineMaxNicsValue);
862862
}
863863

864864
@Test
865-
public void getVirtualMachineMaxNicsValueTestVirtualMachineWithoutDeployAndTemplateReturnsSmallestGlobalValueBetweenVirtualMachineMaxNicsSettings() throws NoSuchFieldException, IllegalAccessException {
865+
public void getVirtualMachineMaxNicsValueTestVirtualMachineIncompatibleHypervisorReturnsNull() {
866866
VirtualMachineProfile virtualMachineProfileMock = Mockito.mock(VirtualMachineProfile.class);
867867
VirtualMachine virtualMachineMock = Mockito.mock(VirtualMachine.class);
868-
ConfigKey virtualMachineMaxNicsKvmMock = Mockito.mock(ConfigKey.class);
869-
ConfigKey virtualMachineMaxNicsVmwareMock = Mockito.mock(ConfigKey.class);
870-
ConfigKey virtualMachineMaxNicsXenserverMock = Mockito.mock(ConfigKey.class);
871868
Mockito.doReturn(virtualMachineMock).when(virtualMachineProfileMock).getVirtualMachine();
872869
Mockito.doReturn(null).when(virtualMachineMock).getHypervisorType();
873870
Mockito.doReturn(null).when(testOrchastrator).getVirtualMachineMaxNicsValueFromCluster(virtualMachineMock);
874-
Mockito.doReturn(23).when(virtualMachineMaxNicsKvmMock).value();
875-
Mockito.doReturn(10).when(virtualMachineMaxNicsVmwareMock).value();
876-
Mockito.doReturn(7).when(virtualMachineMaxNicsXenserverMock).value();
877-
updateFinalStaticField(testOrchastrator.getClass().getField("VirtualMachineMaxNicsKvm"), virtualMachineMaxNicsKvmMock);
878-
updateFinalStaticField(testOrchastrator.getClass().getField("VirtualMachineMaxNicsVmware"), virtualMachineMaxNicsVmwareMock);
879-
updateFinalStaticField(testOrchastrator.getClass().getField("VirtualMachineMaxNicsXenserver"), virtualMachineMaxNicsXenserverMock);
880871

881-
int virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValue(virtualMachineMock);
872+
Integer virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValue(virtualMachineMock);
882873

883-
Assert.assertEquals(7, virtualMachineMaxNicsValue);
874+
Assert.assertNull(virtualMachineMaxNicsValue);
884875
}
885876

886877
@Test
@@ -892,10 +883,10 @@ public void getVirtualMachineMaxNicsValueTestVirtualMachineWithoutDeployReturnsV
892883
Mockito.doReturn(null).when(testOrchastrator).getVirtualMachineMaxNicsValueFromCluster(virtualMachineMock);
893884
Mockito.doReturn(33).when(testOrchastrator).getVirtualMachineMaxNicsValueFromVmHypervisorType(virtualMachineMock);
894885

895-
int virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValue(virtualMachineMock);
886+
Integer virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValue(virtualMachineMock);
896887

897888
Mockito.verify(testOrchastrator, Mockito.times(1)).getVirtualMachineMaxNicsValueFromVmHypervisorType(Mockito.any());
898-
Assert.assertEquals(33, virtualMachineMaxNicsValue);
889+
Assert.assertEquals((Integer) 33, virtualMachineMaxNicsValue);
899890
}
900891

901892
@Test
@@ -932,9 +923,9 @@ public void getVirtualMachineMaxNicsValueFromClusterTestKvmClusterReturnsVirtual
932923
Mockito.doReturn(33).when(virtualMachineMaxNicsKvmMock).valueIn(1L);
933924
updateFinalStaticField(testOrchastrator.getClass().getField("VirtualMachineMaxNicsKvm"), virtualMachineMaxNicsKvmMock);
934925

935-
int virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValueFromCluster(clusterVoMock);
926+
Integer virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValueFromCluster(clusterVoMock);
936927

937-
Assert.assertEquals(33, virtualMachineMaxNicsValue);
928+
Assert.assertEquals((Integer) 33, virtualMachineMaxNicsValue);
938929
}
939930

940931
@Test
@@ -946,9 +937,9 @@ public void getVirtualMachineMaxNicsValueFromClusterTestVmwareClusterReturnsVirt
946937
Mockito.doReturn(22).when(virtualMachineMaxNicsVmwareMock).valueIn(1L);
947938
updateFinalStaticField(testOrchastrator.getClass().getField("VirtualMachineMaxNicsVmware"), virtualMachineMaxNicsVmwareMock);
948939

949-
int virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValueFromCluster(clusterVoMock);
940+
Integer virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValueFromCluster(clusterVoMock);
950941

951-
Assert.assertEquals(22, virtualMachineMaxNicsValue);
942+
Assert.assertEquals((Integer) 22, virtualMachineMaxNicsValue);
952943
}
953944

954945
@Test
@@ -960,9 +951,20 @@ public void getVirtualMachineMaxNicsValueFromClusterTestXenserverClusterReturnsV
960951
Mockito.doReturn(11).when(virtualMachineMaxNicsXenserverMock).valueIn(1L);
961952
updateFinalStaticField(testOrchastrator.getClass().getField("VirtualMachineMaxNicsXenserver"), virtualMachineMaxNicsXenserverMock);
962953

963-
int virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValueFromCluster(clusterVoMock);
954+
Integer virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValueFromCluster(clusterVoMock);
964955

965-
Assert.assertEquals(11, virtualMachineMaxNicsValue);
956+
Assert.assertEquals((Integer) 11, virtualMachineMaxNicsValue);
957+
}
958+
959+
@Test
960+
public void getVirtualMachineMaxNicsValueFromClusterTestIncompatibleHypervisorReturnsNull() {
961+
ClusterVO clusterVoMock = Mockito.mock(ClusterVO.class);
962+
Mockito.doReturn(1L).when(clusterVoMock).getId();
963+
Mockito.doReturn(Hypervisor.HypervisorType.Hyperv).when(clusterVoMock).getHypervisorType();
964+
965+
Integer virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValueFromCluster(clusterVoMock);
966+
967+
Assert.assertNull(virtualMachineMaxNicsValue);
966968
}
967969

968970
@Test
@@ -973,9 +975,9 @@ public void getVirtualMachineMaxNicsValueFromVmHypervisorTypeTestKvmHypervisorRe
973975
Mockito.doReturn(23).when(virtualMachineMaxNicsKvmMock).value();
974976
updateFinalStaticField(testOrchastrator.getClass().getField("VirtualMachineMaxNicsKvm"), virtualMachineMaxNicsKvmMock);
975977

976-
int virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValueFromVmHypervisorType(virtualMachineMock);
978+
Integer virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValueFromVmHypervisorType(virtualMachineMock);
977979

978-
Assert.assertEquals(23, virtualMachineMaxNicsValue);
980+
Assert.assertEquals((Integer) 23, virtualMachineMaxNicsValue);
979981
}
980982

981983
@Test
@@ -986,9 +988,9 @@ public void getVirtualMachineMaxNicsValueFromVmHypervisorTypeTestVmwareHyperviso
986988
Mockito.doReturn(10).when(virtualMachineMaxNicsVmwareMock).value();
987989
updateFinalStaticField(testOrchastrator.getClass().getField("VirtualMachineMaxNicsVmware"), virtualMachineMaxNicsVmwareMock);
988990

989-
int virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValueFromVmHypervisorType(virtualMachineMock);
991+
Integer virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValueFromVmHypervisorType(virtualMachineMock);
990992

991-
Assert.assertEquals(10, virtualMachineMaxNicsValue);
993+
Assert.assertEquals((Integer) 10, virtualMachineMaxNicsValue);
992994
}
993995

994996
@Test
@@ -999,9 +1001,19 @@ public void getVirtualMachineMaxNicsValueFromVmHypervisorTypeTestXenserverHyperv
9991001
Mockito.doReturn(7).when(virtualMachineMaxNicsXenserverMock).value();
10001002
updateFinalStaticField(testOrchastrator.getClass().getField("VirtualMachineMaxNicsXenserver"), virtualMachineMaxNicsXenserverMock);
10011003

1002-
int virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValueFromVmHypervisorType(virtualMachineMock);
1004+
Integer virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValueFromVmHypervisorType(virtualMachineMock);
1005+
1006+
Assert.assertEquals((Integer) 7, virtualMachineMaxNicsValue);
1007+
}
1008+
1009+
@Test
1010+
public void getVirtualMachineMaxNicsValueFromVmHypervisorTypeTestIncompatibleHypervisorReturnsNull() {
1011+
VirtualMachine virtualMachineMock = Mockito.mock(VirtualMachine.class);
1012+
Mockito.doReturn(Hypervisor.HypervisorType.Hyperv).when(virtualMachineMock).getHypervisorType();
1013+
1014+
Integer virtualMachineMaxNicsValue = testOrchastrator.getVirtualMachineMaxNicsValueFromVmHypervisorType(virtualMachineMock);
10031015

1004-
Assert.assertEquals(7, virtualMachineMaxNicsValue);
1016+
Assert.assertNull(virtualMachineMaxNicsValue);
10051017
}
10061018

10071019
void updateFinalStaticField(Field field, Object newValue) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {

server/src/main/java/com/cloud/network/vpc/VpcManagerImpl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1976,7 +1976,13 @@ protected boolean existsVpcDomainRouterWithSufficientNicCapacity(long vpcId) {
19761976
int countRouterDefaultNetworks = domainRouterJoinDao.countDefaultNetworksById(vpcDomainRouter.getId());
19771977
long countVpcNetworks = _ntwkDao.countVpcNetworks(vpcId);
19781978

1979-
int totalNicsAvailable = networkOrchestrationService.getVirtualMachineMaxNicsValue(vpcDomainRouter) - countRouterDefaultNetworks;
1979+
Integer routerMaxNicsValue = networkOrchestrationService.getVirtualMachineMaxNicsValue(vpcDomainRouter);
1980+
1981+
if (routerMaxNicsValue == null) {
1982+
return true;
1983+
}
1984+
1985+
int totalNicsAvailable = routerMaxNicsValue - countRouterDefaultNetworks;
19801986

19811987
return totalNicsAvailable > countVpcNetworks;
19821988
}

0 commit comments

Comments
 (0)