use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.
the class NetworkOrchestrator method destroyNetwork.
@Override
@DB
public boolean destroyNetwork(final long networkId, final ReservationContext context, final boolean forced) {
final Account callerAccount = context.getAccount();
NetworkVO network = _networksDao.findById(networkId);
if (network == null) {
s_logger.debug("Unable to find network with id: " + networkId);
return false;
}
// Make sure that there are no user vms in the network that are not Expunged/Error
final List<UserVmVO> userVms = _userVmDao.listByNetworkIdAndStates(networkId);
for (final UserVmVO vm : userVms) {
if (!(vm.getState() == VirtualMachine.State.Expunging && vm.getRemoved() != null)) {
s_logger.warn("Can't delete the network, not all user vms are expunged. Vm " + vm + " is in " + vm.getState() + " state");
return false;
}
}
// Don't allow to delete network via api call when it has vms assigned to it
final int nicCount = getActiveNicsInNetwork(networkId);
if (nicCount > 0) {
s_logger.debug("The network id=" + networkId + " has active Nics, but shouldn't.");
// at this point we have already determined that there are no active user vms in network
// if the op_networks table shows active nics, it's a bug in releasing nics updating op_networks
_networksDao.changeActiveNicsBy(networkId, -1 * nicCount);
}
// In Basic zone, make sure that there are no non-removed console proxies and SSVMs using the network
final Zone zone = _zoneRepository.findById(network.getDataCenterId()).orElse(null);
if (zone.getNetworkType() == com.cloud.model.enumeration.NetworkType.Basic) {
final List<VMInstanceVO> systemVms = _vmDao.listNonRemovedVmsByTypeAndNetwork(network.getId(), VirtualMachineType.ConsoleProxy, VirtualMachineType.SecondaryStorageVm);
if (systemVms != null && !systemVms.isEmpty()) {
s_logger.warn("Can't delete the network, not all consoleProxy/secondaryStorage vms are expunged");
return false;
}
}
// Shutdown network first
shutdownNetwork(networkId, context, false);
// get updated state for the network
network = _networksDao.findById(networkId);
if (network.getState() != Network.State.Allocated && network.getState() != Network.State.Setup && !forced) {
s_logger.debug("Network is not not in the correct state to be destroyed: " + network.getState());
return false;
}
boolean success = true;
if (!cleanupNetworkResources(networkId, callerAccount, context.getCaller().getId())) {
s_logger.warn("Unable to delete network id=" + networkId + ": failed to cleanup network resources");
return false;
}
// get providers to destroy
final List<Provider> providersToDestroy = getNetworkProviders(network.getId());
for (final NetworkElement element : networkElements) {
if (providersToDestroy.contains(element.getProvider())) {
try {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Sending destroy to " + element);
}
if (!element.destroy(network, context)) {
success = false;
s_logger.warn("Unable to complete destroy of the network: failed to destroy network element " + element.getName());
}
} catch (final ResourceUnavailableException e) {
s_logger.warn("Unable to complete destroy of the network due to element: " + element.getName(), e);
success = false;
} catch (final ConcurrentOperationException e) {
s_logger.warn("Unable to complete destroy of the network due to element: " + element.getName(), e);
success = false;
} catch (final Exception e) {
s_logger.warn("Unable to complete destroy of the network due to element: " + element.getName(), e);
success = false;
}
}
}
if (success) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Network id=" + networkId + " is destroyed successfully, cleaning up corresponding resources now.");
}
final NetworkVO networkFinal = network;
try {
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(final TransactionStatus status) {
final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, networkFinal.getGuruName());
// Deleting sync networks
final List<NetworkVO> syncNetworks = _networksDao.listSyncNetworksByRelatedNetwork(networkId);
syncNetworks.forEach(syncNetwork -> removeAndShutdownSyncNetwork(syncNetwork.getId()));
guru.trash(networkFinal, _networkOfferingDao.findById(networkFinal.getNetworkOfferingId()));
if (!deleteVlansInNetwork(networkFinal.getId(), context.getCaller().getId(), callerAccount)) {
s_logger.warn("Failed to delete network " + networkFinal + "; was unable to cleanup corresponding ip ranges");
throw new CloudRuntimeException("Failed to delete network " + networkFinal + "; was unable to cleanup corresponding ip ranges");
} else {
// commit transaction only when ips and vlans for the network are released successfully
try {
stateTransitTo(networkFinal, Event.DestroyNetwork);
} catch (final NoTransitionException e) {
s_logger.debug(e.getMessage());
}
if (_networksDao.remove(networkFinal.getId())) {
final NetworkDomainVO networkDomain = _networkDomainDao.getDomainNetworkMapByNetworkId(networkFinal.getId());
if (networkDomain != null) {
_networkDomainDao.remove(networkDomain.getId());
}
final NetworkAccountVO networkAccount = _networkAccountDao.getAccountNetworkMapByNetworkId(networkFinal.getId());
if (networkAccount != null) {
_networkAccountDao.remove(networkAccount.getId());
}
}
final NetworkOffering ntwkOff = _entityMgr.findById(NetworkOffering.class, networkFinal.getNetworkOfferingId());
final boolean updateResourceCount = resourceCountNeedsUpdate(ntwkOff, networkFinal.getAclType());
if (updateResourceCount) {
_resourceLimitMgr.decrementResourceCount(networkFinal.getAccountId(), ResourceType.network, networkFinal.getDisplayNetwork());
}
}
}
});
if (_networksDao.findById(network.getId()) == null) {
// remove its related ACL permission
final Pair<Class<?>, Long> networkMsg = new Pair<>(Network.class, networkFinal.getId());
_messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, networkMsg);
}
return true;
} catch (final CloudRuntimeException e) {
s_logger.error("Failed to delete network", e);
return false;
}
}
return success;
}
use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.
the class VmWorkJobHandlerProxy method handleVmWorkJob.
@Override
public Pair<JobInfo.Status, String> handleVmWorkJob(final VmWork work) throws CloudException {
final Method method = getHandlerMethod(work.getClass());
if (method != null) {
try {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Execute VM work job: " + work.getClass().getName() + _gsonLogger.toJson(work));
}
final Object obj = method.invoke(_target, work);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Done executing VM work job: " + work.getClass().getName() + _gsonLogger.toJson(work));
}
assert (obj instanceof Pair);
return (Pair<JobInfo.Status, String>) obj;
} catch (final InvocationTargetException e) {
final Throwable cause = e.getCause();
s_logger.error("Invocation exception, caused by: " + cause);
// we need to re-throw the real exception here
if (cause != null && cause instanceof Exception) {
s_logger.info("Rethrow exception " + cause);
throw new CloudException("Caught exception while handling a VmWorkJob", cause);
}
throw new CloudException("Caught exception while handling a VmWorkJob", e);
} catch (final IllegalAccessException e) {
throw new CloudException("Caught exception while handling a VmWorkJob", e);
}
} else {
s_logger.error("Unable to find handler for VM work job: " + work.getClass().getName() + _gsonLogger.toJson(work));
final RuntimeException ex = new RuntimeException("Unable to find handler for VM work job: " + work.getClass().getName());
return new Pair<>(JobInfo.Status.FAILED, JobSerializerHelper.toObjectSerializedString(ex));
}
}
use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.
the class StoragePoolHostDaoImpl method getDatacenterStoragePoolHostInfo.
@Override
public List<Pair<Long, Integer>> getDatacenterStoragePoolHostInfo(final long dcId, final boolean sharedOnly) {
final ArrayList<Pair<Long, Integer>> l = new ArrayList<>();
final String sql = sharedOnly ? SHARED_STORAGE_POOL_HOST_INFO : STORAGE_POOL_HOST_INFO;
final TransactionLegacy txn = TransactionLegacy.currentTxn();
PreparedStatement pstmt = null;
try {
pstmt = txn.prepareAutoCloseStatement(sql);
pstmt.setLong(1, dcId);
final ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
l.add(new Pair<>(rs.getLong(1), rs.getInt(2)));
}
} catch (final SQLException e) {
s_logger.debug("SQLException: ", e);
}
return l;
}
use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.
the class VolumeServiceImpl method registerVolumeForPostUpload.
@Override
public Pair<EndPoint, DataObject> registerVolumeForPostUpload(final VolumeInfo volume, final DataStore store) {
final EndPoint ep = _epSelector.select(store);
if (ep == null) {
final String errorMessage = "There is no secondary storage VM for image store " + store.getName();
s_logger.warn(errorMessage);
throw new CloudRuntimeException(errorMessage);
}
final DataObject volumeOnStore = store.create(volume);
return new Pair<>(ep, volumeOnStore);
}
use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.
the class AbstractHyperVisorStorageMotionStrategy method buildVolumeMapping.
protected List<Pair<VolumeTO, StorageFilerTO>> buildVolumeMapping(final Map<VolumeInfo, DataStore> volumeToPool) {
final List<Pair<VolumeTO, StorageFilerTO>> volumeToFilerto = new ArrayList<>();
for (final Map.Entry<VolumeInfo, DataStore> entry : volumeToPool.entrySet()) {
final VolumeInfo volume = entry.getKey();
final StoragePool storagePool = storagePoolDao.findById(volume.getPoolId());
final VolumeTO volumeTo = new VolumeTO(volume.getId(), volume.getVolumeType(), storagePool.getPoolType(), storagePool.getUuid(), volume.getName(), volume.getFolder(), volume.getPath(), volume.getSize(), volume.getChainInfo(), volume.getFormat(), volume.getProvisioningType());
final StorageFilerTO filerTo = new StorageFilerTO((StoragePool) entry.getValue());
volumeToFilerto.add(new Pair<>(volumeTo, filerTo));
}
return volumeToFilerto;
}
Aggregations