Search in sources :

Example 1 with OvsSetTagAndFlowCommand

use of com.cloud.agent.api.OvsSetTagAndFlowCommand in project cloudstack by apache.

the class NotAValidCommand method testOvsSetTagAndFlowCommand.

@Test
public void testOvsSetTagAndFlowCommand() {
    final Network network = Mockito.mock(Network.class);
    final Connection conn = Mockito.mock(Connection.class);
    final OvsSetTagAndFlowCommand tagAndFlowCommand = new OvsSetTagAndFlowCommand("Test", "tag", "vlan://1", "123", 1l);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(citrixResourceBase.getConnection()).thenReturn(conn);
    when(citrixResourceBase.setupvSwitchNetwork(conn)).thenReturn(network);
    try {
        when(network.getBridge(conn)).thenReturn("br0");
    } catch (final BadServerResponse e) {
        fail(e.getMessage());
    } catch (final XenAPIException e) {
        fail(e.getMessage());
    } catch (final XmlRpcException e) {
        fail(e.getMessage());
    }
    final Answer answer = wrapper.execute(tagAndFlowCommand, citrixResourceBase);
    verify(citrixResourceBase, times(1)).getConnection();
    verify(citrixResourceBase, times(1)).setupvSwitchNetwork(conn);
    verify(citrixResourceBase, times(1)).setIsOvs(true);
    assertFalse(answer.getResult());
}
Also used : OvsSetTagAndFlowCommand(com.cloud.agent.api.OvsSetTagAndFlowCommand) RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Test(org.junit.Test)

Example 2 with OvsSetTagAndFlowCommand

use of com.cloud.agent.api.OvsSetTagAndFlowCommand in project cloudstack by apache.

the class CitrixStartCommandWrapper method execute.

