use of com.cloud.legacymodel.to.NicTO in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testStartCommandInternalError.
@Test
public void testStartCommandInternalError() {
final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
final Host host = Mockito.mock(Host.class);
final boolean executeInSequence = false;
final StartCommand command = new StartCommand(vmSpec, host, executeInSequence);
final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final Connect conn = Mockito.mock(Connect.class);
final LibvirtVmDef vmDef = Mockito.mock(LibvirtVmDef.class);
final NicTO nic = Mockito.mock(NicTO.class);
final NicTO[] nics = new NicTO[] { nic };
final String vmName = "Test";
when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
when(vmSpec.getNics()).thenReturn(nics);
when(vmSpec.getType()).thenReturn(VirtualMachineType.DomainRouter);
when(vmSpec.getName()).thenReturn(vmName);
when(this.libvirtComputingResource.createVmFromSpec(vmSpec)).thenReturn(vmDef);
when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
try {
when(libvirtUtilitiesHelper.getConnectionByType(vmDef.getHvsType())).thenReturn(conn);
doThrow(InternalErrorException.class).when(this.libvirtComputingResource).createVbd(conn, vmSpec, vmName, vmDef);
} catch (final LibvirtException e) {
fail(e.getMessage());
} catch (final InternalErrorException e) {
fail(e.getMessage());
} catch (final URISyntaxException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertFalse(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByType(vmDef.getHvsType());
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
use of com.cloud.legacymodel.to.NicTO in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testPlugNicCommandMatchMack.
@Test
public void testPlugNicCommandMatchMack() {
final NicTO nic = Mockito.mock(NicTO.class);
final String instanceName = "Test";
final VirtualMachineType vmtype = VirtualMachineType.DomainRouter;
final PlugNicCommand command = new PlugNicCommand(nic, instanceName, vmtype);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final Connect conn = Mockito.mock(Connect.class);
final Domain vm = Mockito.mock(Domain.class);
final List<InterfaceDef> nics = new ArrayList<>();
final InterfaceDef intDef = Mockito.mock(InterfaceDef.class);
nics.add(intDef);
when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(this.libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
when(intDef.getDevName()).thenReturn("eth0");
when(intDef.getBrName()).thenReturn("br0");
when(intDef.getMacAddress()).thenReturn("00:00:00:00");
when(nic.getMac()).thenReturn("00:00:00:00");
try {
when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
when(this.libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertTrue(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
verify(this.libvirtComputingResource, times(1)).getDomain(conn, instanceName);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
use of com.cloud.legacymodel.to.NicTO in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testPlugNicCommandInternalError.
@Test
public void testPlugNicCommandInternalError() {
final NicTO nic = Mockito.mock(NicTO.class);
final String instanceName = "Test";
final VirtualMachineType vmtype = VirtualMachineType.DomainRouter;
final PlugNicCommand command = new PlugNicCommand(nic, instanceName, vmtype);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final Connect conn = Mockito.mock(Connect.class);
final Domain vm = Mockito.mock(Domain.class);
final VifDriver vifDriver = Mockito.mock(VifDriver.class);
final List<InterfaceDef> nics = new ArrayList<>();
final InterfaceDef intDef = Mockito.mock(InterfaceDef.class);
nics.add(intDef);
when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(this.libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
when(intDef.getDevName()).thenReturn("eth0");
when(intDef.getBrName()).thenReturn("br0");
when(intDef.getMacAddress()).thenReturn("00:00:00:00");
when(nic.getMac()).thenReturn("00:00:00:01");
try {
when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
when(this.libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm);
when(this.libvirtComputingResource.getVifDriver(nic.getType())).thenReturn(vifDriver);
when(vifDriver.plug(nic, "Default - VirtIO capable OS (64-bit)", "")).thenThrow(InternalErrorException.class);
} catch (final LibvirtException e) {
fail(e.getMessage());
} catch (final InternalErrorException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertFalse(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
verify(this.libvirtComputingResource, times(1)).getDomain(conn, instanceName);
verify(this.libvirtComputingResource, times(1)).getVifDriver(nic.getType());
verify(vifDriver, times(1)).plug(nic, "Default - VirtIO capable OS (64-bit)", "");
} catch (final LibvirtException e) {
fail(e.getMessage());
} catch (final InternalErrorException e) {
fail(e.getMessage());
}
}
use of com.cloud.legacymodel.to.NicTO in project cosmic by MissionCriticalCloud.
the class CitrixResourceBase method prepareNetworkElementCommand.
protected ExecutionResult prepareNetworkElementCommand(final SetNetworkACLCommand cmd) {
final Connection conn = getConnection();
final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
try {
final VM router = getVM(conn, routerName);
final NicTO nic = cmd.getNic();
if (nic != null) {
final VIF vif = getVifByMac(conn, router, nic.getMac());
if (vif == null) {
final String msg = "Prepare SetNetworkACL failed due to VIF is null for : " + nic.getMac() + " with routername: " + routerName;
s_logger.error(msg);
return new ExecutionResult(false, msg);
}
} else {
final String msg = "Prepare SetNetworkACL failed due to nic is null for : " + routerName;
s_logger.error(msg);
return new ExecutionResult(false, msg);
}
} catch (final Exception e) {
final String msg = "Prepare SetNetworkACL failed due to " + e.toString();
s_logger.error(msg, e);
return new ExecutionResult(false, msg);
}
return new ExecutionResult(true, null);
}
use of com.cloud.legacymodel.to.NicTO in project cosmic by MissionCriticalCloud.
the class HypervisorGuruBase method toVirtualMachineTO.
protected VirtualMachineTO toVirtualMachineTO(final VirtualMachineProfile vmProfile) {
final ServiceOffering offering = _serviceOfferingDao.findById(vmProfile.getId(), vmProfile.getServiceOfferingId());
final VirtualMachine vm = vmProfile.getVirtualMachine();
final Long minMemory = (long) (offering.getRamSize() / vmProfile.getMemoryOvercommitRatio());
final VirtualMachineTO to = new VirtualMachineTO(vm.getId(), vm.getInstanceName(), vm.getType(), offering.getCpu(), minMemory * 1024l * 1024l, offering.getRamSize() * 1024l * 1024l, null, null, vm.isHaEnabled(), vm.limitCpuUse(), vm.getVncPassword());
to.setBootArgs(vmProfile.getBootArgs());
final List<NicProfile> nicProfiles = vmProfile.getNics();
final NicTO[] nics = new NicTO[nicProfiles.size()];
int i = 0;
List<Long> vpcList = new ArrayList<>();
for (final NicProfile nicProfile : nicProfiles) {
nics[i++] = toNicTO(nicProfile);
if (TrafficType.Guest.equals(nicProfile.getTrafficType())) {
final NetworkVO network = _networkDao.findById(nicProfile.getNetworkId());
final Long vpcId = network.getVpcId();
if (vpcId != null) {
vpcList.add(network.getVpcId());
}
}
}
to.setNics(nics);
to.setDisks(vmProfile.getDisks().toArray(new DiskTO[vmProfile.getDisks().size()]));
if (vmProfile.getTemplate().getBits() == 32) {
to.setArch("i686");
} else {
to.setArch("x86_64");
}
final Map<String, String> detailsInVm = _userVmDetailsDao.listDetailsKeyPairs(vm.getId());
if (detailsInVm != null) {
to.setDetails(detailsInVm);
}
// Set GPU details
ServiceOfferingDetailsVO offeringDetail;
if ((offeringDetail = _serviceOfferingDetailsDao.findDetail(offering.getId(), GPU.Keys.vgpuType.toString())) != null) {
final ServiceOfferingDetailsVO groupName = _serviceOfferingDetailsDao.findDetail(offering.getId(), GPU.Keys.pciDevice.toString());
to.setGpuDevice(_resourceMgr.getGPUDevice(vm.getHostId(), groupName.getValue(), offeringDetail.getValue()));
}
// Workaround to make sure the TO has the UUID we need for Niciri integration
final VMInstanceVO vmInstance = _virtualMachineDao.findById(to.getId());
// check if XStools tools are present in the VM and dynamic scaling feature is enabled (per zone/global)
final Boolean isDynamicallyScalable = vmInstance.isDynamicallyScalable() && UserVmManager.EnableDynamicallyScaleVm.valueIn(vm.getDataCenterId());
to.setEnableDynamicallyScaleVm(isDynamicallyScalable);
to.setUuid(vmInstance.getUuid());
to.setManufacturer(vmInstance.getManufacturerString());
to.setOptimiseFor(vmInstance.getOptimiseFor());
to.setMaintenancePolicy(vmInstance.getMaintenancePolicy());
to.setBootMenuTimeout(vmInstance.getBootMenuTimeout());
to.setLastStartVersion(vmInstance.getLastStartVersion());
to.setLastStartDateTime(vmInstance.getLastStartDateTime());
to.setVmData(vmProfile.getVmData());
to.setConfigDriveLabel(vmProfile.getConfigDriveLabel());
to.setConfigDriveIsoRootFolder(vmProfile.getConfigDriveIsoRootFolder());
to.setConfigDriveIsoFile(vmProfile.getConfigDriveIsoFile());
LinkedHashSet<BootOrder> order = new LinkedHashSet<>();
if (vmInstance.getBootOrder() != null) {
for (final String dev : vmInstance.getBootOrder().split(",")) {
order.add(BootOrder.stringToEnum(dev));
}
} else {
// If bootorder column in DB is empty do default behaviour
order.add(BootOrder.CDROM);
order.add(BootOrder.HARDDISK);
}
to.setBootOrder(new ArrayList<>(order));
final MetadataTO metadataTO = new MetadataTO();
final DomainVO domain = _domainDao.findById(vm.getDomainId());
metadataTO.setDomainUuid(domain.getUuid());
metadataTO.setCosmicDomainName(domain.getName());
metadataTO.setCosmicDomainPath(domain.getPath());
metadataTO.setInstanceName(vm.getInstanceName());
metadataTO.setVmId(vm.getId());
metadataTO.setHostname(vm.getHostName());
final Map<String, String> resourceDetails = ApiDBUtils.getResourceDetails(vm.getId(), ResourceTag.ResourceObjectType.UserVm);
final Map<String, String> resourceTags = new HashMap<String, String>();
final List<String> vpcNameList = new LinkedList<>();
if (VirtualMachineType.User.equals(vm.getType())) {
final List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceTag.ResourceObjectType.UserVm, vm.getId());
for (final ResourceTag tag : tags) {
resourceTags.put("instance_" + tag.getKey(), tag.getValue());
}
}
for (final Long vpcId : vpcList) {
final VpcVO vpc = _vpcDao.findById(vpcId);
if (vpc != null) {
vpcNameList.add(vpc.getName());
}
final List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceTag.ResourceObjectType.Vpc, vpcId);
for (final ResourceTag tag : tags) {
if (tag != null) {
resourceTags.put("vpc_" + tag.getKey(), tag.getValue());
}
}
}
metadataTO.setResourceDetails(resourceDetails);
metadataTO.setResourceTags(resourceTags);
metadataTO.setVpcNameList(vpcNameList);
to.setMetadata(metadataTO);
return to;
}
Aggregations