Search in sources :

Example 6 with VIF

use of com.xensource.xenapi.VIF in project cloudstack by apache.

the class CitrixResourceBase method cleanUpTmpDomVif.

public void cleanUpTmpDomVif(final Connection conn, final Network nw) throws XenAPIException, XmlRpcException {
    final Pair<VM, VM.Record> vm = getControlDomain(conn);
    final VM dom0 = vm.first();
    final Set<VIF> dom0Vifs = dom0.getVIFs(conn);
    for (final VIF v : dom0Vifs) {
        String vifName = "unknown";
        try {
            final VIF.Record vifr = v.getRecord(conn);
            if (v.getNetwork(conn).getUuid(conn).equals(nw.getUuid(conn))) {
                if (vifr != null) {
                    final Map<String, String> config = vifr.otherConfig;
                    vifName = config.get("nameLabel");
                }
                s_logger.debug("A VIF in dom0 for the network is found - so destroy the vif");
                v.destroy(conn);
                s_logger.debug("Destroy temp dom0 vif" + vifName + " success");
            }
        } catch (final Exception e) {
            s_logger.warn("Destroy temp dom0 vif " + vifName + "failed", e);
        }
    }
}
Also used : VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 7 with VIF

use of com.xensource.xenapi.VIF in project cloudstack by apache.

the class CitrixResourceBase method setupLinkLocalNetwork.

public void setupLinkLocalNetwork(final Connection conn) {
    try {
        final Network.Record rec = new Network.Record();
        final Set<Network> networks = Network.getByNameLabel(conn, _linkLocalPrivateNetworkName);
        Network linkLocal = null;
        if (networks.size() == 0) {
            rec.nameDescription = "link local network used by system vms";
            rec.nameLabel = _linkLocalPrivateNetworkName;
            final Map<String, String> configs = new HashMap<String, String>();
            configs.put("ip_begin", NetUtils.getLinkLocalGateway());
            configs.put("ip_end", NetUtils.getLinkLocalIpEnd());
            configs.put("netmask", NetUtils.getLinkLocalNetMask());
            configs.put("vswitch-disable-in-band", "true");
            rec.otherConfig = configs;
            linkLocal = Network.create(conn, rec);
        } else {
            linkLocal = networks.iterator().next();
            if (!linkLocal.getOtherConfig(conn).containsKey("vswitch-disable-in-band")) {
                linkLocal.addToOtherConfig(conn, "vswitch-disable-in-band", "true");
            }
        }
        /* Make sure there is a physical bridge on this network */
        VIF dom0vif = null;
        final Pair<VM, VM.Record> vm = getControlDomain(conn);
        final VM dom0 = vm.first();
        final Set<VIF> vifs = dom0.getVIFs(conn);
        if (vifs.size() != 0) {
            for (final VIF vif : vifs) {
                final Map<String, String> otherConfig = vif.getOtherConfig(conn);
                if (otherConfig != null) {
                    final String nameLabel = otherConfig.get("nameLabel");
                    if (nameLabel != null && nameLabel.equalsIgnoreCase("link_local_network_vif")) {
                        dom0vif = vif;
                    }
                }
            }
        }
        /* create temp VIF0 */
        if (dom0vif == null) {
            s_logger.debug("Can't find a vif on dom0 for link local, creating a new one");
            final VIF.Record vifr = new VIF.Record();
            vifr.VM = dom0;
            vifr.device = getLowestAvailableVIFDeviceNum(conn, dom0);
            if (vifr.device == null) {
                s_logger.debug("Failed to create link local network, no vif available");
                return;
            }
            final Map<String, String> config = new HashMap<String, String>();
            config.put("nameLabel", "link_local_network_vif");
            vifr.otherConfig = config;
            vifr.MAC = "FE:FF:FF:FF:FF:FF";
            vifr.network = linkLocal;
            vifr.lockingMode = Types.VifLockingMode.NETWORK_DEFAULT;
            dom0vif = VIF.create(conn, vifr);
            plugDom0Vif(conn, dom0vif);
        } else {
            s_logger.debug("already have a vif on dom0 for link local network");
            if (!dom0vif.getCurrentlyAttached(conn)) {
                plugDom0Vif(conn, dom0vif);
            }
        }
        final String brName = linkLocal.getBridge(conn);
        callHostPlugin(conn, "vmops", "setLinkLocalIP", "brName", brName);
        _host.setLinkLocalNetwork(linkLocal.getUuid(conn));
    } catch (final XenAPIException e) {
        s_logger.warn("Unable to create local link network", e);
        throw new CloudRuntimeException("Unable to create local link network due to " + e.toString(), e);
    } catch (final XmlRpcException e) {
        s_logger.warn("Unable to create local link network", e);
        throw new CloudRuntimeException("Unable to create local link network due to " + e.toString(), e);
    }
}
Also used : HashMap(java.util.HashMap) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VIF(com.xensource.xenapi.VIF) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.xensource.xenapi.Network) VM(com.xensource.xenapi.VM) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 8 with VIF

