use of com.cloud.legacymodel.utils.Ternary in project cosmic by MissionCriticalCloud.
the class ManagementServerImpl method listHostsForMigrationOfVM.
@Override
public Ternary<Pair<List<? extends Host>, Integer>, List<? extends Host>, Map<Host, Boolean>> listHostsForMigrationOfVM(final Long vmId, final Long startIndex, final Long pageSize, final String keyword) {
final Account caller = getCaller();
if (!_accountMgr.isRootAdmin(caller.getId())) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Caller is not a root admin, permission denied to migrate the VM");
}
throw new PermissionDeniedException("No permission to migrate VM, Only Root Admin can migrate a VM!");
}
final VMInstanceVO vm = _vmInstanceDao.findById(vmId);
if (vm == null) {
final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find the VM with given id");
throw ex;
}
if (vm.getState() != State.Running) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("VM is not running, cannot migrate the vm" + vm);
}
final InvalidParameterValueException ex = new InvalidParameterValueException("VM is not Running, cannot " + "migrate the vm with specified id");
ex.addProxyObject(vm.getUuid(), "vmId");
throw ex;
}
if (_serviceOfferingDetailsDao.findDetail(vm.getServiceOfferingId(), GPU.Keys.pciDevice.toString()) != null) {
s_logger.info(" Live Migration of GPU enabled VM : " + vm.getInstanceName() + " is not supported");
// Return empty list.
return new Ternary<>(new Pair<>(new ArrayList<HostVO>(), new Integer(0)), new ArrayList<>(), new HashMap<>());
}
if (!vm.getHypervisorType().equals(HypervisorType.XenServer) && !vm.getHypervisorType().equals(HypervisorType.KVM)) {
if (s_logger.isDebugEnabled()) {
s_logger.debug(vm + " is not XenServer/KVM, cannot migrate this VM.");
}
throw new InvalidParameterValueException("Unsupported Hypervisor Type for VM migration, we support " + "XenServer/KVM only");
}
final long srcHostId = vm.getHostId();
final Host srcHost = _hostDao.findById(srcHostId);
if (srcHost == null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Unable to find the host with id: " + srcHostId + " of this VM:" + vm);
}
final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find the host (with specified id) of VM with specified id");
ex.addProxyObject(String.valueOf(srcHostId), "hostId");
ex.addProxyObject(vm.getUuid(), "vmId");
throw ex;
}
// Check if the vm can be migrated with storage.
boolean canMigrateWithStorage = false;
if (vm.getType() == VirtualMachineType.User) {
final HypervisorCapabilitiesVO capabilities = _hypervisorCapabilitiesDao.findByHypervisorTypeAndVersion(srcHost.getHypervisorType(), srcHost.getHypervisorVersion());
if (capabilities != null) {
canMigrateWithStorage = capabilities.isStorageMotionSupported();
}
}
// Check if the vm is using any disks on local storage.
final VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(vm, null, _offeringDao.findById(vm.getId(), vm.getServiceOfferingId()), null, null);
final List<VolumeVO> volumes = _volumeDao.findCreatedByInstance(vmProfile.getId());
boolean usesLocal = false;
for (final VolumeVO volume : volumes) {
final DiskOfferingVO diskOffering = _diskOfferingDao.findByIdIncludingRemoved(volume.getDiskOfferingId());
final DiskProfile diskProfile = new DiskProfile(volume, diskOffering, vmProfile.getHypervisorType());
if (diskProfile.useLocalStorage()) {
usesLocal = true;
break;
}
}
if (!canMigrateWithStorage && usesLocal) {
throw new InvalidParameterValueException("Unsupported operation, VM uses Local storage, cannot migrate");
}
final HostType hostType = srcHost.getType();
final Pair<List<HostVO>, Integer> allHostsPair;
final List<HostVO> allHosts;
final Map<Host, Boolean> requiresStorageMotion = new HashMap<>();
final DataCenterDeployment plan;
if (canMigrateWithStorage) {
allHostsPair = searchForServers(startIndex, pageSize, null, hostType, null, srcHost.getDataCenterId(), null, null, null, keyword, null, null, srcHost.getHypervisorType(), srcHost.getHypervisorVersion());
allHosts = allHostsPair.first();
allHosts.remove(srcHost);
for (final VolumeVO volume : volumes) {
final Long volClusterId = _poolDao.findById(volume.getPoolId()).getClusterId();
// only check for volume which are not in zone wide primary store, as only those may require storage motion
if (volClusterId != null) {
for (final Iterator<HostVO> iterator = allHosts.iterator(); iterator.hasNext(); ) {
final Host host = iterator.next();
if (!host.getClusterId().equals(volClusterId) || usesLocal) {
if (hasSuitablePoolsForVolume(volume, host, vmProfile)) {
requiresStorageMotion.put(host, true);
} else {
iterator.remove();
}
}
}
}
}
plan = new DataCenterDeployment(srcHost.getDataCenterId(), null, null, null, null, null);
} else {
final Long cluster = srcHost.getClusterId();
if (s_logger.isDebugEnabled()) {
s_logger.debug("Searching for all hosts in cluster " + cluster + " for migrating VM " + vm);
}
allHostsPair = searchForServers(startIndex, pageSize, null, hostType, null, null, null, cluster, null, keyword, null, null, null, null);
// Filter out the current host.
allHosts = allHostsPair.first();
allHosts.remove(srcHost);
plan = new DataCenterDeployment(srcHost.getDataCenterId(), srcHost.getPodId(), srcHost.getClusterId(), null, null, null);
}
final Pair<List<? extends Host>, Integer> otherHosts = new Pair<>(allHosts, new Integer(allHosts.size()));
List<Host> suitableHosts = new ArrayList<>();
final ExcludeList excludes = new ExcludeList();
excludes.addHost(srcHostId);
// call affinitygroup chain
final long vmGroupCount = _affinityGroupVMMapDao.countAffinityGroupsForVm(vm.getId());
if (vmGroupCount > 0) {
for (final AffinityGroupProcessor processor : _affinityProcessors) {
processor.process(vmProfile, plan, excludes);
}
}
final Account account = vmProfile.getOwner();
for (final HostVO host : allHosts) {
final DedicatedResourceVO dedicatedResourceVO = dedicatedResourceDao.findByHostId(host.getId());
if (dedicatedResourceVO != null && dedicatedResourceVO.getDomainId() != account.getDomainId()) {
final Domain domain = _domainDao.findById(dedicatedResourceVO.getDomainId());
if (domain != null) {
s_logger.debug("Host " + host.getName() + " is dedicated to domain " + domain.getName() + " so not suitable for migration for VM " + vmProfile.getInstanceName());
}
excludes.addHost(host.getId());
}
}
for (final HostAllocator allocator : hostAllocators) {
if (canMigrateWithStorage) {
suitableHosts = allocator.allocateTo(vmProfile, plan, HostType.Routing, excludes, allHosts, HostAllocator.RETURN_UPTO_ALL, false);
} else {
suitableHosts = allocator.allocateTo(vmProfile, plan, HostType.Routing, excludes, HostAllocator.RETURN_UPTO_ALL, false);
}
if (suitableHosts != null && !suitableHosts.isEmpty()) {
break;
}
}
if (s_logger.isDebugEnabled()) {
if (suitableHosts.isEmpty()) {
s_logger.debug("No suitable hosts found");
} else {
s_logger.debug("Hosts having capacity and suitable for migration: " + suitableHosts);
}
}
return new Ternary<>(otherHosts, suitableHosts, requiresStorageMotion);
}
use of com.cloud.legacymodel.utils.Ternary in project cosmic by MissionCriticalCloud.
the class OCFS2ManagerImpl method marshalNodes.
private List<Ternary<Integer, String, String>> marshalNodes(final List<HostVO> hosts) {
Integer i = 0;
final List<Ternary<Integer, String, String>> lst = new ArrayList<>();
for (final HostVO h : hosts) {
/**
* Don't show "node" in node name otherwise OVM's utils/config_o2cb.sh will be going crazy
*/
final String nodeName = "ovm_" + h.getPrivateIpAddress().replace(".", "_");
final Ternary<Integer, String, String> node = new Ternary<>(i, h.getPrivateIpAddress(), nodeName);
lst.add(node);
i++;
}
return lst;
}
use of com.cloud.legacymodel.utils.Ternary in project cosmic by MissionCriticalCloud.
the class SnapshotManagerImpl method listSnapshots.
@Override
public Pair<List<? extends Snapshot>, Integer> listSnapshots(final ListSnapshotsCmd cmd) {
final Long volumeId = cmd.getVolumeId();
final String name = cmd.getSnapshotName();
final Long id = cmd.getId();
final String keyword = cmd.getKeyword();
final String snapshotTypeStr = cmd.getSnapshotType();
final String intervalTypeStr = cmd.getIntervalType();
final Map<String, String> tags = cmd.getTags();
final Long zoneId = cmd.getZoneId();
final Account caller = CallContext.current().getCallingAccount();
final List<Long> permittedAccounts = new ArrayList<>();
// Verify parameters
if (volumeId != null) {
final VolumeVO volume = this._volsDao.findById(volumeId);
if (volume != null) {
this._accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, volume);
}
}
final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
this._accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
final Long domainId = domainIdRecursiveListProject.first();
final Boolean isRecursive = domainIdRecursiveListProject.second();
final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
final Filter searchFilter = new Filter(SnapshotVO.class, "created", false, cmd.getStartIndex(), cmd.getPageSizeVal());
final SearchBuilder<SnapshotVO> sb = this._snapshotDao.createSearchBuilder();
this._accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
// exclude those Destroyed snapshot, not showing on UI
sb.and("statusNEQ", sb.entity().getState(), SearchCriteria.Op.NEQ);
sb.and("volumeId", sb.entity().getVolumeId(), SearchCriteria.Op.EQ);
sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
sb.and("snapshotTypeEQ", sb.entity().getsnapshotType(), SearchCriteria.Op.IN);
sb.and("snapshotTypeNEQ", sb.entity().getsnapshotType(), SearchCriteria.Op.NEQ);
sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
if (tags != null && !tags.isEmpty()) {
final SearchBuilder<ResourceTagVO> tagSearch = this._resourceTagDao.createSearchBuilder();
for (int count = 0; count < tags.size(); count++) {
tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ);
tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ);
tagSearch.cp();
}
tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ);
sb.groupBy(sb.entity().getId());
sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
}
final SearchCriteria<SnapshotVO> sc = sb.create();
this._accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
sc.setParameters("statusNEQ", Snapshot.State.Destroyed);
if (volumeId != null) {
sc.setParameters("volumeId", volumeId);
}
if (tags != null && !tags.isEmpty()) {
int count = 0;
sc.setJoinParameters("tagSearch", "resourceType", ResourceObjectType.Snapshot.toString());
for (final String key : tags.keySet()) {
sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key);
sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), tags.get(key));
count++;
}
}
if (zoneId != null) {
sc.setParameters("dataCenterId", zoneId);
}
if (name != null) {
sc.setParameters("name", "%" + name + "%");
}
if (id != null) {
sc.setParameters("id", id);
}
if (keyword != null) {
final SearchCriteria<SnapshotVO> ssc = this._snapshotDao.createSearchCriteria();
ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
sc.addAnd("name", SearchCriteria.Op.SC, ssc);
}
if (snapshotTypeStr != null) {
final Type snapshotType = SnapshotVO.getSnapshotType(snapshotTypeStr);
if (snapshotType == null) {
throw new InvalidParameterValueException("Unsupported snapshot type " + snapshotTypeStr);
}
if (snapshotType == Type.RECURRING) {
sc.setParameters("snapshotTypeEQ", Type.HOURLY.ordinal(), Type.DAILY.ordinal(), Type.WEEKLY.ordinal(), Type.MONTHLY.ordinal());
} else {
sc.setParameters("snapshotTypeEQ", snapshotType.ordinal());
}
} else if (intervalTypeStr != null && volumeId != null) {
final Type type = SnapshotVO.getSnapshotType(intervalTypeStr);
if (type == null) {
throw new InvalidParameterValueException("Unsupported snapstho interval type " + intervalTypeStr);
}
sc.setParameters("snapshotTypeEQ", type.ordinal());
} else {
// Show only MANUAL and RECURRING snapshot types
sc.setParameters("snapshotTypeNEQ", Snapshot.Type.TEMPLATE.ordinal());
}
final Pair<List<SnapshotVO>, Integer> result = this._snapshotDao.searchAndCount(sc, searchFilter);
return new Pair<>(result.first(), result.second());
}
use of com.cloud.legacymodel.utils.Ternary in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method cleanVMSnapshotMetadata.
public List<Ternary<String, Boolean, String>> cleanVMSnapshotMetadata(final Domain dm) throws LibvirtException {
logger.debug("Cleaning the metadata of vm snapshots of vm " + dm.getName());
final List<Ternary<String, Boolean, String>> vmsnapshots = new ArrayList<>();
if (dm.snapshotNum() == 0) {
return vmsnapshots;
}
String currentSnapshotName = null;
try {
final DomainSnapshot snapshotCurrent = dm.snapshotCurrent();
final String snapshotXML = snapshotCurrent.getXMLDesc();
snapshotCurrent.free();
try {
final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final InputSource is = new InputSource();
is.setCharacterStream(new StringReader(snapshotXML));
final Document doc = builder.parse(is);
final Element rootElement = doc.getDocumentElement();
currentSnapshotName = getTagValue("name", rootElement);
} catch (final ParserConfigurationException | SAXException | IOException e) {
logger.debug(e.toString());
}
} catch (final LibvirtException e) {
logger.debug("Fail to get the current vm snapshot for vm: " + dm.getName() + ", continue");
}
// VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY = 2
final int flags = 2;
final String[] snapshotNames = dm.snapshotListNames();
Arrays.sort(snapshotNames);
for (final String snapshotName : snapshotNames) {
final DomainSnapshot snapshot = dm.snapshotLookupByName(snapshotName);
final Boolean isCurrent = currentSnapshotName != null && currentSnapshotName.equals(snapshotName);
vmsnapshots.add(new Ternary<>(snapshotName, isCurrent, snapshot.getXMLDesc()));
}
for (final String snapshotName : snapshotNames) {
final DomainSnapshot snapshot = dm.snapshotLookupByName(snapshotName);
// clean metadata of vm snapshot
snapshot.delete(flags);
}
return vmsnapshots;
}
use of com.cloud.legacymodel.utils.Ternary in project cosmic by MissionCriticalCloud.
the class QueryManagerImpl method searchForIsosInternal.
private Pair<List<TemplateJoinVO>, Integer> searchForIsosInternal(final ListIsosCmd cmd) {
final TemplateFilter isoFilter = TemplateFilter.valueOf(cmd.getIsoFilter());
final Long id = cmd.getId();
final Map<String, String> tags = cmd.getTags();
final boolean showRemovedISO = cmd.getShowRemoved();
final Account caller = CallContext.current().getCallingAccount();
boolean listAll = false;
if (isoFilter != null && isoFilter == TemplateFilter.all) {
if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL) {
throw new InvalidParameterValueException("Filter " + TemplateFilter.all + " can be specified by admin only");
}
listAll = true;
}
final List<Long> permittedAccountIds = new ArrayList<>();
final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
_accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccountIds, domainIdRecursiveListProject, listAll, false);
final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
final List<Account> permittedAccounts = new ArrayList<>();
for (final Long accountId : permittedAccountIds) {
permittedAccounts.add(_accountMgr.getAccount(accountId));
}
final HypervisorType hypervisorType = HypervisorType.getType(cmd.getHypervisor());
return searchForTemplatesInternal(cmd.getId(), cmd.getIsoName(), cmd.getKeyword(), isoFilter, true, cmd.isBootable(), cmd.getPageSizeVal(), cmd.getStartIndex(), cmd.getZoneId(), hypervisorType, true, cmd.listInReadyState(), permittedAccounts, caller, listProjectResourcesCriteria, tags, showRemovedISO);
}
Aggregations