Search in sources :

Example 11 with VIF

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

the class CitrixStopCommandWrapper method execute.

@Override
public Answer execute(final StopCommand command, final CitrixResourceBase citrixResourceBase) {
    final String vmName = command.getVmName();
    String platformstring = null;
    try {
        final Connection conn = citrixResourceBase.getConnection();
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        // stop vm which is running on this host or is in halted state
        final Iterator<VM> iter = vms.iterator();
        while (iter.hasNext()) {
            final VM vm = iter.next();
            final VM.Record vmr = vm.getRecord(conn);
            if (vmr.powerState != VmPowerState.RUNNING) {
                continue;
            }
            if (citrixResourceBase.isRefNull(vmr.residentOn)) {
                continue;
            }
            if (vmr.residentOn.getUuid(conn).equals(citrixResourceBase.getHost().getUuid())) {
                continue;
            }
            iter.remove();
        }
        if (vms.size() == 0) {
            return new StopAnswer(command, "VM does not exist", true);
        }
        for (final VM vm : vms) {
            final VM.Record vmr = vm.getRecord(conn);
            platformstring = StringUtils.mapToString(vmr.platform);
            if (vmr.isControlDomain) {
                final String msg = "Tring to Shutdown control domain";
                s_logger.warn(msg);
                return new StopAnswer(command, msg, false);
            }
            if (vmr.powerState == VmPowerState.RUNNING && !citrixResourceBase.isRefNull(vmr.residentOn) && !vmr.residentOn.getUuid(conn).equals(citrixResourceBase.getHost().getUuid())) {
                final String msg = "Stop Vm " + vmName + " failed due to this vm is not running on this host: " + citrixResourceBase.getHost().getUuid() + " but host:" + vmr.residentOn.getUuid(conn);
                s_logger.warn(msg);
                return new StopAnswer(command, msg, platformstring, false);
            }
            if (command.checkBeforeCleanup() && vmr.powerState == VmPowerState.RUNNING) {
                final String msg = "Vm " + vmName + " is running on host and checkBeforeCleanup flag is set, so bailing out";
                s_logger.debug(msg);
                return new StopAnswer(command, msg, false);
            }
            s_logger.debug("9. The VM " + vmName + " is in Stopping state");
            try {
                if (vmr.powerState == VmPowerState.RUNNING) {
                    /* when stop a vm, set affinity to current xenserver */
                    vm.setAffinity(conn, vm.getResidentOn(conn));
                    if (citrixResourceBase.canBridgeFirewall()) {
                        final String result = citrixResourceBase.callHostPlugin(conn, "vmops", "destroy_network_rules_for_vm", "vmName", command.getVmName());
                        if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
                            s_logger.warn("Failed to remove  network rules for vm " + command.getVmName());
                        } else {
                            s_logger.info("Removed  network rules for vm " + command.getVmName());
                        }
                    }
                    citrixResourceBase.shutdownVM(conn, vm, vmName);
                }
            } catch (final Exception e) {
                final String msg = "Catch exception " + e.getClass().getName() + " when stop VM:" + command.getVmName() + " due to " + e.toString();
                s_logger.debug(msg);
                return new StopAnswer(command, msg, platformstring, false);
            } finally {
                try {
                    if (vm.getPowerState(conn) == VmPowerState.HALTED) {
                        Set<VGPU> vGPUs = null;
                        // Get updated GPU details
                        try {
                            vGPUs = vm.getVGPUs(conn);
                        } catch (final XenAPIException e2) {
                            s_logger.debug("VM " + vmName + " does not have GPU support.");
                        }
                        if (vGPUs != null && !vGPUs.isEmpty()) {
                            final HashMap<String, HashMap<String, VgpuTypesInfo>> groupDetails = citrixResourceBase.getGPUGroupDetails(conn);
                            command.setGpuDevice(new GPUDeviceTO(null, null, groupDetails));
                        }
                        final Set<VIF> vifs = vm.getVIFs(conn);
                        final List<Network> networks = new ArrayList<Network>();
                        for (final VIF vif : vifs) {
                            networks.add(vif.getNetwork(conn));
                        }
                        vm.destroy(conn);
                        final SR sr = citrixResourceBase.getISOSRbyVmName(conn, command.getVmName());
                        citrixResourceBase.removeSR(conn, sr);
                        // anymore
                        for (final Network network : networks) {
                            try {
                                if (network.getNameLabel(conn).startsWith("VLAN")) {
                                    citrixResourceBase.disableVlanNetwork(conn, network);
                                }
                            } catch (final Exception e) {
                            // network might be destroyed by other host
                            }
                        }
                        return new StopAnswer(command, "Stop VM " + vmName + " Succeed", platformstring, true);
                    }
                } catch (final Exception e) {
                    final String msg = "VM destroy failed in Stop " + vmName + " Command due to " + e.getMessage();
                    s_logger.warn(msg, e);
                } finally {
                    s_logger.debug("10. The VM " + vmName + " is in Stopped state");
                }
            }
        }
    } catch (final Exception e) {
        final String msg = "Stop Vm " + vmName + " fail due to " + e.toString();
        s_logger.warn(msg, e);
        return new StopAnswer(command, msg, platformstring, false);
    }
    return new StopAnswer(command, "Stop VM failed", platformstring, false);
}
Also used : HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) XenAPIException(com.xensource.xenapi.Types.XenAPIException) GPUDeviceTO(com.cloud.agent.api.to.GPUDeviceTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Network(com.xensource.xenapi.Network) StopAnswer(com.cloud.agent.api.StopAnswer) VGPU(com.xensource.xenapi.VGPU) SR(com.xensource.xenapi.SR)

Example 12 with VIF

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

the class CitrixUnPlugNicCommandWrapper method execute.

@Override
public Answer execute(final UnPlugNicCommand 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 UnPlugNicAnswer(command, false, "Can not find VM " + vmName);
        }
        final VM vm = vms.iterator().next();
        final NicTO nic = command.getNic();
        final String mac = nic.getMac();
        final VIF vif = citrixResourceBase.getVifByMac(conn, vm, mac);
        if (vif != null) {
            vif.unplug(conn);
            final Network network = vif.getNetwork(conn);
            vif.destroy(conn);
            try {
                if (network.getNameLabel(conn).startsWith("VLAN")) {
                    citrixResourceBase.disableVlanNetwork(conn, network);
                }
            } catch (final Exception e) {
            }
        }
        return new UnPlugNicAnswer(command, true, "success");
    } catch (final Exception e) {
        final String msg = " UnPlug Nic failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new UnPlugNicAnswer(command, false, msg);
    }
}
Also used : VIF(com.xensource.xenapi.VIF) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) VM(com.xensource.xenapi.VM) Network(com.xensource.xenapi.Network) Connection(com.xensource.xenapi.Connection) NicTO(com.cloud.agent.api.to.NicTO)

