use of com.cloud.utils.db.TransactionStatus in project cloudstack by apache.
the class FirewallManagerImpl method revokeRule.
@Override
@DB
public void revokeRule(final FirewallRuleVO rule, Account caller, long userId, final boolean needUsageEvent) {
if (caller != null) {
_accountMgr.checkAccess(caller, null, true, rule);
}
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
boolean generateUsageEvent = false;
if (rule.getState() == State.Staged) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Found a rule that is still in stage state so just removing it: " + rule);
}
removeRule(rule);
generateUsageEvent = true;
} else if (rule.getState() == State.Add || rule.getState() == State.Active) {
rule.setState(State.Revoke);
_firewallDao.update(rule.getId(), rule);
generateUsageEvent = true;
}
if (generateUsageEvent && needUsageEvent) {
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_RULE_DELETE, rule.getAccountId(), 0, rule.getId(), null, rule.getClass().getName(), rule.getUuid());
}
}
});
}
use of com.cloud.utils.db.TransactionStatus in project cloudstack by apache.
the class NetworkOrchestrator method allocate.
@Override
@DB
public void allocate(final VirtualMachineProfile vm, final LinkedHashMap<? extends Network, List<? extends NicProfile>> networks, final Map<String, Map<Integer, String>> extraDhcpOptions) throws InsufficientCapacityException, ConcurrentOperationException {
Transaction.execute(new TransactionCallbackWithExceptionNoReturn<InsufficientCapacityException>() {
@Override
public void doInTransactionWithoutResult(final TransactionStatus status) throws InsufficientCapacityException {
if (s_logger.isTraceEnabled()) {
s_logger.trace(String.format("allocating networks for %s(template %s); %d networks", vm.getInstanceName(), vm.getTemplate().getUuid(), networks.size()));
}
int deviceId = 0;
int size;
size = determineNumberOfNicsRequired();
final boolean[] deviceIds = new boolean[size];
Arrays.fill(deviceIds, false);
List<Pair<Network, NicProfile>> profilesList = getOrderedNetworkNicProfileMapping(networks);
final List<NicProfile> nics = new ArrayList<NicProfile>(size);
NicProfile defaultNic = null;
Network nextNetwork = null;
for (Pair<Network, NicProfile> networkNicPair : profilesList) {
nextNetwork = networkNicPair.first();
Pair<NicProfile, Integer> newDeviceInfo = addRequestedNicToNicListWithDeviceNumberAndRetrieveDefaultDevice(networkNicPair.second(), deviceIds, deviceId, nextNetwork, nics, defaultNic);
defaultNic = newDeviceInfo.first();
deviceId = newDeviceInfo.second();
}
createExtraNics(size, nics, nextNetwork);
if (nics.size() == 1) {
nics.get(0).setDefaultNic(true);
}
}
/**
* private transaction method to check and add devices to the nic list and update the info
*/
Pair<NicProfile, Integer> addRequestedNicToNicListWithDeviceNumberAndRetrieveDefaultDevice(NicProfile requested, boolean[] deviceIds, int deviceId, Network nextNetwork, List<NicProfile> nics, NicProfile defaultNic) throws InsufficientAddressCapacityException, InsufficientVirtualNetworkCapacityException {
Pair<NicProfile, Integer> rc = new Pair<>(null, null);
Boolean isDefaultNic = false;
if (vm != null && requested != null && requested.isDefaultNic()) {
isDefaultNic = true;
}
while (deviceIds[deviceId] && deviceId < deviceIds.length) {
deviceId++;
}
final Pair<NicProfile, Integer> vmNicPair = allocateNic(requested, nextNetwork, isDefaultNic, deviceId, vm);
NicProfile vmNic = null;
if (vmNicPair != null) {
vmNic = vmNicPair.first();
if (vmNic == null) {
return rc;
}
deviceId = vmNicPair.second();
}
final int devId = vmNic.getDeviceId();
if (devId >= deviceIds.length) {
throw new IllegalArgumentException("Device id for nic is too large: " + vmNic);
}
if (deviceIds[devId]) {
throw new IllegalArgumentException("Conflicting device id for two different nics: " + vmNic);
}
deviceIds[devId] = true;
if (vmNic.isDefaultNic()) {
if (defaultNic != null) {
throw new IllegalArgumentException("You cannot specify two nics as default nics: nic 1 = " + defaultNic + "; nic 2 = " + vmNic);
}
defaultNic = vmNic;
}
nics.add(vmNic);
vm.addNic(vmNic);
saveExtraDhcpOptions(nextNetwork.getUuid(), vmNic.getId(), extraDhcpOptions);
rc.first(defaultNic);
rc.second(deviceId);
return rc;
}
/**
* private transaction method to get oredered list of Network and NicProfile pair
* @return ordered list of Network and NicProfile pair
* @param networks the map od networks to nic profiles list
*/
private List<Pair<Network, NicProfile>> getOrderedNetworkNicProfileMapping(final LinkedHashMap<? extends Network, List<? extends NicProfile>> networks) {
List<Pair<Network, NicProfile>> profilesList = new ArrayList<>();
for (final Map.Entry<? extends Network, List<? extends NicProfile>> network : networks.entrySet()) {
List<? extends NicProfile> requestedProfiles = network.getValue();
if (requestedProfiles == null) {
requestedProfiles = new ArrayList<NicProfile>();
}
if (requestedProfiles.isEmpty()) {
requestedProfiles.add(null);
}
for (final NicProfile requested : requestedProfiles) {
profilesList.add(new Pair<Network, NicProfile>(network.getKey(), requested));
}
}
profilesList.sort(new Comparator<Pair<Network, NicProfile>>() {
@Override
public int compare(Pair<Network, NicProfile> pair1, Pair<Network, NicProfile> pair2) {
int profile1Order = Integer.MAX_VALUE;
int profile2Order = Integer.MAX_VALUE;
if (pair1 != null && pair1.second() != null && pair1.second().getOrderIndex() != null) {
profile1Order = pair1.second().getOrderIndex();
}
if (pair2 != null && pair2.second() != null && pair2.second().getOrderIndex() != null) {
profile2Order = pair2.second().getOrderIndex();
}
return profile1Order - profile2Order;
}
});
return profilesList;
}
/**
* private transaction method to run over the objects and determine nic requirements
* @return the total numer of nics required
*/
private int determineNumberOfNicsRequired() {
int size = 0;
for (final Network ntwk : networks.keySet()) {
final List<? extends NicProfile> profiles = networks.get(ntwk);
if (profiles != null && !profiles.isEmpty()) {
size = size + profiles.size();
} else {
size = size + 1;
}
}
List<OVFNetworkTO> netprereqs = templateDeployAsIsDetailsDao.listNetworkRequirementsByTemplateId(vm.getTemplate().getId());
if (size < netprereqs.size()) {
size = netprereqs.size();
}
return size;
}
/**
* private transaction method to add nics as required
* @param size the number needed
* @param nics the list of nics present
* @param finalNetwork the network to add the nics to
* @throws InsufficientVirtualNetworkCapacityException great
* @throws InsufficientAddressCapacityException also magnificent, as the name sugests
*/
private void createExtraNics(int size, List<NicProfile> nics, Network finalNetwork) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
if (nics.size() != size) {
s_logger.warn("Number of nics " + nics.size() + " doesn't match number of requested nics " + size);
if (nics.size() > size) {
throw new CloudRuntimeException("Number of nics " + nics.size() + " doesn't match number of requested networks " + size);
} else {
if (finalNetwork == null) {
throw new CloudRuntimeException(String.format("can not assign network to %d remaining required NICs", size - nics.size()));
}
// create extra
for (int extraNicNum = nics.size(); extraNicNum < size; extraNicNum++) {
final Pair<NicProfile, Integer> vmNicPair = allocateNic(new NicProfile(), finalNetwork, false, extraNicNum, vm);
}
}
}
}
});
}
use of com.cloud.utils.db.TransactionStatus in project cloudstack by apache.
the class NetworkOrchestrator method shutdownNetwork.
@Override
@DB
public boolean shutdownNetwork(final long networkId, final ReservationContext context, final boolean cleanupElements) {
NetworkVO network = _networksDao.findById(networkId);
if (network.getState() == Network.State.Allocated) {
s_logger.debug("Network is already shutdown: " + network);
return true;
}
if (network.getState() != Network.State.Implemented && network.getState() != Network.State.Shutdown) {
s_logger.debug("Network is not implemented: " + network);
return false;
}
try {
// do global lock for the network
network = _networksDao.acquireInLockTable(networkId, NetworkLockTimeout.value());
if (network == null) {
s_logger.warn("Unable to acquire lock for the network " + network + " as a part of network shutdown");
return false;
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Lock is acquired for network " + network + " as a part of network shutdown");
}
if (network.getState() == Network.State.Allocated) {
s_logger.debug("Network is already shutdown: " + network);
return true;
}
if (network.getState() != Network.State.Implemented && network.getState() != Network.State.Shutdown) {
s_logger.debug("Network is not implemented: " + network);
return false;
}
if (isSharedNetworkWithServices(network)) {
network.setState(Network.State.Shutdown);
_networksDao.update(network.getId(), network);
} else {
try {
stateTransitTo(network, Event.DestroyNetwork);
} catch (final NoTransitionException e) {
network.setState(Network.State.Shutdown);
_networksDao.update(network.getId(), network);
}
}
final boolean success = shutdownNetworkElementsAndResources(context, cleanupElements, network);
final NetworkVO networkFinal = network;
final boolean result = Transaction.execute(new TransactionCallback<Boolean>() {
@Override
public Boolean doInTransaction(final TransactionStatus status) {
boolean result = false;
if (success) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Network id=" + networkId + " is shutdown successfully, cleaning up corresponding resources now.");
}
final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, networkFinal.getGuruName());
final NetworkProfile profile = convertNetworkToNetworkProfile(networkFinal.getId());
guru.shutdown(profile, _networkOfferingDao.findById(networkFinal.getNetworkOfferingId()));
applyProfileToNetwork(networkFinal, profile);
final DataCenterVO zone = _dcDao.findById(networkFinal.getDataCenterId());
if (isSharedNetworkOfferingWithServices(networkFinal.getNetworkOfferingId()) && zone.getNetworkType() == NetworkType.Advanced) {
networkFinal.setState(Network.State.Setup);
} else {
try {
stateTransitTo(networkFinal, Event.OperationSucceeded);
} catch (final NoTransitionException e) {
networkFinal.setState(Network.State.Allocated);
networkFinal.setRestartRequired(false);
}
}
_networksDao.update(networkFinal.getId(), networkFinal);
_networksDao.clearCheckForGc(networkId);
result = true;
} else {
try {
stateTransitTo(networkFinal, Event.OperationFailed);
} catch (final NoTransitionException e) {
networkFinal.setState(Network.State.Implemented);
_networksDao.update(networkFinal.getId(), networkFinal);
}
result = false;
}
return result;
}
});
return result;
} finally {
if (network != null) {
_networksDao.releaseFromLockTable(network.getId());
if (s_logger.isDebugEnabled()) {
s_logger.debug("Lock is released for network " + network + " as a part of network shutdown");
}
}
}
}
use of com.cloud.utils.db.TransactionStatus in project cloudstack by apache.
the class VolumeOrchestrator method cleanupVolumes.
@Override
@DB
public void cleanupVolumes(long vmId) throws ConcurrentOperationException {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Cleaning storage for vm: " + vmId);
}
final List<VolumeVO> volumesForVm = _volsDao.findByInstance(vmId);
final List<VolumeVO> toBeExpunged = new ArrayList<VolumeVO>();
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
for (VolumeVO vol : volumesForVm) {
if (vol.getVolumeType().equals(Type.ROOT)) {
// Destroy volume if not already destroyed
boolean volumeAlreadyDestroyed = (vol.getState() == Volume.State.Destroy || vol.getState() == Volume.State.Expunged || vol.getState() == Volume.State.Expunging);
if (!volumeAlreadyDestroyed) {
volService.destroyVolume(vol.getId());
} else {
s_logger.debug("Skipping destroy for the volume " + vol + " as its in state " + vol.getState().toString());
}
toBeExpunged.add(vol);
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Detaching " + vol);
}
VMInstanceVO vm = _userVmDao.findById(vmId);
if (vm.getHypervisorType().equals(HypervisorType.VMware)) {
_volumeApiService.detachVolumeViaDestroyVM(vmId, vol.getId());
}
_volsDao.detachVolume(vol.getId());
}
}
}
});
AsyncCallFuture<VolumeApiResult> future = null;
for (VolumeVO expunge : toBeExpunged) {
future = volService.expungeVolumeAsync(volFactory.getVolume(expunge.getId()));
try {
future.get();
} catch (InterruptedException e) {
s_logger.debug("failed expunge volume" + expunge.getId(), e);
} catch (ExecutionException e) {
s_logger.debug("failed expunge volume" + expunge.getId(), e);
}
}
}
use of com.cloud.utils.db.TransactionStatus 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);
}
}
}
}
Aggregations