use of com.xensource.xenapi.VIF in project cloudstack by apache.
the class XenServer610WrapperTest method testMigrateWithStorageSendCommand.
@Test
public void testMigrateWithStorageSendCommand() {
final String vmName = "small";
final String path = "/";
final String mac = "3c:15:c2:c4:4f:18";
final Connection conn = Mockito.mock(Connection.class);
final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
final VolumeTO volume1 = Mockito.mock(VolumeTO.class);
final VolumeTO volume2 = Mockito.mock(VolumeTO.class);
final SR sr1 = Mockito.mock(SR.class);
final SR sr2 = Mockito.mock(SR.class);
final VDI vdi1 = Mockito.mock(VDI.class);
final VDI vdi2 = Mockito.mock(VDI.class);
final NicTO nic1 = Mockito.mock(NicTO.class);
final NicTO nic2 = Mockito.mock(NicTO.class);
final Network network1 = Mockito.mock(Network.class);
final Network network2 = Mockito.mock(Network.class);
final List<Pair<VolumeTO, Object>> volumeToSr = new ArrayList<Pair<VolumeTO, Object>>();
volumeToSr.add(new Pair<VolumeTO, Object>(volume1, sr1));
volumeToSr.add(new Pair<VolumeTO, Object>(volume2, sr2));
final List<Pair<NicTO, Object>> nicToNetwork = new ArrayList<Pair<NicTO, Object>>();
nicToNetwork.add(new Pair<NicTO, Object>(nic1, network1));
nicToNetwork.add(new Pair<NicTO, Object>(nic2, network2));
final Map<String, String> token = new HashMap<String, String>();
final VIF vif1 = Mockito.mock(VIF.class);
final VIF vif2 = Mockito.mock(VIF.class);
final MigrateWithStorageSendCommand migrateStorageCommand = new MigrateWithStorageSendCommand(vmSpec, volumeToSr, nicToNetwork, token);
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
when(xenServer610Resource.getConnection()).thenReturn(conn);
when(vmSpec.getName()).thenReturn(vmName);
when(volume1.getPath()).thenReturn(path);
when(volume2.getPath()).thenReturn(path);
when(nic1.getMac()).thenReturn(mac);
when(nic2.getMac()).thenReturn(mac);
when(xenServer610Resource.getVDIbyUuid(conn, volume1.getPath())).thenReturn(vdi1);
when(xenServer610Resource.getVDIbyUuid(conn, volume2.getPath())).thenReturn(vdi2);
try {
when(xenServer610Resource.getVifByMac(conn, null, nic1.getMac())).thenReturn(vif1);
when(xenServer610Resource.getVifByMac(conn, null, nic2.getMac())).thenReturn(vif2);
} catch (final XenAPIException e) {
fail(e.getMessage());
} catch (final XmlRpcException e) {
fail(e.getMessage());
}
final Answer answer = wrapper.execute(migrateStorageCommand, xenServer610Resource);
verify(xenServer610Resource, times(1)).getConnection();
try {
verify(xenServer610Resource, times(2)).getVDIbyUuid(conn, volume1.getPath());
verify(xenServer610Resource, times(2)).getVifByMac(conn, null, nic1.getMac());
} catch (final XenAPIException e) {
fail(e.getMessage());
} catch (final XmlRpcException e) {
fail(e.getMessage());
}
assertFalse(answer.getResult());
}
use of com.xensource.xenapi.VIF in project cosmic by MissionCriticalCloud.
the class CitrixResourceBase method getLowestAvailableVIFDeviceNum.
public String getLowestAvailableVIFDeviceNum(final Connection conn, final VM vm) {
String vmName = "";
try {
vmName = vm.getNameLabel(conn);
final List<Integer> usedDeviceNums = new ArrayList<>();
final Set<VIF> vifs = vm.getVIFs(conn);
final Iterator<VIF> vifIter = vifs.iterator();
while (vifIter.hasNext()) {
final VIF vif = vifIter.next();
try {
final String deviceId = vif.getDevice(conn);
if (vm.getIsControlDomain(conn) || vif.getCurrentlyAttached(conn)) {
usedDeviceNums.add(Integer.valueOf(deviceId));
} else {
s_logger.debug("Found unplugged VIF " + deviceId + " in VM " + vmName + " destroy it");
vif.destroy(conn);
}
} catch (final NumberFormatException e) {
final String msg = "Obtained an invalid value for an allocated VIF device number for VM: " + vmName;
s_logger.debug(msg, e);
throw new CloudRuntimeException(msg);
}
}
for (Integer i = 0; i < this._maxNics; i++) {
if (!usedDeviceNums.contains(i)) {
s_logger.debug("Lowest available Vif device number: " + i + " for VM: " + vmName);
return i.toString();
}
}
} catch (final XmlRpcException e) {
final String msg = "Caught XmlRpcException: " + e.getMessage();
s_logger.warn(msg, e);
} catch (final XenAPIException e) {
final String msg = "Caught XenAPIException: " + e.toString();
s_logger.warn(msg, e);
}
throw new CloudRuntimeException("Could not find available VIF slot in VM with name: " + vmName);
}
use of com.xensource.xenapi.VIF in project cosmic by MissionCriticalCloud.
the class CitrixResourceBase method handleVmStartFailure.
public String handleVmStartFailure(final Connection conn, final String vmName, final VM vm, final String message, final Throwable th) {
final String msg = "Unable to start " + vmName + " due to " + message;
s_logger.warn(msg, th);
if (vm == null) {
return msg;
}
try {
final VM.Record vmr = vm.getRecord(conn);
final List<Network> networks = new ArrayList<>();
for (final VIF vif : vmr.VIFs) {
try {
final VIF.Record rec = vif.getRecord(conn);
if (rec != null) {
networks.add(rec.network);
} else {
s_logger.warn("Unable to cleanup VIF: " + vif.toWireString() + " As vif record is null");
}
} catch (final Exception e) {
s_logger.warn("Unable to cleanup VIF", e);
}
}
if (vmr.powerState == VmPowerState.RUNNING) {
try {
vm.hardShutdown(conn);
} catch (final Exception e) {
s_logger.warn("VM hardshutdown failed due to ", e);
}
}
if (vm.getPowerState(conn) == VmPowerState.HALTED) {
try {
vm.destroy(conn);
} catch (final Exception e) {
s_logger.warn("VM destroy failed due to ", e);
}
}
for (final VBD vbd : vmr.VBDs) {
try {
vbd.unplug(conn);
vbd.destroy(conn);
} catch (final Exception e) {
s_logger.warn("Unable to clean up VBD due to ", e);
}
}
for (final VIF vif : vmr.VIFs) {
try {
vif.unplug(conn);
vif.destroy(conn);
} catch (final Exception e) {
s_logger.warn("Unable to cleanup VIF", e);
}
}
for (final Network network : networks) {
if (network.getNameLabel(conn).startsWith("VLAN")) {
disableVlanNetwork(conn, network);
}
}
} catch (final Exception e) {
s_logger.warn("VM getRecord failed due to ", e);
}
return msg;
}
use of com.xensource.xenapi.VIF in project cosmic by MissionCriticalCloud.
the class XenServer610MigrateWithStorageCommandWrapper method execute.
@Override
public Answer execute(final MigrateWithStorageCommand command, final XenServer610Resource xenServer610Resource) {
final Connection connection = xenServer610Resource.getConnection();
final VirtualMachineTO vmSpec = command.getVirtualMachine();
final List<Pair<VolumeTO, StorageFilerTO>> volumeToFiler = command.getVolumeToFilerAsList();
final String vmName = vmSpec.getName();
Task task = null;
final XsHost xsHost = xenServer610Resource.getHost();
final String uuid = xsHost.getUuid();
try {
xenServer610Resource.prepareISO(connection, vmName, null, null);
// Get the list of networks and recreate VLAN, if required.
for (final NicTO nicTo : vmSpec.getNics()) {
xenServer610Resource.getNetwork(connection, nicTo);
}
final Map<String, String> other = new HashMap<>();
other.put("live", "true");
final XsLocalNetwork nativeNetworkForTraffic = xenServer610Resource.getNativeNetworkForTraffic(connection, TrafficType.Storage, null);
final Network networkForSm = nativeNetworkForTraffic.getNetwork();
// Create the vif map. The vm stays in the same cluster so we have to pass an empty vif map.
final Map<VIF, Network> vifMap = new HashMap<>();
final Map<VDI, SR> vdiMap = new HashMap<>();
for (final Pair<VolumeTO, StorageFilerTO> entry : volumeToFiler) {
final VolumeTO volume = entry.first();
final StorageFilerTO sotrageFiler = entry.second();
vdiMap.put(xenServer610Resource.getVDIbyUuid(connection, volume.getPath()), xenServer610Resource.getStorageRepository(connection, sotrageFiler.getUuid()));
}
// Get the vm to migrate.
final Set<VM> vms = VM.getByNameLabel(connection, vmSpec.getName());
final VM vmToMigrate = vms.iterator().next();
// Check migration with storage is possible.
final Host host = Host.getByUuid(connection, uuid);
final Map<String, String> token = host.migrateReceive(connection, networkForSm, other);
task = vmToMigrate.assertCanMigrateAsync(connection, token, true, vdiMap, vifMap, other);
try {
// poll every 1 seconds
final long timeout = xenServer610Resource.getMigrateWait() * 1000L;
xenServer610Resource.waitForTask(connection, task, 1000, timeout);
xenServer610Resource.checkForSuccess(connection, task);
} catch (final Types.HandleInvalid e) {
s_logger.error("Error while checking if vm " + vmName + " can be migrated to the destination host " + host, e);
throw new CloudRuntimeException("Error while checking if vm " + vmName + " can be migrated to the " + "destination host " + host, e);
}
// Migrate now.
task = vmToMigrate.migrateSendAsync(connection, token, true, vdiMap, vifMap, other);
try {
// poll every 1 seconds.
final long timeout = xenServer610Resource.getMigrateWait() * 1000L;
xenServer610Resource.waitForTask(connection, task, 1000, timeout);
xenServer610Resource.checkForSuccess(connection, task);
} catch (final Types.HandleInvalid e) {
s_logger.error("Error while migrating vm " + vmName + " to the destination host " + host, e);
throw new CloudRuntimeException("Error while migrating vm " + vmName + " to the destination host " + host, e);
}
// Volume paths would have changed. Return that information.
final List<VolumeObjectTO> volumeToList = xenServer610Resource.getUpdatedVolumePathsOfMigratedVm(connection, vmToMigrate, vmSpec.getDisks());
vmToMigrate.setAffinity(connection, host);
return new MigrateWithStorageAnswer(command, volumeToList);
} catch (final Exception e) {
s_logger.warn("Catch Exception " + e.getClass().getName() + ". Storage motion failed due to " + e.toString(), e);
return new MigrateWithStorageAnswer(command, e);
} finally {
if (task != null) {
try {
task.destroy(connection);
} catch (final Exception e) {
s_logger.debug("Unable to destroy task " + task.toString() + " on host " + uuid + " due to " + e.toString());
}
}
}
}
use of com.xensource.xenapi.VIF in project cosmic by MissionCriticalCloud.
the class CitrixPlugNicCommandWrapper method execute.
@Override
public Answer execute(final PlugNicCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
final String vmName = command.getVmName();
try {
final Set<VM> vms = VM.getByNameLabel(conn, vmName);
if (vms == null || vms.isEmpty()) {
return new PlugNicAnswer(command, false, "Can not find VM " + vmName);
}
final VM vm = vms.iterator().next();
final NicTO nic = command.getNic();
String mac = nic.getMac();
final Set<VIF> routerVIFs = vm.getVIFs(conn);
mac = mac.trim();
int counter = 0;
for (final VIF vif : routerVIFs) {
final String lmac = vif.getMAC(conn);
if (lmac.trim().equals(mac)) {
counter++;
}
}
// redundant.
if (counter > 2) {
final String msg = " Plug Nic failed due to a VIF with the same mac " + nic.getMac() + " exists in more than 2 routers.";
s_logger.error(msg);
return new PlugNicAnswer(command, false, msg);
}
// Wilder Rodrigues - replaced this code with the code above.
// VIF vif = getVifByMac(conn, vm, nic.getMac());
// if (vif != null) {
// final String msg = " Plug Nic failed due to a VIF with the same mac " + nic.getMac() + " exists";
// s_logger.warn(msg);
// return new PlugNicAnswer(cmd, false, msg);
// }
final VIF vif = citrixResourceBase.createVif(conn, vmName, vm, null, nic);
// vif = createVif(conn, vmName, vm, null, nic);
vif.plug(conn);
return new PlugNicAnswer(command, true, "success");
} catch (final Exception e) {
final String msg = " Plug Nic failed due to " + e.toString();
s_logger.error(msg, e);
return new PlugNicAnswer(command, false, msg);
}
}
Aggregations