use of com.cloud.vm.NicProfile in project cloudstack by apache.
the class NetworkOrchestrator method prepareNicForMigration.
@Override
public void prepareNicForMigration(final VirtualMachineProfile vm, final DeployDestination dest) {
if (vm.getType().equals(VirtualMachine.Type.DomainRouter) && (vm.getHypervisorType().equals(HypervisorType.KVM) || vm.getHypervisorType().equals(HypervisorType.VMware))) {
//Include nics hot plugged and not stored in DB
prepareAllNicsForMigration(vm, dest);
return;
}
final List<NicVO> nics = _nicDao.listByVmId(vm.getId());
final ReservationContext context = new ReservationContextImpl(UUID.randomUUID().toString(), null, null);
for (final NicVO nic : nics) {
final NetworkVO network = _networksDao.findById(nic.getNetworkId());
final Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId());
final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, network.getGuruName());
final NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vm.getHypervisorType(), network));
if (guru instanceof NetworkMigrationResponder) {
if (!((NetworkMigrationResponder) guru).prepareMigration(profile, network, vm, dest, context)) {
// XXX: Transaction error
s_logger.error("NetworkGuru " + guru + " prepareForMigration failed.");
}
}
final List<Provider> providersToImplement = getNetworkProviders(network.getId());
for (final NetworkElement element : networkElements) {
if (providersToImplement.contains(element.getProvider())) {
if (!_networkModel.isProviderEnabledInPhysicalNetwork(_networkModel.getPhysicalNetworkId(network), element.getProvider().getName())) {
throw new CloudRuntimeException("Service provider " + element.getProvider().getName() + " either doesn't exist or is not enabled in physical network id: " + network.getPhysicalNetworkId());
}
if (element instanceof NetworkMigrationResponder) {
if (!((NetworkMigrationResponder) element).prepareMigration(profile, network, vm, dest, context)) {
// XXX: Transaction error
s_logger.error("NetworkElement " + element + " prepareForMigration failed.");
}
}
}
}
guru.updateNicProfile(profile, network);
vm.addNic(profile);
}
}
use of com.cloud.vm.NicProfile in project cloudstack by apache.
the class NetworkOrchestrator method releaseNic.
@DB
protected void releaseNic(final VirtualMachineProfile vmProfile, final long nicId) throws ConcurrentOperationException, ResourceUnavailableException {
final Pair<Network, NicProfile> networkToRelease = Transaction.execute(new TransactionCallback<Pair<Network, NicProfile>>() {
@Override
public Pair<Network, NicProfile> doInTransaction(final TransactionStatus status) {
final NicVO nic = _nicDao.lockRow(nicId, true);
if (nic == null) {
throw new ConcurrentOperationException("Unable to acquire lock on nic " + nic);
}
final Nic.State originalState = nic.getState();
final NetworkVO network = _networksDao.findById(nic.getNetworkId());
if (originalState == Nic.State.Reserved || originalState == Nic.State.Reserving) {
if (nic.getReservationStrategy() == Nic.ReservationStrategy.Start) {
final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, network.getGuruName());
nic.setState(Nic.State.Releasing);
_nicDao.update(nic.getId(), nic);
final NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null, _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vmProfile.getHypervisorType(), network));
if (guru.release(profile, vmProfile, nic.getReservationId())) {
applyProfileToNicForRelease(nic, profile);
nic.setState(Nic.State.Allocated);
if (originalState == Nic.State.Reserved) {
updateNic(nic, network.getId(), -1);
} else {
_nicDao.update(nic.getId(), nic);
}
}
// Perform release on network elements
return new Pair<Network, NicProfile>(network, profile);
} else {
nic.setState(Nic.State.Allocated);
updateNic(nic, network.getId(), -1);
}
}
return null;
}
});
// cleanup the entry in vm_network_map
if (vmProfile.getType().equals(VirtualMachine.Type.User)) {
final NicVO nic = _nicDao.findById(nicId);
if (nic != null) {
final NetworkVO vmNetwork = _networksDao.findById(nic.getNetworkId());
final VMNetworkMapVO vno = _vmNetworkMapDao.findByVmAndNetworkId(vmProfile.getVirtualMachine().getId(), vmNetwork.getId());
if (vno != null) {
_vmNetworkMapDao.remove(vno.getId());
}
}
}
if (networkToRelease != null) {
final Network network = networkToRelease.first();
final NicProfile profile = networkToRelease.second();
final List<Provider> providersToImplement = getNetworkProviders(network.getId());
for (final NetworkElement element : networkElements) {
if (providersToImplement.contains(element.getProvider())) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Asking " + element.getName() + " to release " + profile);
}
//NOTE: Context appear to never be used in release method
//implementations. Consider removing it from interface Element
element.release(network, profile, vmProfile, null);
}
}
}
}
use of com.cloud.vm.NicProfile in project cloudstack by apache.
the class NetworkOrchestrator method reallocate.
@DB
@Override
public boolean reallocate(final VirtualMachineProfile vm, final DataCenterDeployment dest) throws InsufficientCapacityException, ConcurrentOperationException {
final VMInstanceVO vmInstance = _vmDao.findById(vm.getId());
final DataCenterVO dc = _dcDao.findById(vmInstance.getDataCenterId());
if (dc.getNetworkType() == NetworkType.Basic) {
final List<NicVO> nics = _nicDao.listByVmId(vmInstance.getId());
final NetworkVO network = _networksDao.findById(nics.get(0).getNetworkId());
final LinkedHashMap<Network, List<? extends NicProfile>> profiles = new LinkedHashMap<Network, List<? extends NicProfile>>();
profiles.put(network, new ArrayList<NicProfile>());
Transaction.execute(new TransactionCallbackWithExceptionNoReturn<InsufficientCapacityException>() {
@Override
public void doInTransactionWithoutResult(final TransactionStatus status) throws InsufficientCapacityException {
cleanupNics(vm);
allocate(vm, profiles);
}
});
}
return true;
}
use of com.cloud.vm.NicProfile in project cloudstack by apache.
the class NetworkOrchestrator method allocateNic.
@DB
@Override
public Pair<NicProfile, Integer> allocateNic(final NicProfile requested, final Network network, final Boolean isDefaultNic, int deviceId, final VirtualMachineProfile vm) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, ConcurrentOperationException {
final NetworkVO ntwkVO = _networksDao.findById(network.getId());
s_logger.debug("Allocating nic for vm " + vm.getVirtualMachine() + " in network " + network + " with requested profile " + requested);
final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, ntwkVO.getGuruName());
if (requested != null && requested.getMode() == null) {
requested.setMode(network.getMode());
}
final NicProfile profile = guru.allocate(network, requested, vm);
if (profile == null) {
return null;
}
if (isDefaultNic != null) {
profile.setDefaultNic(isDefaultNic);
}
if (requested != null && requested.getMode() == null) {
profile.setMode(requested.getMode());
} else {
profile.setMode(network.getMode());
}
NicVO vo = new NicVO(guru.getName(), vm.getId(), network.getId(), vm.getType());
deviceId = applyProfileToNic(vo, profile, deviceId);
vo = _nicDao.persist(vo);
final Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId());
final NicProfile vmNic = new NicProfile(vo, network, vo.getBroadcastUri(), vo.getIsolationUri(), networkRate, _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vm.getHypervisorType(), network));
return new Pair<NicProfile, Integer>(vmNic, Integer.valueOf(deviceId));
}
use of com.cloud.vm.NicProfile in project cloudstack by apache.
the class NuageVspGuestNetworkGuruTest method testReserve.
@Test
public void testReserve() throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, URISyntaxException {
final NetworkVO network = mock(NetworkVO.class);
when(network.getId()).thenReturn(NETWORK_ID);
when(network.getUuid()).thenReturn("aaaaaa");
when(network.getDataCenterId()).thenReturn(NETWORK_ID);
when(network.getNetworkOfferingId()).thenReturn(NETWORK_ID);
when(network.getPhysicalNetworkId()).thenReturn(NETWORK_ID);
when(network.getDomainId()).thenReturn(NETWORK_ID);
when(network.getAccountId()).thenReturn(NETWORK_ID);
when(network.getVpcId()).thenReturn(null);
when(network.getBroadcastUri()).thenReturn(new URI("vsp://aaaaaa-aavvv/10.1.1.1"));
final DataCenterVO dataCenter = mock(DataCenterVO.class);
when(_dataCenterDao.findById(NETWORK_ID)).thenReturn(dataCenter);
final AccountVO networksAccount = mock(AccountVO.class);
when(networksAccount.getId()).thenReturn(NETWORK_ID);
when(networksAccount.getUuid()).thenReturn("aaaa-abbbb");
when(networksAccount.getType()).thenReturn(Account.ACCOUNT_TYPE_NORMAL);
when(_accountDao.findById(NETWORK_ID)).thenReturn(networksAccount);
final DomainVO networksDomain = mock(DomainVO.class);
when(networksDomain.getId()).thenReturn(NETWORK_ID);
when(networksDomain.getUuid()).thenReturn("aaaaa-bbbbb");
when(_domainDao.findById(NETWORK_ID)).thenReturn(networksDomain);
final NicVO nicvo = mock(NicVO.class);
when(nicvo.getId()).thenReturn(NETWORK_ID);
when(nicvo.getMacAddress()).thenReturn("aa-aa-aa-aa-aa-aa");
when(nicvo.getUuid()).thenReturn("aaaa-fffff");
when(nicvo.getNetworkId()).thenReturn(NETWORK_ID);
when(nicvo.getInstanceId()).thenReturn(NETWORK_ID);
when(_nicDao.findById(NETWORK_ID)).thenReturn(nicvo);
when(_nicDao.findDefaultNicForVM(NETWORK_ID)).thenReturn(nicvo);
final VirtualMachine vm = mock(VirtualMachine.class);
when(vm.getId()).thenReturn(NETWORK_ID);
when(vm.getType()).thenReturn(VirtualMachine.Type.User);
final VirtualMachineProfile vmProfile = mock(VirtualMachineProfile.class);
when(vmProfile.getType()).thenReturn(VirtualMachine.Type.User);
when(vmProfile.getInstanceName()).thenReturn("");
when(vmProfile.getUuid()).thenReturn("aaaa-bbbbb");
when(vmProfile.getVirtualMachine()).thenReturn(vm);
NicProfile nicProfile = mock(NicProfile.class);
when(nicProfile.getUuid()).thenReturn("aaa-bbbb");
when(nicProfile.getId()).thenReturn(NETWORK_ID);
when(nicProfile.getMacAddress()).thenReturn("aa-aa-aa-aa-aa-aa");
final NetworkOfferingVO ntwkoffering = mock(NetworkOfferingVO.class);
when(ntwkoffering.getId()).thenReturn(NETWORK_ID);
when(_networkOfferingDao.findById(NETWORK_ID)).thenReturn(ntwkoffering);
when(_networkDao.acquireInLockTable(NETWORK_ID, 1200)).thenReturn(network);
when(_ipAddressDao.findByVmIdAndNetworkId(NETWORK_ID, NETWORK_ID)).thenReturn(null);
when(_domainDao.findById(NETWORK_ID)).thenReturn(mock(DomainVO.class));
final Answer answer = mock(Answer.class);
when(answer.getResult()).thenReturn(true);
when(_agentManager.easySend(eq(NETWORK_ID), (Command) any())).thenReturn(answer);
final ReservationContext reservationContext = mock(ReservationContext.class);
when(reservationContext.getAccount()).thenReturn(networksAccount);
when(reservationContext.getDomain()).thenReturn(networksDomain);
_nuageVspGuestNetworkGuru.reserve(nicProfile, network, vmProfile, mock(DeployDestination.class), reservationContext);
}
Aggregations