Search in sources :

Example 26 with VIF

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());
}
Also used : HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) Answer(com.cloud.agent.api.Answer) VolumeTO(com.cloud.agent.api.to.VolumeTO) VIF(com.xensource.xenapi.VIF) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) VDI(com.xensource.xenapi.VDI) MigrateWithStorageSendCommand(com.cloud.agent.api.MigrateWithStorageSendCommand) XmlRpcException(org.apache.xmlrpc.XmlRpcException) SR(com.xensource.xenapi.SR) NicTO(com.cloud.agent.api.to.NicTO) Pair(com.cloud.utils.Pair) Test(org.junit.Test)

Example 27 with VIF

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);
}
Also used : VIF(com.xensource.xenapi.VIF) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ArrayList(java.util.ArrayList) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 28 with VIF

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;
}
Also used : VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Network(com.xensource.xenapi.Network) ArrayList(java.util.ArrayList) VBD(com.xensource.xenapi.VBD) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 29 with VIF

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());
            }
        }
    }
}
Also used : Types(com.xensource.xenapi.Types) Task(com.xensource.xenapi.Task) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) HashMap(java.util.HashMap) StorageFilerTO(com.cloud.legacymodel.to.StorageFilerTO) VirtualMachineTO(com.cloud.legacymodel.to.VirtualMachineTO) VolumeTO(com.cloud.legacymodel.to.VolumeTO) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) Pair(com.cloud.legacymodel.utils.Pair) NicTO(com.cloud.legacymodel.to.NicTO) SR(com.xensource.xenapi.SR) MigrateWithStorageAnswer(com.cloud.legacymodel.communication.answer.MigrateWithStorageAnswer) Connection(com.xensource.xenapi.Connection) Host(com.xensource.xenapi.Host) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM)

Example 30 with VIF

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);
    }
}
Also used : VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) PlugNicAnswer(com.cloud.legacymodel.communication.answer.PlugNicAnswer) NicTO(com.cloud.legacymodel.to.NicTO)

Aggregations

VIF (com.xensource.xenapi.VIF)31 VM (com.xensource.xenapi.VM)22 Connection (com.xensource.xenapi.Connection)19 XenAPIException (com.xensource.xenapi.Types.XenAPIException)18 Network (com.xensource.xenapi.Network)17 XmlRpcException (org.apache.xmlrpc.XmlRpcException)17 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 NicTO (com.cloud.agent.api.to.NicTO)10 IOException (java.io.IOException)10 MalformedURLException (java.net.MalformedURLException)10 URISyntaxException (java.net.URISyntaxException)10 HashMap (java.util.HashMap)10 TimeoutException (java.util.concurrent.TimeoutException)10 ConfigurationException (javax.naming.ConfigurationException)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)10 SAXException (org.xml.sax.SAXException)10 InternalErrorException (com.cloud.exception.InternalErrorException)8 SR (com.xensource.xenapi.SR)8 ArrayList (java.util.ArrayList)8 URLConnection (java.net.URLConnection)7