@Override
public Answer execute(final StartCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final VirtualMachineTO vmSpec = command.getVirtualMachine();
    final String vmName = vmSpec.getName();
    VmPowerState state = VmPowerState.HALTED;
    VM vm = null;
    // if a VDI is created, record its UUID to send back to the CS MS
    final Map<String, String> iqnToPath = new HashMap<String, String>();
    try {
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        if (vms != null) {
            for (final VM v : vms) {
                final VM.Record vRec = v.getRecord(conn);
                if (vRec.powerState == VmPowerState.HALTED) {
                    v.destroy(conn);
                } else if (vRec.powerState == VmPowerState.RUNNING) {
                    final String host = vRec.residentOn.getUuid(conn);
                    final String msg = "VM " + vmName + " is runing on host " + host;
                    s_logger.debug(msg);
                    return new StartAnswer(command, msg, host);
                } else {
                    final String msg = "There is already a VM having the same name " + vmName + " vm record " + vRec.toString();
                    s_logger.warn(msg);
                    return new StartAnswer(command, msg);
                }
            }
        }
        s_logger.debug("1. The VM " + vmName + " is in Starting state.");
        final Host host = Host.getByUuid(conn, citrixResourceBase.getHost().getUuid());
        vm = citrixResourceBase.createVmFromTemplate(conn, vmSpec, host);
        final GPUDeviceTO gpuDevice = vmSpec.getGpuDevice();
        if (gpuDevice != null) {
            s_logger.debug("Creating VGPU for of VGPU type: " + gpuDevice.getVgpuType() + " in GPU group " + gpuDevice.getGpuGroup() + " for VM " + vmName);
            citrixResourceBase.createVGPU(conn, command, vm, gpuDevice);
        }
        if (vmSpec.getType() != VirtualMachine.Type.User) {
            citrixResourceBase.createPatchVbd(conn, vmName, vm);
        }
        // put cdrom at the first place in the list
        List<DiskTO> disks = new ArrayList<DiskTO>(vmSpec.getDisks().length);
        int index = 0;
        for (final DiskTO disk : vmSpec.getDisks()) {
            if (Volume.Type.ISO.equals(disk.getType())) {
                disks.add(0, disk);
            } else {
                disks.add(index, disk);
            }
            index++;
        }
        for (DiskTO disk : disks) {
            final VDI newVdi = citrixResourceBase.prepareManagedDisk(conn, disk, vmSpec.getId(), vmSpec.getName());
            if (newVdi != null) {
                final String path = newVdi.getUuid(conn);
                iqnToPath.put(disk.getDetails().get(DiskTO.IQN), path);
            }
            citrixResourceBase.createVbd(conn, disk, vmName, vm, vmSpec.getBootloader(), newVdi);
        }
        for (final NicTO nic : vmSpec.getNics()) {
            citrixResourceBase.createVif(conn, vmName, vm, vmSpec, nic);
        }
        citrixResourceBase.startVM(conn, host, vm, vmName);
        if (citrixResourceBase.isOvs()) {
            // TODO(Salvatore-orlando): This code should go
            for (final NicTO nic : vmSpec.getNics()) {
                if (nic.getBroadcastType() == Networks.BroadcastDomainType.Vswitch) {
                    final HashMap<String, String> args = citrixResourceBase.parseDefaultOvsRuleComamnd(BroadcastDomainType.getValue(nic.getBroadcastUri()));
                    final OvsSetTagAndFlowCommand flowCmd = new OvsSetTagAndFlowCommand(args.get("vmName"), args.get("tag"), args.get("vlans"), args.get("seqno"), Long.parseLong(args.get("vmId")));
                    final CitrixRequestWrapper citrixRequestWrapper = CitrixRequestWrapper.getInstance();
                    final OvsSetTagAndFlowAnswer r = (OvsSetTagAndFlowAnswer) citrixRequestWrapper.execute(flowCmd, citrixResourceBase);
                    if (!r.getResult()) {
                        s_logger.warn("Failed to set flow for VM " + r.getVmId());
                    } else {
                        s_logger.info("Success to set flow for VM " + r.getVmId());
                    }
                }
            }
        }
        if (citrixResourceBase.canBridgeFirewall()) {
            String result = null;
            if (vmSpec.getType() != VirtualMachine.Type.User) {
                final NicTO[] nics = vmSpec.getNics();
                boolean secGrpEnabled = false;
                for (final NicTO nic : nics) {
                    if (nic.isSecurityGroupEnabled() || nic.getIsolationUri() != null && nic.getIsolationUri().getScheme().equalsIgnoreCase(IsolationType.Ec2.toString())) {
                        secGrpEnabled = true;
                        break;
                    }
                }
                if (secGrpEnabled) {
                    result = citrixResourceBase.callHostPlugin(conn, "vmops", "default_network_rules_systemvm", "vmName", vmName);
                    if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
                        s_logger.warn("Failed to program default network rules for " + vmName);
                    } else {
                        s_logger.info("Programmed default network rules for " + vmName);
                    }
                }
            } else {
                // For user vm, program the rules for each nic if the
                // isolation uri scheme is ec2
                final NicTO[] nics = vmSpec.getNics();
                for (final NicTO nic : nics) {
                    if (nic.isSecurityGroupEnabled() || nic.getIsolationUri() != null && nic.getIsolationUri().getScheme().equalsIgnoreCase(IsolationType.Ec2.toString())) {
                        final List<String> nicSecIps = nic.getNicSecIps();
                        String secIpsStr;
                        final StringBuilder sb = new StringBuilder();
                        if (nicSecIps != null) {
                            for (final String ip : nicSecIps) {
                                sb.append(ip).append(":");
                            }
                            secIpsStr = sb.toString();
                        } else {
                            secIpsStr = "0:";
                        }
                        result = citrixResourceBase.callHostPlugin(conn, "vmops", "default_network_rules", "vmName", vmName, "vmIP", nic.getIp(), "vmMAC", nic.getMac(), "vmID", Long.toString(vmSpec.getId()), "secIps", secIpsStr);
                        if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
                            s_logger.warn("Failed to program default network rules for " + vmName + " on nic with ip:" + nic.getIp() + " mac:" + nic.getMac());
                        } else {
                            s_logger.info("Programmed default network rules for " + vmName + " on nic with ip:" + nic.getIp() + " mac:" + nic.getMac());
                        }
                    }
                }
            }
        }
        state = VmPowerState.RUNNING;
        final StartAnswer startAnswer = new StartAnswer(command);
        startAnswer.setIqnToPath(iqnToPath);
        return startAnswer;
    } catch (final Exception e) {
        s_logger.warn("Catch Exception: " + e.getClass().toString() + " due to " + e.toString(), e);
        final String msg = citrixResourceBase.handleVmStartFailure(conn, vmName, vm, "", e);
        final StartAnswer startAnswer = new StartAnswer(command, msg);
        startAnswer.setIqnToPath(iqnToPath);
        return startAnswer;
    } finally {
        if (state != VmPowerState.HALTED) {
            s_logger.debug("2. The VM " + vmName + " is in " + state + " state.");
        } else {
            s_logger.debug("The VM is in stopped state, detected problem during startup : " + vmName);
        }
    }
}
Also used : StartAnswer(com.cloud.agent.api.StartAnswer) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) VDI(com.xensource.xenapi.VDI) NicTO(com.cloud.agent.api.to.NicTO) Connection(com.xensource.xenapi.Connection) Host(com.xensource.xenapi.Host) OvsSetTagAndFlowAnswer(com.cloud.agent.api.OvsSetTagAndFlowAnswer) GPUDeviceTO(com.cloud.agent.api.to.GPUDeviceTO) OvsSetTagAndFlowCommand(com.cloud.agent.api.OvsSetTagAndFlowCommand) VM(com.xensource.xenapi.VM) VmPowerState(com.xensource.xenapi.Types.VmPowerState) DiskTO(com.cloud.agent.api.to.DiskTO)

Aggregations

OvsSetTagAndFlowCommand (com.cloud.agent.api.OvsSetTagAndFlowCommand)2 Connection (com.xensource.xenapi.Connection)2 Answer (com.cloud.agent.api.Answer)1 OvsSetTagAndFlowAnswer (com.cloud.agent.api.OvsSetTagAndFlowAnswer)1 RebootAnswer (com.cloud.agent.api.RebootAnswer)1 StartAnswer (com.cloud.agent.api.StartAnswer)1 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)1 DiskTO (com.cloud.agent.api.to.DiskTO)1 GPUDeviceTO (com.cloud.agent.api.to.GPUDeviceTO)1 NicTO (com.cloud.agent.api.to.NicTO)1 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)1 XsLocalNetwork (com.cloud.hypervisor.xenserver.resource.XsLocalNetwork)1 Host (com.xensource.xenapi.Host)1 Network (com.xensource.xenapi.Network)1 BadServerResponse (com.xensource.xenapi.Types.BadServerResponse)1 VmPowerState (com.xensource.xenapi.Types.VmPowerState)1 XenAPIException (com.xensource.xenapi.Types.XenAPIException)1 VDI (com.xensource.xenapi.VDI)1 VM (com.xensource.xenapi.VM)1 ArrayList (java.util.ArrayList)1