Search in sources :

Example 51 with Network

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

the class CitrixOvsDestroyTunnelCommandWrapper method execute.

@Override
public Answer execute(final OvsDestroyTunnelCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    try {
        final Network nw = citrixResourceBase.findOrCreateTunnelNetwork(conn, command.getBridgeName());
        if (nw == null) {
            s_logger.warn("Unable to find tunnel network for GRE key:" + command.getBridgeName());
            return new Answer(command, false, "No network found");
        }
        final String bridge = nw.getBridge(conn);
        final String result = citrixResourceBase.callHostPlugin(conn, "ovstunnel", "destroy_tunnel", "bridge", bridge, "in_port", command.getInPortName());
        if (result.equalsIgnoreCase("SUCCESS")) {
            return new Answer(command, true, result);
        } else {
            return new Answer(command, false, result);
        }
    } catch (final Exception e) {
        s_logger.warn("caught execption when destroy ovs tunnel", e);
        return new Answer(command, false, e.getMessage());
    }
}
Also used : Answer(com.cloud.agent.api.Answer) Network(com.xensource.xenapi.Network) Connection(com.xensource.xenapi.Connection)

Example 52 with Network

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

the class CitrixOvsSetTagAndFlowCommandWrapper method execute.

@Override
public Answer execute(final OvsSetTagAndFlowCommand command, final CitrixResourceBase citrixResourceBase) {
    citrixResourceBase.setIsOvs(true);
    final Connection conn = citrixResourceBase.getConnection();
    try {
        final Network nw = citrixResourceBase.setupvSwitchNetwork(conn);
        final String bridge = nw.getBridge(conn);
        /*
             * If VM is domainRouter, this will try to set flow and tag on its
             * none guest network nic. don't worry, it will fail silently at
             * host plugin side
             */
        final String result = citrixResourceBase.callHostPlugin(conn, "ovsgre", "ovs_set_tag_and_flow", "bridge", bridge, "vmName", command.getVmName(), "tag", command.getTag(), "vlans", command.getVlans(), "seqno", command.getSeqNo());
        s_logger.debug("set flow for " + command.getVmName() + " " + result);
        if (result != null && result.equalsIgnoreCase("SUCCESS")) {
            return new OvsSetTagAndFlowAnswer(command, true, result);
        } else {
            return new OvsSetTagAndFlowAnswer(command, false, result);
        }
    } catch (final BadServerResponse e) {
        s_logger.error("Failed to set tag and flow", e);
    } catch (final XenAPIException e) {
        s_logger.error("Failed to set tag and flow", e);
    } catch (final XmlRpcException e) {
        s_logger.error("Failed to set tag and flow", e);
    }
    return new OvsSetTagAndFlowAnswer(command, false, "EXCEPTION");
}
Also used : BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) Network(com.xensource.xenapi.Network) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) OvsSetTagAndFlowAnswer(com.cloud.agent.api.OvsSetTagAndFlowAnswer) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 53 with Network

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

the class CitrixOvsVpcPhysicalTopologyConfigCommandWrapper method execute.

@Override
public Answer execute(final OvsVpcPhysicalTopologyConfigCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    try {
        final Network nw = citrixResourceBase.findOrCreateTunnelNetwork(conn, command.getBridgeName());
        final String bridgeName = nw.getBridge(conn);
        final long sequenceNo = command.getSequenceNumber();
        final String result = citrixResourceBase.callHostPlugin(conn, "ovstunnel", "configure_ovs_bridge_for_network_topology", "bridge", bridgeName, "config", command.getVpcConfigInJson(), "host-id", ((Long) command.getHostId()).toString(), "seq-no", Long.toString(sequenceNo));
        if (result.startsWith("SUCCESS")) {
            return new Answer(command, true, result);
        } else {
            return new Answer(command, false, result);
        }
    } catch (final Exception e) {
        s_logger.warn("caught exception while updating host with latest VPC topology", e);
        return new Answer(command, false, e.getMessage());
    }
}
Also used : Answer(com.cloud.agent.api.Answer) Network(com.xensource.xenapi.Network) Connection(com.xensource.xenapi.Connection)

Example 54 with Network

use of com.xensource.xenapi.Network 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();
    final Map<String, Boolean> vlanToPersistenceMap = command.getVlanToPersistenceMap();
    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, command.isForceStop());
                }
            } 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")) {
                                    String networkLabel = network.getNameLabel(conn);
                                    citrixResourceBase.disableVlanNetwork(conn, network, shouldDeleteVlan(networkLabel, vlanToPersistenceMap));
                                }
                            } 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 55 with Network

use of com.xensource.xenapi.Network 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();
    final Map<String, Boolean> vlanToPersistenceMap = command.getVlanToPersistenceMap();
    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")) {
                    String networkLabel = network.getNameLabel(conn);
                    citrixResourceBase.disableVlanNetwork(conn, network, shouldDeleteVlan(networkLabel, vlanToPersistenceMap));
                }
            } 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) XmlRpcException(org.apache.xmlrpc.XmlRpcException) NicTO(com.cloud.agent.api.to.NicTO)

Aggregations

Network (com.xensource.xenapi.Network)69 Connection (com.xensource.xenapi.Connection)50 XenAPIException (com.xensource.xenapi.Types.XenAPIException)45 XmlRpcException (org.apache.xmlrpc.XmlRpcException)41 Answer (com.cloud.agent.api.Answer)31 XsLocalNetwork (com.cloud.hypervisor.xenserver.resource.XsLocalNetwork)30 Test (org.junit.Test)26 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)23 RebootAnswer (com.cloud.agent.api.RebootAnswer)20 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)20 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 NicTO (com.cloud.agent.api.to.NicTO)18 VIF (com.xensource.xenapi.VIF)17 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)16 HashMap (java.util.HashMap)15 SR (com.xensource.xenapi.SR)14 BadServerResponse (com.xensource.xenapi.Types.BadServerResponse)14 VM (com.xensource.xenapi.VM)14 XsHost (com.cloud.hypervisor.xenserver.resource.XsHost)13 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)12