use of com.xensource.xenapi.VIF in project cloudstack by apache.

the class CitrixResourceBase method getCorrectVif.

protected VIF getCorrectVif(final Connection conn, final VM router, final IpAddressTO ip) throws XmlRpcException, XenAPIException {
    final NicTO nic = new NicTO();
    nic.setType(ip.getTrafficType());
    nic.setName(ip.getNetworkName());
    if (ip.getBroadcastUri() == null) {
        nic.setBroadcastType(BroadcastDomainType.Native);
    } else {
        final URI uri = BroadcastDomainType.fromString(ip.getBroadcastUri());
        nic.setBroadcastType(BroadcastDomainType.getSchemeValue(uri));
        nic.setBroadcastUri(uri);
    }
    final Network network = getNetwork(conn, nic);
    // Determine the correct VIF on DomR to associate/disassociate the
    // IP address with
    final Set<VIF> routerVIFs = router.getVIFs(conn);
    for (final VIF vif : routerVIFs) {
        final Network vifNetwork = vif.getNetwork(conn);
        if (vifNetwork.getUuid(conn).equals(network.getUuid(conn))) {
            return vif;
        }
    }
    return null;
}
Also used : VIF(com.xensource.xenapi.VIF) Network(com.xensource.xenapi.Network) URI(java.net.URI) NicTO(com.cloud.agent.api.to.NicTO)

Example 9 with VIF

use of com.xensource.xenapi.VIF in project cloudstack by apache.

the class XenServer610MigrateWithStorageSendCommandWrapper method execute.

