|
37 | 37 | import javax.inject.Inject; |
38 | 38 | import javax.naming.ConfigurationException; |
39 | 39 |
|
| 40 | +import com.cloud.network.Networks; |
40 | 41 | import org.apache.log4j.Logger; |
41 | 42 | import org.apache.cloudstack.acl.ControlledEntity.ACLType; |
42 | 43 | import org.apache.cloudstack.context.CallContext; |
@@ -1376,6 +1377,11 @@ public NicProfile prepareNic(VirtualMachineProfile vmProfile, DeployDestination |
1376 | 1377 |
|
1377 | 1378 | @Override |
1378 | 1379 | public void prepareNicForMigration(VirtualMachineProfile vm, DeployDestination dest) { |
| 1380 | + if(vm.getType().equals(VirtualMachine.Type.DomainRouter) && vm.getHypervisorType().equals(HypervisorType.KVM)){ |
| 1381 | + //Include nics hot plugged and not stored in DB |
| 1382 | + prepareAllNicsForMigration(vm, dest); |
| 1383 | + return; |
| 1384 | + } |
1379 | 1385 | List<NicVO> nics = _nicDao.listByVmId(vm.getId()); |
1380 | 1386 | ReservationContext context = new ReservationContextImpl(UUID.randomUUID().toString(), null, null); |
1381 | 1387 | for (NicVO nic : nics) { |
@@ -1409,6 +1415,86 @@ public void prepareNicForMigration(VirtualMachineProfile vm, DeployDestination d |
1409 | 1415 | } |
1410 | 1416 | } |
1411 | 1417 |
|
| 1418 | + /* |
| 1419 | + Prepare All Nics for migration including the nics dynamically created and not stored in DB |
| 1420 | + This is a temporary workaround work KVM migration |
| 1421 | + Once clean fix is added by stored dynamically nics is DB, this workaround won't be needed |
| 1422 | + */ |
| 1423 | + @Override |
| 1424 | + public void prepareAllNicsForMigration(VirtualMachineProfile vm, DeployDestination dest) { |
| 1425 | + List<NicVO> nics = _nicDao.listByVmId(vm.getId()); |
| 1426 | + ReservationContext context = new ReservationContextImpl(UUID.randomUUID().toString(), null, null); |
| 1427 | + Long guestNetworkId = null; |
| 1428 | + for (NicVO nic : nics) { |
| 1429 | + NetworkVO network = _networksDao.findById(nic.getNetworkId()); |
| 1430 | + if(network.getTrafficType().equals(TrafficType.Guest) && network.getGuestType().equals(GuestType.Isolated)){ |
| 1431 | + guestNetworkId = network.getId(); |
| 1432 | + } |
| 1433 | + Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId()); |
| 1434 | + |
| 1435 | + NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, network.getGuruName()); |
| 1436 | + NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, |
| 1437 | + _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vm.getHypervisorType(), network)); |
| 1438 | + if(guru instanceof NetworkMigrationResponder){ |
| 1439 | + if(!((NetworkMigrationResponder) guru).prepareMigration(profile, network, vm, dest, context)){ |
| 1440 | + s_logger.error("NetworkGuru "+guru+" prepareForMigration failed."); // XXX: Transaction error |
| 1441 | + } |
| 1442 | + } |
| 1443 | + List<Provider> providersToImplement = getNetworkProviders(network.getId()); |
| 1444 | + for (NetworkElement element : networkElements) { |
| 1445 | + if (providersToImplement.contains(element.getProvider())) { |
| 1446 | + if (!_networkModel.isProviderEnabledInPhysicalNetwork(_networkModel.getPhysicalNetworkId(network), element.getProvider().getName())) { |
| 1447 | + throw new CloudRuntimeException("Service provider " + element.getProvider().getName() + " either doesn't exist or is not enabled in physical network id: " + network.getPhysicalNetworkId()); |
| 1448 | + } |
| 1449 | + if(element instanceof NetworkMigrationResponder){ |
| 1450 | + if(!((NetworkMigrationResponder) element).prepareMigration(profile, network, vm, dest, context)){ |
| 1451 | + s_logger.error("NetworkElement "+element+" prepareForMigration failed."); // XXX: Transaction error |
| 1452 | + } |
| 1453 | + } |
| 1454 | + } |
| 1455 | + } |
| 1456 | + guru.updateNicProfile(profile, network); |
| 1457 | + vm.addNic(profile); |
| 1458 | + } |
| 1459 | + |
| 1460 | + List<String> addedURIs = new ArrayList<String>(); |
| 1461 | + if(guestNetworkId != null){ |
| 1462 | + List<IPAddressVO> publicIps = _ipAddressDao.listByAssociatedNetwork(guestNetworkId, null); |
| 1463 | + for (IPAddressVO userIp : publicIps){ |
| 1464 | + PublicIp publicIp = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId())); |
| 1465 | + URI broadcastUri = BroadcastDomainType.Vlan.toUri(publicIp.getVlanTag()); |
| 1466 | + long ntwkId = publicIp.getNetworkId(); |
| 1467 | + Nic nic = _nicDao.findByNetworkIdInstanceIdAndBroadcastUri(ntwkId, vm.getId(), |
| 1468 | + broadcastUri.toString()); |
| 1469 | + if(nic == null && !addedURIs.contains(broadcastUri.toString())){ |
| 1470 | + //Nic details are not available in DB |
| 1471 | + //Create nic profile for migration |
| 1472 | + s_logger.debug("Creating nic profile for migration. BroadcastUri: "+broadcastUri.toString()+" NetworkId: "+ntwkId+" Vm: "+vm.getId()); |
| 1473 | + NetworkVO network = _networksDao.findById(ntwkId); |
| 1474 | + Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId()); |
| 1475 | + NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, network.getGuruName()); |
| 1476 | + NicProfile profile = new NicProfile(); |
| 1477 | + profile.setDeviceId(255); //dummyId |
| 1478 | + profile.setIp4Address(userIp.getAddress().toString()); |
| 1479 | + profile.setNetmask(publicIp.getNetmask()); |
| 1480 | + profile.setGateway(publicIp.getGateway()); |
| 1481 | + profile.setMacAddress(publicIp.getMacAddress()); |
| 1482 | + profile.setBroadcastType(network.getBroadcastDomainType()); |
| 1483 | + profile.setTrafficType(network.getTrafficType()); |
| 1484 | + profile.setBroadcastUri(broadcastUri); |
| 1485 | + profile.setIsolationUri(Networks.IsolationType.Vlan.toUri(publicIp.getVlanTag())); |
| 1486 | + profile.setSecurityGroupEnabled(_networkModel.isSecurityGroupSupportedInNetwork(network)); |
| 1487 | + profile.setName(_networkModel.getNetworkTag(vm.getHypervisorType(), network)); |
| 1488 | + profile.setNetworId(network.getId()); |
| 1489 | + |
| 1490 | + guru.updateNicProfile(profile, network); |
| 1491 | + vm.addNic(profile); |
| 1492 | + addedURIs.add(broadcastUri.toString()); |
| 1493 | + } |
| 1494 | + } |
| 1495 | + } |
| 1496 | + } |
| 1497 | + |
1412 | 1498 | private NicProfile findNicProfileById(VirtualMachineProfile vm, long id) { |
1413 | 1499 | for (NicProfile nic : vm.getNics()) { |
1414 | 1500 | if (nic.getId() == id) { |
|
0 commit comments