use of org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO in project cloudstack by apache.
the class VMNetworkMapDaoImpl method getNetworks.
@Override
public List<Long> getNetworks(long vmId) {
SearchCriteria<VMNetworkMapVO> sc = VmIdSearch.create();
sc.setParameters("vmId", vmId);
List<VMNetworkMapVO> results = search(sc, null);
List<Long> networks = new ArrayList<Long>(results.size());
for (VMNetworkMapVO result : results) {
networks.add(result.getNetworkId());
}
return networks;
}
use of org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO in project cloudstack by apache.
the class VMNetworkMapDaoImpl method findByVmAndNetworkId.
@Override
public VMNetworkMapVO findByVmAndNetworkId(long vmId, long networkId) {
SearchCriteria<VMNetworkMapVO> sc = VmNetworkSearch.create();
sc.setParameters("vmId", vmId);
sc.setParameters("networkId", networkId);
VMNetworkMapVO network = findOneBy(sc);
return network;
}
use of org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO 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 org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO in project cloudstack by apache.
the class NetworkOrchestrator method createNicForVm.
@Override
public NicProfile createNicForVm(final Network network, final NicProfile requested, final ReservationContext context, final VirtualMachineProfile vmProfile, final boolean prepare) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
final VirtualMachine vm = vmProfile.getVirtualMachine();
final DataCenter dc = _entityMgr.findById(DataCenter.class, network.getDataCenterId());
final Host host = _hostDao.findById(vm.getHostId());
final DeployDestination dest = new DeployDestination(dc, null, null, host);
NicProfile nic = getNicProfileForVm(network, requested, vm);
// 1) allocate nic (if needed) Always allocate if it is a user vm
if (nic == null || vmProfile.getType() == VirtualMachine.Type.User) {
final int deviceId = _nicDao.getFreeDeviceId(vm.getId());
nic = allocateNic(requested, network, false, deviceId, vmProfile).first();
if (nic == null) {
throw new CloudRuntimeException("Failed to allocate nic for vm " + vm + " in network " + network);
}
// Update vm_network_map table
if (vmProfile.getType() == VirtualMachine.Type.User) {
final VMNetworkMapVO vno = new VMNetworkMapVO(vm.getId(), network.getId());
_vmNetworkMapDao.persist(vno);
}
s_logger.debug("Nic is allocated successfully for vm " + vm + " in network " + network);
}
// 2) prepare nic
if (prepare) {
final Pair<NetworkGuru, NetworkVO> implemented = implementNetwork(nic.getNetworkId(), dest, context, vmProfile.getVirtualMachine().getType() == Type.DomainRouter);
if (implemented == null || implemented.first() == null) {
s_logger.warn("Failed to implement network id=" + nic.getNetworkId() + " as a part of preparing nic id=" + nic.getId());
throw new CloudRuntimeException("Failed to implement network id=" + nic.getNetworkId() + " as a part preparing nic id=" + nic.getId());
}
nic = prepareNic(vmProfile, dest, context, nic.getId(), implemented.second());
s_logger.debug("Nic is prepared successfully for vm " + vm + " in network " + network);
}
return nic;
}
use of org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO in project cloudstack by apache.
the class NetworkMigrationManagerImpl method migrateNicsInDB.
private Boolean migrateNicsInDB(NicVO originalNic, Network networkInNewPhysicalNet, DataCenter dc, ReservationContext context) {
s_logger.debug("migrating nics in database.");
UserVmVO vmVO = _vmDao.findById(originalNic.getInstanceId());
VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(vmVO, null, null, null, null);
NicProfile nicProfile = new NicProfile(originalNic, networkInNewPhysicalNet, null, null, null, _networkModel.isSecurityGroupSupportedInNetwork(networkInNewPhysicalNet), null);
try {
nicProfile = _networkMgr.allocateNic(nicProfile, networkInNewPhysicalNet, originalNic.isDefaultNic(), nicProfile.getDeviceId(), vmProfile).first();
} catch (InsufficientVirtualNetworkCapacityException | InsufficientAddressCapacityException e) {
throw new CloudRuntimeException("Allocation of new nicProfile failed during migration", e);
}
// Update vm_network_map table
if (vmProfile.getType() == VirtualMachine.Type.User) {
final VMNetworkMapVO vno = new VMNetworkMapVO(vmVO.getId(), networkInNewPhysicalNet.getId());
_vmNetworkMapDao.persist(vno);
}
NicVO newNic = _nicDao.findById(nicProfile.getId());
copyNicDetails(originalNic.getId(), newNic.getId());
// Update nic uuid here
moveServices(originalNic, newNic);
if (originalNic.getState() == Nic.State.Reserved) {
final VirtualMachine vm = vmProfile.getVirtualMachine();
final Host host = _hostDao.findById(vm.getHostId());
final DeployDestination dest = new DeployDestination(dc, null, null, host);
try {
nicProfile = _networkMgr.prepareNic(vmProfile, dest, context, nicProfile.getId(), networkInNewPhysicalNet);
_itMgr.replugNic(networkInNewPhysicalNet, _itMgr.toNicTO(nicProfile, host.getHypervisorType()), _itMgr.toVmTO(vmProfile), dest.getHost());
} catch (ResourceUnavailableException | InsufficientCapacityException e) {
throw new CloudRuntimeException("Migration of Nic failed", e);
}
}
// Mark the old nic as removed
markAsNonDefault(originalNic);
_networkMgr.removeNic(vmProfile, originalNic);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Nic is migrated successfully for vm " + vmVO + " to " + networkInNewPhysicalNet);
}
return true;
}
Aggregations