@Override
public Answer execute(final MigrateWithStorageSendCommand command, final XenServer610Resource xenServer610Resource) {
    final Connection connection = xenServer610Resource.getConnection();
    final VirtualMachineTO vmSpec = command.getVirtualMachine();
    final List<Pair<VolumeTO, Object>> volumeToSr = command.getVolumeToSr();
    final List<Pair<NicTO, Object>> nicToNetwork = command.getNicToNetwork();
    final Map<String, String> token = command.getToken();
    final String vmName = vmSpec.getName();
    Task task = null;
    try {
        // In a cluster management server setup, the migrate with storage receive and send
        // commands and answers may have to be forwarded to another management server. This
        // happens when the host/resource on which the command has to be executed is owned
        // by the second management server. The serialization/deserialization of the command
        // and answers fails as the xapi SR and Network class type isn't understand by the
        // agent attache. Seriliaze the SR and Network objects here to a string and pass in
        // the answer object. It'll be deserialzed and object created in migrate with
        // storage send command execution.
        Gson gson = new Gson();
        final Map<String, String> other = new HashMap<String, String>();
        other.put("live", "true");
        // Create the vdi map which tells what volumes of the vm need to go
        // on which sr on the destination.
        final Map<VDI, SR> vdiMap = new HashMap<VDI, SR>();
        for (final Pair<VolumeTO, Object> entry : volumeToSr) {
            if (entry.second() instanceof SR) {
                final SR sr = (SR) entry.second();
                final VDI vdi = xenServer610Resource.getVDIbyUuid(connection, entry.first().getPath());
                vdiMap.put(vdi, sr);
            } else {
                throw new CloudRuntimeException("The object " + entry.second() + " passed is not of type SR.");
            }
        }
        final Set<VM> vms = VM.getByNameLabel(connection, vmSpec.getName());
        VM vmToMigrate = null;
        if (vms != null) {
            vmToMigrate = vms.iterator().next();
        }
        // Create the vif map.
        final Map<VIF, Network> vifMap = new HashMap<VIF, Network>();
        for (final Pair<NicTO, Object> entry : nicToNetwork) {
            if (entry.second() instanceof Network) {
                final Network network = (Network) entry.second();
                final VIF vif = xenServer610Resource.getVifByMac(connection, vmToMigrate, entry.first().getMac());
                vifMap.put(vif, network);
            } else {
                throw new CloudRuntimeException("The object " + entry.second() + " passed is not of type Network.");
            }
        }
        // Check migration with storage is possible.
        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.", e);
            throw new CloudRuntimeException("Error while checking if vm " + vmName + " can be migrated.", 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, e);
            throw new CloudRuntimeException("Error while migrating vm " + vmName, e);
        }
        final Set<VolumeTO> volumeToSet = null;
        return new MigrateWithStorageSendAnswer(command, volumeToSet);
    } catch (final CloudRuntimeException e) {
        s_logger.error("Migration of vm " + vmName + " with storage failed due to " + e.toString(), e);
        return new MigrateWithStorageSendAnswer(command, e);
    } catch (final Exception e) {
        s_logger.error("Migration of vm " + vmName + " with storage failed due to " + e.toString(), e);
        return new MigrateWithStorageSendAnswer(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 " + xenServer610Resource.getHost().getUuid() + " due to " + e.toString());
            }
        }
    }
}
Also used : Types(com.xensource.xenapi.Types) Task(com.xensource.xenapi.Task) MigrateWithStorageSendAnswer(com.cloud.agent.api.MigrateWithStorageSendAnswer) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) VolumeTO(com.cloud.agent.api.to.VolumeTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.xensource.xenapi.Network) VDI(com.xensource.xenapi.VDI) Pair(com.cloud.utils.Pair) SR(com.xensource.xenapi.SR) NicTO(com.cloud.agent.api.to.NicTO) Connection(com.xensource.xenapi.Connection) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM)

Example 10 with VIF

use of com.xensource.xenapi.VIF in project cloudstack by apache.

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<String, String>();
        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<VIF, Network>();
        final Map<VDI, SR> vdiMap = new HashMap<VDI, SR>();
        for (final Pair<VolumeTO, StorageFilerTO> entry : volumeToFiler) {
            final StorageFilerTO storageFiler = entry.second();
            final VolumeTO volume = entry.first();
            vdiMap.put(xenServer610Resource.getVDIbyUuid(connection, volume.getPath()), xenServer610Resource.getStorageRepository(connection, storageFiler.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.agent.api.to.StorageFilerTO) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) VolumeTO(com.cloud.agent.api.to.VolumeTO) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) Pair(com.cloud.utils.Pair) NicTO(com.cloud.agent.api.to.NicTO) SR(com.xensource.xenapi.SR) MigrateWithStorageAnswer(com.cloud.agent.api.MigrateWithStorageAnswer) Connection(com.xensource.xenapi.Connection) Host(com.xensource.xenapi.Host) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM)

Aggregations

VIF (com.xensource.xenapi.VIF)19 VM (com.xensource.xenapi.VM)14 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 Connection (com.xensource.xenapi.Connection)12 XenAPIException (com.xensource.xenapi.Types.XenAPIException)12 XmlRpcException (org.apache.xmlrpc.XmlRpcException)11 NicTO (com.cloud.agent.api.to.NicTO)10 Network (com.xensource.xenapi.Network)10 InternalErrorException (com.cloud.exception.InternalErrorException)8 IOException (java.io.IOException)8 MalformedURLException (java.net.MalformedURLException)8 URISyntaxException (java.net.URISyntaxException)8 TimeoutException (java.util.concurrent.TimeoutException)8 ConfigurationException (javax.naming.ConfigurationException)8 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)8 SAXException (org.xml.sax.SAXException)8 ExecutionResult (com.cloud.utils.ExecutionResult)6 URLConnection (java.net.URLConnection)6 HashMap (java.util.HashMap)5 IpAddressTO (com.cloud.agent.api.to.IpAddressTO)4