Example 13 with VIF

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

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 String deviceId = citrixResourceBase.getLowestAvailableVIFDeviceNum(conn, vm);
        nic.setDeviceId(Integer.parseInt(deviceId));
        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.agent.api.PlugNicAnswer) NicTO(com.cloud.agent.api.to.NicTO)

Example 14 with VIF

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

the class CitrixResourceBase method createVif.

public VIF createVif(final Connection conn, final String vmName, final VM vm, final VirtualMachineTO vmSpec, final NicTO nic) throws XmlRpcException, XenAPIException {
    assert nic.getUuid() != null : "Nic should have a uuid value";
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Creating VIF for " + vmName + " on nic " + nic);
    }
    VIF.Record vifr = new VIF.Record();
    vifr.VM = vm;
    vifr.device = Integer.toString(nic.getDeviceId());
    vifr.MAC = nic.getMac();
    // Nicira needs these IDs to find the NIC
    vifr.otherConfig = new HashMap<String, String>();
    vifr.otherConfig.put("nicira-iface-id", nic.getUuid());
    vifr.otherConfig.put("nicira-vm-id", vm.getUuid(conn));
    // Provide XAPI with the cloudstack vm and nic uids.
    vifr.otherConfig.put("cloudstack-nic-id", nic.getUuid());
    if (vmSpec != null) {
        vifr.otherConfig.put("cloudstack-vm-id", vmSpec.getUuid());
    }
    // OVS plugin looks at network UUID in the vif 'otherconfig' details to
    // group VIF's & tunnel ports as part of tier
    // when bridge is setup for distributed routing
    vifr.otherConfig.put("cloudstack-network-id", nic.getNetworkUuid());
    // Nuage Vsp needs Virtual Router IP to be passed in the otherconfig
    // get the virtual router IP information from broadcast uri
    final URI broadcastUri = nic.getBroadcastUri();
    if (broadcastUri != null && broadcastUri.getScheme().equalsIgnoreCase(Networks.BroadcastDomainType.Vsp.scheme())) {
        final String path = broadcastUri.getPath();
        vifr.otherConfig.put("vsp-vr-ip", path.substring(1));
    }
    vifr.network = getNetwork(conn, nic);
    if (nic.getNetworkRateMbps() != null && nic.getNetworkRateMbps().intValue() != -1) {
        vifr.qosAlgorithmType = "ratelimit";
        vifr.qosAlgorithmParams = new HashMap<String, String>();
        // convert mbs to kilobyte per second
        vifr.qosAlgorithmParams.put("kbps", Integer.toString(nic.getNetworkRateMbps() * 128));
    }
    vifr.lockingMode = Types.VifLockingMode.NETWORK_DEFAULT;
    final VIF vif = VIF.create(conn, vifr);
    if (s_logger.isDebugEnabled()) {
        vifr = vif.getRecord(conn);
        if (vifr != null) {
            s_logger.debug("Created a vif " + vifr.uuid + " on " + nic.getDeviceId());
        }
    }
    return vif;
}
Also used : VIF(com.xensource.xenapi.VIF) URI(java.net.URI)

Example 15 with VIF

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

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);
            }
            nic.setDeviceId(Integer.parseInt(vif.getDevice(conn)));
        } 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);
}
Also used : VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) URLConnection(java.net.URLConnection) ExecutionResult(com.cloud.utils.ExecutionResult) 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) NicTO(com.cloud.agent.api.to.NicTO)

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