Skip to content

Commit 2abe27a

Browse files
committed
prepare changes
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 10a90e8 commit 2abe27a

File tree

7 files changed

+141
-73
lines changed

7 files changed

+141
-73
lines changed

api/src/main/java/com/cloud/network/NetworkModel.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ public interface NetworkModel {
305305

306306
NicProfile getNicProfile(VirtualMachine vm, long networkId, String broadcastUri);
307307

308+
NicProfile getNicProfile(VirtualMachine vm, Nic nic, DataCenter dataCenter);
309+
308310
Set<Long> getAvailableIps(Network network, String requestedIp);
309311

310312
String getDomainNetworkDomain(long domainId, long zoneId);

engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,10 +1244,12 @@ protected void updateExternalVmFromPrepareAnswer(VirtualMachineTO vmTO, VirtualM
12441244
}
12451245
updateExternalVmDataFromPrepareAnswer(vmTO, updatedTO);
12461246
updateExternalVmNicsFromPrepareAnswer(vmTO, updatedTO);
1247+
return;
12471248
}
12481249

1249-
protected void processPrepareExternalProvisioning(boolean firstStart, Host host, VirtualMachineTO virtualMachineTO,
1250-
VirtualMachineTemplate template) {
1250+
protected void processPrepareExternalProvisioning(boolean firstStart, Host host,
1251+
VirtualMachineProfile vmProfile, DataCenter dataCenter) throws CloudRuntimeException {
1252+
VirtualMachineTemplate template = vmProfile.getTemplate();
12511253
if (!firstStart || host == null || !HypervisorType.External.equals(host.getHypervisorType()) ||
12521254
template.getExtensionId() == null) {
12531255
return;
@@ -1257,7 +1259,18 @@ protected void processPrepareExternalProvisioning(boolean firstStart, Host host,
12571259
if (detailsVO == null || !Boolean.parseBoolean(detailsVO.getValue())) {
12581260
return;
12591261
}
1260-
logger.debug("Sending PrepareExternalProvisioningCommand for {}", virtualMachineTO);
1262+
logger.debug("Sending PrepareExternalProvisioningCommand for {}", vmProfile);
1263+
VirtualMachineTO virtualMachineTO = toVmTO(vmProfile);
1264+
if (virtualMachineTO.getNics() == null || virtualMachineTO.getNics().length == 0) {
1265+
List<NicVO> nics = _nicsDao.listByVmId(vmProfile.getId());
1266+
NicTO[] nicTOs = new NicTO[nics.size()];
1267+
nics.forEach(nicVO -> {
1268+
NicTO nicTO = toNicTO(_networkModel.getNicProfile(vmProfile.getVirtualMachine(), nicVO, dataCenter),
1269+
HypervisorType.External);
1270+
nicTOs[nicTO.getDeviceId()] = nicTO;
1271+
});
1272+
virtualMachineTO.setNics(nicTOs);
1273+
}
12611274
Map<String, String> vmDetails = virtualMachineTO.getExternalDetails();
12621275
Map<String, Object> externalDetails = extensionsManager.getExternalAccessDetails(host,
12631276
vmDetails);
@@ -1401,7 +1414,7 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil
14011414
}
14021415
}
14031416

1404-
final VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, template, offering, owner, params);
1417+
VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, template, offering, owner, params);
14051418
logBootModeParameters(params);
14061419
DeployDestination dest = null;
14071420
try {
@@ -1444,6 +1457,9 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil
14441457

14451458
try {
14461459
resetVmNicsDeviceId(vm.getId());
1460+
1461+
processPrepareExternalProvisioning(firstStart, dest.getHost(), vmProfile, dest.getDataCenter());
1462+
14471463
_networkMgr.prepare(vmProfile, dest, ctx);
14481464
if (vm.getHypervisorType() != HypervisorType.BareMetal && vm.getHypervisorType() != HypervisorType.External) {
14491465
checkAndAttemptMigrateVmAcrossCluster(vm, clusterId, dest.getStorageForDisks());
@@ -1464,8 +1480,6 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil
14641480
handlePath(vmTO.getDisks(), vm.getHypervisorType());
14651481
setVmNetworkDetails(vm, vmTO);
14661482

1467-
processPrepareExternalProvisioning(firstStart, dest.getHost(), vmTO, template);
1468-
14691483
Commands cmds = new Commands(Command.OnError.Stop);
14701484
final Map<String, String> sshAccessDetails = _networkMgr.getSystemVMAccessDetails(vm);
14711485
final Map<String, String> ipAddressDetails = new HashMap<>(sshAccessDetails);

engine/orchestration/src/test/java/com/cloud/vm/VirtualMachineManagerImplTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import com.cloud.dc.ClusterDetailsDao;
8787
import com.cloud.dc.ClusterDetailsVO;
8888
import com.cloud.dc.ClusterVO;
89+
import com.cloud.dc.DataCenter;
8990
import com.cloud.dc.DataCenterVO;
9091
import com.cloud.dc.Pod;
9192
import com.cloud.dc.dao.ClusterDao;
@@ -1612,27 +1613,34 @@ public void updateExternalVmNicsFromPrepareAnswer_emptyNics_noAction() {
16121613
@Test
16131614
public void processPrepareExternalProvisioning_nonExternalHypervisor_noAction() throws OperationTimedoutException, AgentUnavailableException {
16141615
Host host = mock(Host.class);
1615-
VirtualMachineTO vmTO = mock(VirtualMachineTO.class);
1616+
VirtualMachineProfile vmProfile = mock(VirtualMachineProfile.class);
16161617
VirtualMachineTemplate template = mock(VirtualMachineTemplate.class);
1618+
when(vmProfile.getTemplate()).thenReturn(template);
16171619
when(host.getHypervisorType()).thenReturn(HypervisorType.KVM);
1618-
virtualMachineManagerImpl.processPrepareExternalProvisioning(true, host, vmTO, template);
1620+
virtualMachineManagerImpl.processPrepareExternalProvisioning(true, host, vmProfile, mock(DataCenter.class));
16191621
verify(agentManagerMock, never()).send(anyLong(), any(Command.class));
16201622
}
16211623

16221624
@Test
16231625
public void processPrepareExternalProvisioning_externalHypervisor_sendsCommand() throws OperationTimedoutException, AgentUnavailableException {
16241626
Host host = mock(Host.class);
1625-
VirtualMachineTO vmTO = mock(VirtualMachineTO.class);
1627+
VirtualMachineProfile vmProfile = mock(VirtualMachineProfile.class);
16261628
VirtualMachineTemplate template = mock(VirtualMachineTemplate.class);
1629+
when(vmProfile.getTemplate()).thenReturn(template);
1630+
NicTO[] nics = new NicTO[]{mock(NicTO.class)};
1631+
VirtualMachineTO vmTO = mock(VirtualMachineTO.class);
1632+
when(vmTO.getNics()).thenReturn(nics);
1633+
doReturn(vmTO).when(virtualMachineManagerImpl).toVmTO(vmProfile);
16271634
ExtensionDetailsVO detailsVO = mock(ExtensionDetailsVO.class);
16281635
when(host.getHypervisorType()).thenReturn(HypervisorType.External);
16291636
when(template.getExtensionId()).thenReturn(1L);
16301637
when(extensionDetailsDao.findDetail(eq(1L), eq(ApiConstants.ORCHESTRATOR_REQUIRES_PREPARE_VM))).thenReturn(detailsVO);
16311638
when(detailsVO.getValue()).thenReturn("true");
16321639
PrepareExternalProvisioningAnswer answer = mock(PrepareExternalProvisioningAnswer.class);
16331640
when(answer.getResult()).thenReturn(true);
1641+
when(answer.getVirtualMachineTO()).thenReturn(vmTO);
16341642
when(agentManagerMock.send(anyLong(), any(Command.class))).thenReturn(answer);
1635-
virtualMachineManagerImpl.processPrepareExternalProvisioning(true, host, vmTO, template);
1643+
virtualMachineManagerImpl.processPrepareExternalProvisioning(true, host, vmProfile, mock(DataCenter.class));
16361644
verify(agentManagerMock).send(anyLong(), any(Command.class));
16371645
}
16381646
}

server/src/main/java/com/cloud/network/NetworkModelImpl.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package com.cloud.network;
1919

20+
import static com.cloud.network.Network.Service.SecurityGroup;
21+
2022
import java.math.BigInteger;
2123
import java.security.MessageDigest;
2224
import java.security.NoSuchAlgorithmException;
@@ -145,8 +147,6 @@
145147
import com.cloud.vm.dao.NicSecondaryIpDao;
146148
import com.cloud.vm.dao.VMInstanceDao;
147149

148-
import static com.cloud.network.Network.Service.SecurityGroup;
149-
150150
public class NetworkModelImpl extends ManagerBase implements NetworkModel, Configurable {
151151
public static final String UNABLE_TO_USE_NETWORK = "Unable to use network with id= %s, permission denied";
152152
@Inject
@@ -2195,29 +2195,29 @@ public NicProfile getNicProfile(VirtualMachine vm, long networkId, String broadc
21952195
if (nic == null) {
21962196
return null;
21972197
}
2198-
NetworkVO network = _networksDao.findById(networkId);
2199-
Integer networkRate = getNetworkRate(network.getId(), vm.getId());
2198+
DataCenter dc = _dcDao.findById(vm.getDataCenterId());
2199+
return getNicProfile(vm, nic, dc);
2200+
}
22002201

2202+
@Override
2203+
public NicProfile getNicProfile(VirtualMachine vm, Nic nic, DataCenter dataCenter) {
2204+
NetworkVO network = _networksDao.findById(nic.getNetworkId());
2205+
Integer networkRate = getNetworkRate(network.getId(), vm.getId());
22012206
NicProfile profile =
2202-
new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, isSecurityGroupSupportedInNetwork(network), getNetworkTag(
2203-
vm.getHypervisorType(), network));
2207+
new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate,
2208+
isSecurityGroupSupportedInNetwork(network), getNetworkTag(vm.getHypervisorType(), network));
22042209
if (network.getTrafficType() == TrafficType.Public && network.getPublicMtu() != null) {
22052210
profile.setMtu(network.getPublicMtu());
22062211
}
22072212
if (network.getTrafficType() == TrafficType.Guest && network.getPrivateMtu() != null) {
22082213
profile.setMtu(network.getPrivateMtu());
22092214
}
2210-
2211-
DataCenter dc = _dcDao.findById(network.getDataCenterId());
2212-
2213-
Pair<String, String> ip4Dns = getNetworkIp4Dns(network, dc);
2215+
Pair<String, String> ip4Dns = getNetworkIp4Dns(network, dataCenter);
22142216
profile.setIPv4Dns1(ip4Dns.first());
22152217
profile.setIPv4Dns2(ip4Dns.second());
2216-
2217-
Pair<String, String> ip6Dns = getNetworkIp6Dns(network, dc);
2218+
Pair<String, String> ip6Dns = getNetworkIp6Dns(network, dataCenter);
22182219
profile.setIPv6Dns1(ip6Dns.first());
22192220
profile.setIPv6Dns2(ip6Dns.second());
2220-
22212221
return profile;
22222222
}
22232223

server/src/test/java/com/cloud/network/MockNetworkModelImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,11 @@ public NicProfile getNicProfile(VirtualMachine vm, long networkId, String broadc
775775
return null;
776776
}
777777

778+
@Override
779+
public NicProfile getNicProfile(VirtualMachine vm, Nic nic, DataCenter dataCenter) {
780+
return null;
781+
}
782+
778783
/* (non-Javadoc)
779784
* @see com.cloud.network.NetworkModel#getAvailableIps(com.cloud.network.Network, java.lang.String)
780785
*/

0 commit comments

Comments
 (0)