use of com.cloud.vm.VirtualMachine in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testFenceCommand.
@Test
public void testFenceCommand() {
final VirtualMachine vm = Mockito.mock(VirtualMachine.class);
final com.cloud.host.Host host = Mockito.mock(com.cloud.host.Host.class);
final FenceCommand command = new FenceCommand(vm, host);
final KvmHaMonitor monitor = Mockito.mock(KvmHaMonitor.class);
final NfsStoragePool storagePool = Mockito.mock(NfsStoragePool.class);
final List<NfsStoragePool> pools = new ArrayList<>();
pools.add(storagePool);
when(libvirtComputingResource.getMonitor()).thenReturn(monitor);
when(monitor.getStoragePools()).thenReturn(pools);
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, libvirtComputingResource);
assertFalse(answer.getResult());
verify(libvirtComputingResource, times(1)).getMonitor();
verify(monitor, times(1)).getStoragePools();
}
use of com.cloud.vm.VirtualMachine in project cosmic by MissionCriticalCloud.
the class ApiResponseHelper method createNetworkResponse.
@Override
public NetworkResponse createNetworkResponse(final ResponseView view, final Network network) {
// need to get network profile in order to retrieve dns information from
// there
final NetworkProfile profile = ApiDBUtils.getNetworkProfile(network.getId());
final NetworkResponse response = new NetworkResponse();
response.setId(network.getUuid());
response.setName(network.getName());
response.setDisplaytext(network.getDisplayText());
if (network.getBroadcastDomainType() != null) {
response.setBroadcastDomainType(network.getBroadcastDomainType().toString());
}
if (network.getTrafficType() != null) {
response.setTrafficType(network.getTrafficType().name());
}
if (network.getGuestType() != null) {
response.setType(network.getGuestType().toString());
}
response.setGateway(network.getGateway());
// FIXME - either set netmask or cidr
response.setCidr(network.getCidr());
response.setNetworkCidr(network.getNetworkCidr());
// network cidr
if (network.getNetworkCidr() != null) {
response.setNetmask(NetUtils.cidr2Netmask(network.getNetworkCidr()));
}
if (network.getCidr() != null && network.getNetworkCidr() == null) {
response.setNetmask(NetUtils.cidr2Netmask(network.getCidr()));
}
response.setIpExclusionList(((NetworkVO) network).getIpExclusionList());
response.setIp6Gateway(network.getIp6Gateway());
response.setIp6Cidr(network.getIp6Cidr());
// create response for reserved IP ranges that can be used for
// non-cloudstack purposes
String reservation = null;
if (network.getCidr() != null && NetUtils.isNetworkAWithinNetworkB(network.getCidr(), network.getNetworkCidr())) {
final String[] guestVmCidrPair = network.getCidr().split("\\/");
final String[] guestCidrPair = network.getNetworkCidr().split("\\/");
final Long guestVmCidrSize = Long.valueOf(guestVmCidrPair[1]);
final Long guestCidrSize = Long.valueOf(guestCidrPair[1]);
final String[] guestVmIpRange = NetUtils.getIpRangeFromCidr(guestVmCidrPair[0], guestVmCidrSize);
final String[] guestIpRange = NetUtils.getIpRangeFromCidr(guestCidrPair[0], guestCidrSize);
final long startGuestIp = NetUtils.ip2Long(guestIpRange[0]);
final long endGuestIp = NetUtils.ip2Long(guestIpRange[1]);
final long startVmIp = NetUtils.ip2Long(guestVmIpRange[0]);
final long endVmIp = NetUtils.ip2Long(guestVmIpRange[1]);
if (startVmIp == startGuestIp && endVmIp < endGuestIp - 1) {
reservation = NetUtils.long2Ip(endVmIp + 1) + "-" + NetUtils.long2Ip(endGuestIp);
}
if (endVmIp == endGuestIp && startVmIp > startGuestIp + 1) {
reservation = NetUtils.long2Ip(startGuestIp) + "-" + NetUtils.long2Ip(startVmIp - 1);
}
if (startVmIp > startGuestIp + 1 && endVmIp < endGuestIp - 1) {
reservation = NetUtils.long2Ip(startGuestIp) + "-" + NetUtils.long2Ip(startVmIp - 1) + " , " + NetUtils.long2Ip(endVmIp + 1) + "-" + NetUtils.long2Ip(endGuestIp);
}
}
response.setReservedIpRange(reservation);
if (network.getBroadcastUri() != null) {
final String broadcastUri = network.getBroadcastUri().toString();
response.setBroadcastUri(broadcastUri);
String vlan = "N/A";
switch(BroadcastDomainType.getSchemeValue(network.getBroadcastUri())) {
case Vlan:
case Vxlan:
vlan = BroadcastDomainType.getValue(network.getBroadcastUri());
break;
}
response.setVlan(vlan);
}
final DataCenter zone = ApiDBUtils.findZoneById(network.getDataCenterId());
if (zone != null) {
response.setZoneId(zone.getUuid());
response.setZoneName(zone.getName());
}
if (network.getPhysicalNetworkId() != null) {
final PhysicalNetworkVO pnet = ApiDBUtils.findPhysicalNetworkById(network.getPhysicalNetworkId());
response.setPhysicalNetworkId(pnet.getUuid());
}
// populate network offering information
final NetworkOffering networkOffering = ApiDBUtils.findNetworkOfferingById(network.getNetworkOfferingId());
if (networkOffering != null) {
response.setNetworkOfferingId(networkOffering.getUuid());
response.setNetworkOfferingName(networkOffering.getName());
response.setNetworkOfferingDisplayText(networkOffering.getDisplayText());
response.setNetworkOfferingConserveMode(networkOffering.isConserveMode());
response.setIsSystem(networkOffering.isSystemOnly());
response.setNetworkOfferingAvailability(networkOffering.getAvailability().toString());
response.setIsPersistent(networkOffering.getIsPersistent());
}
if (network.getAclType() != null) {
response.setAclType(network.getAclType().toString());
}
response.setDisplayNetwork(network.getDisplayNetwork());
response.setState(network.getState().toString());
response.setRestartRequired(network.isRestartRequired());
final NetworkVO nw = ApiDBUtils.findNetworkById(network.getRelated());
if (nw != null) {
response.setRelated(nw.getUuid());
}
response.setNetworkDomain(network.getNetworkDomain());
response.setDns1(profile.getDns1());
response.setDns2(profile.getDns2());
// populate capability
final Map<Service, Map<Capability, String>> serviceCapabilitiesMap = ApiDBUtils.getNetworkCapabilities(network.getId(), network.getDataCenterId());
final List<ServiceResponse> serviceResponses = new ArrayList<>();
if (serviceCapabilitiesMap != null) {
for (final Map.Entry<Service, Map<Capability, String>> entry : serviceCapabilitiesMap.entrySet()) {
final Service service = entry.getKey();
final ServiceResponse serviceResponse = new ServiceResponse();
// skip gateway service
if (service == Service.Gateway) {
continue;
}
serviceResponse.setName(service.getName());
// set list of capabilities for the service
final List<CapabilityResponse> capabilityResponses = new ArrayList<>();
final Map<Capability, String> serviceCapabilities = entry.getValue();
if (serviceCapabilities != null) {
for (final Map.Entry<Capability, String> ser_cap_entries : serviceCapabilities.entrySet()) {
final Capability capability = ser_cap_entries.getKey();
final CapabilityResponse capabilityResponse = new CapabilityResponse();
final String capabilityValue = ser_cap_entries.getValue();
capabilityResponse.setName(capability.getName());
capabilityResponse.setValue(capabilityValue);
capabilityResponse.setObjectName("capability");
capabilityResponses.add(capabilityResponse);
}
serviceResponse.setCapabilities(capabilityResponses);
}
serviceResponse.setObjectName("service");
serviceResponses.add(serviceResponse);
}
}
response.setServices(serviceResponses);
if (network.getAclType() == null || network.getAclType() == ACLType.Account) {
populateOwner(response, network);
} else {
// get domain from network_domain table
final Pair<Long, Boolean> domainNetworkDetails = ApiDBUtils.getDomainNetworkDetails(network.getId());
if (domainNetworkDetails.first() != null) {
final Domain domain = ApiDBUtils.findDomainById(domainNetworkDetails.first());
if (domain != null) {
response.setDomainId(domain.getUuid());
}
}
response.setSubdomainAccess(domainNetworkDetails.second());
}
final Long dedicatedDomainId = ApiDBUtils.getDedicatedNetworkDomain(network.getId());
if (dedicatedDomainId != null) {
final Domain domain = ApiDBUtils.findDomainById(dedicatedDomainId);
if (domain != null) {
response.setDomainId(domain.getUuid());
response.setDomainName(domain.getName());
}
}
response.setSpecifyIpRanges(network.getSpecifyIpRanges());
if (network.getVpcId() != null) {
final Vpc vpc = ApiDBUtils.findVpcById(network.getVpcId());
if (vpc != null) {
response.setVpcId(vpc.getUuid());
response.setVpcName(vpc.getName());
}
}
response.setCanUseForDeploy(ApiDBUtils.canUseForDeploy(network));
// set tag information
final List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.Network, network.getId());
final List<ResourceTagResponse> tagResponses = new ArrayList<>();
for (final ResourceTag tag : tags) {
final ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
}
response.setTags(tagResponses);
if (network.getNetworkACLId() != null) {
final NetworkACL acl = ApiDBUtils.findByNetworkACLId(network.getNetworkACLId());
if (acl != null) {
response.setAclId(acl.getUuid());
response.setAclName(acl.getName());
}
}
response.setStrechedL2Subnet(network.isStrechedL2Network());
if (network.isStrechedL2Network()) {
final Set<String> networkSpannedZones = new HashSet<>();
final List<VMInstanceVO> vmInstances = new ArrayList<>();
vmInstances.addAll(ApiDBUtils.listUserVMsByNetworkId(network.getId()));
vmInstances.addAll(ApiDBUtils.listDomainRoutersByNetworkId(network.getId()));
for (final VirtualMachine vm : vmInstances) {
final DataCenter vmZone = ApiDBUtils.findZoneById(vm.getDataCenterId());
networkSpannedZones.add(vmZone.getUuid());
}
response.setNetworkSpannedZones(networkSpannedZones);
}
response.setObjectName("network");
return response;
}
use of com.cloud.vm.VirtualMachine in project cosmic by MissionCriticalCloud.
the class QueryManagerImpl method listHAWorkers.
@Override
public ListResponse<HAWorkerResponse> listHAWorkers(final ListHAWorkersCmd cmd) {
final ListResponse<HAWorkerResponse> haWorkerList = new ListResponse<>();
final List<HaWorkVO> haWorkers = _haDao.listAll();
final List<HAWorkerResponse> haWorkerResponses = new ArrayList<>();
haWorkers.stream().filter(haWorker -> {
final Account account = CallContext.current().getCallingAccount();
if (account.getDomainId() == Domain.ROOT_DOMAIN) {
return cmd.getId() == null || (cmd.getId() != null && cmd.getId() == haWorker.getId());
}
// Not a root administrator, check if the user owns the virtual machine
final VirtualMachine virtualMachine = _vmInstanceDao.findByIdIncludingRemoved(haWorker.getInstanceId());
return virtualMachine != null && virtualMachine.getDomainId() == account.getDomainId() && (cmd.getId() == null || (cmd.getId() != null && haWorker.getId() == cmd.getId()));
}).forEach(haWorker -> {
final HAWorkerResponse haWorkerResponse = new HAWorkerResponse();
haWorkerResponse.setObjectName("haworker");
haWorkerResponse.setId(haWorker.getId());
haWorkerResponse.setType(haWorker.getWorkType());
haWorkerResponse.setCreated(haWorker.getCreated());
haWorkerResponse.setStep(haWorker.getStep());
haWorkerResponse.setTaken(haWorker.getDateTaken());
haWorkerResponse.setState(haWorker.getPreviousState());
final VirtualMachine virtualMachine = _vmInstanceDao.findByIdIncludingRemoved(haWorker.getInstanceId());
if (virtualMachine != null) {
haWorkerResponse.setVirtualMachineId(virtualMachine.getUuid());
haWorkerResponse.setVirtualMachineName(virtualMachine.getInstanceName());
haWorkerResponse.setVirtualMachineState(virtualMachine.getState());
final Domain domain = _domainDao.findById(virtualMachine.getDomainId());
if (domain != null) {
haWorkerResponse.setDomainId(domain.getUuid());
haWorkerResponse.setDomainName(domain.getName());
}
}
final Host host = _hostDao.findById(haWorker.getHostId());
if (host != null) {
haWorkerResponse.setHypervisor(host.getName());
}
if (haWorker.getServerId() != null) {
final ManagementServerHost managementServerHost = _mgmtServerHostDao.findByMsid(haWorker.getServerId());
if (managementServerHost != null) {
haWorkerResponse.setManagementServerName(managementServerHost.getName());
}
}
haWorkerResponses.add(haWorkerResponse);
});
haWorkerList.setResponses(haWorkerResponses);
return haWorkerList;
}
use of com.cloud.vm.VirtualMachine in project cosmic by MissionCriticalCloud.
the class DeploymentPlanningManagerImpl method finalizeReservation.
@DB
@Override
public String finalizeReservation(final DeployDestination plannedDestination, final VirtualMachineProfile vmProfile, final DeploymentPlan plan, final ExcludeList avoids, final DeploymentPlanner planner) throws InsufficientServerCapacityException, AffinityConflictException {
final VirtualMachine vm = vmProfile.getVirtualMachine();
final long vmGroupCount = _affinityGroupVMMapDao.countAffinityGroupsForVm(vm.getId());
return Transaction.execute(new TransactionCallback<String>() {
@Override
public String doInTransaction(final TransactionStatus status) {
boolean saveReservation = true;
if (vmGroupCount > 0) {
final List<Long> groupIds = _affinityGroupVMMapDao.listAffinityGroupIdsByVmId(vm.getId());
final SearchCriteria<AffinityGroupVO> criteria = _affinityGroupDao.createSearchCriteria();
criteria.addAnd("id", SearchCriteria.Op.IN, groupIds.toArray(new Object[groupIds.size()]));
_affinityGroupDao.lockRows(criteria, null, true);
for (final AffinityGroupProcessor processor : _affinityProcessors) {
if (!processor.check(vmProfile, plannedDestination)) {
saveReservation = false;
break;
}
}
}
if (saveReservation) {
final VMReservationVO vmReservation = new VMReservationVO(vm.getId(), plannedDestination.getZone().getId(), plannedDestination.getPod().getId(), plannedDestination.getCluster().getId(), plannedDestination.getHost().getId());
if (planner != null) {
vmReservation.setDeploymentPlanner(planner.getName());
}
final Map<Long, Long> volumeReservationMap = new HashMap<>();
for (final Volume vo : plannedDestination.getStorageForDisks().keySet()) {
volumeReservationMap.put(vo.getId(), plannedDestination.getStorageForDisks().get(vo).getId());
}
vmReservation.setVolumeReservation(volumeReservationMap);
_reservationDao.persist(vmReservation);
return vmReservation.getUuid();
}
return null;
}
});
}
use of com.cloud.vm.VirtualMachine in project cosmic by MissionCriticalCloud.
the class FirstFitPlanner method scanClustersForDestinationInZoneOrPod.
private List<Long> scanClustersForDestinationInZoneOrPod(final long id, final boolean isZone, final VirtualMachineProfile vmProfile, final DeploymentPlan plan, final ExcludeList avoid) {
final VirtualMachine vm = vmProfile.getVirtualMachine();
final ServiceOffering offering = vmProfile.getServiceOffering();
final int requiredCpu = offering.getCpu();
final long requiredRam = offering.getRamSize() * 1024L * 1024L;
// list clusters under this zone by cpu and ram capacity
final Pair<List<Long>, Map<Long, Double>> clusterCapacityInfo = listClustersByCapacity(id, requiredCpu, requiredRam, avoid, isZone);
final List<Long> prioritizedClusterIds = clusterCapacityInfo.first();
if (!prioritizedClusterIds.isEmpty()) {
if (avoid.getClustersToAvoid() != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Removing from the clusterId list these clusters from avoid set: " + avoid.getClustersToAvoid());
}
prioritizedClusterIds.removeAll(avoid.getClustersToAvoid());
}
if (!isRootAdmin(vmProfile)) {
List<Long> disabledClusters = new ArrayList<>();
if (isZone) {
disabledClusters = listDisabledClusters(plan.getDataCenterId(), null);
} else {
disabledClusters = listDisabledClusters(plan.getDataCenterId(), id);
}
if (!disabledClusters.isEmpty()) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Removing from the clusterId list these clusters that are disabled/clusters under disabled pods: " + disabledClusters);
}
prioritizedClusterIds.removeAll(disabledClusters);
}
}
removeClustersCrossingThreshold(prioritizedClusterIds, avoid, vmProfile, plan);
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("No clusters found having a host with enough capacity, returning.");
}
return null;
}
if (!prioritizedClusterIds.isEmpty()) {
// return checkClustersforDestination(clusterList, vmProfile, plan, avoid, dc);
return reorderClusters(id, isZone, clusterCapacityInfo, vmProfile, plan);
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("No clusters found after removing disabled clusters and clusters in avoid list, returning.");
}
return null;
}
}
Aggregations